;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
;pro form_dither_list_xrg
;
;PURPOSE:
;  To create a file that can be used with Combine_Dither_xRG.pro.  This file
;  contains all of the filenames that will be included in the mosaic along with
;  an approximation of offsets of each of the files from the others.
;
;  The format of the file will be the following
;   
;  FitsFileName  (\t)  XOffset  (\t)    YOffset
;
;  where FitsFileName is the fully qualified path of the file and XOffset
;  and YOffset are the offsets of object positiions in that image relative to the
;  object position in the first file of the stack.
;
;INPUTS:
;    A combination of:
;          DETSTR       - 'H1RG-022', 'H2RG-32-147', 'H4RG-007' 
;          ELECSTR      - 'ASIC', 'LEACH'
;	   DATE		- The date when the exposures were taken (ex. '07Dec12')
;          NREADS       - Number of reads in all of the files
;          OBJECTNAME   - The name of the object ('M1', 'M44', etc.)
;          THISFILTER   - The filter used to observe ('G', 'I', 'Y')
;          OUTNAME      - The name of the output file
;          [ObjectText] - Optional string for LEACH files
;
;  OUTNAME: (string) 
;    The name of the output file.  It will be placed in OBJECTDIR.
;
;KEYWORDS:
;  OBJECTTEXT: (string)
;    A string that identifies all of the files to be included in the output 
;    file list.  It can be a regular expression that matches files with * or ?.
;    Also, this string should contain the filter name.
;
;  MODE: (int)
;    The type of dither pattern that was performed.  It's either
;	2 - 2x2 box
;	3 - 3x3 box
;  RAWOUTNAME: (string)
;    If this keyword is set to a non-zero string value, the list of raw files sorted by
;    time of exposure will be output to this filenam, i.e. OutDir+RawOutName.
;
;OUTPUTS:
;  A file with the name [FitsDir+'/'+Outname] that will contain the filenames and offsets.
;
;CALLING SEQUENCE:
;  Form_Dither_List_xRG, DetStr, ElecStr, Date, NReads, ObjectName, ThisFilter, 
;                        OutName
;
;EXAMPLES:
;  ASIC:
;    form_dither_list_xrg, 'H2RG-32-147','ASIC','07Dec14',30, 'M77', 'Y', 'M77_Y_Slopes.lst', objecttext='Y_Dither'
;    form_dither_list_xrg, 'H2RG-32-147','ASIC','07Dec14',30, 'M77', 'I', 'M77_I_Slopes2.lst', objecttext='I_Dither', RawOutName = 'M77_I_RawFiles.lst'
;  LEACH:
;    form_dither_list_xrg, 'H1RG-022', 'LEACH', '2007Nov14', 200, 'NGC708', 'I', 'NG708_I_Slopes.lst', objecttext='200_1_2'
;    form_dither_list_xrg, 'H1RG-022', 'LEACH', '2007Nov18', 300, '17PHolmes', 'I', '17PHolmes_I_Slopes.lst', objecttext='1_0'
;    form_dither_list_xrg, 'H1RG-022', 'LEACH', '2007Nov18', 600, 'NGC2416', 'I', 'NGC2416_I_Slopes.lst', objecttext='1_0'
;NOTES:
;
pro Form_Dither_List_xRG, DetStr, ElecStr, Date, NReads, ObjectName, ThisFilter,$
	                  OutName, RawOutName = RawOutName, ObjectText = ObjectText, Mode=Mode

  ;Keyword Parameters
  If N_Elements(Mode)       eq 0 then Mode = 3
  If N_Elements(ObjectText) eq 0 then ObjectText = ''
  If N_Elements(RawOutName) eq 0 then RawOut = 0 else RawOut = 1
  If N_Elements(OutName)    eq 0 then OutName = ObjectName+'_'+ThisFilter+'_Slopes.lst'

  ;Prepare the common block
  Common KeyParams, KeyStr

  ;Get all of the defaults for the keywords from the KeywordStruct file
  @KeywordStruct_xRG.pro

  ;Get the raw files that were turned into slope fits for the date ordering
  ;THIS NEEDS TO BE MODIFIED AS THE TIME WILL BE WRITTEN TO THE SLOPE HEADER
  RawFiles    = File_Search(KeyStr.RawObjectDir+KeyStr.PathDelim+$
			    KeyStr.ObjectName+'*'+KeyStr.ObjectText+'*.fits', $
			    Count= NumRawFiles, /Fully_Qualify_Path)
  RawFiles    = Return_File_List_xRG(RawFiles, ThisFilter=KeyStr.ThisFilter,$ 
                                     ThisNAxis3 = KeyStr.NAxis3)
  NumRawFiles = N_Elements(RawFiles)
  RawTimes    = strarr(NumRawFiles)
  For FileNum = 0, NumRawFiles-1 do begin
      Fits_Read, RawFiles(FileNum), D, Header, /header_only
      RawTimes(FileNum) = SxPar(Header, 'TIME')
  EndFor

  ;Get the files that should be used in the dither
  DitherFiles = File_Search(KeyStr.RedObjectDir+KeyStr.PathDelim+$
			    KeyStr.ObjectName+'*'+ObjectText+'*FlatFielded.fits', $
                            /fully_qualify_path, Count=NumDitherFiles)
  DitherFiles = Return_File_List_xRG(DitherFiles, ThisFilter=KeyStr.ThisFilter,$
                                     ThisNAxis3=0)
  NumDitherFiles = N_Elements(DitherFiles)
  DitherFiles = DitherFiles(sort(RawTimes))
  RawFiles    = RawFiles(sort(RawTimes))

  ;/////////////////////////////////////////////////////////////////////////////
  ;Get the file handle for the output file
  Get_Lun, OutFile
  OpenW, OutFile, KeyStr.RedObjectDir+OutName

  if RawOut ne 0 then begin
    Get_Lun, RawOutFile
    OpenW, RawOutFile, KeyStr.RedObjectDir+RawOutName
  endif

  If KeyStr.ElecStr eq 'ASIC' then begin
    If Mode eq 3 then begin
      RADECText = ['Dither0_0ArcSecondsEast_0ArcSecondsNorth',$
                   'Dither1_20ArcSecondsEast_0ArcSecondsNorth',$
                   'Dither2_20ArcSecondsEast_20ArcSecondsNorth',$
                   'Dither3_0ArcSecondsEast_20ArcSecondsNorth',$
                   'Dither4_20ArcSecondsWest_20ArcSecondsNorth',$
                   'Dither5_20ArcSecondsWest_0ArcSecondsNorth',$
                   'Dither6_20ArcSecondsWest_20ArcSecondsSouth',$
                   'Dither7_0ArcSecondsEast_20ArcSecondsSouth',$
                   'Dither8_20ArcSecondsEast_20ArcSecondsSouth']
      RAOffset  = [0, 20, 20,  0,  -20, -20, -20,   0, 20]
      DECOffset = [0, 0 , 20,  20,  20,   0, -20, -20, -20]

    EndIf Else Begin
      RADECText = ['Dither0_0ArcSecondsEast_0ArcSecondsNorth',$
                   'Dither1_20ArcSecondsEast_0ArcSecondsNorth',$
                   'Dither2_20ArcSecondsEast_20ArcSecondsNorth',$
                   'Dither3_0ArcSecondsEast_20ArcSecondsNorth']
      RAOffset  = [0, 20, 20,  0]
      DECOffset = [0, 0 , 20,  20]

    EndElse

      RAMin    = 20*(1./KeyStr.PlateScale)
      DECMin   = 20*(1./KeyStr.PlateScale)
      For FileNum = 0, NumDitherFiles-1 do begin
        RADECMatch = 0
	RADECInd   = 0
        while RADECMatch eq 0 do begin
          if Stregex(DitherFiles(FileNum), RADECText(RADECInd), /boolean) $
          then RADECMatch = 1 else RADECInd+=1
        endwhile 
        RAOff  = KeyStr.EWKey*(1./KeyStr.PlateScale)*RAOffset(RADECInd)
	DECOff = KeyStr.NSKey*(1./KeyStr.PlateScale)*DECOffset(RADECInd)
        printf, OutFile, DitherFiles(FileNum), RAOff, DECOff, format='(A, I, I)'
        if RawOut ne 0 then printf, RawOutFile, RawFiles(FileNum), format='(A)'

      EndFor

  EndIf Else If KeyStr.ElecStr eq 'LEACH' then Begin
    if KeyStr.Date ne '2007Nov18' and KeyStr.Date ne '2007Nov13_2' then begin
      RAOffset   = [-20., -20., -20.,   0.,  0.,  20., 20., 20., 0.]
      DECOffset  = [-20., 0.  , 20. , -20., 20., -20., 0  , 20., 0.]
      RADECText  = ['_-20_-20_0_', '_-20_0_0_', '_-20_20_0_', $
                  '_0_-20_0_'  , '_0_20_0_',   '_20_-20_0_' , $
                  '_20_0_0_' , '_20_20_0_', '_0_0_0_']
    endif else if stregex(KeyStr.Date, '2007Nov13_2', /boolean) then begin
      RAOffset   = [-20., -20., -20.,   0.,  0.,  20., 20., 20., 0.]
      DECOffset  = [-20., 0.  , 20. , -20., 20., -20., 0  , 20., 0.]
      RADECText  = ['_0_-20_-20_', '0_-20_0_', '_0_-20_20_', $
                  '_0_0_-20_'  , '_0_0_20_',   '_0_20_-20_' , $
                  '_0_20_0_' , '_0_20_20_', '_0_0_0_']
    endif else begin
      DECOffset  = [-20., -20., -20.,   0.,  0.,  20., 20., 20., 0.]
      RAOffset   = [-20., 0.  , 20. , -20., 20., -20., 0  , 20., 0.]
      RADECText  = ['_-20_-20_VSUB15', '_-20_0_VSUB15', '_-20_20_VSUB15', $
                  '_0_-20_VSUB15'  , '_0_20_VSUB15',   '_20_-20_VSUB15' , $
                  '_20_0_VSUB15' , '_20_20_VSUB15', '_0_0_VSUB15']
    endelse
    RAMin	   = 20*(1./KeyStr.PlateScale)
    DECMin	   = 20*(1./KeyStr.PlateScale)
    For FileNum = 0, NumDitherFiles -1 do begin
        RADECMatch = 0
        RADECInd   = 0
        while RADECMatch eq 0 do begin
          if Stregex(DitherFiles(FileNum), RADECText(RADECInd), /boolean) $
          then RADECMatch = 1 else RADECInd+=1
        endwhile
        print, radecind
        if KeyStr.Date eq '2007Nov14' or KeyStr.Date eq '2007Nov13_2' then begin
          RAOff  = KeyStr.EWKey*((1./KeyStr.PlateScale)*DECOffset(RADECInd)+RAMin)
          DECOff = KeyStr.NSKey*((1./KeyStr.PlateScale)*RAOffset(RADECInd)+DECMin)
        endif else begin
          RAOff  = KeyStr.EWKey*((1./KeyStr.PlateScale)*RAOffset(RADECInd)+RAMin)
          DECOff = KeyStr.NSKey*((1./KeyStr.PlateScale)*DECOffset(RADECInd)+DECMin)
        endelse
        printf, OutFile, DitherFiles(FileNum), RAOff, DECOff, format='(A, I, I)'
        if RawOut ne 0 then printf, RawOutFile, RawFiles(FileNum), format='(A)'
    EndFor
  EndIf

  ;Close the file and free the handle
  Close, OutFile
  Free_Lun, OutFile
  if RawOut ne 0 then begin
    Close, RawOutFile
    Free_Lun, RawOutFile
  endif
  stop

end
