;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
;pro Examin_VRESET_VS_DSUB__xRG
;
;PURPOSE:
;	To take a list of files that were taken with a range of DSUB and VRESET 
;       values and plot the behavior of the reference and science pixels.  The 
;       behavior should yield information on the capacitance of the reference pixels. 
;
;INPUTS:
;	XPix - The Column of the pixl
;	YPix = The Row of the pixel
;
;KEYWORDS:
;       FILELIST: str
;         The full path to a file that contains the names of the files to 
;         use for the VRESET and DSUB plot
;       TVFLAG: 
;         0 - Default, Don't plot anything
;         1 - Plot to the X window      
;       SAVEIMG: int
;         0 : don't save the plots
;         1 : save the plots
;CALLING SEQUENCE:
;
;
;**************************************************************************************************************
;
pro Examine_VRESET_Vs_DSUB_xRG, XPix, YPix, TvFlag = TvFlag, SaveImg=SaveImg, $
  FileList = FileList

  If N_Elements(SaveImg)     eq 0 then SaveImg       = 0
  If N_Elements(TvFlag)      eq 0 then TvFlag        = 1
  If N_Elements(FileList)    eq 0 then FileList      = $
    '/nfs/slac/g/ki/ki04/lances/H1RG-022/ASIC/08Jun28/DSUB_VRESET_Files.txt' 

  ;If filename ends in .lst, slopefit all of the filenames in that file
  ReadCol, FileList, RawFiles, Format = 'A'
  NumFiles = N_Elements(RawFiles)
  FitsFileName = RawFiles(0)
 
  Common KeyParams, KeyStr

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

  BiasDiffs = fltarr(NumFiles)
  VRESETs   = fltarr(NumFiles)
  DSUBs     = fltarr(NumFiles)
  PixelVals = fltarr(NumFiles)
  Colors    = fltarr(NumFiles)

  set_plot, 'x'
  plot, [0], [0], /nodata, yrange=[-300,800], xrange=[0,1.5], $
    background=fsc_color('white'), color=fsc_color('black'), $
    position = [0.1, 0.1, 0.75, 0.9], /normal
  device, decomposed=0
  loadct, 17
  Items   = ['']
  Syms    = [0.]
  Colors  = [0.]
  DSUBOld = 0
  DSUBNew = 0 
 
  ;Loop over all of the files
  For FileNum = 0, NumFiles-1 do begin

    Fits_Read_DataCube, RawFiles(FileNum), Im1, H, $
      XStart=XPix, XStop=XPix, YStart=YPix, YStop=YPix, $
      ZStart=0, ZStop=0
    Fits_Read_DataCube, RawFiles(FileNum), Im2, H, $
      XStart=XPix, XStop=XPix, YStart=YPix, YStop=YPix, $
      ZStart=49, ZStop=49

    VRESET   = float(sxpar(H, 'VRESET'))
    DSUB     = float(sxpar(H, 'DSUB'))
    BiasDiffs(FileNum)  = DSUB-VRESET
    VRESETs(FileNum)   = VRESET
    DSUBs(FileNum)     = DSUB
    PixelVals(FileNum) = Im2-float(Im1)
    Color              = long(DSUB*170)
  
    DSUBNew = round(DSUB*10)
    if DSUBNew ne DSUBOld then begin
      DSUBOld = DSUBNew
      Items   = [Items, strtrim(DSUB,2)]
      Syms    = [Syms, 2]
      Colors  = [Colors, Color]
    endif 

    oplot, [BiasDiffs(FileNum)], [PixelVals(FileNum)], psym=2, color=Color

  EndFor
  legend, Items, colors=colors, psym=syms, textcolors=fsc_color('black'), $
    position = [0.775, 0.9], /normal

stop

end

