##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
##PlotRamps.py
##
##PURPOSE: 
##To plot ramps for a pixel with (Row, Col) for a series of consecutive exposures 
##in order to see the effects of persistence.
##
##There are two modes of operation
##  1) Make radial plots around a center spot and show the corresponding 
##     images
##     For this mode, Col, Row, and BoxSize must be defined
##
##  2) Plot the ramps for a set of pixels.
##     For this mode, Row and Col must be input as a set of arrays
##
##INPUTS: 
##      DetStr       - 'H1RG-022','H2RG-32-147', or 'H4RG-10-007' 
##	ElecSt       - 'ASIC' or 'LEACH'
##      Date         - A string containing the date in the path (ex. '07Nov16')
##      ObjectName   - A string containing the name of the object under study
##	Filter	     - A string designating the filter used: 'G,I,Y'
##      NReads       - A string with the number of reads in the dark images
##
##KEYWORDS:
##	FirstFile     - The index of the first read to be plotted
##	LastFile      - The index of the last read to be plotted
##	UseRef	      - A Boolean; 0-Don't subtract reference 1-Do subtract
##      PlotMode      - 0 - Plot ramps individually, clearing plot each time
##			1 - Plot all the ramps over each other
##	DarkSub       - 0 - Don't subtract a dark
##		        1 - Subtract a median dark with the same NReads
##	FileList      - Optionally, all of the files to be examined can 
##			be specified in this file, one per line
##	DoCentroid    - 0 - Don't center image around intenisty peak
##			1 - Center the image around intensity
##      PlotMode      - 0 - Plot The ramps, along with reference pixels below
##                      1 - Only plot ramps after ref pixs have been subtracted
##                      2 - Only plot the bias read
##  	PlotTitle     - The title to write on the plot
##	SaveImg	      - 0 - Don't save plot
##		        1 - Save the plot
##	Row	      - The row(s) of the pixels (either a single int or an array)
##	Col	      - The col(s) of the pixels (either a single int or an array)
##      IncLeg        - 0 - Don't put a legend on the plot
##                    - 1 - Put a legend on the plot
##CALLING SEQUENCE:
##	run PlotRamps.py [DetStr] [ElecSt] [Date] [ObjectName] [NReads] [Filter] 
##
##EXAMPLES:
##________
##H2RG
##run PlotRamps.py 'H2RG-32-147' 'ASIC' '07Dec12' 'NGC2683' 30 'G' --UseRef=1 --LastFile=2 --Col=1530 --Row=1290
##run PlotRamps.py 'H2RG-32-147' 'ASIC' '07Dec12' 'NGC2683' 30 'I' --UseRef=1 --LastFile=2 --Col=1556 --Row=1326
##run PlotRamps.py 'H2RG-32-147' 'ASIC' '07Dec14' 'HorseHead' 30 'G' --UseRef=1 --LastFile=5 Col=272 --Row=325 
##
##H4RG
##run PlotRamps.py 'H4RG-10-007' 'ASIC' '07Apr29' 'M16' 15 'I' --UseRef=0 --LastFile=8 --Col=2875 --Row=2890 --DarkSub=1
##run PlotRamps.py 'H4RG-10-007' 'ASIC' '07Apr29' 'M16' 20 'G' --UseRef=0 --LastFile=8 --Col=1312 --Row=2100 --DarkSub=1
##run PlotRamps.py 'H4RG-10-007' 'ASIC' '07Apr26' 'M13' 30 'G' --UseRef=0 --LastFile=8 --Col=2605 --Row=3790 --DarkSub=1
##
##H2RG_Mars
##run PlotRamps.py 'H2RG-32-147' 'ASIC' '07Dec17' 15 'I' 'Mars' --FileList='/nfs/slac/g/ki/ki03/lances/H2RG-32-147/ASIC/MarsBurnedIn/MarsYBandExposures.lst' --Col=1182 --Row=1102 --UseRef=1
##
##FORWARD BIASED DIODES
##run PlotRamps.py 'H2RG-32-147' 'ASIC' '07Dec17' 15 'I' 'ForwardBiasedDiodes' --FileList='/nfs/slac/g/ki/ki03/lances/H2RG-32-147/ForwardBiasedDiodes/NegPersistenceForwardBiasedDiodes1.lst' --Col=912 --Row=1010 --UseRef=1
##run PlotRamps.py 'H2RG-32-147' 'ASIC' '07Dec17' 15 'I' 'ForwardBiasedDiodes' --FileList='/nfs/slac/g/ki/ki03/lances/H2RG-32-147/NegPersistence/NegPersistenceForwardBiasedDiodes2.lst' --Col=882 --Row=1017 --UseRef=1
##run PlotRamps.py 'H2RG-32-147' 'ASIC' '07Dec17' 15 'I' 'ForwardBiasedDiodes' --FileList='/nfs/slac/g/ki/ki03/lances/H2RG-32-147/ForwardBiasedDiodes/DarksAndForwardBiasedDiodes.lst' --Col=882 --Row=1017 --UseRef=1
##
##COSMIC RAYS IN SUCCESSIVE DARKS:
##run PlotRamps.py 'H2RG-001' 'ASIC' '08Dec13' 'Dark' '100' 'Blank' --FileList='/nfs/slac/g/ki/ki04/lances/H2RG-001/ASIC/08Dec13/Dark/DarkFiles_160K_5V.txt' --Row=1230 --Col=176
##
##VSUB EXPERIMENTS
##run PlotRamps.py 'H1RG-022' 'ASIC' '07Nov16' 'VSUB' '100' 'All' --FileList='/nfs/slac/g/ki/ki04/lances/H1RG-022/ASIC/07Nov16/1.5V.txt' --Row=100 --Col=100
##////////////////////////////////////////////////////////////////////////
##SETUP
execfile('/afs/slac/u/ki/lances/python/python_HxRG/HxRG_Setup.py')
execfile('/afs/slac/u/ki/lances/python/SPIEPlotSettings.py')
from HxRG_Class import *
from ReturnCentroid import *
from ReturnRadialAvg import *
import TableIO
import csv
import pdb

