;pro return_leach_file_params
;
;PURPOSE:
;	To return the parameters of a file generated by the LEACH electronics
;	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:
;	FileName - The name of the file from which the keywords will be
;		   extracted.
;
;KEYWORDS:
;	Slope - 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
;
;OUTPUTS:
;	The KeyStr structure will be updated with the keywords from
;	the file FILENAME.

function Return_LEACH_File_Params_2RG, FileName, Slope=Slope, Month = Month, $
	 Year = Year

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

Common KeyParams

  ;Determine whether the ASIC or LEACH system was used
  If Stregex(FileName, 'ASIC', /boolean) then begin
     KeyStr.Electronics    = 0
     KeyStr.ElectronicsStr = 'ASIC'
  EndIf Else Begin
     KeyStr.Electronics    = 1
     KeyStr.ElectronicsStr = 'LEACH'
  EndElse

  ;Get the file information
  KeyStr.RawName = FileName
  ;Make a guess at the object that's being examined
  FileParts = strsplit(KeyStr.RawName, KeyStr.PathDelim,/extract,count=NumDirs)
  If NumDirs gt 0 then begin
     FileBase = FileParts(NumDirs-1)
     FileDir  = File_DirName(KeyStr.RawName)
  EndIf Else begin
     FileBase = KeyStr.RawName
     FileDir  = KeyStr.RootDir
  EndElse
  LastPathD   = strpos(KeyStr.RawName, KeyStr.PathDelim, /reverse_search)
  LastDot     = strpos(KeyStr.RawName, '.', /reverse_search)
  KeyStr.RawKey        = strmid(KeyStr.RawName,LastPathD+1,LastDot-(LastPathD+1))
  KeyStr.RawDir        = FileDir+KeyStr.PathDelim
  KeyStr.RawHeaderFile = KeyStr.RawDir+KeyStr.PathDelim+'Header_'+FileBase

  ;Look at the header to get some info. And keep it for the LEACH
  If Stregex(KeyStr.RawName, 'Median', /boolean) or $
  	 Stregex(KeyStr.RawName, 'Slope', /boolean) then begin
    Fits_Read, KeyStr.RawName, NoData, FitsHeader, /header_only
    KeyStr            = Create_Struct(KeyStr, 'RawHeader', FitsHeader)
    KeyStr.TotalReads = Long(SxPar(FitsHeader,'NAXIS3'))
    KeyStr.ThisFilter = Strtrim(SxPar(FitsHeader, 'FILTER'),2)
  EndIf Else If Stregex(KeyStr.RawName, 'Flat', /boolean) or $
  				Stregex(KeyStr.RawName, 'Dark', /boolean) then Begin
    Fits_Read, KeyStr.RawName, NoData, FitsHeader, /header_only
    KeyStr            = Create_Struct(KeyStr, 'RawHeader', FitsHeader)
    KeyStr.TotalReads = Long(SxPar(FitsHeader,'NAXIS3'))
    KeyStr.ThisFilter = KeyStr.ThisFilter
  EndIf Else begin
    Fits_Read, KeyStr.RawHeaderFile, NoData, FitsHeader, /header_only
    If (Where(StrMatch(Tag_Names(KeyStr), 'RAWHEADER') eq 1))[0] $
        eq -1 then KeyStr = Create_Struct(KeyStr, 'RawHeader', FitsHeader)
    KeyStr.NGroups    = Long(SxPar(FitsHeader, 'NGROUPS'))
    KeyStr.NReads     = Long(SxPar(FitsHeader, 'NREADS'))
    KeyStr.TotalReads = KeyStr.NGroups*KeyStr.NReads
    KeyStr.NAxis3     = KeyStr.TotalReads
    KeyStr.ThisFilter = Strtrim(SxPar(FitsHeader, 'FILTER'),2)

  EndElse

  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'))

  ;Form the Reduced, Dark, and Flat Directories
  KeyStr.ReducedDir = KeyStr.RootDir+KeyStr.ElectronicsStr+KeyStr.PathDelim+$
		             'Reduced'+KeyStr.PathDelim+KeyStr.Date+KeyStr.PathDelim
  KeyStr.DarkDir    = KeyStr.ReducedDir+'Dark'+KeyStr.PathDelim
  KeyStr.FlatDir    = KeyStr.ReducedDir+'Flat'+KeyStr.PathDelim
  If Stregex(KeyStr.RawName, 'Flat', /boolean) then $
     KeyStr.ReducedDir = KeyStr.FlatDir
  If Stregex(KeyStr.RawName, 'Dark', /boolean) then $
     KeyStr.ReducedDir = KeyStr.DarkDir

  ;******************DARKS************************************
  ;Get the Median Darks and Flats for this image if applicable
  KeyStr.DarkName = (File_Search(KeyStr.DarkDir+'Median*'+$
                        Strtrim(KeyStr.TotalReads,2)+'Reads*'+$
                        KeyStr.RefPixStr+'*.fits'))[0]
  KeyStr.DarkKey = File_BaseName(KeyStr.DarkName)

  ;*****************FLAT BEHAVIOR****************************
  ;Determine where to write the files
  If Not Stregex(KeyStr.ObjectName, 'Flat', /boolean) then begin
    KeyStr.ObjectName = (strsplit(FileBase,'_',/extract))[0]
  EndIf
  KeyStr.FlatName = (File_Search(KeyStr.FlatDir+'MedianFlat*'+$
		     KeyStr.ThisFilter+'_Slope.fits'))[0]
  ;Determine where to output the files
  If Stregex(KeyStr.ObjectName, 'Dark', /boolean) then begin
        KeyStr.ObjectDir = KeyStr.DarkDir
  EndIf Else If Stregex(KeyStr.ObjectName, 'Flat', /boolean) then begin
        KeyStr.ObjectDir = KeyStr.FlatDir
  EndIf Else begin
        KeyStr.ObjectDir  = KeyStr.ReducedDir+KeyStr.PathDelim+$
                      KeyStr.ObjectName+KeyStr.PathDelim
  EndElse
  File_MkDir, KeyStr.ObjectDir

  @NightParams_2RG.pro
  return, 1


End

