;pro Read_Noise
;
;PURPOSE:
;	To examine a region of a frame from the detector and return the mean
;	and stddev as an estimate of the read noise
;INPUTS:
;	FitsFileName - The path to a fits file that will be used to extract 
;		       the noise
;
pro Read_Noise_Ch0, FitsFileName, RootDir=RootDir, Date=Date, TvFlag = TvFlag

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

  ;Load the color table that looks like the BB in fits
  LoadCt, 3 
  ;Move to the directory
  cd, RootDir + Date

  Position = Fits_Open_DataCube(FitsFileName, FCB, FitsHeader, 'Read')

  Noise = [0.]
  ;Look first at Channel 0
  For ReadNum = 0, NReads-1 do begin
    print, 'Looking at Channel 0 for Read : '+strtrim(ReadNum, 2)
    Frame = Fits_Read_DataCube_Region(FitsFileName, FCB, FitsHeader, $
                        0, Naxis1-1, 0, Naxis2-1, ReadNum, ReadNum)
    For Row = 0, Naxis2-1 do begin
      Noise=[Noise,Frame(0:127,Row) - Mean(Frame(0:127,Row))]
    EndFor
  EndFor

  ;Get Standard Deviation
  StdDevNoise = StdDev(Noise)

  If TvFlag ne 0 then begin
    If TvFlag eq 2 then device, filename = ReducedDir+'NoiseChannel0'+Date+'.ps'
    !p.multi = [0,1,1]
    NoiseHist = histogram(Noise, locations = LocNoise)
    Plot, LocNoise, NoiseHist, psym = 10, $
	title = 'Read noise on Channel 0 from ' + strtrim(NReads, 2) + $
	        ' reads from night of ' + strtrim(Date, 2), $
	xtitle = 'Counts', ytitle = 'Number', color = fsc_color('black'), $
	background = fsc_color('white')
    xyouts, 0.7, 0.8, 'Std. Dev. : ' + strtrim(StdDevNoise, 2), /normal, $
	color = fsc_color('black') 
    If TvFlag eq 2 then device, /close

  EndIf
  FitsClose = Fits_Close_DataCube(FCB)
  stop
end
