;FILE:
;	trace_keywords.tdb
;PURPOSE:
;	Master database definition of the TRACE keywords.  It is used
;	to extract information from various sources to make the image
;	index stucture as well as the catalog structures.
;GUIDELINES:
;	* Tag names must be 8 characters or less to be able to be mapped to
;	  FITS ASCII headers/keywords.  Tag names over 8 characters will not
;	  be saved (?).  Tags which are of non-scalar are not saved anyway
;	  so they could be longer than 8 characters (?)
;	* First character conventions for tag names (which must be 8 chars and less)
;		"m" - mechanisms ("ms" for string denotation of the position)
;		"t" - temperatures
;		"v" - voltages/currents
;	* Input variables which can be used in the instruction
;		ph - primary header
;		be - binary extension
;		dph- binary data product header ("505")
;		tim1 - time (passed in) (secondary reference time?)
;MAIN ROUTINES USED:
;	gt_tagval	- Simple extraction of value from a structure
;	gtt_mnem	- TRACE extract mnemonic from 505 integer data product header
;	gtt_info	- TRACE extract information from 505 integer data product header
;	gtt_pnt		- TRACE extract pointing information from 505 integer data product header
;	gtt_orbit	- TRACE report orbit information (from external database information)
;	gtt_sciplan	- TRACE routine to access the IAP and science plan database
;	fxpar		- Extract a tag/field out of the primary header
;
;TODO:
;	* Create a master database of TRACE keywords and what their definitions are and how they are defined
;	* Create the gtt* routines
;	* Create routine to use this file and to create the structure (file can be read with RD_ULIN_COL)
;	* Review required tag names - delete some, add:
;		exposure time
;		a standard required keyword describing the wavelengths/filter combo (for labeling images)
;	* Tool to create custom tabular listings using pull down menus of tags to include
;	* Tool to display index information using the description field as the label/descriptor
;	* What is the form of the Flags ("ON"/"OFF") yes/no, 0/1, ...
;DETAILS:
;	Some keywords are "read-only" and show the state of the instrument when the image was taken.
;	Other keywords are "read-write" and should be updated when any processing of the image is
;	done.  The following is a list of the keywords which should be changed when processing
;	is done to change it's characteristics
;		Extraction/Rebinning/...
;			naxisN, crpixN, crvalN, cdeltN, ctypeN, crota
;			off_x, off_y
;		Exposure normalization
;			exposure
;HISTORY:
;	Created 9-Jan-98 by M.Morrison
;	15-Jan-98 (MDM) - Renamed file to "trace_keywords.tdb"
;			- Changed some of the time definitions
;	29-Jan-98 (SLF) - Merged some newly created tags from Neil Hurlburt
;	29-Jan-98 (MDM) - Expanded and re-organized somewhat
;			- Change "be.dp_header" to "be"
;			- Changes to work with new GTT_INFO and TR_LUT_CONV
;	30-Jan-98 (MDM) - Changed all gtt_mnem calls to use dph variable
;	19-Feb-98 (MDM) - Made "it" be internal form
;			- Pass the time on the LUT calls
;			- Renamed "wave_name" to "wave_nam"
;	24-Feb-98 (MDM) - Modified "wave_nam" and "wave_len" to use the
;			  memory load input file as the lookup table file
;			- Added "sht_opn" and "sht_cls" times
;			- Added "sht_expl"
;			- Enabled "F_DARK"
;	19-Mar-98 (MDM) - Added some pointing keywords
;			- Added information on what keywords are read/write
;			- Added some sequence register keywords
;	19-Mar-98 (MDM) - Removed pointing keywords
;	24-Mar-98 (MDM) - Added "sht_mdur"
;       12-Apr-98 (CJS) - Changed target class from iksqrg27 to iksqrg25
;                       - added keyword mwdg2_180 which prints 180-mwdg2 to correct for difference in input/readout 
;       13-Apr-98 (CJS) - Added keyword safecnt to print $g7 for safety monitoring
;       22-Apr-98 (CJS) - Changed keyword mwdg2_180 to mwdg2cor to be 8 characters
;       23-Apr-98 S.L.Freeland - enable some pointing keywords
;	28-Apr-98 (MDM) - Enabled "wave_tru"
;	28-Apr-98 (MDM) - Disabled "wave_tru"
;       12-May-98 (SLF) - enabled "wave_tru" (be only for now)
;	24-Feb-99 (RWN) - enabled "F_ATMOBS"
;	 3-Feb-00 (RWN) - corrected sign of second half of equations of xcen,ycen,am_fovx,am_fovy
;
TagName  	Instruction						Description
--------------	-------------------------------------------------------	----------------------------------------------
;
;--- Time information
;
.NONE           it = anytim2ints(gtt_info(be, 'IMG_TIME', /str))  	Image time (start of exposure)
img_time	gtt_info(be, 'IMG_TIME', /str)				Image time (start of exposure)
time_tag	gtt_info(be, 'TIME_TAG', /str)				Time tagged with image (end of expos)
;
date            anytim(it, /yymmdd)                            		??
mjd             gt_tagval(anytim(it, /mjd),/mjd)                   	??
day             gt_tagval(anytim(it, /ints), 'DAY')                     Days since 1-Jan-79
time            gt_tagval(anytim(it, /ints), 'TIME')                    Milliseconds in day
time_obs        gt_time(it, /str, /msec)                                Time of image
date_obs        anytim(it, /ccsds)					Date/Time of Image
;
date_str	gt_day(it, /str)					Date (DD-MMM-YY)
time_str	gt_time(it, /str, /msec)				Time String (HH:MM:SS.MMM)
doy_str		anytim2doy(it, /str)					Date in Day of Year form (YYDDD)
;
;--- Misc science and instrument
;
origin          'TRACE'                                        		Origin
telescop        'TRACE'                                        		Telescope
instrume        'TRACE'                                        		Instrument
obs_prog        id2nam(gtt_mnem(dph, 'ikmsqid'), 'SEQ')        		Sequence name (observing program)
CMP_ID  	gtt_sciplan(it, 'CMP_ID')		           	SOHO Campaign ID number
SCI_OBJ 	gtt_sciplan(it, 'SCI_OBJ')		           	Science objective
OBJECT  	gtt_sciplan(it, 'OBJECT')		         	Target Description
;
;--- Basic Image information
;
naxis1		gt_tagval(be, 'NX_OUT')					X Size of image
naxis2		gt_tagval(be, 'NY_OUT')					Y Size of image
;
;--- Orbit information
;
;min2saa         gtt_orbit(it, 'MIN2SAA')                       	Minutes before an SAA starts
;minssaa         gtt_orbit(it, 'MINSSAA')                       	Minutes since an SAA
;lat									Latitude
;lon									Longitude
;alt									Altitude
;               
;--- Sequence/state info
;
mseqid          gtt_mnem(dph, 'ikmsqid')              			Main Sequence identifier
mseq_nam        id2nam(gtt_mnem(dph, 'ikmsqid'), 'SEQ')			Main Sequence name
seqid           gtt_mnem(dph, 'iksqid')               			Sequence identifier
seq_nam         id2nam(gtt_mnem(dph, 'iksqid'), 'SEQ')       		Sequence name
frmid           gtt_mnem(dph, 'iksqfid')              			Frame ID
frm_nam        	id2nam(gtt_mnem(dph, 'iksqfid'), 'FRM')       		Frame Name
trgid           gtt_mnem(dph, 'iksqtargl')            			Target ID
trg_nam         id2nam(gtt_mnem(dph, 'iksqtargl'), 'TRG')      		Target Name
;
fn_inseq        gtt_mnem(dph, 'iksqfrm')              			Frame number in sequence
frm_num         gtt_mnem(dph, 'ikdpfrmn')             			Frame number 
;               
;--- Sequencer status info
;
S_SAA	 	gtt_mnem(dph, 'iksqrg1b')		                SAA flag on board (built in register)
S_ATMOBS	gtt_mnem(dph, 'iksqrg1c')				Atmospheric absorption (built in reg)
S_HLAT		gtt_mnem(dph, 'iksqrg1d')				High Latitude flag (built in reg)
;F_SAA		gtt_orbit(it, 'F_SAA')					SAA flag predicted from ground
;F_RADZON	gtt_orbit(it, 'F_RADZON')				Radiation Zone flag
F_ATMOBS	gtt_orbit(it, 'F_ATMOBS')				Atmospheric absorption flag
;F_HLAT		gtt_orbit(it, 'F_HLAT')					High Latitude flag
;
S_FLARE		gtt_mnem(dph, 'iksqrg20')				Flare flag (built in register)
S_SFLARE	gtt_mnem(dph, 'iksqrg21')				Super Flare flag (built in register)
S_TRANS		gtt_mnem(dph, 'iksqrg22')				Transient flag (built in register)
S_TRGCLS	gtt_mnem(dph, 'iksqrg25')				Target Class (for AEC)
;
S_RSTAT		gtt_mnem(dph, 'iksqrg2c')				Sequence Return Status
S_PRIO		gtt_mnem(dph, 'iksqrg2d')				Sequence Priority 
S_PND_F		gtt_mnem(dph, 'iksqrg2e')				Sequence Pending flag
S_REPCNT	gtt_mnem(dph, 'iksqrg2f')				Sequence Repeat Count
;
F_DARK		gtt_mnem(dph, 'fdbdark')				Dark frame flag (0=normal, 1=Dark)
;F_AEC		gtt_mnem(dph, 'fdb??')					AEC flag
;
;--- Mechanism info
;
mquad		gtt_mnem(dph, 'ikqspos')				Quadrant Selector Position
msquad		gtt_info(be, 'mquad', /lut, times=it)			Quadrant Selector Position name
mfilt1          gtt_mnem(dph, 'ikf1pos')              			Filter wheel 1 Position
mfilt2          gtt_mnem(dph, 'ikf2pos')              			Filter wheel 2 Position
msfilt1         gtt_info(be, 'mfilt1', /lut, times=it)              	Filter wheel 1 Position name
msfilt2         gtt_info(be, 'mfilt2', /lut, times=it)            	Filter wheel 2 Position name
mfocus		gtt_mnem(dph, 'ikfmpos')				Focus Motor Position
mwdg1		gtt_mnem(dph, 'ikw1pos')				Wedge Motor 1 Position
mwdg2		gtt_mnem(dph, 'ikw2pos')				Wedge Motor 2 Position
mwdg2cor	180-gtt_mnem(dph, 'ikw2pos')				180 - Wedge Motor 2 Position
;
wave_num	gtt_mnem(dph, 'FDBWAVE')				Wavelength table entry number
wave_nam        gtt_info(be, 'wave_num', /lut, oc=8, times=it) 		Wavelength name (descriptor)
wave_len	gtt_info(be, 'wave_num', /lut, oc=9, times=it) 		Wavelength length class (descriptor)
;wave_tru	gtt_wave_tru(be) 					"True" wav name base on encoders
;
;--- General  info
;
apid            gt_tagval(be, 'APID')                          		Application ID
;
;--- Image and CCD information
;
sou_area        gtt_mnem(dph, 'DPHSAI')          	     		Source Area Index
amp		gtt_mnem(dph, 'ikcmamp')				Amplifier used
amp_cmd		gtt_mnem(dph, 'FDBAMP')					Amplified commanded
sum_ccdx	gtt_mnem(dph, 'ikcmsumx')				On-chip summing mode x (Serial)
sum_ccdy	gtt_mnem(dph, 'ikcmsumy')				On-chip summing mode y (parallel)
bin_ccd		gtt_info(be, 'bin_ccd')					IP binning
tbin_ccd	gtt_mnem(dph, 'ikcmsumx')*gtt_info(be, 'bin_ccd')	Overall binning*summing factor
;
cro_fpox	gtt_info(be, 'cro_fpox')				CCD Readout, 1st pix out X
cro_fpoy	gtt_info(be, 'cro_fpoy')				CCD Readout, 1st pix out Y
cro_nlin	gtt_mnem(be, 'ipbbpyl')					CCD Readout, number of lines
cro_llex	gtt_info(be, 'cro_llex')				CCD Readout, lower-left Extract X
cro_lley	gtt_info(be, 'cro_lley')				CCD Readout, lower-left Extract Y
cdl_ncol	gtt_info(be, 'cdl_ncol')				CCD Downlink, number of CCD columns	
cdl_nlin	gtt_info(be, 'cdl_nlin')				CCD Downlink, number of CCD lines	
sri_llex	gtt_info(be, 'sri_llex')				Solar Rotated Image, L-L Extract X
sri_lley	gtt_info(be, 'sri_lley')				Solar Rotated Image, L-L Extract Y
OFF_X   	gtt_info(be, 'sri_llex')				Lower-left corner X extract ("SRI" pix)
OFF_Y   	gtt_info(be, 'sri_lley')				Lower-left corner Y extract ("SRI" pix)
;^^^ corrects for amp a/b, summing, extract, ...?
;
;Units and comments:
;	"SRI" pix are "Solar-rotated image" pixels, in full resulution equivalent (not the 
;	output binned/summed pixel units) with (0,0) being the lower-left corner pixel (center of the pixel)
;
;--- Pointing info
;
pzta_avg	gtt_mnem(dph, 'iuispzta')				Guider PZT A avgerage
pztb_avg	gtt_mnem(dph, 'iuispztb')				Guider PZT B avgerage
pztc_avg	gtt_mnem(dph, 'iuispztc')				Guider PZT C avgerage
pzta_off	gtt_mnem(dph, 'ikisaos')				Guider PZT A offset
pztb_off	gtt_mnem(dph, 'ikisbos')				Guider PZT B offset
pztc_off	gtt_mnem(dph, 'ikiscos')				Guider PZT C offset
;
ctype1          'Solar-x'                                      		Type of Axis1           
ctype2          'Solar-y'                                      		Type of Axis2
cdelt1          gtt_info(be, 'cdelt1')        		      		arcseconds of output pixel X
cdelt2          gtt_info(be, 'cdelt2')              			arcseconds of output pixel Y
crpix1          gtt_pnt(be, 'xsuncen')                			Pixel location of sun center X
crpix2          gtt_pnt(be, 'ysuncen')                			Pixel location of sun center Y
crval1          gtt_pnt(be, 'xoff')                                     Refernce 
crval2          gtt_pnt(be, 'yoff')					Reference 
;
f_guider	gtt_mnem(dph, 'ikisloop')				Guider Loop Open/Closed
;
.NONE		ccdcen = gtt_ccd_cen(be, it, dph=dph)
am_ccdx		ccdcen(*,0)/60.						Solar coord, arc min for CCD center x
am_ccdy		ccdcen(*,1)/60.						Solar coord, arc min for CCD center y
.NONE		sri_fovx = gtt_info(be, 'sri_fovx')
.NONE		sri_fovy = gtt_info(be, 'sri_fovy')
sri_fovx	sri_fovx						Solar Rot Img, FOV X center ("SRI" pix)
sri_fovy	sri_fovy						Solar Rot Img, FOV Y center ("SRI" pix)
;					     ;corrected sign of second half in next 4 equations--- (RWN 2/3/00)
am_fovx		(ccdcen(*,0) - (511.5-sri_fovx)*0.5)/60.		Solar coord, arc min for FOV center x
am_fovy		(ccdcen(*,1) - (511.5-sri_fovy)*0.5)/60.		Solar coord, arc min for FOV center y
;
xcen		(ccdcen(*,0) - (511.5-sri_fovx)*0.5)			Solar coord, arc sec for FOV center x
ycen		(ccdcen(*,1) - (511.5-sri_fovy)*0.5)			Solar coord, arc sec for FOV center y
;
;--- Expsosure information
;
sht_expl	gtt_mnem(dph, 'fdbexp_index')				Exposure level (in table)									
sht_nomt	gtt_mnem(dph, 'ikshnexp')				Expected exposure duration (ticks)
sht_nom		gtt_mnem(dph, 'ikshnexp')*.004				Expected exposure duration (sec)
sht_mswp	gtt_mnem(dph, 'ikshnswp')+1				Shutter multiple sweep count
sht_mdur	gtt_info(be, 'sht_mdur')				Measured exposure duration (sec)
;exposure	gtt_info(be, 'sht_mdur')				(RW) Exposure duration
sht_opn		gtt_mnem(dph, 'iksqsho')				Shutter Open time (4 usec ticks?)
sht_cls		gtt_mnem(dph, 'iksqshc')				Shutter Close time (4 usec ticks?)
;TIME_AVG	gtt_mnem(dph, 'IPBTAI??')				Frames in time average
;
;--- Temperature information
;
t_fstr		gtt_mnem(dph, 'itteth1')				Temperature of Telescope Front Struct
t_spider	gtt_mnem(dph, 'itteth2')				Temperature of Spider Frame
t_ccd_a		gtt_mnem(dph, 'itcmtmp8')				Temperature of CCD Amplifier A  
;
;--- Image data information
;
DL_TYPE    	gtt_info(be, 'dl_type')   				Downlink type (
;COMP_TAB    	gtt_info(be, 'comp_ht')   				JPEG huffman table (or lookup table)
;COMP_QT	gtt_info(be, 'comp_qt')					JPEG quality table
;
image_ss        long(gt_tagval(be, 'xxx',missing=0l))                   Relative image number
comp_code       byte(gt_tagval(be, 'xxx',missing=0b))                   Compress code (jpeg=1)
;
img_min         gt_tagval(be, 'img_min')                                Image Minimum
img_max         gt_tagval(be, 'img_max')                       		Image Maximum
img_avg         gt_tagval(be, 'img_avg')                       		Image Average
img_dev         gt_tagval(be, 'img_dev')                       		Image Deviation
percentd	gt_tagval(be, 'img_percentd')				Percent of image present
safecnt         gtt_mnem(dph, 'iksqrg17')                               Dangerous frame counter	
;
;--- Spacecraft memory
scmem           gtt_mnem(dph, 'iksqrg1e')                               Main buffer memory usage
;
;--- Copy full raw header
;
dp_header       gt_tagval(be, 'dp_header')                     		Full raw data product header
