##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
##PlotMultiDarkRamps.py
##
##PURPOSE: 
##To plot a series of dark current ramps all taken in succession.  In the hybrid
##SIPIN devices, certain pixels seem to have a memory so that the dark current
##in one exposure is affected by the exposures taken before it.
##
##Optionally, one can specify ObjectName to be something like "Flat" so that
##the Flat files will be returned instead.
##
##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    - The name of the object to be studied ("Flat", 
##                      "Dark", etc.)
##      NReads       - A string with the number of reads in the dark images
##	Row	     - The Row of the pixel desired
##	Col	     - The Column of the pixel desired
##
##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
##CALLING SEQUENCE:
##	run PlotMultiDarkRamps.py [DetStr] [ElecSt] [Date] [ObjectName] [NReads]##				  [Row] [Col]
##
##EXAMPLES:
##________
##H2RG
##  run PlotMultiDarkRamps.py 'H2RG-32-147' 'ASIC' '07Dec18' 'Dark' 30 100 200
##  run PlotMultiDarkRamps.py 'H2RG-32-147' 'ASIC' '07Dec12' 'Dark' '15.0000' 100 200 
##H1RG:
##  run PlotMultiDarkRamps.py 'H1RG-022' 'ASIC' '07Nov16' 'Dark' 100 100 200
##  run PlotMultiDarkRamps.py 'H1RG-022' 'ASIC' '07Nov16' 'Dark' 100 100 200 --UseRef=1
##
##////////////////////////////////////////////////////////////////////////
##SETUP
execfile('/afs/slac/u/ki/lances/python/python_HxRG/HxRG_Setup.py')
from HxRG_Class import *

#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]
Row	     = int(sys.argv[6])
Col          = int(sys.argv[7])

#Form the class that will contain the necessary info
HxRG = HxRG_C(DetStr=DetStr, ElecStr=ElecStr, ObjectName=ObjectName)
HxRG.Get_Date(Date)

##PARSE KEYWORDS
keywords    = ['FirstFile=','LastFile=','UseRef=', 'PlotMode=']
FirstFile   = 0
LastFile    = 0
UseRef      = 0
PlotMode    = 0
opts, extraparams = getopt.getopt(sys.argv[8:],'',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 ['--PlotMode']:
    PlotMode= int(p)

##FIND THE DARK FILES THAT MATCH THE SEQUENCE IN MIND
DarkFiles    = sort_files_by_date(HxRG.RawObjectDir+HxRG.ObjectName+\
				  '*'+'_'+NReads+'_*.fits')
if LastFile  == 0 : LastFile = len(DarkFiles)
NumDarkFiles = LastFile-FirstFile

TvFlag = 1
if PlotMode == 1 : TvFlag = 0

mplot.clf()
for FileNum in arange(NumDarkFiles) :
  ThisFileNum = FirstFile+FileNum
  FitsFileName = DarkFiles[ThisFileNum]
  HxRG.Get_Raw_Header(FitsFileName)
  FitsHDU     = pyfits.open(FitsFileName)
  ThisRamp    = zeros(HxRG.NAxis3, dtype=float64)
  if UseRef:
    ThisRefRamp = ReturnRSRamp(FitsHDU, Row, Col)
  else:
    ThisRefRamp = zeros(HxRG.NAxis3, dtype=float64)
  for ReadNum in arange(HxRG.NAxis3): 
    ThisRead = FitsHDU[0].section[ReadNum,Row,Col]+HxRG.BZero
    ThisRamp[ReadNum] = ThisRead[0]

  ThisRCRamp = ThisRamp-HxRG.CFac*ThisRefRamp
  m,b,Error,CosE = Linefit(arange(len(ThisRCRamp)),ThisRCRamp,\
                                  TvFlag=TvFlag, Verbose=0)
  if PlotMode == 1 :
     mplot.plot(ThisRCRamp)

  print 'Slope: ' + str(m)
