;NAME:
;  pro cmos_ramp_diff_slideshow
;
;PURPOSE:
;  To provide a visual display of the difference between two successive reads
;  of an H2RG array dark exposure.  The difference image should consist 
;  entirely of cosmic ray events.
;
;CALLING SEQUENCE:
;  cmos_ramp_diff_slideshow,[filename[,cmos_dir]]
;
;KEYWORDS: 
;  filename             the name of the particular .fits file that will
;                       be displayed
;  cmos_dir             the path to the directory containing the files
;                       the default is 
;                       /nfs/slac/g/ki/ki08/lsst/sipinrocwell/65_Meg_Files
;
pro analysecmosimage,Filename=Filename,$
    Threshold=Threshold,SearchRegion=SearchRegion,$
    Sigma=Sigma,$
    ThresholdSlope=ThresholdSlope,$
    MinThreshold=MinThreshold,$
    TvFlag=TvFlag

If (not keyword_set(MinThreshold)) then MinThreshold = 1500
If (not keyword_set(ThresholdSlope)) then ThresholdSlope = 0
If (not keyword_set(Sigma)) then Sigma = 1.1
If (not keyword_set(FileName)) then Filename ='dark_140.K_65_30000_1_02.fits'
If (not keyword_set(TvFlag)) then TvFlag=1

CMOSDir='/nfs/slac/g/ki/ki13/lances/darkcurrent_test.9Oct04/'
CMOSFilePath=CMOSDir+FileName
Fits_Read,CMOSFilePath,Im,H       ;read in image and header
NAXIS1=sxpar(h,'NAXIS1')          ;number of pixels in a row
NAXIS2=sxpar(h,'NAXIS2')          ;number of pixels in a column
TotReads=sxpar(h,'NAXIS3')        ;number of reads in the file
ArrSize=NAXIS1*NAXIS2	          ;number of pixels in single read
threshold=1000                    ;Threshold to flag pixel as Cosmic Ray


DiffArr=fltarr(2)
If TvFlag eq 1 then begin
  set_plot,'x'
  window,xsize=512,ysize=624
endif
for ReadNum=6,TotReads-2 do begin
  ;Take only two reads into memory...5GB files can't be opened with mrdfits
  Fits_Read,CMOSFilePath,frame1,first=long(ReadNum*ArrSize),$
                            last=long((ReadNum+1)*Arrsize)-1
  Fits_Read,CMOSFilePath,frame2,first=long((ReadNum+1)*ArrSize),$
                            last=long((ReadNum+2)*ArrSize)-1
  Diff=float(Frame2-Frame1)
  ;frame1=reform(float(frame1),row_count,column_count)          
  ;frame2=reform(float(frame2),row_count,column_count)
  Diff=reform(float(Diff),NAXIS1,NAXIS2)
  tv,bytscl(congrid(diff,512,512,/center),min=0,max=1000)
  ;tv,congrid(diff,1024,1024)
  colorbar,range=[min(Diff),max(Diff)],$
        format='(f10.2)',$
        position=[0.1,0.92,0.9,0.97],color=255,$
        title='intensity'
  xyouts,.1,.85,'Read Numbers '+strtrim(ReadNum,2)+' and '+$
         strtrim(ReadNum+1,2),color=255,/normal
  wait,0.9
  erase
endfor
stop

end

