;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
;pro return_file_params_xRG
;
;PURPOSE:
;	To return the parameters of a file and put them into a keyword
;	structure.
;
;	NOTE: This file uses NightParams.pro as an input.  That file
;	      contains the settings for a given night of observing.
;
;INPUTS:
;	FITSFILENAME - The name of the file from which the keywords will be
;		   extracted.
;
;KEYWORDS:
;       MONTH: int
;	    The month of the observations
;	YEAR: int  
;	    The year of the observations 
;	SLOPE: int
;	    If the file is a slope fit, it only has one read, so NAXIS3 is
;	    undefined.  Also, we do not need to subtract a dark or
;	    divide by a flat
;	USESKYFLAT: int
;	    0 - Use the normal flat
;	    1 - Use the sky flat
;OUTPUTS:
;	The KeyStr structure will be updated with the keywords from
;	the file FILENAME.

function Return_File_Params_xRG, FitsFileName, $
	 Slope=Slope, Month = Month, Year = Year, $
	 UseSkyFlat = UseSkyFlat

If N_Elements(Year)       eq 0 then Year  = '07'
If N_Elements(Month)      eq 0 then Month = 'Nov'
If N_Elements(Slope)      eq 0 then Slope = 0
If N_Elements(UseSkyFlat) eq 0 then UseSkyFlat = 0
Common KeyParams

;Get the file information
KeyStr.RawObjectPath = FitsFileName
KeyStr.RawObjectBase = File_BaseName(KeyStr.RawObjectPath)
KeyStr.RawObjectDir  = File_DirName(KeyStr.RawObjectPath)

;Form a key without the .fits extension
LastDot              = strpos(KeyStr.RawObjectBase, '.', /reverse_search)
KeyStr.RawObjectKey  = strmid(KeyStr.RawObjectBase, 0, LastDot)

;Use the special header file if the LEACH was used with the H1RG
If KeyStr.DetStr eq 'H1RG-022' and KeyStr.ElecStr eq 'LEACH' and $
   KeyStr.Date ne '05May08' and $
   not stregex(KeyStr.RawObjectPath, 'Reduced', /boolean) and $
   not stregex(KeyStr.RawObjectPath, 'Flat', /boolean) then begin
   Fits_Read, FitsFileName, NoData, FitsHeader, /header_only
   KeyStr.Naxis      = long(SxPar(FitsHeader, 'NAXIS'))
   KeyStr.Naxis1     = long(SxPar(FitsHeader, 'NAXIS1'))
   KeyStr.Naxis2     = long(SxPar(FitsHeader, 'NAXIS2'))
   KeyStr.Naxis3     = long(SxPar(FitsHeader, 'NAXIS3'))
   KeyStr.TotalReads = long(SxPar(FitsHeader, 'NAXIS3'))
   HeaderName   = KeyStr.RawObjectDir+KeyStr.PathDelim+'Header_'+$
		KeyStr.RawObjectBase
   Fits_Read, HeaderName, NoData, FitsHeader, /header_only
EndIf else begin
   Fits_Read, FitsFileName, NoData, FitsHeader, /header_only
   KeyStr.Naxis      = long(SxPar(FitsHeader, 'NAXIS'))
   KeyStr.Naxis1     = long(SxPar(FitsHeader, 'NAXIS1'))
   KeyStr.Naxis2     = long(SxPar(FitsHeader, 'NAXIS2'))
   KeyStr.Naxis3     = long(SxPar(FitsHeader, 'NAXIS3'))
   KeyStr.TotalReads = long(SxPar(FitsHeader, 'NAXIS3'))
   KeyStr.MCDFile    = SxPar(FitsHeader, 'MCD_FILE')
EndElse

;EXPOSURE PARAMETERS from header---------------------------------------
KeyStr.WindowMode = long(SxPar(FitsHeader, 'WINDOWM'))
KeyStr.NReads     = long(SxPar(FitsHeader, 'NREADS'))
KeyStr.NGroups    = long(SxPar(FitsHeader, 'NGROUPS'))
KeyStr.NDrops     = long(SxPar(FitsHeader, 'NDROPS'))
KeyStr.NOUT       = long(SxPar(FitsHeader, 'NOUT'))
KeyStr.NADC       = (long(SxPar(FitsHeader, 'NADC')) > 1)
KeyStr.ITime	  = Double(SxPar(FitsHeader, 'ITIME'))
KeyStr.NumWindows = Double(SxPar(FitsHeader, 'WINDOWS'))
KeyStr.TotalReads = long(SxPar(FitsHeader,'TOTREADS'))

If KeyStr.ElecStr eq 'LEACH' then begin
   KeyStr.ITime = KeyStr.ITime*KeyStr.Naxis3
   if KeyStr.Date eq '04Jun06' then begin
     KeyStr.ITime = float(FxPar(FitsHeader, 'EXP_TIME'))
     KeyStr.FrameTime = float(FxPar(FitsHeader, 'FRMTIME'))
   endif 
   if KeyStr.NGroups eq 0 then KeyStr.NGroups   = 1
