;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
;pro Examin_Persistence_vs_VSUB_xRG
;
;PURPOSE:
;	To take a list of files that were used in measuring persistence vs 
;       VSUB and plot the maximum pixel value vs VSUB to measure the 
;       dependence 
;
;INPUTS:
;
;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:
;       Examine_Persistence_vs_VSUB_xRG
;
;**************************************************************************************************************
;
pro Examine_Persistence_vs_VSUB_xRG, TvFlag = TvFlag, SaveImg=SaveImg, $
  FileList = FileList

  If N_Elements(SaveImg)     eq 0 then SaveImg       = 0
  If N_Elements(TvFlag)      eq 0 then TvFlag        = 0
  If N_Elements(FileList)    eq 0 then FileList      = $
    '/nfs/slac/g/ki/ki04/lances/H1RG-022/ASIC/07Nov16/VSUB_Dither3_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)
  VSUBs     = fltarr(NumFiles)
  PixelVals = fltarr(NumFiles)
  Colors    = fltarr(NumFiles)
  Filters   = strarr(NumFiles)

  set_plot, 'x'
  window, 0
  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,decompose=1           ;Disable R+256*G+256^2*B
  loadct,0                     ;Load Rainbow+White Color Table
  resetplt                     ;Reset all the settings
  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, $
      ZStart=0, ZStop=0
    Fits_Read_DataCube, RawFiles(FileNum), Im2, H, $
      ZStart=99, ZStop=99

    ;Get the value from the header
    Filter   = sxpar(H, 'FILTER')
    VRESET   = float(sxpar(H, 'VRESET'))
    VSUB     = float(sxpar(H, 'VSUB'))
    LMF      = Im2-float(Im1)

    ;Get the upper right quadrant with the freshest persistence
    if Stregex(FileList, 'Dither3', /boolean) then begin
      UpRQuad  = LMF(512:*, 512:*)
      if Stregex(Filter, 'G', /boolean) eq 1 then begin
        SubQuad  = UpRQuad(160:280,70:250)
      endif else if Stregex(Filter, 'I', /boolean) then begin 
        SubQuad  = UpRQuad(180:300,120:240)
      endif else begin
        SubQuad  = UpRQuad(180:320,100:280)
      endelse
    endif else begin

    endelse
  
    if TvFlag eq 1 then begin
      window, 1
      tv, bytscl(congrid(SubQuad,512,512), min=-200, max=1000) 
    endif 
    ;Append to the array
    VRESETs(FileNum)    = VRESET
    VSUBs(FileNum)      = VSUB
    Filters(FileNum)    = Filter
 
    MaxLoc  = where(SubQuad eq max(SubQuad))
    MaxLocX = MaxLoc mod (size(SubQuad))[1]
    MaxLocY = MaxLoc / (size(SubQuad))[1]
    BS = 2
    PixAvg  = mean(SubQuad(MaxLocX-BS: MaxLocX+BS, MaxLocY-BS:MaxLocY+BS))
    PixelVals(FileNum)  = PixAvg
    print, 'FileNum: '+Strtrim(RawFiles(FileNum),2)
    print, 'VSUB: '   +Strtrim(VSUB,2)
    print, 'Filter: ' +Strtrim(Filter,2)
    print, 'Max Pix:' +Strtrim(Max(SubQuad),2)
    if TvFlag eq 1 then stop 
  EndFor
 
  GVals = where(stregex(Filters, 'G', /boolean) eq 1)
  IVals = where(stregex(Filters, 'I', /boolean) eq 1)
  YVals = where(stregex(Filters, 'Y', /boolean) eq 1)
  
  wset, 0
  plot,  VSUBs(GVals), PixelVals(GVals), color=fsc_color('black'),psym=2, $
         background=fsc_color('white')
  oplot,  VSUBs(IVals), PixelVals(IVals), color=fsc_color('blue'),psym=2
  oplot,  VSUBs(YVals), PixelVals(YVals), color=fsc_color('red'),psym=2
  items = ['G','I','Y']
  pcolors= [fsc_color('black'), fsc_color('blue'), fsc_color('red')]
  psyms = [2,2,2]
  legend, items, psym=psyms, colors=pcolors,textcolors=fsc_color('black'), $
    position=[.8,.8],/normal
stop

end

