;pro raw_noise
;
;PURPOSE:
;	To calculate the noise of a read across an entire frame and across a single
;	channel.
;INPUTS:
;	FitsFileName - The path to a fits file that will be used to extract 
;		       the noise
;EXAMPLE:
;Read_Noise_2_CDS,'/nfs/slac/g/ki/ki09/lances/07Oct28/kTCNoiseRemovalVRefMainLNROnDualStageBuffLaserBeam_H2RG_SIPIN_OneWindow4_Reads_Oct28_2007_16_10_19.fits', date='07Oct28', TvFlag=1
;Read_Noise_2_CDS,'/nfs/slac/g/ki/ki09/lances/07Oct28/NokTCNoiseRemovalVRefMainLNROnDualStageBuffLaserBeam_H2RG_SIPIN_OneWindow4_Reads_Oct28_2007_16_09_21.fits',date='07Oct28', TvFlag=1

pro Read_Noise_2_CDS, FitsFileName1, RootDir=RootDir, Date=Date, $
  TvFlag = TvFlag, BadRows = BadRows, Frame=Frame, Read1=Read1, $
  Read2=Read2

  ;A list of the bad rows on the detector
  if N_Elements(RootDir) eq 0 then RootDir='/u2/ki/SIDECAR_Images/'
  if N_Elements(TvFlag)  eq 0 then TvFlag=1
  if N_Elements(Frame)   eq 0 then Frame=0
  if N_Elements(Read1)   eq 0 then Read1=0
  if N_Elements(Read2)   eq 0 then Read2=1

  ;Include all of the keywords from the KeywordStruct file
  @KeywordStruct.pro
  @PlotSettings.pro

  ;Load the color table that looks like the ds9 BB
  loadct, 3
 
  ;Change the Plot Directory
  KeyStr.PlotDir = '/u1/ki/KPNO/Latex/SIDECAR/'   
  KeyStr.RootDir = '/nfs/slac/g/ki/ki09/lances/'
  cd, KeyStr.RootDir+Date

  ;Get the  
  Fits_Read, FitsFileName1, Im1, FitsHeader1
  FileParams = return_file_params(FitsFileName1)

  If KeyStr.TvFlag eq 1 then begin
    window, 0, xpos=0, ypos=100, xsize=1200, ysize=900
    !p.charthick=3.0
    !p.charsize=3.0
    !p.thick=3.0
  EndIf Else If KeyStr.TvFlag eq 2 then begin
    device, filename = KeyStr.PlotDir+SxPar(FitsHeader1,'OBJECT')+'.eps'
    device,ysize=8.5,/inches        ;Height of plot in y
    device,xsize=8.5,/inches        ;Width of plot in x
    !p.charsize=.65
    !p.charthick=4
  EndIf Else If KeyStr.TvFlag eq 3 then begin
    Set_Plot,'z', /copy
    device, z_buffer=0
    loadct, 3
  EndIf

  ;Consider two reads of the file
  CDS = float(im1(*,*,Read2))-float(im1(*,*,Read1))
  MinCDS    = Min(CDS)
  MaxCDS    = Max(CDS)
  MeanCDS   = Mean(CDS)
  StdDevCDS = StdDev(CDS)
  CDSHist = Histogram(im1(*,*,Read2)-float(im1(*,*,Read1)), locations = locPix) 
  plot, locPix, CDSHist, psym=10, position = [0.075, 0.06, 0.475, 0.4250],$
        /normal, color=fsc_color('black'), background=fsc_color('white'), $
	xtitle='Counts', ytitle= 'Number'
  !p.noerase=1
  tvimage,bytscl(congrid(im1(*,*,2),512,512)),$
	position=[0.075,0.55,0.4750,0.975],/normal, $
	background = fsc_color('white') 
  colorbar, position=[0.075,0.5,0.475, 0.55], /normal, $
        color=fsc_color('black'), minrange=min(im1(*,*,Read2)), $
	maxrange=max(im1(*,*,2)), charsize=1.75, charthick=2.
  xyouts, 0.100, 0.925, 'Raw Image', /normal, color=fsc_color('white')
  tvimage,bytscl(congrid(CDS,512,512)),$
        position=[0.525,0.55,0.95,0.975],/normal
  colorbar, position=[0.525,0.50,0.95,0.55], color=fsc_color('black'),$
	minrange=min(CDS), maxrange=max(CDS), $
	charsize=1.75, charthick=2.
  xyouts, 0.550, 0.925, 'CDS Image', /normal, color=fsc_color('white')
  !p.noerase=0
  xyouts, 0.525, 0.40, SxPar(FitsHeader1, 'MCD_FILE'), $
          color = fsc_color('black'), charsize=2.25, /normal
  xyouts, (!X.CRANGE(1)-!X.CRANGE(0))*0.5+!X.CRANGE(0),  $
          (!Y.CRANGE(1)-!Y.CRANGE(0))*0.8500+!Y.CRANGE(0), $
          '#Cols  = '+ strtrim(string(KeyStr.Naxis1,format='(I10.0)'),2), $
          color=fsc_color('black'), charsize=2.5
  xyouts, (!X.CRANGE(1)-!X.CRANGE(0))*0.5+!X.CRANGE(0),  $
          (!Y.CRANGE(1)-!Y.CRANGE(0))*0.7500+!Y.CRANGE(0), $
          '#Rows = '+ strtrim(string(KeyStr.Naxis2,format='(I10.0)'),2), $
          color=fsc_color('black'), charsize=2.5
  xyouts, (!X.CRANGE(1)-!X.CRANGE(0))*0.5+!X.CRANGE(0),  $
          (!Y.CRANGE(1)-!Y.CRANGE(0))*0.6500+!Y.CRANGE(0), $
          'Min   = '+ strtrim(string(MinCDS,format='(I10.0)'),2), $
          color=fsc_color('black'), charsize=2.5
  xyouts, (!X.CRANGE(1)-!X.CRANGE(0))*0.5+!X.CRANGE(0),  $
          (!Y.CRANGE(1)-!Y.CRANGE(0))*0.5500+!Y.CRANGE(0), $
          'Max   = '+ strtrim(string(MaxCDS,format='(I10.0)'),2), $
          color=fsc_color('black'), charsize=2.5
  xyouts, (!X.CRANGE(1)-!X.CRANGE(0))*0.5+!X.CRANGE(0),  $
          (!Y.CRANGE(1)-!Y.CRANGE(0))*0.4500+!Y.CRANGE(0), $
          'Mean  = '+ strtrim(string(MeanCDS,format='(F10.4)'),2), $
          color=fsc_color('black'), charsize=2.5
  xyouts, (!X.CRANGE(1)-!X.CRANGE(0))*0.5+!X.CRANGE(0),  $
          (!Y.CRANGE(1)-!Y.CRANGE(0))*0.3500+!Y.CRANGE(0), $
          'StdDev = '+ strtrim(string(StdDevCDS,format='(F10.4)'),2), $
          color=fsc_color('black'), charsize=2.25
  xyouts, 0.52500, 0.350, $
          'h6044 = '+ strtrim(SxPar(FitsHeader1,'H6044'),2), $
          color=fsc_color('black'), charsize=2.25, /normal
  xyouts, 0.52500, 0.30, $
          'h61cc = '+ strtrim(SxPar(FitsHeader1,'H611C'),2), $
          color=fsc_color('black'), charsize=2.25, /normal
  xyouts, 0.52500, 0.2500, $
          'PAmp Gain= 1',$;+ strtrim(SxPar(FitsHeader1,'PAMPGAIN'),2), $
          color=fsc_color('black'), charsize=2.25, /normal
  xyouts, 0.52500, 0.20, $
          '# ADCs = '+ strtrim(SxPar(FitsHeader1,'NADC'),2), $
          color=fsc_color('black'), charsize=2.25, /normal
  xyouts, 0.52500, 0.150, $
          'PixRate = '+ strtrim(SxPar(FitsHeader1,'PIXRATE'),2), $
          color=fsc_color('black'), charsize=2.25, /normal

  If KeyStr.TvFlag eq 2 then begin
     device, /close
  EndIf Else If KeyStr.TvFlag eq 1 then begin
     Img  = tvrd(true=3)
     Img24=reverse(Img,2)
     TVLCT, R, G, B, /Get
     ;Device, Z_Buffer=1
     ;Img24 = LonArr(1024,768,3)
     ;Img24(*,*,0) = R[Img] & Img24(*,*,1) = G[Img] & Img24(*,*,2) = B[Img]
     Write_Tiff, KeyStr.PlotDir+SxPar(FitsHeader1,'OBJECT')+'.tif', $
       red=Img24(*,*,0), green=Img24(*,*,1), blue=Img24(*,*,2), $
       planarconfig=2, orientation=0, compression=0, $
       xresol=1200, yresol=900
  EndIf
  stop


end
