##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
##PlotRadialProfiles.py
##
##PURPOSE: 
##To plot a series of radial profiles to try and understand the persistence 
##
##INPUTS: 
##      FitsFileName: str
##        The full path to the fits file that will be plotted
##      
##KEYWORDS:
##      UseRef: int
##          0-Don't subtract reference 
##          1-Do subtract
##      PlotMode: int      
##	    0 - Plot ramps individually, clearing plot each time
##	    1 - Plot all the ramps over each other
##	DarkSub: int
##	    0 - Don't subtract a dark
##	    1 - Subtract a median dark with the same NReads
##	DoCentroid: int    
##	    0 - Don't center image around intenisty peak
##	    1 - Center the image around intensity
##      XCen: int       
##	    X center coordinate for region of interest
##      YCen: int
##	    Y center coordinate for region of interest
##      BoxSize: int
##	    Size of box centered around (XCen, YCen)
##      Read1: int
##          The first read to use in bias subtraction
##      Read2: int
##          The second read to use in bias subtraction
##          If Read1=Read2, just use that Read
##      PlotTitle: str
##          The title of the plot
##      PlotPath: str
##          The relative path where the file will be written
##	AttFit: int
##	    0 - Don't try to fit the radial function
##      TextLoc: int
##          0 - Do the figure text in upper right hand corner
##          1 - Do the figure text in lower right hand corner
##
##CALLING SEQUENCE:
##  run PlotRadialProfile.py [FitsFileName]
##
##EXAMPLES:
##________
##
##OFFSET FROM PERSISTENCE:
##  run PlotRadialProfile.py '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/ASIC/07Dec19/HD53791/HD53791_Dark_H2RG_SIPIN_OneWindow2000_Reads_Dec20_2007_00_54_25.fits' --YCen=68 --XCen=78 --BoxSize=45 --PlotTitle='Initial Offset After Saturation' --PlotPath='Plots/NegPersistence/HD53791_OffsetInDarkAfterSaturation' --TextLoc=1
##
##THESIS PLOT FOR HD53791 IN WINDOW MODE
##  run PlotRadialProfile.py '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/ASIC/07Dec19/HD53791/HD53791_Dark_H2RG_SIPIN_OneWindow2000_Reads_Dec20_2007_00_54_25.fits' --YCen=65 --XCen=79 --BoxSize=40 --Read1=0 Read2=1999 
##
##EXPOSURE THAT INDUCED PERSISTENCE:
##  run PlotRadialProfile.py '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/ASIC/07Dec19/HD53791/HD53791_H2RG_SIPIN_10_Reads_Dec20_2007_00_53_18.fits' --YCen=68 --XCen=78 --BoxSize=45 --PlotTitle='Last Read of Saturation from HD53791' --PlotPath='Plots/NegPersistence/HD53791_SaturationFrom_00_53_18' --TextLoc=0
##
##////////////////////////////////////////////////////////////////////////
##SETUP
execfile('/afs/slac/u/ki/lances/python/python_HxRG/HxRG_Setup.py')
execfile('/afs/slac/u/ki/lances/python/ThesisPlotSettings.py')
from HxRG_Class import *
from ReturnCentroid import *
from ReturnRadialAvg import *
from ReturnRadialProfiles import *
from ReadCol import *
from Return_Persistence_Quantities import *
import TableIO
import csv
import pdb
import time

#Get the filename and options from command line
FitsFileName       = sys.argv[1]

##INITIAL SETUP
HxRG = HxRG_C(FitsFileName=FitsFileName)

##PARSE KEYWORDS
keywords    = ['UseRef=','DarkSub=', \
               'DoCentroid=','XCen=', 'YCen=', 'BoxSize=', \
               'Read1=', 'Read2=', 'PlotTitle=', 'PlotPath=', \
               'AttFit=', 'TvFlag=', 'TextLoc=']
UseRef      = 0
DarkSub     = 0
DoCentroid  = 1
XCen        = 100
YCen        = 100
BoxSize	    = 50
AttFit      = 0
TvFlag      = 0
Read1       = 0
Read2       = 0
PlotTitle   = ''
PlotPath    = ''
TextLoc     = 0

opts, extraparams = getopt.getopt(sys.argv[2:],'',keywords)
for o,p in opts:
  if o in ['--UseRef']:
    UseRef  = int(p)
  elif o in ['--DarkSub']:
    DarkSub  = int(p)
  elif o in ['--PlotMode']:
    PlotMode = int(p)
  elif o in ['--DoCentroid']:
    DoCentroid =int(p)
  elif o in ['--XCen']:
    XCen =int(p)
  elif o in ['--YCen']:
    YCen  =int(p)
  elif o in ['--BoxSize']:
    BoxSize = int(p)
  elif o in ['--AttFit']:
    AttFit = int(p)
  elif o in ['--Read1']:
    Read1 = int(p)
  elif o in ['--Read2']:
    Read2 = int(p)
  elif o in ['--PlotPath']:
    PlotPath = p
  elif o in ['--PlotTitle']:
    PlotTitle = p
  elif o in ['--TvFlag']:
    TvFlag = int(p)
  elif o in ['--TextLoc']:
    TextLoc= int(p)

Tp = PlotRadialAvg(FitsFileName, XCen, YCen, BoxSize, Read1, Read2, \
                   PlotTitle = PlotTitle, \
                   PlotPath=PlotPath, TextLoc=TextLoc)