EndIf
If KeyStr.DetStr eq 'H2RG-001' then begin
   KeyStr.ITime = float(FxPar(FitsHeader, 'ITIME'))
   KeyStr.FrameTime = float(KeyStr.ITime/KeyStr.NReads)
endif 

If KeyStr.Naxis3 eq 0 then KeyStr.NAxis3 = KeyStr.TotalReads
If KeyStr.TotalReads eq 0 then KeyStr.TotalReads = KeyStr.Naxis3
If KeyStr.DetStr eq 'H1RG-018' or $
  (KeyStr.DetStr eq 'H1RG-022' and KeyStr.Date eq '05May08') then begin
  KeyStr.NReads     = KeyStr.NAxis3
  KeyStr.NGroups    = 1
  KeyStr.NDrops     = 0
endif
if KeyStr.NReads eq 0 and Slope eq 0 then KeyStr.NReads = KeyStr.Naxis3

;WINDOW MODE PARAMETERS from header------------------------------------
;If the exposure was from windowmode, get all the coordinates
If KeyStr.WindowMode then begin
   KeyStr.NOUT = 1
   For WindowNum=0, KeyStr.NumWindows-1 do begin
       WindowNumStr = Strtrim(WindowNum,2)
	         KeyStr.XWindowStart(WindowNum) = $
 		 	UInt(SxPar(FitsHeader,'WNDW'+WindowNumStr+'XMI'))
 		 KeyStr.XWindowStop(WindowNum) = $
 		 	UInt(SxPar(FitsHeader,'WNDW'+WindowNumStr+'XMA'))
 		 KeyStr.YWindowStart(WindowNum) = $
 		 	UInt(SxPar(FitsHeader,'WNDW'+WindowNumStr+'YMI'))
 		 KeyStr.YWindowStop(WindowNum) = $
 		 	UInt(SxPar(FitsHeader,'WNDW'+WindowNumStr+'YMA'))
        EndFor
EndIf

;WCS INFORMATION------------------------------------------------------
KeyStr.ThisFilter  = Strtrim(SxPar(FitsHeader, 'FILTER'),2)
if KeyStr.Date eq '05May08' then begin
  KeyStr.ThisFilter= SxPar(FitsHeader,'WHEEL1')
  if KeyStr.ThisFilter eq 'Closed' then KeyStr.ThisFilter='Blank'
Endif
KeyStr.RA  	   = Double(SxPar(FitsHeader,'RA'))
KeyStr.DEC 	   = Double(SxPar(FitsHeader,'DEC'))
KeyStr.ZD	   = Double(SxPar(FitsHeader,'ZD'))
KeyStr.AZ	   = Double(SxPar(FitsHeader,'AZ'))
KeyStr.UT	   = Double(SxPar(FitsHeader,'UT'))
KeyStr.HA	   = Double(SxPar(FitsHeader,'HA'))
KeyStr.LST	   = Double(SxPar(FitsHeader,'LST'))
KeyStr.AIRMASS     = Double(SxPar(FitsHeader,'AIRMASS'))
KeyStr.CDELT1      = Double(SxPar(FitsHeader,'CDELT1'))
KeyStr.CDELT2      = Double(SxPar(FitsHeader,'CDELT2'))
KeyStr.CRVAL1      = Double(SxPar(FitsHeader,'CRVAL1'))
KeyStr.CRVAL2	   = Double(SxPar(FitsHeader,'CRVAL2'))
KeyStr.CTYPE1      = Strtrim(SxPar(FitsHeader,'CTYPE1'),2)
KeyStr.CTYPE2      = Strtrim(SxPar(FitsHeader,'CTYPE2'),2)
KeyStr.CRPIX1      = Double(SxPar(FitsHeader,'CRPIX1'))
KeyStr.CRPIX2      = Double(SxPar(FitsHeader,'CRPIX2'))
KeyStr.Time        = SxPar(FitsHeader,'TIME')
if KeyStr.ElecStr eq 'ASIC' then begin
  KeyStr.Sec       = long(strmid(KeyStr.Time, 17,2))
  KeyStr.Min       = long(strmid(KeyStr.Time, 14,2))
  KeyStr.Hour      = long(strmid(KeyStr.Time, 11,2))
  KeyStr.DateNum   = long(strmid(KeyStr.Time, 8,2))
  KeyStr.Day       = strmid(KeyStr.Time, 0,3)
  KeyStr.Mon       = strmid(KeyStr.Time, 4,3)
  if KeyStr.Mon eq 'Jan' then KeyStr.MonNum = 1
  if KeyStr.Mon eq 'Feb' then KeyStr.MonNum = 2
  if KeyStr.Mon eq 'Mar' then KeyStr.MonNum = 3
  if KeyStr.Mon eq 'Apr' then KeyStr.MonNum = 4
  if KeyStr.Mon eq 'May' then KeyStr.MonNum = 5
  if KeyStr.Mon eq 'Jun' then KeyStr.MonNum = 6
  if KeyStr.Mon eq 'Jul' then KeyStr.MonNum = 7
  if KeyStr.Mon eq 'Aug' then KeyStr.MonNum = 8
  if KeyStr.Mon eq 'Sep' then KeyStr.MonNum = 9
  if KeyStr.Mon eq 'Oct' then KeyStr.MonNum = 10
  if KeyStr.Mon eq 'Nov' then KeyStr.MonNum = 11
  if KeyStr.Mon eq 'Dec' then KeyStr.MonNum = 12
  KeyStr.Year      = long(strmid(KeyStr.Time, 20 , 4))
