;make last_minus_first
;
;PURPOSE:
;  To create a .fits file consisting of only the last minus first read.
;  This deals with the large files that ds9 cannot handle
;KEYWORDS:
;  MKJPG - if set, a .tif image will be generated that has the 
;            coordinates, XStartJPG, XStopJPG, YStartJPG, YStopJPG
;  MKSLDSHW - if set, a 10 image slide sequence of reads will be output
;	      in .tif format
;  VERTICAL - if set, the slideshow will be vertical rather than horizontal
;  NUMSEQ   - the number of slides to put in the image.  They will be distributed
;	      uniformly in time throughout the ramp
;EXAMPLES:
;make_last_minus_first_1rg,'/nfs/slac/g/ki/ki09/lances/H1RG/KPNO_11_07/ASIC/07Nov19/NGC7419_I_Dither1_20ArcSecondsEast_0ArcSecondsNorth_H1RG_SIPIN_100_Reads_Nov19_2007_21_01_10.fits',/MakeJPG, date='07Nov19'
;
;VSUBS =======================
;
;
;SUPER LONG RAMP===============
;make_last_minus_first_1rg,'/nfs/slac/g/ki/ki09/lances/H1RG/KPNO_11_07/ASIC/07Nov19/SAO5417_I_NegPerDark_H1RG_SIPIN_2500_Reads_Nov19_2007_22_45_50.fits', date='07Nov19'
;make_last_minus_first_1rg,'/nfs/slac/g/ki/ki09/lances/H1RG/KPNO_11_07/ASIC/07Nov19/SAO5417_I_NegPerDark_H1RG_SIPIN_2500_Reads_Nov19_2007_22_45_50.fits', date='07Nov19', xstartjpg=318, xstopjpg = 358, ystartjpg = 245, ystopjpg = 285, /mkjpg
pro make_last_minus_first_1rg, FitsFileName, $
	TvFlag = TvFlag, Mode = Mode, Date = Date, Leach=Leach, $
	MkJPG = MkJPG, MkSldShw = MkSldShw, Vertical = Vertical,$
	XStartJPG = XStartJPG, XStopJPG = XStopJPG,$
	YStartJPG = YStartJPG, YStopJPG = YStopJPG, $
	FullFrame=FullFrame, Last = Last, NumSeq = NumSeq
 
;Include all the keywords from the KeywordStruct.pro file
Common KeyParams, KeyStr
@KeywordStruct_1RG.pro
@PlotSettings_1RG.pro
;Load the color table that looks like the ds9 BB
loadct,3

;*****KEYWORDS**************************************************************************
If N_Elements(Date)	 eq 0 then Date      = '07Nov19'
If N_Elements(Mode)      eq 0 then Mode      = 1
If N_Elements(TvFlag)    eq 0 then TvFlag    = 0
If N_Elements(Leach)	 eq 0 then Leach     = 0
If N_Elements(MkJPG)     eq 0 then MkJPG     = 0
If N_Elements(MkSldShw)  eq 0 then MkSldShw  = 0
If N_Elements(XStartJPG) eq 0 then XStartJPG = 378
If N_Elements(XStopJPG)  eq 0 then XStopJPG  = 420
If N_Elements(YStartJPG) eq 0 then YStartJPG = 450
IF N_Elements(YStopJPG)  eq 0 then YStopJPG  = 490
If N_Elements(FullFrame) eq 0 then FullFrame = 0
If N_Elements(Vertical)  eq 0 then Vertical  = 0
If N_Elements(NumSeq)    eq 0 then NumSeq    = 10
If FullFrame then begin
   XStartJPG = 0 & XStopJPG = KeyStr.Naxis1-1
   YStartJPG = 0 & YStopJPG = KeyStr.Naxis2-1
EndIf