#Get the filename and options from command line
DetStr       = sys.argv[1]
ElecStr      = sys.argv[2]
Date	     = sys.argv[3]
ObjectName   = sys.argv[4]
NReads       = sys.argv[5]
Filter       = sys.argv[6]

##INITIAL SETUP
HxRG = HxRG_C(DetStr=DetStr, ElecStr=ElecStr, ObjectName=ObjectName)
##PARSE KEYWORDS
keywords    = ['FirstFile=','LastFile=','UseRef=','DarkSub=','PlotMode=',\
	       'SaveImg=','PlotTitle=','FileList=','Col=', 'Row=', 'IncLeg=']
FirstFile   = 0
LastFile    = 0
UseRef      = 0
DarkSub     = 0
PlotMode    = 0
PlotTitle   = ''
FileList    = ''
SaveImg     = 0
Col         = 100
Row         = 100
IncLeg      = 0

opts, extraparams = getopt.getopt(sys.argv[7:],'',keywords)
for o,p in opts:
  if o in ['--FirstFile']:
    FirstFile = int(p)
  elif o in ['--LastFile']:
    LastFile  = int(p)
  elif o in ['--UseRef']:
    UseRef  = int(p)
  elif o in ['--DarkSub']:
    DarkSub = int(p)
  elif o in ['--PlotMode']:
    PlotMode= int(p)
  elif o in ['--PlotTitle']:
    PlotTitle = p
  elif o in ['--FileList']:
    FileList=p
  elif o in ['--Col']:
    Col =int(p)
  elif o in ['--Row']:
    Row  =int(p)
  elif o in ['--SaveImg']:
    SaveImg=int(p) 
  elif o in ['--IncLeg']:
    IncLeg =int(p) 
#Form the class that will contain the necessary info
HxRG = HxRG_C(DetStr=DetStr, ElecStr=ElecStr, ObjectName=ObjectName, \
              Filter=Filter, Date=Date, UseRef=UseRef, NReads=NReads)
HxRG.Get_Date(Date)
PlotDir = HxRG.DataDir+HxRG.DetStr+'/Plots/NegPersistence/'

##FIND THE MATCHING DARK
if DarkSub == 1:
   HxRG.Get_Median_Dark(HxRG.NReads)
   DarkHDU = pyfits.open(HxRG.DarkName)

if FileList=='':
  ##FIND THE FILES THAT MATCH THE SEQUENCE IN MIND AND ORDER THEM BY TIME
  if HxRG.DetStr != 'H4RG-10-007':
    FitsFiles    = sort_files_by_date(HxRG.RawObjectDir+HxRG.ObjectName+'*_'\
				  +HxRG.Filter+'_*'+NReads+'_Reads_*.fits')
  else:
    FitsFiles    = sort_files_by_date(HxRG.RawObjectDir+HxRG.ObjectName+'*_'\
                                    +NReads+'_Reads_*.fits')

  ##SELECT ONLY THE ONES NEEDED
  if LastFile  == 0 : LastFile = len(FitsFiles)
  FitsFiles    = SortFilesByTime(FitsFiles)
  NumFitsFiles = LastFile-FirstFile
  FitsFiles    = FitsFiles[FirstFile:LastFile]
