;pro return_file_params
;
;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:
;	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_File_Params, FileName, Slope=Slope, Month = Month, $
	 Year = Year

If N_Elements(Year)  eq 0 then Year  = '07'
If N_Elements(Month) eq 0 then Month = 'Apr'
If N_Elements(Slope) eq 0 then Slope = 0

Common KeyParams

  ;Get the header of the file
  If Not Slope then cd, KeyStr.RootDir+KeyStr.PathDelim+KeyStr.Date
  KeyStr.RawName = FileName
  Fits_Read, FileName, NoData, FitsHeader, /header_only

  ;Parameters from Header
  KeyStr.WindowMode = long(SxPar(FitsHeader, 'WINDOWM'))
  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.NReads     = long(SxPar(FitsHeader, 'NREADS'))
  KeyStr.NGroups    = long(SxPar(FitsHeader, 'NGROUPS'))
  KeyStr.NDrops     = long(SxPar(FitsHeader, 'NDROPS'))
  KeyStr.NOUT       = long(SxPar(FitsHeader, 'NOUT'))
  If KeyStr.WindowMode then KeyStr.NOUT = 1
  KeyStr.MCDFile    = Strtrim(SxPar(FitsHeader, 'MCD_FILE'),2)
  KeyStr.ThisFilter = Strtrim(SxPar(FitsHeader, 'FILTER'),2)
  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')

  ;Parameters relating to processing files/directory structure
  KeyStr.RawFullPath = File_Expand_Path(KeyStr.RawName)
  KeyStr.Date = strmid(File_Expand_Path(KeyStr.RawFullPath),$
     stregex(KeyStr.RawFullPath,KeyStr.Year+KeyStr.Month),7)
  If stregex(KeyStr.RootDir, 'Reduced', /boolean) then begin
     KeyStr.ReducedDir=KeyStr.RootDir
  EndIf else begin
     KeyStr.ReducedDir = $
     KeyStr.RootDir+KeyStr.PathDelim+'Reduced'+KeyStr.PathDelim+$
	    KeyStr.Date+KeyStr.PathDelim
  EndElse
  ;Find out if the file is flat fielded
  If Stregex(KeyStr.RawName, 'FlatFielded', /boolean) then $
     KeyStr.FlatFieldStr = 1

  ;Find out whether it is a slope
  If Stregex(KeyStr.RawName, 'Slope*.fits', /boolean) then $
     KeyStr.TotalReads = $
	long(strmid(KeyStr.RawName, stregex(KeyStr.RawName, '_Reads')-2,2))

  ;Form the dark directory and the flat directory
  KeyStr.DarkDir = KeyStr.ReducedDir+KeyStr.PathDelim+'Darks'+KeyStr.PathDelim
  KeyStr.FlatDir = KeyStr.ReducedDir+KeyStr.PathDelim+'Flats'+KeyStr.PathDelim
 
  ;Make a guess at the object that's being examined
  FileParts = strsplit(KeyStr.RawName, '/',/extract,count=NumDirs)
  If NumDirs gt 0 then begin
     FileBase = FileParts(NumDirs-1)
  EndIf Else begin
     FileBase = KeyStr.RawName
  EndElse

  ;Determine where to write the files
  KeyStr.ObjectName = (strsplit(FileBase,'_',/extract))[0]
  ;Eliminate the H4RG in the objectname
  If Stregex(KeyStr.ObjectName, 'H4RG',/boolean) then $
    KeyStr.ObjectName = strmid(keystr.objectname,0,$
			stregex(keystr.objectname,'H4RG'))
  ;Determine where to output the files
  If Stregex(KeyStr.ObjectName, 'Dark', /boolean) then begin
	KeyStr.ObjectDir = KeyStr.ReducedDir+KeyStr.PathDelim+$
                           'Darks'+KeyStr.PathDelim
  EndIf Else If Stregex(KeyStr.ObjectName, 'Flat', /boolean) then begin
	KeyStr.ObjectDir = KeyStr.ReducedDir+KeyStr.PathDelim+$
                           'Flats'+KeyStr.PathDelim
  EndIf Else begin
        KeyStr.ObjectDir  = KeyStr.ReducedDir+KeyStr.PathDelim+$
		      KeyStr.ObjectName+KeyStr.PathDelim
  EndElse
  File_MkDir, KeyStr.ObjectDir
 
  ;Get the Key Identifiers for the file
  KeyStr.RawKey = (strsplit(FileBase,'.',/extract))[0]

  ;Look for 60 Hz removal
  If KeyStr.Rem60Hz eq 1 then KeyStr.Rem60HzStr = '_60HzRemoved'

  ;If Not Stregex(KeyStr.ObjectName, 'Dark', /boolean) and $
  ;   Not Stregex(KeyStr.ObjectName, 'Bias', /boolean) then begin
  KeyStr.DarkName = (File_Search(KeyStr.DarkDir+'Median*'+$
			Strtrim(KeyStr.TotalReads,2)+'Reads'+$
			KeyStr.Rem60HzStr+'.fits'))[0]
  KeyStr.DarkKey = File_BaseName(KeyStr.DarkName)
  ;EndIf 
  If Not Stregex(KeyStr.ObjectName, 'Dark', /boolean) and $
     Not Stregex(KeyStr.ObjectName, 'Bias', /boolean) then begin
     KeyStr.FlatName = (File_Search(KeyStr.FlatDir+'Median*'+$
			Strtrim(KeyStr.ThisFilter,2) +'*2Reads.fits'))[0]
    If KeyStr.FlatName ne '' then begin
      KeyStr.FlatKey  = File_BaseName(KeyStr.FlatName) 
    EndIf
  EndIF

  ;Update the linearity limits
  @NightParams.pro

  return, 1

End