If KeyStr.TvFlag eq 1 then begin
    If Vertical eq 0 then begin
      window, 0, xpos=0, ypos=100, xsize=1000, ysize=200
    Endif else begin
      window, 0, xpos=0, ypos=100, xsize=280, ysize=700
    EndElse
    !p.charthick=1.9
    !p.charsize=1.9
    !p.thick=2.0
    plot, [0,0], /nodata, background=fsc_color('white')
EndIf Else If KeyStr.TvFlag eq 3 then begin
    Set_Plot,'z', /copy
    device, z_buffer=0
    If Vertical eq 0 then begin
      device, set_resolution = [ 1000, 400]
    EndIf else begin
      device, set_resolution = [ 400, 1000]
    EndElse
    loadct, 3
    plot, [0,0], /nodata, background=fsc_color('white')
EndIf

;The Default File is the one with the negative persistence
KeyStr.RawFullPath = FitsFileName
KeyStr.RawName     = FitsFileName
FileParams = Return_File_Params_1RG(KeyStr.RawFullPath)

;Allow for the last read to be one other than NAXIS3
If N_Elements(Last) ne 0 then begin
   LastRead = Last
EndIf Else Begin
   LastRead = KeyStr.Naxis3-1
EndElse

;Output JPG showing development of persistence
NumImages = NumSeq
ImageMult = KeyStr.Naxis3/NumImages


;***************************************************************************
;Ramp of pixel exhibiting positive persistence
Fits_read_datacube, KeyStr.RawFullPath, ImL, NH, $
    ZStart = LastRead, ZStop = LastRead
Fits_read_datacube, KeyStr.RawFullPath, ImF, NH, $
    ZStart = 0, ZStop = 0
LMF = Long(ImL)-Long(ImF)
Fits_Write, KeyStr.ReducedDir+KeyStr.Date+KeyStr.PathDelim+$
	    KeyStr.RawKey+'LMF.fits', LMF

If MkJPG then begin
   SubIm = LMF(XStartJPG:XStopJPG, YStartJPG:YStopJPG)
   MeanSubIm   = Mean(SubIm)
   StdDevSubIm = StdDev(SubIm)
   MinSubIm    = MeanSubIm-0.5*StdDevSubIm
   MaxSubIm    = MeanSubIm+1.5*StdDevSubIm
   window, xsize = 512, ysize=512
   plot, [0,0], /nodata, background=fsc_color('white')
   TvImage, bytscl(congrid(SubIm,768,768), $
	    min = MinSubIm, $
	    max = MaxSubIm), $ 
	background=fsc_color('white'), $
	position = [0.00, 0.1, 1.0, 1.0], /normal
   ColorBar, position = [0.05, 0.05, 0.95, 0.08], $
          minrange = MinSubIm, maxrange = MaxSubIm, $
          charsize=1.5, charthick=1.5, $
          color = fsc_color('black')
   Img  = tvrd(true=3)
   Img24=reverse(Img,2)
   TVLCT, R, G, B, /Get
   Write_Tiff, KeyStr.PlotDir+KeyStr.RawKey+'_'+$
	 Strtrim(XStartJPG,2)+'-'+Strtrim(XStopJPG,2)+$
	 Strtrim(YStartJPG,2)+'-'+Strtrim(YStopJPG,2)+'.tif', $
         red=Img24(*,*,0), green=Img24(*,*,1), blue=Img24(*,*,2), $
         planarconfig=2, orientation=1, compression=0
