;pro Tri_Color_Image
;
;PURPOSE:
;	To form a tri-color image from a set of three images (either 'I' and 
;	'G' and ('I'+'G')/2 or 'I', 'G', and 'Y'

pro Tri_Color_Image, FileList, XStart = XStart, YStart = YStart, $
	RootDir=RootDir, Date=Date, $
	ReducedDir = ReducedDir, ObjectName=ObjectName, NReads = NReads,$
	ThisFilter = ThisFilter, TvFlag = TvFlag, SkipStar=SkipStar, $
	SkipY = SkipY, StretchT = StretchT

  ;Make some common blocks
  Common KeyParams, KeyStr
  Common ColorStr, ColNameStr, ColNumStr, ColorTri, ColorNumTri

  If N_Elements(StretchT) eq 0 then StretchT = 0
  If N_Elements(SkipY) eq 0 then SkipY = 0 
  If N_Elements(SkipStar) eq 0 then SkipStar = 0
 
  ;Get defaults for keywords
  @KeywordStruct.pro
  @PlotSettings
  cd, ReducedDir

  ;Determine whether files should be taken from a list or searched for
  If N_Elements(FileList) eq 0 then begin
     ;Get the files from a directory search
     RawFiles = File_Search(KeyStr.ObjectName+'*'+strtrim(KeyStr.NReads, 2) +$
                 '_Reads*'+FlatFieldStr+'*.fits')
     RawFiles = Return_File_List(RawFiles, ThisFilter=ThisFilter)
     RawFiles = RawFiles(SkipFileNum:N_Elements(RawFiles)-1)
     print, 'Dithering with Files : '
     print, RawFiles
     KeyStr.ObjectDir = KeyStr.ReducedDir+KeyStr.ObjectName+PathDelim
     File_MkDir, KeyStr.ObjectDir
  EndIf else begin
     ;Get the files from a list in a file
     ReadCol, FileList, RawFiles, XOff, YOff, Format = 'A,I,I'
     ;Put the info in the structure
     KeyStr.DitherFiles(0:N_Elements(RawFiles)-1) = RawFiles
     KeyStr.NumDithers = N_Elements(RawFiles)
     ;Get the bright star coordinates from the same file
     ReadCol, FileList, XStart, YStart, Format = 'I,I', $
       SkipLine = N_Elements(RawFiles)
     For i = 0, N_Elements(XStart)-1 do begin 
       KeyStr.RAs(0:N_Elements(RawFiles)-1,i)  = XOff
       KeyStr.DECs(0:N_Elements(RawFiles)-1,i) = YOff
     EndFor
	 
     ;Get the color minima and maxima from the file
     ReadCol, FileList, Mins, MinVals, Maxs, MaxVals, Gams, GammaVals, $
	 Format='A,D,A,D,A,D'
  EndElse

  ;Get a few kewyords, pointings, and offsets
  FileCheck = Return_File_Params(RawFiles(0), /slope)    

  ;Get the offsets of the images
  Return_Dither_Offsets, XStart, YStart, TvFlag=TvFlag, SkipStar = SkipStar
  NumRawFiles = N_elements(RawFiles)

  ;Figure out the biggest dimensions of the colored images
  XSizeMax = 0L & YSizeMax = 0L
  For FileNum = 0, NumRawFiles -1 do begin
     Fits_Read, RawFiles(FileNum), NoData, FitsHeader, /header_only
     XSizeMax = XSizeMax > long(SxPar(FitsHeader, 'NAXIS1'))
     YSizeMax = YSizeMax > long(SxPar(FitsHeader, 'NAXIS2'))
  EndFor

  ;Find the Maximums for the dimensions of the array
  ValidRAs  = KeyStr.XOffs(0:NumRawFiles-1)
  ValidDECs = KeyStr.YOffs(0:NumRawFiles-1)
  MinRAOff  = Min(ValidRAs)  & MaxRAOff  = Max(ValidRAs)
  MinDECOff = Min(ValidDECs) & MaxDECOff = Max(ValidDECs)
  NewArrXSize = XSizeMax + (MaxRAOff-MinRAOff)
  NewArrYSize = YSizeMax + (MaxDECOff - MinDECOff)

  ;Form an array big enough for all the dithers
  FullIms  = DblArr(NewArrXSize, NewArrYSize, 3)
  FoundY = 0
  FiltersUsed = ''

  For FileNum= 0, NumRawFiles - 1 do begin
      print, 'Dithering with File : ' + RawFiles(FileNum)

      ;Read in the frame
      Fits_Read,  RawFiles(FileNum), Frame, FitsHeader
      ThisNaxis1 = long(SxPar(FitsHeader,'NAXIS1'))
      ThisNaxis2 = long(SxPar(FitsHeader,'NAXIS2'))
      ThisFilter = Strtrim(SxPar(FitsHeader, 'FILTER'),2)
      print,min(frame), max(frame)

      ;Put Proper Color in proper spot 
      ;I - 742 nm ; G - 476 nm ; I+G/2 - 620 or so
      If ThisFilter eq 'I' then begin
         Cube = 0
	 FiltersUsed = FiltersUsed+'I'
      EndIf Else if ThisFilter eq 'Y' then begin
         Cube   = 1
	 FoundY = 1
	 If not skipY then FiltersUsed = FiltersUsed+'Y'
      EndIf Else if ThisFilter eq 'G' then begin 
	 Cube = 2
	 FiltersUsed = FiltersUsed+'G'
      EndIf

      print, 'Putting Filter : ' + ThisFilter + ' In ' +Strtrim(Cube,2)
      ;Shift the images in the opposite direction to stack
      XOff = -(KeyStr.XOffs(FileNum)-MaxRAOff)
      YOff = -(KeyStr.YOffs(FileNum)-MaxDECOff)

      ;Put it in the right spot in the dithered large cube
      If not (SkipY and ThisFilter eq 'Y') then $ 
      FullIms(XOff : XOff +ThisNaxis1-1, $
              YOff : YOff +ThisNaxis2-1, $
              Cube) = Frame

  EndFor
  
  ;If no image in Y, make it the average of the I and G
  If FoundY eq 0 or SkipY eq 1 then begin
      FullIms(*,*,1) = (FullIms(*,*,0)+FullIms(*,*,2))/2
      MinVals = [MinVals, MinVals(1)] & MaxVals = [MaxVals, MaxVals(1)]
  EndIf

  If StretchT eq 0 then begin
    ;Linear Scaling
    FullImScaled = BytArr(NewArrXSize, NewArrYSize, 3)
    FullImScaled(*,*,0)=BytScl(FullIms(*,*,0),$
        min=MinVals(1),max=MaxVals(1))
    FullImScaled(*,*,1)=BytScl(FullIms(*,*,1),$
        min=MinVals(2),max=MaxVals(2))
    FullImScaled(*,*,2)=BytScl(FullIms(*,*,2),$
        min=MinVals(0),max=MaxVals(0))
     
  EndIf else if StretchT eq 1 then begin 
    FullImScaled = BytArr(NewArrXSize, NewArrYSize, 3)
    FullImScaled(*,*,0)=GMaScl(FullIms(*,*,0),Gamma=GammaVals(1), $
	min=MinVals(1),max=MaxVals(1))
    FullImScaled(*,*,1)=GMaScl(FullIms(*,*,1),Gamma=GammaVals(2), $
	min=MinVals(2),max=MaxVals(2))
    FullImScaled(*,*,2)=GMaScl(FullIms(*,*,2),Gamma=GammaVals(0), $
  	min=MinVals(0),max=MaxVals(0))
  EndIf

  ;Write a fits file
  Fits_Write, KeyStr.ObjectDir+KeyStr.ObjectName+Strtrim(FiltersUsed,2)+$
	'Color.fits', FullIms
  ;Attempt to put image in right orientation
  FullImScaled = Reverse(FullImScaled,1)

  ;Window,xsize=700,ysize=700
  Set_Plot,'z'
  Write_Tiff, KeyStr.ObjectDir+KeyStr.ObjectName+Strtrim(FiltersUsed,2)+$
	'_Color.tif', $
	red=FullImScaled(*,*,0),$
	green=FullImScaled(*,*,1),$
	blue=FullImScaled(*,*,2),$
	planarconfig=2
  set_plot,'x'
  Window,xsize=700,ysize=700
  Tv,congrid(FullImScaled(1500:2500,2000:3000,*),700,700,3),true=3
  stop

end
