;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:
;raw_noise,'/nfs/slac/g/ki/ki09/lances/07Oct20/WindowModeNoKTCorElectricalNoise_H4RG_SIPIN_OneWindow10_Reads_Oct20_2007_17_41_25.fits',tvflag=2
pro Raw_Noise, FitsFileName, RootDir=RootDir, Date=Date, $
  TvFlag = TvFlag, BadRows = BadRows, Frame=Frame

  ;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

  ;Include all of the keywords from the KeywordStruct file
  @KeywordStruct.pro
  @PlotSettings.pro
 
  ;Change the Plot Directory
  KeyStr.PlotDir = '/u1/ki/KPNO/Latex/SIDECAR/'   
  KeyStr.RootDir = '/nfs/slac/g/ki/ki09/lances/'

  ;Get the  
  Fits_Read, FitsFileName, Im, FitsHeader
  FileParams = return_file_params(FitsFileName)

  ;Determine if Window Mode was in operation
  WindowMode = long(SxPar(FitsHeader,'WINDOWM'))
  If WindowMode then KeyStr.NOUT=1
  NColPerChannel = KeyStr.Naxis2/KeyStr.NOUT

  ;Calculate noise on per channel basis for full frame
  RdNChan = DblArr(KeyStr.NOUT)
  
  If WindowMode eq 0 then begin
     ;Get read noise of first frame
     RdNFrame = StDev(Im(*,*,Frame))
     MeanFrame= Mean(Im(*,*,Frame))
     MinFrame = Min(Im(*,*,Frame))
     MaxFrame = Max(Im(*,*,Frame))
     print, 'Total Noise over frame '+Strtrim(Frame,2)+ '= ', RdNFrame

    for OutPut = 0, KeyStr.NOUT-1 do begin
      RdNChannel = StdDev(Im(OutPut*NColPerChannel:$
			    (OutPut+1)*NColPerChannel-1,*))
      print, 'Total Noise for Channel'+Strtrim(OutPut,2)+' = ', RdNChannel
      RdNChan(OutPut) = RdNChannel
    endfor
    print, 'Mean Noise per Channel ='+Strtrim(Mean(RdNChan))
  EndIf Else Begin
    For Frame=0, KeyStr.NAxis3 - 1 do begin
       RdNFrame = StDev(Im(*,*,Frame))
       MeanFrame= Mean(Im(*,*,Frame))
       MinFrame = Min(Im(*,*,Frame))
       MaxFrame = Max(Im(*,*,Frame))
       print, 'Total Noise over frame ' + Strtrim(Frame,2)+ ' = ', RdNFrame
    EndFor
  EndElse
  ;Histgram the digitization values
  ADCHist = histogram(Im(*,*,0),locations=ADCLocs)

  If KeyStr.TvFlag eq 2 then begin
    device, filename = KeyStr.PlotDir+SxPar(FitsHeader,'OBJECT')+'.eps'
    device,ysize=4.5,/inches        ;Height of plot in y
    device,xsize=8.5,/inches        ;Width of plot in x
    !p.charsize=.65
    !p.charthick=4
  endif

  plot, ADCLocs, ADCHist,psym=10, color=fsc_color('black'), $
	background=fsc_color('white'), position = [0.05,0.05,0.675,0.875], $
	/normal 
  xyouts, 0.05, 0.95, SxPar(FitsHeader, 'MCD_FILE'), $
	  color = fsc_color('black'), charsize=1.25, /normal
  xyouts, (!X.CRANGE(1)-!X.CRANGE(0))*0.065+!X.CRANGE(0),  $
          (!Y.CRANGE(1)-!Y.CRANGE(0))*0.900+!Y.CRANGE(0), $
          '#Cols  = '+ strtrim(string(KeyStr.Naxis1,format='(I10.0)'),2), $
          color=fsc_color('black'), charsize=1.25
  xyouts, (!X.CRANGE(1)-!X.CRANGE(0))*0.065+!X.CRANGE(0),  $
          (!Y.CRANGE(1)-!Y.CRANGE(0))*0.825+!Y.CRANGE(0), $
          '#Rows = '+ strtrim(string(KeyStr.Naxis2,format='(I10.0)'),2), $
          color=fsc_color('black'), charsize=1.25
  xyouts, (!X.CRANGE(1)-!X.CRANGE(0))*0.065+!X.CRANGE(0),  $
          (!Y.CRANGE(1)-!Y.CRANGE(0))*0.750+!Y.CRANGE(0), $
          'Min   = '+ strtrim(string(MinFrame,format='(I10.0)'),2), $
          color=fsc_color('black'), charsize=1.25
  xyouts, (!X.CRANGE(1)-!X.CRANGE(0))*0.065+!X.CRANGE(0),  $
          (!Y.CRANGE(1)-!Y.CRANGE(0))*0.675+!Y.CRANGE(0), $
          'Max   = '+ strtrim(string(MaxFrame,format='(I10.0)'),2), $
          color=fsc_color('black'), charsize=1.25
  xyouts, (!X.CRANGE(1)-!X.CRANGE(0))*0.065+!X.CRANGE(0),  $
          (!Y.CRANGE(1)-!Y.CRANGE(0))*0.600+!Y.CRANGE(0), $
          'Mean  = '+ strtrim(string(MeanFrame,format='(I10.0)'),2), $
          color=fsc_color('black'), charsize=1.25
  xyouts, (!X.CRANGE(1)-!X.CRANGE(0))*0.065+!X.CRANGE(0),  $
          (!Y.CRANGE(1)-!Y.CRANGE(0))*0.525+!Y.CRANGE(0), $
          'StdDev = '+ strtrim(string(RdNFrame,format='(F10.4)'),2), $
          color=fsc_color('black'), charsize=1.25
  xyouts, 0.700, 0.850, $
          'h6044 = '+ strtrim(SxPar(FitsHeader,'H6044'),2), $
          color=fsc_color('black'), charsize=1.25, /normal
  xyouts, 0.700, 0.7750, $
          'h61cc = '+ strtrim(SxPar(FitsHeader,'H611C'),2), $
          color=fsc_color('black'), charsize=1.25, /normal
  xyouts, 0.700, 0.700, $
          'PAmp Gain= '+ strtrim(SxPar(FitsHeader,'PAMPGAIN'),2), $
          color=fsc_color('black'), charsize=1.25, /normal
  xyouts, 0.700, 0.625, $
          '# ADCs = '+ strtrim(SxPar(FitsHeader,'NADC'),2), $
          color=fsc_color('black'), charsize=1.25, /normal
  xyouts, 0.700, 0.550, $
          'PixRate = '+ strtrim(SxPar(FitsHeader,'PIXRATE'),2), $
          color=fsc_color('black'), charsize=1.25, /normal


  If KeyStr.TvFlag eq 2 then device, /close  
  stop

end