EndIf
;*************************************************************************
;SlideShow of 10 reads in sequence
If MkSldShw then begin
  Fits_read_datacube, KeyStr.RawFullPath, Im, FitsHeader, $
        XStart = XStartJPG, XStop = XStopJPG,$
        YStart = YStartJPG, YStop = YStopJPG
  XBoxSize = XStopJPG - XStartJPG
  YBoxSize = YStopJPG - YStartJPG
  ImBias  = Long(Im(*,*,0)) 
  ImMB    = LonArr(XBoxSize+1, YBoxSize+1,KeyStr.Naxis3)

  For FrameNum = 0, KeyStr.Naxis3-1 do begin
    ImMB(*,*,FrameNum) = Im(*,*,FrameNum)-ImBias(*,*)
  Endfor
  MinIm  = min(ImMB)
  MaxIm  = max(ImMB)
  MeanIm   = Mean(ImMB)
  StdDevIm = StdDev(ImMB)
  MinIm    = MeanIm-2.5*StdDevIm
  MaxIm    = MeanIm+5.5*StdDevIm

  ITIME  = Float(SxPar(FitsHeader, 'ITIME'))
  Naxis1 = Long(SxPar(FitsHeader, 'NAXIS1'))
  Naxis2 = Long(SxPar(FitsHeader, 'NAXIS2'))
  Depth  = Long(SxPar(FitsHeader, 'NAXIS3'))
  KeyStr.Naxis3 = Depth
  MeanBoxArr = Fltarr(KeyStr.Naxis3)
  For FrameNum = 0, KeyStr.Naxis3-1 do begin

    FrameTime = ITIME/Depth
    MeanBoxArr[FrameNum] = mean(ImMB(*,*,FrameNum))
    print, MeanBoxArr[FrameNum] 
    If FrameNum mod ImageMult eq 9 and FrameNum gt 0 then begin
       XOffSet = FrameNum/ImageMult
       YOffSet = FrameNum/ImageMult
       If Vertical eq 0 then begin
           tvimage, bytscl(congrid(ImMB(*,*,FrameNum),  512,512), $
                         min = MinIm, max = MaxIm), $
             position = [XOffset*0.2, 0.5*(XOffset/5), $
		         XOffset*0.2+0.2, 0.5+0.5*(XOffset/5)], $
             /normal, background=fsc_color('white')
           xyouts, XOffset*0.1+0.01, 0.9, 'Read ' + Strtrim(FrameNum,2), $
             color=fsc_color('white'), /normal
        EndIf Else begin
           tvimage, bytscl(congrid(ImMB(*,*,FrameNum),  512,512), $
                         min = MinIm, max = MaxIm), $
             position = [(YOffset/5)*0.5,     (YOffset mod 5)*0.18+.1,$
			 (YOffset/5)*0.5+0.5, (YOffset mod 5)*0.18+0.18+.1], $
             /normal, background=fsc_color('white')
           xyouts, (Yoffset/5)*0.5, (YOffset mod 5)*0.18+0.12, 'Read ' + Strtrim(FrameNum,2), $
             color=fsc_color('white'), /normal
        EndElse
     EndIf
   EndFor
   If Vertical eq 0 then begin
       ColorBar, position = [0.06, 0.1, 0.08, 0.9], /vertical, $
          minrange = MinIm, maxrange = MaxIm, $
          charsize=1.5, charthick=1.5, $
          color = fsc_color('black')
   Endif Else begin
       ColorBar, position = [0.1, 0.06, 0.9, 0.08],  $
          minrange = MinIm, maxrange = MaxIm, $
          charsize=1.0, charthick=1.0, $
          color = fsc_color('black')
   EndElse
   If KeyStr.TvFlag eq 1 or KeyStr.TvFlag eq 3 then begin
      Img  = tvread(/tiff, FileName = KeyStr.PlotDir+KeyStr.RawKey+$
                     '_'+Strtrim(XStartJPG,2)+'_'+Strtrim(XStopJPG,2)+$
		     '_'+Strtrim(YStartJPG,2)+'_'+Strtrim(YStopJPG,2)+'ReadSequence', $
                     /nodialog)
      stop
      Img24=reverse(Img,2)
      TVLCT, R, G, B, /Get
      Write_Tiff, KeyStr.PlotDir+KeyStr.RawKey+'_'+Strtrim(XPix,2)+'_'+$
        Strtrim(YPix,2)+'ReadSequence.tif', $
        red=Img24(*,*,0), green=Img24(*,*,1), blue=Img24(*,*,2), $
        planarconfig=2, orientation=1, compression=0
       stop
     EndIf
EndIf

stop

end