else:
  ##READ THE FILES IN FROM FILELIST
  FitsFiles=ReadFileList(FileList)
  if LastFile  == 0 : LastFile = len(FitsFiles)
  NumFitsFiles = LastFile-FirstFile
  FitsFiles    = FitsFiles[FirstFile:LastFile]
 
#GO THROUGH THE FILES AND PLOT THE PERSISTENCE
mplot.close('all')
RadFig = mplot.figure(0)
RadFig.add_axes([0.05,0.05,0.95,0.7])
##USE THESE SYMBOLS AND COLORS
PSyms = ['r','b','g','m','c','orange']
PlotHandles = []
LegendLabs  = [r'\textbf{Time\ \ \ \ \ \ \ \ \ \ \ Slope'+\
		            '\ \ \ \ Error}']
LegendSyms  = ['']
FiltersArr  = []
FiltersStr  = ''

##GO THROUGH THE FILES IN SEQUENCE
for FileNum, FitsFileName in zip(arange(NumFitsFiles), FitsFiles):
  print 'FileNum: '+str(FileNum)
  FitsHDU     = pyfits.open(FitsFileName)
  HxRG = HxRG_C(FitsFileName=FitsFileName, ObjectName=ObjectName)
  HxRG.Get_Raw_Header(FitsFileName)
  ThisRamp     = zeros(HxRG.NAxis3, dtype=float64)
  print 'Analysing File :' + FitsFileName

  if UseRef:
    ThisRefRamp = ReturnRSRamp(FitsHDU, Row, Col, HxRG=HxRG)
  else:
    ThisRefRamp = zeros(HxRG.NAxis3, dtype=float64)
  for ReadNum in arange(HxRG.NAxis3): 
    ThisRead = FitsHDU[0].section[ReadNum,Row,Col]+float(HxRG.BZero)
    ThisRamp[ReadNum] = ThisRead[0]

  ##Now try to determine C_Fac by finding the straightest line
  HxRG.CFac = 1.0
  ThisRamp = ThisRamp-HxRG.CFac*ThisRefRamp

  ##Clear the plot on the first file
  if FileNum == 0:
    mplot.clf()
    Fig = mplot.figure(0)
    Fig.add_axes([0.100,0.100,0.875,0.750])
  ##Plot the ramp
  if PlotMode == 0 :
    m,b,Error,CosE = Linefit(arange(len(ThisRamp)),ThisRamp, RejectCos=0)
    if NumFitsFiles < 6:
      mplot.plot(ThisRamp, PSyms[FileNum])
      ThisP=mplot.plot(arange(len(ThisRamp))*m+b, PSyms[FileNum]+'--')
    else:
      mplot.plot(ThisRamp)
      ThisP=mplot.plot(arange(len(ThisRamp))*m+b, '--') 

    mplot.xlabel('Read Number')
    mplot.ylabel('ADU (Raw)')
    mStr = "%.2f" % m
    ErrStr = "%.2f" % Error[0]
    LegendLabs.append(HxRG.Time+'\ \ \ \ \ '+mStr+'\ \ \ \ \ '+ErrStr)
    LegendSyms.append(ThisP[0])
    if FileNum==NumFitsFiles-1: 
      if IncLeg == 1: mplot.legend(LegendSyms,LegendLabs,loc=2)
      mplot.xlabel('Read Number')
      mplot.ylabel('ADU (Raw)')
      thisylim=ylim()
      ax2=twiny()
      xlim(0, HxRG.ITime)
      ylim(thisylim[0],thisylim[1])
      mplot.xlabel('Time (Seconds)')
      mplot.figtext(.7,.2, 'Row : ' +str(Row))
      mplot.figtext(.7,.15,'Col : ' +str(Col))
      gca().yaxis.set_major_formatter(ScalarFormatter(useOffset=False))
      if PlotTitle != '': 
         mplot.figtext(0.55,0.95,PlotTitle, \
                       horizontalalignment='center',fontsize=14)
  else :
    mplot.clf()
    mplot.subplot(211)
    mplot.plot(ThisRamp, 'k')
    mplot.figtext(.5,.95,strip_sc(BaseName), fontsize=10,
                horizontalalignment='center')
    mplot.figtext(.7,.4, 'Row : ' +str(Row))
    mplot.figtext(.7,.35,'Col : ' +str(Col))
    mplot.ylabel('Sci. Pixel ADU', horizontalalignment='right')
    mplot.subplot(212)
    mplot.plot(ThisRefRamp, 'k')
    mplot.ylabel('Ref. Pixel ADU')
    mplot.xlabel('Read Number')

#Save the file if requested
if SaveImg==1:
   PlotDir=HxRG.DataDir+HxRG.DetStr+'/'+'Plots/Ramps/'
   mplot.savefig(PlotDir+HxRG.RawObjectKey+'_'+str(Row)+'_'+str(Col)+'.png') 
