;pro examine_refpix_xrg
;
;PURPOSE:
;	To subtract the noise apparent on the reference pixels of the HxRG from the science 
;	pixels and show the effects in a series of plots.  There appears to be a factor of 
;       2.5 difference between the peak to peak amplitude of the science and reference pixels.  
;       This may have to do with the parasitic capacitance mentioned in the manual
;
;INPUTS:
;       FitsFileName: str
;         Full Path to the File to examine
;
;KEYWORD PARAMETERS:
;   TvFlag: int
;     1 - Only plot to the x-window
;     3 - Plot to the z-buffer and save a copy in .png format
;   XPix1, YPix1, XPix2, YPix2: int
;     The pixel values to use for the two pixels that will be plotted
;EXAMPLES:
;
;  THESIS PLOTS
;    Examine_RefPix_xRG, '/nfs/slac/g/ki/ki04/lances/H1RG-022/ASIC/07Nov19/Dark_H1RG_SIPIN_500_Reads_Nov20_2007_05_31_11.fits', XPix1=220, YPix1=100, XPix2=40, YPix2=40
;
;*************************************************************************
pro Examine_RefPix_xRG, FitsFileName, TvFlag = TvFlag, $
	 LastRead=LastRead, XPix1 = XPix1, YPix1 = YPix1, $
	 XPix2 = XPix2, YPix2 = YPix2

  If N_Elements(TvFlag)  eq 0 then TvFlag = 1 
  If N_Elements(XPix1)   eq 0 then XPix1  = 20
  If N_Elements(YPix1)   eq 0 then YPix1  = 20
  If N_Elements(XPix2)   eq 0 then XPix2  = 40
  If N_Elements(YPix2)   eq 0 then YPix2  = 40

  Common KeyParams, KeyStr

  ;Get all of the defaults for the keywords from the KeywordStruct file
  @KeywordStruct_xRG.pro
  @PlotSettings_xRG.pro

  KeyStr.RawObjectPath = FitsFileName
  Fits_read_datacube, KeyStr.RawObjectPath, Im, FitsHeader
  FileParams = Return_File_Params_xRG(KeyStr.RawObjectPath)

  MeanRefColL = fltarr(KeyStr.Naxis2, KeyStr.Naxis3)  ; Signals vector on left
  MeanRefColR = fltarr(KeyStr.Naxis2, KeyStr.Naxis3)  ; Signals vector on right
  MeanRowsL   = fltarr(KeyStr.Naxis2, KeyStr.Naxis3)
  MeanRowsR   = fltarr(KeyStr.Naxis2, KeyStr.Naxis3)

  For ReadNum = 0, KeyStr.Naxis3-1 do begin
    For row=0, KeyStr.Naxis2 - 1 do begin
      ; Do not use the first reference pixel on each side. 
      ; HAWAII-xRG works best like this.
      MeanRefColL[row, ReadNum] = mean(Im(1:3, row, ReadNum))
      MeanRefColR[row, ReadNum] = mean(Im(KeyStr.Naxis1-4:KeyStr.Naxis1-2, row, ReadNum))
      MeanRowsL[row, ReadNum]   = mean(Im(4:103, row, ReadNum))
    EndFor
  EndFor

  ;Get the exposure times and frame times for axes purposes
  FrameTime = KeyStr.ITime/KeyStr.Naxis3
  If N_Elements(LastRead) eq 0 then LastRead = KeyStr.NAxis3
  ITime     = KeyStr.ITime
  ReadTimes = FindGen(KeyStr.Naxis3-1)*FrameTime

  ;Plot the difference between the reference pixels and the science pixels
  RefRamp1 = MeanRefColL(YPix1, 1:*)-MeanRefColL(YPix1,0)
  SciRamp1 = Im(XPix1,YPix1,1:*) - Float(Im(XPix1,YPix1,0))
  RefRamp2 = MeanRefColL(YPix2, 1:*)-MeanRefColL(YPix2,0)
  SciRamp2 = Im(XPix2,YPix2,1:*) - Float(Im(XPix2,YPix2,0))
  YMin = min([min(SciRamp1(0:LastRead)), min(RefRamp1(0:LastRead))])
  YMax = max([max(SciRamp1(0:LastRead)), max(RefRamp1(0:LastRead))])

  ;Plot just the Science Ramp and the Reference Ramp Separately
  Plot, RefRamp1(0:LastRead), background = fsc_color('white'), color=fsc_color('black'), $
	xtitle = 'Read Number', ytitle = 'ADU', $
	YRange = [YMin, YMax], XStyle = 8, $
        position = [0.175, 0.14, 0.920, 0.860], /normal
  OPlot, SciRamp1(0:LastRead), color=fsc_color('red')
  Axis, XAxis = 1, color=fsc_color('black'), xrange=[0, ITime*(float(LastRead)/KeyStr.NAxis3)], $
            XStyle = 1, xtitle = 'Seconds'
  ItemsP  = ['Science Pixel '+Strtrim(XPix1,2)+','+Strtrim(YPix1,2), $
            'Avg. Ref Pixels in Row '+Strtrim(XPix1,2)]
  ColorsP = [fsc_color('black'), fsc_color('red')]
  PSymP   = [0, 0 ]
  TextColorsP = [fsc_color('black'), fsc_color('black')]
  legend, ItemsP, Psym=PSymP, Colors=ColorsP, TextColors = TextColorsP, $
     thick=3, linestyle=0, number=0.25, charsize=1.750
  If KeyStr.TvFlag eq 1 then stop
  If KeyStr.TvFlag eq 3 then begin
     Img  = tvread(/tiff, FileName = KeyStr.PlotDir+KeyStr.RawObjectKey+$
                   'RefSciPixelRamps_NoCFactor_'+Strtrim(XPix1,2)+'_'+Strtrim(YPix1,2),$
                   /nodialog)
     Img  = tvread(/png, FileName = KeyStr.PlotDir+KeyStr.RawObjectKey+$
                   'RefSciPixelRamps_NoCFactor_'+Strtrim(XPix1,2)+'_'+Strtrim(YPix1,2),$
                   /nodialog) 
  EndIf

 
  ;Show the difference Sci-Ref without capacitance factor
  YMin = min([min(SciRamp1(0:LastRead)-RefRamp1(0:LastRead)), min(SciRamp2(0:LastRead)-RefRamp2(0:LastRead))])
  YMax = max([max(SciRamp1(0:LastRead)-RefRamp1(0:LastRead)), max(SciRamp2(0:LastRead)-RefRamp2(0:LastRead))])
  Plot, SciRamp1(0:LastRead)-RefRamp1(0:LastRead),background = fsc_color('white'), color=fsc_color('black'), $
        xtitle = 'Read Number', ytitle = 'ADU', XStyle = 8, $
        position = [0.175, 0.14, 0.9200, 0.860], /normal, YRange = [YMin, YMax]
  OPlot,SciRamp2(0:LastRead)-RefRamp2(0:LastRead), color=fsc_color('red')
  Axis, XAxis = 1, color=fsc_color('black'), xrange=[0, ITime*(float(LastRead)/KeyStr.NAxis3)], $
            XStyle = 1, xtitle = 'Seconds'
  ItemsP  = ['Sci-Ref '+Strtrim(XPix1,2)+','+Strtrim(YPix1,2), $
	    'Sci-Ref '+Strtrim(XPix2,2)+','+Strtrim(YPix2,2)]
  ColorsP = [fsc_color('black'), fsc_color('red')]
  PSymP   = [0, 0 ]
  TextColorsP = [fsc_color('black'), fsc_color('black')]
  legend, ItemsP, Psym=PSymP, Colors=ColorsP, TextColors = TextColorsP, $
     thick=3, linestyle=0, number=0.25, charsize=1.750
  If KeyStr.TvFlag eq 1 then stop
  If KeyStr.TvFlag eq 3 then begin
     Img  = tvread(/tiff, FileName = KeyStr.PlotDir+KeyStr.RawObjectKey+$
                   'RefMinusSciPixelRamps_NoCFactor_'+Strtrim(XPix1,2)+'_'+Strtrim(YPix1,2),$
                   /nodialog)
     Img  = tvread(/png, FileName = KeyStr.PlotDir+KeyStr.RawObjectKey+$
                   'RefMinusSciPixelRamps_NoCFactor_'+Strtrim(XPix1,2)+'_'+Strtrim(YPix1,2),$
                   /nodialog)
  EndIf

  ;Show the difference Sci-Ref with capacitance factor
  Plot, SciRamp1(0:LastRead)-RefRamp1(0:LastRead)/3.75,background = fsc_color('white'), color=fsc_color('black'), $
        xtitle = 'Read Number', ytitle = 'ADU', XStyle = 8, $
        position = [0.175, 0.14, 0.9200, 0.86], /normal, YRange = [YMin, YMax]
  OPlot,SciRamp2(0:LastRead)-RefRamp2(0:LastRead)/3.75, color=fsc_color('red')
  Axis, XAxis = 1, color=fsc_color('black'), xrange=[0, ITime*(float(LastRead)/KeyStr.NAxis3)], $
            XStyle = 1, xtitle = 'Seconds'
  ItemsP  = ['Sci-Ref*CFactor  '+Strtrim(XPix1,2)+','+Strtrim(YPix1,2), $
            'Sci-Ref*CFactor  '+Strtrim(XPix2,2)+','+Strtrim(YPix2,2)]
  ColorsP = [fsc_color('black'), fsc_color('red')]
  PSymP   = [0, 0 ]
  TextColorsP = [fsc_color('black'), fsc_color('black')]
  legend, ItemsP, Psym=PSymP, Colors=ColorsP, TextColors = TextColorsP,  $
     thick=3, linestyle=0, number=0.25, charsize=1.750
  If KeyStr.TvFlag eq 1 then stop
  If KeyStr.TvFlag eq 3 then begin
     Img  = tvread(/tiff, FileName = KeyStr.PlotDir+KeyStr.RawObjectKey+$
                   'RefMinusSciPixelRamps_CFactor_'+Strtrim(XPix1,2)+'_'+Strtrim(YPix1,2),$
                   /nodialog)
     Img  = tvread(/png, FileName = KeyStr.PlotDir+KeyStr.RawObjectKey+$
                   'RefMinusSciPixelRamps_CFactor_'+Strtrim(XPix1,2)+'_'+Strtrim(YPix1,2),$
                   /nodialog)
  EndIf
  stop
end
