;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, 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

;*****KEYWORDS**************************************************************************
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

;***************************************************************************
;Get the last read value
Fits_read, FitsFileName, I, H, /Header_Only
LastRead = Long(SxPar(H, 'NAXIS3'))

;Get the Key of the file
Path_Delim  = Path_Sep()
LastPathD   = strpos(FitsFileName, Path_Delim, /reverse_search)
LastDot     = strpos(FitsFileName, '.', /reverse_search)
RawKey      = strmid(FitsFileName, LastPathD+1,LastDot-(LastPathD+1))
RawDir      = strmid(FitsFileName,0, LastPathD+1)
If RawDir eq '' then RawDir = '.'+Path_Sep()

Fits_read_datacube, FitsFileName, ImL, NH, $
    ZStart = LastRead-1, ZStop = LastRead-1
Fits_read_datacube, FitsFileName, ImF, NH, $
    ZStart = 0, ZStop = 0
LMF = Long(ImL)-Long(ImF)
Fits_Write, RawDir+Path_Delim+RawKey+'_LMF.fits', LMF

stop

end
