Pro soarimages,date=date,SingleImage=SingleImage,$
	       threshold=threshold
If (not keyword_set(SingleImage)) then SingleImage = 1
If (not keyword_set(date)) then date = '11'
If (not keyword_set(Threshold)) then Threshold = 1500
If (not keyword_set(SubBack)) then SubBack = 0
;PRIMITIVE SUBTRACTION AND FLAT FIELD
CalibrationDir='/nfs/slac/g/ki/ki08/lsst/CPcampaign/SOAR/2005-05-09/'+$
	      'Calibration/Images/'
BiasFrameFile=CalibrationDir+'Zero09May2005.fits'
fits_read,BiasFrameFile,Bias,BiasHeader
FlatFrameFile=CalibrationDir+'SFlat_09May2005R.fits'
fits_read,FlatFrameFile,Flat,FlatHeader
FlatNormal=float(Flat)/max(Flat)
ZeroFrameFile='/nfs/slac/g/ki/ki08/lsst/CPcampaign/SOAR/2005-05-09/'+$
              'Raw/zero.1024.fits'
fits_read,ZeroFrameFile,ZeroFrame,ZeroHeader
;SOAR IMAGES IN RESPECTIVE 2005-05-{9,10,11,12} DIRECTORIES
SOARDir='/nfs/slac/g/ki/ki08/lsst/CPcampaign/SOAR/2005-05-'+Date+$
	'/Raw/'
SOARFiles=file_search(SOARDir+'*.fits',count=NumSOARFiles)

!p.noerase=1
for i=0, NumSOARFiles-2 do begin
  fits_read,SOARFiles(i),Im1,header1
  fits_read,SOARFiles(i+1),Im2,header2
  Diff=float(Im2)-float(Im1)
  ExpTime=SXPAR(header1,'EXPTIME')
  WindDir=SXPAR(header1,'WINDDIR')
  TimeObs=SXPAR(header1,'TIME-OBS')
  TelFocus=SXPAR(header1,'TELFOCUS')
  print,"File              =    " , SOARFILES(i)
  print,"Exposure Time     =    " , ExpTime
  print,"Observation Time  =  " , TimeObs
  print,"Telescope Focus   =   " , TelFocus
  ;Get rid of nasty pixels
  SatPix=where(Im1 gt 50000)
  Im1(SatPix)=0                 ;BAD PIXELS
  Im1(0:1536)=0                  ;TWO BAD ROWS
  If SubBack eq 1 then begin
     Im1(where(Im1 lt threshold)) = 0
     Im2(where(Im2 lt threshold)) = 0
  endif
  DiffHist=histogram(Diff,Nbins=40,locations=locDiffHist)
  ImHist=histogram(Im1,Nbins=40,locations=locImHist)
  window,xsize=950,ysize=1024
  if SingleImage eq 1 then begin
    tvscl,congrid(Im1,512,1024,/center)
  endif else begin
    tvscl,congrid(Diff,512,1024,/center)
  endelse
    plot,locDiffHist,DiffHist,psym=10,xrange=[50,5000]
    plot,locImHist,ImHist,psym=10,xrange=[0,5000],$
			  position=[.6,.05,.95,.5]
  stop
endfor

end
