;pro raw_noise_pub
;
;PURPOSE:
;	To calculate the noise of a read across an entire frame and across a single
;	channel.  This is intended to be used for publication purposes.
;
;INPUTS:
;	FitsFileName - The path to a fits file that will be used to extract 
;		       the noise
;EXAMPLE:
;raw_noise_pub,'/nfs/slac/g/ki/ki09/lances/07Oct20/WindowModeNoKTCorElectricalNoise_H4RG_SIPIN_OneWindow10_Reads_Oct20_2007_17_41_25.fits',tvflag=2
;
;*****************************************************************************************
pro Raw_Noise_Pub, 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='/nfs/slac/g/ki/ki03/lances/SIDECAR_Tests/'
  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_xRG.pro
;  @PlotSettings_xRG.pro

  ;Change the Plot Directory
  KeyStr.PlotDir = '/nfs/slac/g/ki/ki03/lances/SIDECAR_Tests/Plots/'   
  KeyStr.RootDir = '/nfs/slac/g/ki/ki03/lances/'

  ;Get the  
  Fits_Read, FitsFileName, Im, FitsHeader
  FileParams = Return_File_Params_xRG(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))
     MedianFrame = Median(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))
       MedianFrame = Median(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)
  MinADC  = min(ADCLocs)
  MaxADC  = max(ADCLocs)
  FR      = MaxADC-float(MinADC)

  If KeyStr.TvFlag eq 2 then begin
    set_plot,'ps'
    device, filename = KeyStr.PlotDir+SxPar(FitsHeader,'OBJECT')+'.eps'
    device, BITS=8, COLOR=1, ENCAPSULATED=1, xsize=12, ysize=5, /inches
    loadct, 3
    !p.charsize=.65
    !p.charthick=4
  endif

  plot, ADCLocs, ADCHist,psym=10, color=fsc_color('black'), $
	background=fsc_color('white'), position = [0.45,0.05,0.965,0.925], $
	/normal, xrange=[MinADC-0.6*FR, MaxADC], charsize=0.90
  xyouts, (!X.CRANGE(1)-!X.CRANGE(0))*0.065+!X.CRANGE(0),  $
          (!Y.CRANGE(1)-!Y.CRANGE(0))*0.900+!Y.CRANGE(0), $
          'Gain= '+ strtrim(SxPar(FitsHeader,'PAMPGAIN'),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), $
          'PixRate = '+ strtrim(string(SxPar(FitsHeader,'PIXRATE'),format='(F10.1)'),2)+$
          '!7l!3S/Pix', $
          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), $
          '#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.675+!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.600+!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.525+!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.450+!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.375+!Y.CRANGE(0), $
          'Median = '+ strtrim(string(MedianFrame,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.300+!Y.CRANGE(0), $
          'StdDev = '+ strtrim(string(RdNFrame,format='(F10.2)'),2), $
          color=fsc_color('black'), charsize=1.25
  a = bindgen(256,256)

  if stregex(FitsFileName, 'OncePerFrame', /boolean) then begin
    xyouts, 0.02, 0.16, 'Reset Once Per Frame', /normal, $
      color=fsc_color('black'), charsize = 1.5
  endif else if stregex(FitsFileName, 'NoKTC', /boolean) then begin
    xyouts, 0.02, 0.16, 'Reset Once Per Row; No kTC removal', /normal, $
      color=fsc_color('black'), $
      charsize = 1.5
  endif else begin
    xyouts, 0.02, 0.16, 'Reset Once Per Row; With kTC removal', /normal, $
      color=fsc_color('black'), $
      charsize = 1.5
  endelse
  
  ;Print the noise
  xyouts, 0.02, 0.10, 'Noise (ADU): ' + strtrim(string(RdNFrame,format='(F10.2)'),2),$
    color=fsc_color('black'), charsize=1.5, /normal
  ;Assume a gain of 1 unless the gain is 4
  if long(SxPar(FitsHeader,'PAMPGAIN')) eq 4 then begin
    xyouts, 0.02, 0.04, 'Noise (!7l!3V) : ' + strtrim(string(16.43*RdNFrame,format='(F10.2)'),2),$
      color=fsc_color('black'), charsize=1.5, /normal
  endif else begin
    xyouts, 0.02, 0.04, 'Noise (!7l!3V): ' + strtrim(string(64*RdNFrame,format='(F10.2)'),2),$
      color=fsc_color('black'), charsize=1.5, /normal
  endelse

  ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  ;++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  ;NOTE: The values of 173 and 150 are improperly mapped !!!!!!
  ImScl = bytscl(congrid(ceil(Im(*,*)),300,300), min=min(Im), max=max(Im))
  ImSclBlack = where(ImScl eq 173)
  if ImSclBlack(0) ne -1 then ImScl(where(ImScl eq 173)) = byte(172)
  ImSclWhite = where(ImScl eq 150)
  if ImSclWhite(0) ne -1 then ImScl(where(ImScl eq 150)) = byte(151)
  tvimage, ImScl,  position = [0.01, 0.25, 0.38, 0.925] , /minus_one, $
         /nointerpolation 

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

end
