;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
;pro Examin_Sin_Pix_Rst_xRG
;
;PURPOSE:
;	To take a list of files that were taken in single pixel reset mode 
;       (either pixel 100,100 or 110,100) and look at the behavior of it and 
;       its neighbors as a function of VRESET for the full frame and the single 
;       pixel.
;
;INPUTS:
;	XPix - The Column of the pixl
;	YPix = The Row of the pixel
;
;KEYWORDS:
;       RST: int
;         0 - use the files where the reset was not applied to the single pixel
;         1 - use the files where the reset was applied to the single pixel
;         2 - use files beginning with 'Dark_Gain4_'
;         3 - use files beginning with 'Fe55_Gain4_'
;       TEMP: int
;         The temperature at which the files were taken.  This should be in the file name
;       PLOTTEMPNUM: int
;         If MODE == 3, then this is the index for the temperature used in the second plot
;       VOLTAGE: int
;         The VSUB voltage at which the files were taken: 5, 15, or 25
;       VRESETW: float
;         The voltage used to reset the single pixel window       
;       MODE: int
;         0 - Collect all the files in the directory with certain Temp and Voltage
;         1 - Collect all files in the directory with same Temp, Voltage, VResetW
;         2 - Plot IPC values (Thesis Plot)
;         3 - Plot ADU vs VRESET (Thesis Plot)
;         4 - Plot Diffusion vs Temp and VRESET (Thesis Plot)
;       DIR: int
;         0 - don't cd; stay in current directory
;         1 - cd into /nfs/slac/g/ki/ki04/lances/H2RG-001/ASIC/08Dec17/SinPixResetExperiment/ where VRESET=0.3 V
;         2 - cd into /nfs/slac/g/ki/ki04/lances/H2RG-001/ASIC/08Dec19/SinPixResetExperiment/ where VRESET=0.7 V	
;       RSTN: int
;         0-3 : [25,50,75,100] : corresponds to number of resets performed on single pixel'
;       PLOTREADNUM: int
;         0-9 : the read number to plot
;       MULTIPLOT: int
;         0 : don't do all of the plots on one page
;         1 : do all the plots on one page with 
;       ZOOMP: int
;         0 : don't zoom on the multiplot
;         1 : zoom on the multiplot
;       SAVEIMG: int
;         0 : don't save the plots
;         1 : save the plots
;CALLING SEQUENCE:
;  cd, '/mnt/SeaGate/08Dec16/Dark/'
;  examine_sin_pix_rst_xrg, 1, 100, temp=100, voltage=20, rst=2
;  NOTE: use temp=100 to get all files with 100 reads
;
;  Arbitrary Directory:
;    examine_sin_pix_rst_xrg, 100, 100, Rst=2, Voltage=15, VResetW=0.8, Dir=0, Mode=0
;  Double Plot with VREST vs ADU
;     examine_sin_pix_rst_xrg, 111, 99, Rst=1, VResetW=0.2, Mode=3, dir=1, RestN=1, voltage=5, tvflag=1, zoomp=1
;  Plot Coupling to nearest neighbors
;    examine_sin_pix_rst_xrg, 100, 100, Rst=2, Voltage=15, VResetW=0.8, Dir=1, Mode=2
;THESIS PLOTS:
;  Multiplots with VRESET vs ADU
;    examine_sin_pix_rst_xrg, 100, 100, Rst=1, VResetW=0.2, Mode=3, dir=1, RestN=0, /multiplot, tvflag=1, zoomp=0, PlotReadNum=9
;    examine_sin_pix_rst_xrg, 100, 100, Rst=1, VResetW=0.2, Mode=3, dir=1, RestN=0, /multiplot, tvflag=1, zoomp=1, PlotReadNum=9
;  Plots of IPC vs. TEMP
;     examine_sin_pix_rst_xrg, 100, 100, Rst=2, Voltage=15, VResetW=1.0, Dir=1, Mode=2,/saveimg, tvflag=1
;
;**************************************************************************************************************
;
pro Examine_Sin_Pix_Rst_xRG, XPix, YPix, Rst=Rst, Temp=Temp, VResetW=VResetW, $ 
    Voltage=Voltage, TvFlag = TvFlag, FileNum1 = FileNum1, FileNum2 = FileNum2, $
    FitsDir=FitsDir, Mode=Mode, Dir=Dir, RestN = RestN, MultiPlot = MultiPlot, $
    ZoomP=ZoomP, SaveImg=SaveImg, PlotReadNum=PlotReadNum, PlotTempNum=PlotTempNum

  If N_Elements(Mode)        eq 0 then Mode          = 0
  If N_Elements(TvFlag)      eq 0 then TvFlag        = 1
  If N_Elements(Temp)        eq 0 then Temp          = 100
  If N_Elements(Rst)         eq 0 then Rst           = 0
  If N_Elements(FileNum1)    eq 0 then FileNum1      = 0
  If N_Elements(FileNum2)    eq 0 then FileNum2      = 0
  If N_Elements(Dir)         eq 0 then Dir           = 0
  If N_Elements(RestN)       eq 0 then RestN         = 0
  If N_Elements(MultiPlot)   eq 0 then MultiPlot     = 0
  If N_Elements(ZoomP)       eq 0 then ZoomP         = 0
  If N_Elements(SaveImg)     eq 0 then SaveImg       = 0
  If N_Elements(PlotReadNum) eq 0 then PlotReadNum   = 9
  If N_Elements(PlotTempNum) eq 0 then PlotTempNum   = 0

  RstN = RestN

  ;For certain modes, only H2RG-001 and ASIC were used
  if Mode ge 0 then begin
    DetStr = 'H2RG-001'
    ElecStr = 'ASIC'
    if Dir eq 0 then Date = '08Dec13'
    if Dir eq 1 then Date = '08Dec17'
    if Dir eq 2 then Date = '08Dec19'
  endif
 
  Common KeyParams, KeyStr

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

  ;Change to FitsDir if it is set, otherwise stay here
  If N_Elements(FitsDir) ne 0 then cd, FitsDir
  
  If SaveImg eq 1 then begin
    KeyStr.PlotDir = '/nfs/slac/g/ki/ki04/lances/H2RG-001/ASIC/Plots/SinglePixelReset/'
    !p.charthick=2.9
    !p.charsize=2.5
    !p.thick=2.0
    !p.font=1
    DEVICE, GET_FONTNAMES=fnames, SET_FONT='*'
    device, set_font = 'helvetica bold', /tt_font
    device, set_character_size=[6,8]
    !p.charsize=2.0  ;Set to True-Type fonts (described on
    !p.charthick=5
  endif

  case Mode of 
    0: begin
      If Rst eq 0 then begin 
        RawFiles = File_Search('*_'+Strtrim(Voltage,2)+'.00V_*'+Strtrim(Temp,2)+'*wRst*.fits') 
      endif else if Rst eq 1 then begin 
        RawFiles = File_Search('*_'+Strtrim(Voltage,2)+'.00V_*'+Strtrim(Temp,2)+'NoRst*.fits')
      endif else if Rst eq 2 then begin 
        RawFiles = File_Search('*_'+Strtrim(Voltage,2)+'.00V_*'+Strtrim(Temp,2)+'*.fits')    
      endif else if Rst eq 3 then begin
        RawFiles = File_Search('FE55_Gain4_8ADC_*.fits')
      endif else if Rst eq 4 then begin
        RawFiles = File_Search('dark*.fits')
      endif

      ;Now all of the files have been found, do processing
      NumFiles   = N_Elements(RawFiles)
      if FileNum2 eq 0 then FileNum2 = NumFiles

      if TvFlag ge 0 then begin
        window, 0
        window, 1
      EndIf 
      For FileNumLoop = FileNum1, FileNum2-1 do begin
        FileNum = FileNumLoop - FileNum1
        Fits_Read_DataCube, RawFiles(FileNumLoop), Im, H, $
          XStart = XPix-1, XStop = XPix+1, $
          YStart = YPix-1, YStop = YPix+1 
        Temp = SxPar(H, 'DETTEMP')
        print, 'File: '+ RawFiles(FileNumLoop) + ' mean center: ' + string(9B)+ $
            strtrim(Temp, 2) + string(9B)+ $
            strtrim(mean(Im(1,1,*)),2) + string(9B) + $
            strtrim(mean(Im(1,0,*)),2)
        If FileNum eq 0 then begin
          wset, 0
          plot, [FileNumLoop], [mean(Im(1,1,*)/float(Im(1,0,*)))] , color = fsc_color('black'), $
              background = fsc_color('white'), yrange=[0.0,2.0], xrange=[0,FileNum2]
          wset, 1
          plot, [FileNumLoop], [Im(1,1,*)] , color = fsc_color('black'), $
              background = fsc_color('white'), xrange=[0,FileNum2]
        EndIf Else Begin
          wset, 0
          oplot, [FileNumLoop], [mean(Im(1,1,*)/float(Im(1,0,*)))], color= fsc_color('black'), psym=2
          wset, 1
          oplot, [FileNumLoop], [Im(1,1,*)] , color = fsc_color('black'), psym=2
        EndElse
      EndFor
    End
    1: Begin
      if Rst eq 0 then begin
        SrchStr  = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                             Strtrim(XPix,2)+'x_'+Strtrim(YPix,2)+'y_NoRst*.fits'
      endif else begin
        SrchStr  = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                             Strtrim(XPix,2)+'x_'+Strtrim(YPix,2)+'y_wRst*.fits'
      endelse
      Print, 'Looking for files with: ' + SrchStr
      RawFiles = File_Search(SrchStr)
      NumFiles = N_Elements(RawFiles)
      if FileNum2 eq 0 then FileNum2 = NumFiles
      For FileNumLoop = FileNum1, FileNum2-1 do begin
        Fits_Read_DataCube, RawFiles(FileNumLoop), Im, H, $
          XStart = XPix-1, XStop = XPix+1, $
          YStart = YPix-1, YStop = YPix+1
        delvarx, Im
      EndFor
    End
    2: Begin
      if Dir eq 1 then begin
        cd, '/nfs/slac/g/ki/ki04/lances/H2RG-001/ASIC/08Dec17/SinPixResetExperiment/'
      endif else if Dir eq 2 then begin
        cd, '/nfs/slac/g/ki/ki04/lances/H2RG-001/ASIC/08Dec19/SinPixResetExperiment/'
      endif
      TempArr      = [100, 110, 120, 130, 140, 150, 160, 180]
      NRstArr      = [100, 25, 50, 75]
      NumTemps     = N_Elements(TempArr)
      NumPix       = 9
      NumReads     = 10
      WArr         = Fltarr(NumTemps, 4, 3, 3, NumReads)
      WDiffArr     = Fltarr(NumTemps, 4, 3, 3, NumReads)
      WArr2        = Fltarr(NumTemps, 4, 3, 3, NumReads)
      WDiffArr2    = Fltarr(NumTemps, 4, 3, 3, NumReads)
      WArrMD       = Fltarr(NumTemps, 4, 3, 3, NumReads) 
      WRefArr      = Fltarr(NumTemps, 4, 3, 3)

      print, 'Reset Pixel: ' + string(9B) + ' Num Winodws ' + string(9B) + ' : ' + $
               string(9B) + ' Center ' + string(9B) + 'Center Lost'
      For TempNum = 0, NumTemps-1 do begin 
        Temp = TempArr(TempNum)
        if Rst eq 0 then begin
          if abs(XPix - 100) lt 2 then begin
            SrchStr  = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                            Strtrim(100,2)+'x_'+Strtrim(YPix,2)+'y_NoRst*.fits'
            SrchStr2 = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                             Strtrim(110,2)+'x_'+Strtrim(YPix,2)+'y_NoRst*.fits'
          endif else begin
            SrchStr  = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                            Strtrim(110,2)+'x_'+Strtrim(YPix,2)+'y_NoRst*.fits'
            SrchStr2 = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                             Strtrim(100,2)+'x_'+Strtrim(YPix,2)+'y_NoRst*.fits'
          endelse
        endif else begin       
          if XPix eq 100 then begin
            SrchStr   = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                             Strtrim(100,2)+'x_'+Strtrim(YPix,2)+'y_wRst*.fits'
            SrchStr2  = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                             Strtrim(110,2)+'x_'+Strtrim(YPix,2)+'y_NoRst*.fits'
            if Dir eq 2 then begin
              SrchStr2  = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                             Strtrim(100,2)+'x_'+Strtrim(YPix,2)+'y_NoRst*.fits'
            endif
            if Dir eq 1 then begin
              RefStr    = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(0.4, format='(F10.1)'),2)+'_*'+$
                             Strtrim(100,2)+'x_'+Strtrim(YPix,2)+'y_NoRst*.fits'
            endif else begin
              RefStr    = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(0.4, format='(F10.1)'),2)+'_*'+$
                             Strtrim(100,2)+'x_'+Strtrim(YPix,2)+'y_NoRst*.fits'
            endelse
          endif else begin
            SrchStr  = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                            Strtrim(110,2)+'x_'+Strtrim(YPix,2)+'y_wRst*.fits'
            SrchStr2 = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                             Strtrim(100,2)+'x_'+Strtrim(YPix,2)+'y_NoRst*.fits'
            if Dir eq 1 then begin
              RefStr    = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(0.4, format='(F10.1)'),2)+'_*'+$
                               Strtrim(110,2)+'x_'+Strtrim(YPix,2)+'y_NoRst*.fits'
            endif else begin
              RefStr    = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(0.4, format='(F10.1)'),2)+'_*'+$
                               Strtrim(110,2)+'x_'+Strtrim(YPix,2)+'y_NoRst*.fits'
            endelse

          endelse
        endelse
        Print, 'Looking for files with: ' + SrchStr
        RawFiles  = File_Search(SrchStr)
        RawFiles2 = File_Search(SrchStr2)
        RefFiles  = File_Search(RefStr)
        NumFiles = N_Elements(RawFiles)
        if FileNum2 eq 0 then FileNum2 = NumFiles
        ;100 resets comes before 25
        RawFilesTemp  = RawFiles
        RawFilesTemp2 = RawFiles2 
        RawFiles(0)   = RawFilesTemp(1) & RawFiles2(0)   = RawFilesTemp2(1) 
        RawFiles(3)   = RawFilesTemp(0) & RawFiles2(3)   = RawFilesTemp2(0)  
        RawFiles(1)   = RawFilesTemp(2) & RawFiles2(1)   = RawFilesTemp2(2) 
        RawFiles(2)   = RawFilesTemp(3) & RawFiles2(2)   = RawFilesTemp2(3) 
        delvarx, RawFilesTemp

        For FileNumLoop = FileNum1, FileNum2-1 do begin
          Fits_Read_DataCube, RawFiles(FileNumLoop), Im, H, $
            XStart = XPix-1,  XStop  = XPix+1, $
            YStart = YPix-1,  YStop = YPix+1
          Fits_Read_DataCube, RawFiles2(FileNumLoop), Im2, H2, $
            XStart = XPix-1,  XStop = XPix+1, $ 
            YStart = YPix-1,  YStop = YPix+1
          print, 'Rsts : ' + $
               (strmid(RawFiles(FileNumLoop), stregex(RawFiles(FileNumLoop), Strtrim(String(VResetW, format='(F10.1)'),2))+4,3)) + $
                ' , ' + strtrim(SxPar(H, 'DETTEMP'),2) + ' , ' + strtrim(SxPar(H, 'TIME'), 2)   + $
                ' : ' + Strtrim(Im(1,1,0)) + ' , ' + Strtrim(Im(1,1,9)-long(Im(1,1,0))) + $ 
                ' : ' + Strtrim(Im(1,0,0)) + ' , ' + Strtrim(Im(1,0,9)-long(Im(1,0,0))) + $
                ' : ' + Strtrim(Im(0,0,0)) + ' , ' + Strtrim(Im(0,0,9)-long(Im(0,0,0))) + $
                ' : ' + Strtrim(Im(2,0,0)) + ' , ' + Strtrim(Im(2,0,9)-long(Im(2,0,0))) + $ 
                ' : ' + Strtrim(Im2(1,1,0))+ ' , ' + Strtrim(Im2(1,0,0))

          WArr(TempNum,  FileNumLoop, *,*, *)    = Im(*,*,*)
          WArr2(TempNum, FileNumLoop, *, *, *)   = Im2(*,*,*)
          For ReadNum = 0, NumReads-1 do begin
            WDiffArr(TempNum, FileNumLoop, *, *, ReadNum)  = Im(*,*,ReadNum)-long(Im(*,*,0)) 
            WArr2(   TempNum, FileNumLoop, *, *, ReadNum)  = Im2(*,*,ReadNum)-long(Im2(*,*,0))
          EndFor

          ;Take the difference between the frame with reset and without reset, with ample time to settle
          WArrMD(TempNum, FileNumLoop, *, *, *) = abs(float(Im(*,*,*))-Im2(*,*,*))
          delvarx, Im
        EndFor
      EndFor
      !p.noerase=0
      ;Plot Absolute Values of reset
      window, 0
      wset, 0
      plot,  TempArr, WArr(*,0,1,1,0), color=fsc_color('black'), background=fsc_color('white'), psym=2, $
             yrange=[min(WArr)-1000, max(WArr)+1000]
      oplot, TempArr, WArr(*,0,0,1,0), color=fsc_color('black'), psym=4
      oplot, TempArr, WArr(*,0,0,0,0), color=fsc_color('black'), psym=6
      for i=1, 3 do begin
        oplot, TempArr, WArr(*,i,1,1,0), color=ColorNum.ColorNums(i+1),  psym=2
        oplot, TempArr, WArr(*,i,0,1,0), color=ColorNum.ColorNums(i+1),  psym=4
        oplot, TempArr, WArr(*,i,0,0,0), color=ColorNum.ColorNums(i+1),  psym=6
        oplot, TempArr, WArr(*,i,2,1,0), color=ColorNum.ColorNums(i+1),  psym=5
        oplot, TempArr, WArr(*,i,0,1,0), color=ColorNum.ColorNums(i+1),  psym=1
        oplot, TempArr, WArr(*,i,2,0,0), color=ColorNum.ColorNums(i+1),  psym=7
      endfor
      ItemsArr=['25', '50', '75', '100', 'Diag', 'Side', 'Cen']
      SymArr  =[2, 2, 2, 2, 6, 4, 2]
      ColArr  =[ColorNum.ColorNums(1), ColorNum.ColorNums(2), ColorNum.ColorNums(3), $
                ColorNum.ColorNums(4), $
                ColorNum.ColorNums(1), ColorNum.ColorNums(1), ColorNum.ColorNums(1)]
      ;Plot first minus last
      window, 1
      wset, 1
      plot,  TempArr, WDiffArr(*,0,1,1,9), color=fsc_color('black'), background=fsc_color('white'), psym=2, $
             yrange=[-1000, 1000]
      oplot, TempArr, WDiffArr(*,0,0,1,9), color=fsc_color('black'), psym=4
      oplot, TempArr, WDiffArr(*,0,0,0,9), color=fsc_color('black'), psym=6
      for i=1, 3 do begin
        oplot, TempArr, WDiffArr(*,i,1,1,9), color=ColorNum.ColorNums(i+1),  psym=2
        oplot, TempArr, WDiffArr(*,i,0,1,9), color=ColorNum.ColorNums(i+1),  psym=4
        oplot, TempArr, WDiffArr(*,i,0,0,9), color=ColorNum.ColorNums(i+1),  psym=6
        oplot, TempArr, WDiffArr(*,i,2,1,9), color=ColorNum.ColorNums(i+1),  psym=5
        oplot, TempArr, WDiffArr(*,i,0,1,9), color=ColorNum.ColorNums(i+1),  psym=1
        oplot, TempArr, WDiffArr(*,i,2,0,9), color=ColorNum.ColorNums(i+1),  psym=7
      endfor
      legend, ItemsArr, psym=SymArr, Colors=ColArr, TextColors=fsc_color('black')
      ;Plot Ratios
      window, 2
      wset,   2
      plot,   TempArr, WArr(*,RstN,1,0,0)/float(WArr(*,RstN,1,1,0)), $
              color=fsc_color('black'), background=fsc_color('white'), psym=2, $
              yrange=[min(WArr(*,RstN,*,*,0)/float(WArr(*,RstN,1,1,0)))-0.05, $
                      max(WArr(*,RstN,*,*,0)/float(WArr(*,RstN,1,1,0)))+0.05], $
                      xtitle='!3 Temperature'
      oplot,  TempArr, WArr(*,RstN,1,2,0)/float(WArr(*,RstN,1,1,0)), color=fsc_color('red'),   psym=2
      oplot,  TempArr, WArr(*,RstN,0,1,0)/float(WArr(*,RstN,1,1,0)), color=fsc_color('purple'), psym=2
      oplot,  TempArr, WArr(*,RstN,2,1,0)/float(WArr(*,RstN,1,1,0)), color=fsc_color('magenta'),   psym=2
      oplot,  TempArr, WArr(*,RstN,0,2,0)/float(WArr(*,RstN,1,1,0)), color=fsc_color('orange'), psym=2
      oplot,  TempArr, WArr(*,RstN,2,0,0)/float(WArr(*,RstN,1,1,0)), color=fsc_color('green'), psym=2
      oplot,  TempArr, WArr(*,RstN,2,2,0)/float(WArr(*,RstN,1,1,0)), color=fsc_color('blue'),   psym=2
      oplot,  TempArr, WArr(*,RstN,0,0,0)/float(WArr(*,RstN,1,1,0)), color=fsc_color('brown'), psym=2

      ;Plot Ratios with "Dark" subtraction
      window, 3, xpos=100, ypos=100
      wset,   3
      device, set_font = 'helvetica bold', /tt_font
      plot,   TempArr, WArrMD(*,RstN,1,0,0)/float(WArrMD(*,RstN,1,1,0)), $
              color=fsc_color('black'), background=fsc_color('white'), psym=2, $
              yrange=[0, max(float(WArrMD(*,RstN,1,0,0))/float(WArrMD(*,RstN,1,1,0)))+0.08], $
              xrange=[90, 190],$
              position=[0.125, 0.150, 0.810, 0.95], /normal, $
              xtitle='Temperature (K)', ytitle='Electrical Crosstalk', $
              xstyle=1, ystyle=1
      oplot,  TempArr, WArrMD(*,RstN,1,2,0)/float(WArrMD(*,RstN,1,1,0)), color=fsc_color('red'),   psym=2
      oplot,  TempArr, WArrMD(*,RstN,0,1,0)/float(WArrMD(*,RstN,1,1,0)), color=fsc_color('purple'), psym=2
      oplot,  TempArr, WArrMD(*,RstN,2,1,0)/float(WArrMD(*,RstN,1,1,0)), color=fsc_color('magenta'),   psym=2
      oplot,  TempArr, WArrMD(*,RstN,0,2,0)/float(WArrMD(*,RstN,1,1,0)), color=fsc_color('orange'), psym=2
      oplot,  TempArr, WArrMD(*,RstN,2,0,0)/float(WArrMD(*,RstN,1,1,0)), color=fsc_color('green'), psym=2
      oplot,  TempArr, WArrMD(*,RstN,2,2,0)/float(WArrMD(*,RstN,1,1,0)), color=fsc_color('blue'),   psym=2
      oplot,  TempArr, WArrMD(*,RstN,0,0,0)/float(WArrMD(*,RstN,1,1,0)), color=fsc_color('brown'), psym=2

      ItemsArr = ['Bot', 'Top', 'Left', 'Right', 'Top Left', 'Bot Right', 'Top Right', 'Bot Left']
      ColArr   = [fsc_color('black'), fsc_color('red'), fsc_color('purple'), fsc_color('magenta'), $
                  fsc_color('orange'), fsc_color('green'), fsc_color('blue'), fsc_color('brown')]
      Legend, ItemsArr, psym=2, Colors=ColArr, position=[0.820,0.96], /normal, TextColors=fsc_color('black')
      xyouts, 0.84, 0.50, 'V!lSUB!N ', color=fsc_color('black'), /normal
      xyouts, 0.89, 0.50, '= ' + Strtrim(String(Voltage, format='(F10.2)'),2), color=fsc_color('black'), /normal
      VResetFF = float(sxpar(h, 'VRESET'))
      xyouts, 0.84, 0.42, 'V!lFF!N ', color=fsc_color('black'), /normal 
      xyouts, 0.89, 0.42, '= ' + Strtrim(String(VResetFF, format='(F10.2)'),2), color=fsc_color('black'), /normal
      xyouts, 0.84, 0.34, 'V!lW!N  ', color=fsc_color('black'), /normal
      xyouts, 0.89, 0.34, '= ' + Strtrim(String(VResetW, format='(F10.2)'),2), color=fsc_color('black'), /normal
      device, set_font = 'symbol', /tt_font
      xyouts, 0.05, 0.75, '(a)', /normal, color=fsc_color('black'), charsize=3, charthick=6, orientation=90
      if SaveImg eq 1 then begin
        if Dir eq 1 then begin
          FileName = KeyStr.PlotDir+'IPC_Vs_Temp_'+$
                   Strtrim(Voltage,2)+'V_VSUB_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_V_Window_'+$
                   Strtrim(100,2)+'x_'+Strtrim(YPix,2)+'_VResetFF_300mV'
          Strreplace, FileName, '.', '_'
          Img  = tvread(/png, FileName = FileName, /nodialog)
        endif else if Dir eq 2 then begin
          FileName =  KeyStr.PlotDir+'IPC_Vs_Temp_'+$
                   Strtrim(Voltage,2)+'V_VSUB_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_V_Window_'+$
                   Strtrim(100,2)+'x_'+Strtrim(YPix,2)+'_VResetFF_700mV'
          Strreplace, FileName, '.', '_'
          Img  = tvread(/png, FileName = FileName, /nodialog)
        endif
      endif 
    End
    3: Begin

      if MultiPlot eq 1 then NumPlots = 4 else NumPlots = 1
      if MultiPlot eq 1 then begin
        VSUBArr     = [5, 15, 25, 25]
        DirArr      = [1, 1, 1, 2]
        VResetFFArr = [0.3, 0.3, 0.3, 0.7]
      endif

      For PlotNum=0, NumPlots-1 do begin

        if MultiPlot eq 1 then begin
          Voltage  = VSUBArr(PlotNum)
          Dir      = DirArr(PlotNum)
          VResetFF = VResetFFArr(PlotNum) 
        endif

        if Dir eq 1 then begin
          cd, '/nfs/slac/g/ki/ki04/lances/H2RG-001/ASIC/08Dec17/SinPixResetExperiment/'
        endif else if Dir eq 2 then begin
          cd, '/nfs/slac/g/ki/ki04/lances/H2RG-001/ASIC/08Dec19/SinPixResetExperiment/'
        endif
        TempArr      = [100, 110, 120, 130, 140, 150, 160, 180]
        TempArr      = [100, 110, 120, 130, 140, 150, 160, 180]
        VoltArr      = [0.1, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6]
        NRstArr      = [100, 25, 50, 75]
        NumTemps     = N_Elements(TempArr)
        NumVolts     = N_Elements(VoltArr)
        NumPix       = 9
        NumReads     = 10
        ReadArr      = Indgen(NumReads)
        WArr         = Fltarr(NumVolts, NumTemps, 4, 3, 3, NumReads)
        WDiffArr     = Fltarr(NumVolts, NumTemps, 4, 3, 3, NumReads)
        WArr2        = Fltarr(NumVolts, NumTemps, 4, 3, 3, NumReads)
        WDiffArr2    = Fltarr(NumVolts, NumTemps, 4, 3, 3, NumReads)
        WArrMD       = Fltarr(NumVolts, NumTemps, 4, 3, 3, NumReads)
  
        print, 'Reset Pixel: ' + string(9B) + ' Num Winodws ' + string(9B) + ' : ' + $
                 string(9B) + ' Center ' + string(9B) + 'Center Lost'
  
        For VoltNum = 0, NumVolts-1 do begin
          VResetW = VoltArr(VoltNum)
          For TempNum = 0, NumTemps-1 do begin
            Temp = TempArr(TempNum)
            if Rst eq 0 then begin
              if abs(XPix-100) lt 2 or Dir eq 2 then begin
                SrchStr  = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                              Strtrim(100,2)+'x_'+Strtrim(100,2)+'y_NoRst*.fits'
                SrchStr2 = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                               Strtrim(110,2)+'x_'+Strtrim(100,2)+'y_NoRst*.fits'
              endif else begin
                SrchStr  = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                              Strtrim(110,2)+'x_'+Strtrim(100,2)+'y_NoRst*.fits'
                SrchStr2 = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                               Strtrim(100,2)+'x_'+Strtrim(100,2)+'y_NoRst*.fits'
              endelse
            endif else begin
              if abs(XPix-100) lt 2 or Dir eq 2 then begin
                SrchStr  = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                               Strtrim(100,2)+'x_'+Strtrim(100,2)+'y_wRst*.fits'
                SrchStr2  = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                               Strtrim(110,2)+'x_'+Strtrim(100,2)+'y_NoRst*.fits'
              endif else begin
                SrchStr  = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                              Strtrim(110,2)+'x_'+Strtrim(100,2)+'y_wRst*.fits'
                SrchStr2 = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                               Strtrim(100,2)+'x_'+Strtrim(100,2)+'y_NoRst*.fits'
              endelse
            endelse
            if Dir eq 2 then SrchStr2 = SrchStr
  
            Print, 'Looking for files with: ' + SrchStr
            RawFiles  = File_Search(SrchStr)
            RawFiles2 = File_Search(SrchStr2)
            NumFiles = N_Elements(RawFiles)
            if FileNum2 eq 0 then FileNum2 = NumFiles
            ;100 resets comes before 25
            RawFilesTemp  = RawFiles
            RawFilesTemp2 = RawFiles2
            RawFiles(0)   = RawFilesTemp(1) & RawFiles2(0)   = RawFilesTemp2(1)
            RawFiles(3)   = RawFilesTemp(0) & RawFiles2(3)   = RawFilesTemp2(0)
            RawFiles(1)   = RawFilesTemp(2) & RawFiles2(1)   = RawFilesTemp2(2)
            RawFiles(2)   = RawFilesTemp(3) & RawFiles2(2)   = RawFilesTemp2(3)
            delvarx, RawFilesTemp
  
            For FileNumLoop = FileNum1, FileNum2-1 do begin
              Fits_Read_DataCube, RawFiles(FileNumLoop), Im, H, $
                XStart = XPix-1,  XStop  = XPix+1, $
                YStart = YPix-1,  YStop = YPix+1
              Fits_Read_DataCube, RawFiles2(FileNumLoop), Im2, H2, $
                XStart = XPix-1,  XStop = XPix+1, $
                YStart = YPix-1,  YStop = YPix+1
              WArr( VoltNum, TempNum, FileNumLoop, *,*, *)    = Im(*,*,*)
              WArr2(VoltNum, TempNum, FileNumLoop, *, *, *)   = Im2(*,*,*)
              For ReadNum = 0, NumReads-1 do begin
                WDiffArr(VoltNum, TempNum, FileNumLoop, *, *, ReadNum)  = Im(*,*,ReadNum)-long(Im(*,*,0))
                WArr2(   VoltNum, TempNum, FileNumLoop, *, *, ReadNum)  = Im2(*,*,ReadNum)-long(Im2(*,*,0))
              EndFor
              ;Take the difference between the frame with reset and without reset, with ample time to settle
              WArrMD(VoltNum, TempNum, FileNumLoop, *, *, *) = abs(float(Im(*,*,*))-Im2(*,*,*))
              delvarx, Im, Im2
            EndFor
          EndFor
        EndFor
        ;Do Just One Set of Plots
        if MultiPlot ne 1 then begin 
          window, 0
          TempNum=0
          ItemsArr  = strarr(NumTemps)
          ColorsArr = [ColorNum.ColorNums(1:8), fsc_color('black'), fsc_color('black'), fsc_color('black'), fsc_color('black')]
          PSymArr   = [2,2,2,2,2,2,2,2,2,4,5,6]
          if (XPix eq 100 and YPix eq 100) or (XPix eq 110 and YPix eq 100) then begin
            plot,  VoltArr, WArr(*,TempNum,0,1,1,PlotReadNum), color=fsc_color('black'), background=fsc_color('white'), psym=2, $
               yrange=[0, 65536]
          endif else begin
            plot,  VoltArr, WArr(*,TempNum,0,1,1,PlotReadNum), color=fsc_color('black'), background=fsc_color('white'), psym=2, $
                yrange = [min(WArr(*,*,*,1,1,PlotReadNum))-1000, max(WArr(*,*,*,1,1,PlotReadNum))+1000]
          endelse
          for TempNum = 0, NumTemps-1 do begin
            ItemsArr(TempNum) = Strtrim(TempArr(TempNum),2)+' K'
            oplot, VoltArr, WArr(*,TempNum,0,1,1,PlotReadNum),  color=ColorNum.ColorNums(TempNum+1), psym=2
            oplot, VoltArr, WArr(*,TempNum,1,1,1,PlotReadNum),  color=ColorNum.ColorNums(TempNum+1), psym=4
            oplot, VoltArr, WArr(*,TempNum,2,1,1,PlotReadNum),  color=ColorNum.ColorNums(TempNum+1), psym=5
            oplot, VoltArr, WArr(*,TempNum,3,1,1,PlotReadNum),  color=ColorNum.ColorNums(TempNum+1), psym=6
          endfor
          ItemsArr = [ItemsArr, '25', '50', '75', '100']
          Legend, ItemsArr, Colors=ColorsArr, Psym=PSymArr, TextColors=fsc_color('black')
   
          ;Try to do a linefit
          linefit_xrg, VoltArr(0:3), WArr(0:3, 0, RestN, 1, 1, PlotReadNum), m, b, sigm, E, CosE, CosR, CosT
          oplot, VoltArr(0:4), m*VoltArr(0:4)+b, color=fsc_color('black'), linestyle=2 
          print, 'Slope for lower temp : ' + Strtrim(m, 2) + ', Inc : ' + Strtrim(b, 2)
          linefit_xrg, VoltArr(0:3), WArr(0:3, 6, RestN, 1, 1, PlotReadNum), m, b, sigm, E, CosE, CosR, CosT
          oplot, VoltArr(0:4), m*VoltArr(0:4)+b, color=fsc_color('black'), linestyle=2
          print, 'Slope for lower temp : ' + Strtrim(m, 2) + ', Inc : ' + Strtrim(b, 2)

          ;Second plot, either zoom or difference between fit and data 
          window, 1
          TempNum=0
          plot,  VoltArr, WArr(*,TempNum,0,1,1,PlotReadNum), color=fsc_color('black'), background=fsc_color('white'), psym=2, $
             xrange=[0.7,1.5], yrange=[50000, 70000]
          for TempNum = 0, NumTemps-1 do begin
            ItemsArr(TempNum) = Strtrim(TempArr(TempNum),2)+' K'
            oplot, VoltArr, WArr(*,TempNum,0,1,1,0),  color=ColorNum.ColorNums(TempNum+1), psym=2
            oplot, VoltArr, WArr(*,TempNum,1,1,1,0),  color=ColorNum.ColorNums(TempNum+1), psym=4
            oplot, VoltArr, WArr(*,TempNum,2,1,1,0),  color=ColorNum.ColorNums(TempNum+1), psym=5
            oplot, VoltArr, WArr(*,TempNum,3,1,1,0),  color=ColorNum.ColorNums(TempNum+1), psym=6
          endfor
          Legend, ItemsArr, Colors=ColorsArr, Psym=PSymArr, TextColors=fsc_color('black')

          ;Second plot, either zoom or difference between fit and data 
          window, 1
          ItemsArr=Strtrim(indgen(10),2)
          ColorsArr = [ColorNum.ColorNums(1:10)]
          PSymArr   = [2,2,2,2,2,2,2,2,2,2]
          plot,  VoltArr, WArr(*,PlotTempNum,0,1,1,PlotReadNum), color=fsc_color('black'), background=fsc_color('white'), psym=2, $
             xrange=[0.0,1.7], yrange=[-4000, 4000]
          for ThisPlotReadNum = 0, 9 do begin
            linefit_xrg, VoltArr(0:3), WArr(0:3, PlotTempNum, RestN, 1, 1, ThisPlotReadNum), m, b, sigm, E, CosE, CosR, CosT
            oplot, VoltArr, WArr(*,PlotTempNum,0,1,1,ThisPlotReadNum)-(m*VoltArr+b), color=ColorNum.ColorNums(ThisPlotReadNum+1), psym=2
            oplot, VoltArr, WArr(*,PlotTempNum,1,1,1,ThisPlotReadNum)-(m*VoltArr+b), color=ColorNum.ColorNums(ThisPlotReadNum+1), psym=4
            oplot, VoltArr, WArr(*,PlotTempNum,2,1,1,ThisPlotReadNum)-(m*VoltArr+b), color=ColorNum.ColorNums(ThisPlotReadNum+1), psym=5
            oplot, VoltArr, WArr(*,PlotTempNum,3,1,1,ThisPlotReadNum)-(m*VoltArr+b), color=ColorNum.ColorNums(ThisPlotReadNum+1), psym=6
            print, 'ReadNum: ' + Strtrim(m,2) + 'Intercept: ' + Strtrim(b,2)
          endfor
          Legend, ItemsArr, Colors=ColorsArr, Psym=PSymArr, TextColors=fsc_color('black'),position=[0.0,0.9],/normal

          ;Fourth Plot
          window, 5
          plot,  [0], [0], color=fsc_color('black'), background=fsc_color('white'), psym=2, $
             xrange=[0,10], yrange=[min(WDiffArr(*,PlotTempNum,0,1,1,*)), max(WDiffArr(*,PlotTempNum,0,1,1,*))], /nodata
          for VoltNum = 0, NumVolts-1 do begin
            ItemsArr(VoltNum) = Strtrim(String(VoltArr(VoltNum),format='(F10.1)'),2)+' V'
            oplot, ReadArr, WDiffArr(VoltNum,PlotTempNum,0,1,1,*),  color=ColorNum.ColorNums(VoltNum+1), psym=2
            oplot, ReadArr, WDiffArr(VoltNum,PlotTempNum,1,1,1,*),  color=ColorNum.ColorNums(VoltNum+1), psym=4
            oplot, ReadArr, WDiffArr(VoltNum,PlotTempNum,2,1,1,*),  color=ColorNum.ColorNums(VoltNum+1), psym=5
            oplot, ReadArr, WDiffArr(VoltNum,PlotTempNum,3,1,1,*),  color=ColorNum.ColorNums(VoltNum+1), psym=6
          endfor
          Legend, ItemsArr, Colors=ColorsArr, Psym=PSymArr, TextColors=fsc_color('black'),position=[0.0,0.9],/normal

          ;Third Plot
          window, 3
          plot,  [0], [0], color=fsc_color('black'), background=fsc_color('white'), psym=2, $
             xrange=[0.0,1.7], yrange=[-4000, 4000], /nodata
          for TempNum = 0, NumTemps-1 do begin
            ItemsArr(TempNum) = Strtrim(TempArr(TempNum),2)+' K'
            linefit_xrg, VoltArr(0:3), WArr(0:3, TempNum, RestN, 1, 1, PlotReadNum), m, b, sigm, E, CosE, CosR, CosT
            print, 'Temp: ' + Strtrim(TempArr(TempNum),2)+' K' + ' m: ' + Strtrim(m,2) + ' b: ' + Strtrim(b,2)
            oplot, VoltArr, WArr(*,TempNum,0,1,1,PlotReadNum)-(m*VoltArr+b),  color=ColorNum.ColorNums(TempNum+1), psym=2
            oplot, VoltArr, WArr(*,TempNum,1,1,1,PlotReadNum)-(m*VoltArr+b),  color=ColorNum.ColorNums(TempNum+1), psym=4
            oplot, VoltArr, WArr(*,TempNum,2,1,1,PlotReadNum)-(m*VoltArr+b),  color=ColorNum.ColorNums(TempNum+1), psym=5
            oplot, VoltArr, WArr(*,TempNum,3,1,1,PlotReadNum)-(m*VoltArr+b),  color=ColorNum.ColorNums(TempNum+1), psym=6
          endfor
          stop

        EndIf Else Begin
          If KeyStr.TvFlag eq 2 then begin
            set_plot,'ps'
            device, filename = KeyStr.PlotDir+'SinglePixelReset_ADU_vs_VRESET_4Plots.eps'
            device, BITS=8, COLOR=1, ENCAPSULATED=1, xsize=7.5, ysize=10, /inches
            !p.charsize=.65
            !p.charthick=4
          Endif
        
          if PlotNum eq 0 and TvFlag eq 1 then begin
            if ZoomP eq 0 then begin
              PNGName = KeyStr.PlotDir+'SinglePixelReset_ADU_vs_VRESET_4Plots'
              TIFFName = KeyStr.PlotDir+'SinglePixelReset_ADU_vs_VRESET_4Plots'
            endif else begin
              PNGName  = KeyStr.PlotDir+'SinglePixelReset_ADU_vs_VRESET_4Plots_CloseUp'
              TIFFName  = KeyStr.PlotDir+'SinglePixelReset_ADU_vs_VRESET_4Plots_CloseUp'
            endelse
            window, 0, xsize=900, ysize=1160
            !p.font=1
            DEVICE, GET_FONTNAMES=fnames, SET_FONT='*'
            device, set_font = 'courier bold', /tt_font
            set_character_size=[180,120]
            !p.charsize=1.3  ;Set to True-Type fonts (described on page 
            !p.noerase = 0
            Erase
            plot, [0], [0], /nodata, background=fsc_color('white')
            !p.noerase = 1

          endif else if PlotNum eq 0 and TvFlag eq 2 then begin
            Erase
            !p.noerase = 1
          endif  
         
          XPlotPos = 0.110 + PlotNum mod 2 * 0.430 
          YPlotPos = 0.075 + PlotNum/2     * 0.450
          XTextPos = 0.0
          TempNum=0
          ItemsArr1  = strarr(NumTemps)
          ColorsArr1 = [ColorNum.ColorNums(1:8)]
          PSymArr1   = [2,2,2,2,2,2,2,2]
          ItemsArr2  = ['25 Rsts', '50 Rsts', '75 Rsts', '100 Rsts']
          ColorsArr2 = [fsc_color('black'), fsc_color('black'), $
                        fsc_color('black'), fsc_color('black')] 
          PSymArr2   = [2,4,5,6]
 
          if ZoomP eq 0 then begin
            ThisYRange = [0, 65536]
            if PlotNum eq 0 then begin
              ThisYTckN  = ['0', '20000', '40000', '60000' ,' ']
            endif else if PlotNum eq 1 then begin
              ThisYTckN  = [' ', ' ', ' ' ,' ' , ' ']
            endif else if PlotNum eq 2 then begin
              ThisYTckN  = ['0', '20000', '40000', '60000' ,'80000']
            endif else begin
              ThisYTckN  = [' ', ' ', ' ' ,' ' , ' ']
            endelse            
          endif else begin
            ThisYRange = [40000, 65536]
            if PlotNum eq 0 then begin
              ThisYTckN  = ['40000', ' ', '50000', ' ' ,'60000', ' ', ' ']
            endif else if PlotNum eq 1 then begin
              ThisYTckN  = [' ', ' ', ' ' ,' ' , ' ', ' ', ' ']
            endif else if PlotNum eq 2 then begin
              ThisYTckN  = ['40000', ' ', '50000', ' ' ,'60000', ' ', '70000']
            endif else begin
              ThisYTckN  = [' ', ' ', ' ' ,' ' , ' ', ' ', ' ']
            endelse            
          endelse
          if PlotNum eq 0 then begin         
            plot,  VoltArr, WArr(*,TempNum,0,1,1,PlotReadNum), color=fsc_color('black'), background=fsc_color('white'), psym=2, $
               yrange=ThisYRange, xrange=[0,1.8], $ 
               position = [XPlotPos, YPlotPos, XPlotPos+0.43, YPlotPos+0.45], $ 
               yticks=0,  ytickname = ThisYTckN, $
               xticks=0,  xtickname = ['0.0', '0.5', '1.0', '1.5', ' ']
          endif else if PlotNum eq 1 then begin
            plot,  VoltArr, WArr(*,TempNum,0,1,1,PlotReadNum), color=fsc_color('black'), background=fsc_color('white'), psym=2, $
               yrange=ThisYRange, xrange=[0,1.8], $
               position = [XPlotPos, YPlotPos, XPlotPos+0.43, YPlotPos+0.45], $
               yticks=0,  ytickname = ThisYTckN , $
               xticks=0,  xtickname=['0.0', '0.5', '1.0', '1.5', '2.0']
          endif else if PlotNum eq 2 then begin
            plot,  VoltArr, WArr(*,TempNum,0,1,1,PlotReadNum), color=fsc_color('black'), background=fsc_color('white'), psym=2, $
               yrange=ThisYRange, xrange=[0,1.8], $
               position = [XPlotPos, YPlotPos, XPlotPos+0.43, YPlotPos+0.45], $
               yticks=0,  ytickname=ThisYTckN, $
               xticks=0,  xtickname=[' ', ' ', ' ', ' ' , ' '] 
          endif else if PlotNum eq 3 then begin
            plot,  VoltArr, WArr(*,TempNum,0,1,1,PlotReadNum), color=fsc_color('black'), background=fsc_color('white'), psym=2, $
               yrange=ThisYRange, xrange=[0,1.8], $
               position = [XPlotPos, YPlotPos, XPlotPos+0.43, YPlotPos+0.45], $
               yticks=0 , ytickname = ThisYTckN , $
               xticks=0 , xtickname = [' ', ' ', ' ' ,' ', ' ']
          endif
          for TempNum = 0, NumTemps-1 do begin
            ItemsArr1(TempNum) = Strtrim(TempArr(TempNum),2)+' K'
            oplot, VoltArr, WArr(*,TempNum,0,1,1,0),  color=ColorNum.ColorNums(TempNum+1), psym=2
            oplot, VoltArr, WArr(*,TempNum,1,1,1,0),  color=ColorNum.ColorNums(TempNum+1), psym=4
            oplot, VoltArr, WArr(*,TempNum,2,1,1,0),  color=ColorNum.ColorNums(TempNum+1), psym=5
            oplot, VoltArr, WArr(*,TempNum,3,1,1,0),  color=ColorNum.ColorNums(TempNum+1), psym=6
          endfor
          if PlotNum eq 0 then Legend, ItemsArr1, Colors=ColorsArr1, Psym=PSymArr1, TextColors=fsc_color('black')
          if PlotNum eq 1 then Legend, ItemsArr2, Colors=ColorsArr2, Psym=PSymArr2, TextColors=fsc_color('black')

          ;Try to do a linefit
          if ZoomP eq 0 then begin
            linefit_xrg, VoltArr(0:4), WArr(0:4, 0, 0, 1, 1, 0), ml, bl, sigm, E, CosE, CosR, CosT
            if PlotNum lt 2 then begin
              oplot, VoltArr(0:4), ml*VoltArr(0:4)+bl, color=fsc_color('black'), linestyle=2
            endif else begin
              oplot, VoltArr(0:5), ml*VoltArr(0:5)+bl, color=fsc_color('black'), linestyle=2
            endelse
            print, 'Slope for lower temp : ' + Strtrim(ml, 2) + ', Inc : ' + Strtrim(bl, 2)
            linefit_xrg, VoltArr(0:4), WArr(0:4, 6, 0, 1, 1, 0), mh, bh, sigm, E, CosE, CosR, CosT
            oplot, VoltArr(0:4), mh*VoltArr(0:4)+bh, color=fsc_color('black'), linestyle=2
            print, 'Slope for upper temp : ' + Strtrim(mh, 2) + ', Inc : ' + Strtrim(bh, 2)
          endif
          xyouts, 0.02,  0.5,  'ADU of Single Pixel in First Read', orientation=90, color=fsc_color('black'), $
                alignment=0.5, /normal, charsize=1.9, font=1
          xyouts, 0.55,  0.02, 'VRESET of Single Pixel (Volts)', color=fsc_color('black'), $
                  alignment=0.5, /normal, charsize=1.9, font=1

          ;Fill in VRESET and other info
          if ZoomP eq 0 then begin
            xyouts, 0.28+PlotNum mod 2 * 0.43,  0.20 + PlotNum/2 * 0.45,  $
                  'VSUB = ' + Strtrim(String(VSUBArr(PlotNum), format='(I10.0)'),2), $
                  color=fsc_color('black'), /normal, font=1
            xyouts, 0.28+PlotNum mod 2 * 0.43,  0.18 + PlotNum/2 * 0.45,  $
                  'VReset FF = ' + Strtrim(String(VResetFFArr(PlotNum), format='(F10.2)'),2), $
                  color=fsc_color('black'), /normal, font=1
            xyouts, 0.28+PlotNum mod 2 * 0.43,  0.16 + PlotNum/2 * 0.45,  $ 
                  'm (<140 K ) = ' +Strtrim(String(ml, format='(I10.0)'),2), $
                   color=fsc_color('black'), /normal, font=1
            xyouts, 0.28+PlotNum mod 2 * 0.43,  0.14 + PlotNum/2 * 0.45,  $
                  'b (<140 K ) = ' +Strtrim(String(bl, format='(I10.0)'),2), $
                  color=fsc_color('black'), /normal, font=1
            xyouts, 0.28+PlotNum mod 2 * 0.43,  0.12 + PlotNum/2 * 0.45,  $ 
                  'm (>140 K ) = ' +Strtrim(String(mh, format='(I10.0)'),2), $
                   color=fsc_color('black'), /normal, font=1
            xyouts, 0.28+PlotNum mod 2 * 0.43,  0.10 + PlotNum/2 * 0.45,  $
                  'b (>140 K ) = ' +Strtrim(String(bh, format='(I10.0)'),2), $
                  color=fsc_color('black'), /normal,  font =1
          endif else begin
            xyouts, 0.28+PlotNum mod 2 * 0.43,  0.14 + PlotNum/2 * 0.45,  $
                  'VSUB = ' + Strtrim(String(VSUBArr(PlotNum), format='(I10.0)'),2), $
                  color=fsc_color('black'), /normal, font=1
            xyouts, 0.28+PlotNum mod 2 * 0.43,  0.12 + PlotNum/2 * 0.45,  $
                  'VReset FF = ' + Strtrim(String(VResetFFArr(PlotNum), format='(F10.2)'),2), $
                  color=fsc_color('black'), /normal, font=1
          endelse
        EndElse
      EndFor       
      If SaveImg eq 1 then begin
        If KeyStr.TvFlag eq 2 then device, /close
        If KeyStr.TvFlag eq 1 then begin
          Img = tvread(/png, FileName  = PNGName, /nodialog)
          Img = tvread(/tiff, FileName = TIFFName, /nodialog)
        endif
      EndIf
    End
    4: Begin
      if Dir eq 1 then begin
        cd, '/nfs/slac/g/ki/ki04/lances/H2RG-001/ASIC/08Dec17/SinPixResetExperiment/'
      endif else if Dir eq 2 then begin
        cd, '/nfs/slac/g/ki/ki04/lances/H2RG-001/ASIC/08Dec19/SinPixResetExperiment/'
      endif
      TempArr      = [100, 110, 120, 130, 140, 150, 160, 180]
      NRstArr      = [100, 25, 50, 75]
      NumTemps     = N_Elements(TempArr)
      NumPix       = 9
      NumReads     = 10
      BoxSize      = 21
      WArr1        = Fltarr(NumTemps, 4, BoxSize, BoxSize, NumReads)
      WArr2        = Fltarr(NumTemps, 4, BoxSize, BoxSize, NumReads)
      WDiffArr     = Fltarr(NumTemps, 4, BoxSize, BoxSize, NumReads)

      print, 'Reset Pixel: ' + string(9B) + ' Num Winodws ' + string(9B) + ' : ' + $
               string(9B) + ' Center ' + string(9B) + 'Center Lost'
      For TempNum = 0, NumTemps-1 do begin 
        Temp = TempArr(TempNum)
        If XPix eq 100 then begin
          SrchStr  = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                          Strtrim(100,2)+'x_'+Strtrim(YPix,2)+'y_wRst*.fits'
          SrchStr2 = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                          Strtrim(110,2)+'x_'+Strtrim(YPix,2)+'y_NoRst*.fits'
        Endif else if XPix eq 110 then begin
          SrchStr  = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                          Strtrim(110,2)+'x_'+Strtrim(YPix,2)+'y_wRst*.fits'
          SrchStr2 = '*_'+Strtrim(Voltage,2)+'*'+Strtrim(Temp,2)+'_'+Strtrim(String(VResetW, format='(F10.1)'),2)+'_*'+$
                          Strtrim(100,2)+'x_'+Strtrim(YPix,2)+'y_NoRst*.fits'
        EndIf

        Print, 'Looking for files with: ' + SrchStr
        RawFiles  = File_Search(SrchStr)
        RawFiles2 = File_Search(SrchStr2)
        NumFiles = N_Elements(RawFiles)
        if FileNum2 eq 0 then FileNum2 = NumFiles
        ;100 resets comes before 25
        RawFilesTemp  = RawFiles
        RawFilesTemp2 = RawFiles2 
        RawFiles(0)   = RawFilesTemp(1) & RawFiles2(0)   = RawFilesTemp2(1) 
        RawFiles(3)   = RawFilesTemp(0) & RawFiles2(3)   = RawFilesTemp2(0)  
        RawFiles(1)   = RawFilesTemp(2) & RawFiles2(1)   = RawFilesTemp2(2) 
        RawFiles(2)   = RawFilesTemp(3) & RawFiles2(2)   = RawFilesTemp2(3) 
        delvarx, RawFilesTemp

        For FileNumLoop = FileNum1, FileNum2-1 do begin
          Fits_Read_DataCube, RawFiles(FileNumLoop), Im, H, $
            XStart = XPix-BoxSize/2,  XStop = XPix+BoxSize/2, $
            YStart = YPix-BoxSize/2,  YStop = YPix+BoxSize/2
          Fits_Read_DataCube, RawFiles2(FileNumLoop), Im2, H2, $
            XStart = XPix-BoxSize/2,  XStop = XPix+BoxSize/2, $ 
            YStart = YPix-BoxSize/2,  YStop = YPix+BoxSize/2
          print, 'Rsts : ' + $
               (strmid(RawFiles(FileNumLoop), stregex(RawFiles(FileNumLoop), Strtrim(String(VResetW, format='(F10.1)'),2))+4,3)) + $
                ' , ' + strtrim(SxPar(H, 'DETTEMP'),2) + ' , ' + strtrim(SxPar(H, 'TIME'), 2)   + $
                ' : ' + Strtrim(Im(1,1,0)) + ' , ' + Strtrim(Im(1,1,9)-long(Im(1,1,0))) + $ 
                ' : ' + Strtrim(Im(1,0,0)) + ' , ' + Strtrim(Im(1,0,9)-long(Im(1,0,0))) + $
                ' : ' + Strtrim(Im(0,0,0)) + ' , ' + Strtrim(Im(0,0,9)-long(Im(0,0,0))) + $
                ' : ' + Strtrim(Im(2,0,0)) + ' , ' + Strtrim(Im(2,0,9)-long(Im(2,0,0))) + $ 
                ' : ' + Strtrim(Im2(1,1,0))+ ' , ' + Strtrim(Im2(1,0,0))

          WArr1(TempNum,  FileNumLoop, *,*, *)    = Im(*,*,*)
          WArr2(TempNum, FileNumLoop, *, *, *)    = Im2(*,*,*)
          For ReadNum = 0, NumReads-1 do begin
            WDiffArr(TempNum, FileNumLoop, *, *, ReadNum)  = Im(*,*,ReadNum)-long(Im2(*,*,ReadNum)) 
          EndFor 
          delvarx, Im
          delvarx, Im2
        EndFor
      EndFor
      !p.noerase=0
    End
  EndCase
  stop
  
end