endif else if KeyStr.ElecStr eq 'LEACH' then begin
  KeyStr.Time = FxPar(FitsHeader, 'TIME') 
endif

;VOLTAGES AND TEMPS, etc.----------------------------------------------
KeyStr.Gain	   = Double(SxPar(FitsHeader,'PAMPGAIN'))
KeyStr.PixRate     = Double(SxPar(FitsHeader,'PIXRATE'))
KeyStr.VRESET      = Double(SxPar(FitsHeader,'VRESET'))
KeyStr.DSUB	   = Double(SxPar(FitsHeader,'DSUB'))
KeyStr.VBIASGATE   = Double(SxPar(FitsHeader,'VBIASGATE'))
KeyStr.DRAIN       = Double(SxPar(FitsHeader,'DRAIN'))
KeyStr.VDD         = Double(SxPar(FitsHeader,'VDD'))
KeyStr.VDDA	   = Double(SxPar(FitsHeader,'VDDA'))
KeyStr.CELLDRAIN   = Double(SxPar(FitsHeader,'CELLDRAIN'))
KeyStr.VBIASPWR    = Double(SxPar(FitsHeader,'VBIASPWR'))
KeyStr.VSUB	   = Double(SxPar(FitsHeader,'VSUB'))
KeyStr.DETTEMP     = Double(SxPar(FitsHeader,'MOLYTABL'))
if KeyStr.DETTEMP eq 0 then begin
  KeyStr.DETTEMP   = Double(SxPar(FitsHeader,'DETTEMP'))
endif

;INFORMATION ADDED TO HEADERS OF REDUCED IMAGES-----------------------
BiasSubVal         = Long(SxPar(FitsHeader,'BIASSUB',Count=BiasSubExists))
If BiasSubExists then KeyStr.BiasSub=BiasSubVal


;DIRECTORIES AND MEDIAN FILES
if KeyStr.DetStr ne 'H4RG-10-007' then begin
  
   ;If the reference pixels have been subtracted already, find the dark with RPS
   RefSubVal = stregex(SxPar(FitsHeader, 'REFPXSUB'), 'RPS', /boolean)
   if RefSubVal eq 1 then KeyStr.RefPixStr = 'RPS'

   if KeyStr.Date ne '05May08' then begin
     DarkFiles         =  File_Search(KeyStr.DarkDir+'MedianDark*'+$
		          KeyStr.ObjectText+'*_'+$
                          strtrim(KeyStr.NAxis3,2)+'_Reads*'+$
		          KeyStr.RefPixStr+'.fits')
     KeyStr.DarkName   =  DarkFiles(0)
   endif
   if stregex(KeyStr.DetStr, 'H2RG-001',/boolean) then begin
     AvgTemp           =  KeyStr.DetTemp
     AvgTemp           =  10*round(float(AvgTemp)/10)
     DarkText          =  Strtrim(AvgTemp,2)+'K'
     DarkFiles         =  File_Search(KeyStr.DarkDir+'MedianDark*'+$
                          DarkText+'*_'+$
                          strtrim(KeyStr.NAxis3,2)+'_Reads*'+$
                          KeyStr.RefPixStr+'.fits')
     KeyStr.DarkName   =  DarkFiles(0)
   endif else begin
     DarkFiles       =  File_Search(KeyStr.DarkDir+'MedianDark*'+$
                        KeyStr.ObjectText+'*_'+$
                        strtrim(KeyStr.NAxis3,2)+'_Reads*'+$
                        KeyStr.RefPixStr+'.fits')
     KeyStr.DarkName =  DarkFiles(0)
   endelse
   KeyStr.DarkKey  =  File_BaseName(KeyStr.DarkName)
   If UseSkyFlat eq 0 then begin
     KeyStr.FlatName =  (File_Search(KeyStr.FlatDir+'MedianFlat_'+$
		        KeyStr.ThisFilter+'*_Reads*'+$
		        KeyStr.RefPixStr+'*Slope.fits'))[0]
   endif else begin
     KeyStr.FlatName =  File_Search(KeyStr.FlatDir+'MedianFlatSky*'+$
                        KeyStr.ThisFilter+'*_Reads*'+$
                        KeyStr.RefPixStr+'*Slope.fits')
   endelse
   KeyStr.FlatKey  =  File_BaseName(KeyStr.FlatName)
endif

@NightParams_xRG.pro
return, 1

End
