;plot_guide_mode_ipc
;
;PURPOSE:
;  To plot areas of the HxRG detector that have been afflicted by
;  interpixel capacitance in guide mode.
;
;KEYWORDS:
;  MODE:
;	0 - Plot set of three pixels on the same plot.
;	1 - Plot 9 pixels out from the central persistence core
;
;  XSPIX, YSPIX:
;	  - the x and y pixel values in saturated regime
;  XBPIX, YBPIX;
;	  - the x and y pixel values in the border of the window
;  XWPIX, YWPIX;
;	  - the x and y pixel values in the window
;EXAMPLES:
;m36=============
;plot_guide_mode_ipc_2rg,'E:\ASIC\07Dec17\SAO55426\SAO55426_G_H2RG_SIPIN_30_Reads_Dec17_2007_21_24_41.fits',xspix=959,yspix=1041,xbpix=960,ybpix =1041,xwpix=962,ywpix=1041,mode=0
;plot_guide_mode_ipc_2rg, '/nfs/slac/g/ki/ki09/lances/H1RG/KPNO_11_07/ASIC/LEACH/2007Nov13_2/M36/M36_Yfilter_200_1_0_-20_20.fits', '', xpppix = 532, ypppix = 685, xnppix = 523, ynppix = 684, mode=0,tvflag=1
;
pro plot_guide_mode_ipc_2rg, NegFileName,$
	XSPix=XSPix,  YSPix = YSPix, $
	XBPix=XBPix,  YBPix = YBPix, $
    XWPix=XWPix,  YWPix = YWPix, $
	TvFlag = TvFlag, Mode = Mode, Date = Date, Leach=Leach

;Include all the keywords from the KeywordStruct.pro file
Common KeyParams, KeyStr
@KeywordStruct_2RG.pro
@PlotSettings_2RG.pro

;Declare the pixel values that will be used for the positive and negative persistence
If N_Elements(XSPix)    eq 0  then XSPix    = 771
If N_Elements(YSPix)    eq 0  then YSPix    = 852
If N_Elements(XBPix)    eq 0  then XBPix    = 512
If N_Elements(YBPix)    eq 0  then YBPix    = 450
If N_Elements(XWPix)    eq 0  then XWPix    = 690
If N_Elements(YWPix)    eq 0  then YWPix    = 400
If N_Elements(Mode)     eq 0  then Mode     = 1
If N_Elements(TvFlag)   eq 0  then TvFlag   = 0
If N_Elements(Leach)	eq 0  then Leach    = 0
If SatFileName		    eq '' then PlotSat  = 0 $
   			                  else PlotSat  = 1
;Make the Common Block
Common KeyParams, KeyStr

;The Default File is the one with the negative persistence
KeyStr.RawFullPath = KeyStr.RootDir+KeyStr.PathDelim+KeyStr.Date+KeyStr.PathDelim+NegFileName
KeyStr.RawFullPath = NegFileName
KeyStr.RawName     = NegFileName
Fits_Read, KeyStr.RawFullPath, NoData, NH, /header_only
FileParams = Return_File_Params_1RG(KeyStr.RawFullPath)
If KeyStr.WindowMode then begin
   XBix = XPix - KeyStr.XWindowStart(0)
   YBix = YPix - KeyStr.YWindowStart(0)
EndIf

;Get the exposure time in the negative persistence image
If KeyStr.ITime eq 0 then begin
    If Leach ne 0 then begin
      FrameNTime = (KeyStr.Naxis1*10.e-6+40.e-6)*KeyStr.Naxis2
      INTime     = KeyStr.Naxis3*FrameNTime
    EndIf else begin
      FrameNTime = 0.33
      INTime     = KeyStr.Naxis3*FrameNTime
    Endelse
EndIf Else Begin
    FrameNTime  = KeyStr.ITime/KeyStr.Naxis3
    INTime	= KeyStr.ITime
EndElse

;***************************************************************************************
Case Mode of
  0: begin ;**************************************PLOT THREE RAMPS, WY, NEG, POS.

    ;Ramp of pixel exhibiting positive persistence
    Fits_read_datacube, KeyStr.RawFullPath, ImS, NH, $
	    XStart = XSPix, XStop = XSPix, $
        YStart = YSPix, YStop = YSPix
    ;Ramp of pixel exhibiting negative persistence
    Fits_read_datacube, KeyStr.RawFullPath, ImB, NH, $
        XStart = XBPix, XStop = XBPix, $
        YStart = YBPix, YStop = YBPix
    ;Ramp of pixel integrating Wyflux
    Fits_read_datacube, KeyStr.RawFullPath, ImW, NH, $
        XStart = XWPix, XStop = XWPix, $
        YStart = YWPix, YStop = YWPix

    ;Plot the Wy, S, and B pixels
    erase
    MinPlot   = Min([min(ImS), min(ImB), min(ImW)])
    MinBSPlot = Min([min(ImS-long(ImS(0))), min(ImB-long(ImB(0))), $
		     min(ImW-long(ImW(0)))])
    MaxPlot   = Max([max(ImS), max(ImB), max(ImW)])
    MaxBSPlot = Max([max(ImS-long(ImS(0))), max(ImB-long(ImB(0))), $
		     max(ImW-long(ImW(0)))])
    Plot, ImS, Yrange = [MinPlot, MaxPlot], $
          XTitle = 'Read Number', $
          YTitle = 'ADU' , background = fsc_color('white'), $
          color=fsc_color('black'), position = [0.175, 0.10, 0.9250, 0.90], $
          /normal, xstyle = 8
    OPlot, ImB, color=fsc_color('red')
    OPlot, ImW, color=fsc_color('blue')
    !p.noerase=1
    Axis, XAxis = 1, color=fsc_color('black'), xrange=[0, INTime], $
	  XStyle = 1, xtitle = 'Seconds'
    ItemsN = [Strtrim(XSPix,2)+','+Strtrim(YSPix,2), $
	     Strtrim(XBPix,2)+','+Strtrim(YBPix,2), $
	     Strtrim(XWPix,2)+','+Strtrim(YWPix,2)]
    ColorsN = [fsc_color('black'), fsc_color('red'), fsc_color('blue')]
    TextColorsN =[fsc_color('black'),fsc_color('black'),fsc_color('black')]
    PSymsN = [0,0,0]
    legend, ItemsN, PSym=PSymsN, Color=ColorsN, TextColors = TextColorsN

    If KeyStr.TvFlag eq 1 then begin
       Img  = tvrd(true=3)
       Img24=reverse(Img,2)
       TVLCT, R, G, B, /Get
       Write_Tiff, KeyStr.PlotDir+KeyStr.RawKey+'3Curves.tif', $
         red=Img24(*,*,0), green=Img24(*,*,1), blue=Img24(*,*,2), $
         planarconfig=2, orientation=1, compression=0
    EndIf

    If PlotSat then begin
      Plot, ImS, Yrange = [Min(ImS)-100, Max(Ims)+100], $
	  xstyle = 4, ystyle = 4, $
          color = fsc_color('black'), background = fsc_color('white'), $
          position = [0.125, 0.10, 0.90, 0.90], $
          /normal, XRange = [0, INTime]
      Axis, YAxis = 1, color=fsc_color('black'), yrange=[Min(ImS)-100, Max(ImS)+100], $
          ystyle =1, YTitle = 'ADU in First Ramp'
    EndIf

    !P.Noerase = 0
    If KeyStr.TvFlag eq 3 then begin
           PNGFileName = KeyStr.RawKey+'_'+Strtrim(XPix, 2)+'_'+Strtrim(YPix,2)+'.png'
           PNGImg      = tvrd()
           tvlct, reds, greens, blues, /get
           write_png, KeyStr.PlotDir+PNGFileName, PNGImg, Reds, Greens, Blues
           stop
     EndIf
     stop
  end

endcase

stop

end
