##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
##ExaminePersistenceNoise.py
##
##PURPOSE: 
##  To examine the noise in the core-halo structure of the persistence in 
##  the HxRG detectors and determine whether it follows poisson statistics.
##
##  The same star will be followed in a series of dithers and centered.
##  
##  1) Make radial plots around a center spot and show the corresponding 
##     images
##     For this mode, XCen, YCen, and BoxSize must be defined
##
##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: int
##           The index of the first read to be plotted
##	LastFile: int      
##	     The index of the last read to be plotted
##	UseRef: int	      
##	      A Boolean; 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
##	FileList: string      
##	      Optionally, all of the files to be examined can 
##            be specified in this file, one per line
##	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)
##	CenSize: int	   
##	      Size of box used to do centroid
##	TvFlag: int         
##	      0 - Don't plot
##            1 - Plot
##	TrackStar: int
##	      0 - Look at the noise in the actual star
##	      1 - Look at the noise in the persistence 
##	AllReads: int
##	      0 - Collect data only for read specified by ReadNum
##	      1 - Collect data for all reads in all exposures
##	ReadNum: int
##	      The read for which the data will be collected for all
##	      files in FileList
##
##CALLING SEQUENCE:
##	run ExaminePersistenceNoise.py.py [DetStr] [ElecSt] [Date] [ObjectName] [NReads] [Filter] 
##
##EXAMPLES:
##
##H2RG_Mars
##
##run ExaminePersistenceNoise.py 'H2RG-32-147' 'ASIC' '07Dec17' 15 'I' 'ForwardBiasedDiodes' --FileList='/nfs/slac/g/ki/ki03/lances/H2RG-32-147/NegPersistence/NGC2683_I_Slopes.lst' --XCen=912 --YCen=1010 --BoxSize=40 --DoCentroid=0 --UseRef=1
##run ExaminePersistenceNoise.py 'H2RG-32-147' 'ASIC' '07Dec17' 15 'I' 'ForwardBiasedDiodes' --FileList='/nfs/slac/g/ki/ki03/lances/H2RG-32-147/NegPersistence/NGC2683_I_Slopes.lst' --XCen=1555 --YCen=1326 --BoxSize=40 --DoCentroid=0 --UseRef=1
##
##run ExaminePersistenceNoise.py 'H2RG-32-147' 'ASIC' '07Dec17' 15 'I' 'ForwardBiasedDiodes' --FileList='/nfs/slac/g/ki/ki03/lances/H2RG-32-147/NegPersistence/NGC2683_I_Slopes.lst' --XCen=1555 --YCen=1326 --BoxSize=30 --DoCentroid=1 --UseRef=1 --TvFlag=0 --CenSize=5
##////////////////////////////////////////////////////////////////////////
##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
import asciidata

#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=',\
	       'FileList=','DoCentroid=','XCen=', 'YCen=', 'BoxSize=',\
	       'CenSize=','TvFlag=', 'TrackStar=', 'AllReads=', 'ReadNum=']
FirstFile   = 0
LastFile    = 0
UseRef      = 0
DarkSub     = 0
PlotMode    = 0
FileList    = ''
DoCentroid  = 1
XCen        = 100
YCen        = 100
BoxSize	    = 50
CenSize     = 8
TvFlag	    = 0
TrackStar   = 0
AllReads    = 0
ReadNum     = 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 ['--FileList']:
    FileList=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 ['--CenSize']:
    CenSize = int(p) 
  elif o in ['--TvFlag']:
    TvFlag = int(p)
  elif o in ['--TrackStar']:
    TrackStar = int(p)
  elif o in ['--AllReads']:
    AllReads = int(p)
  elif o in ['--ReadNum']:
    ReadNum = int(p)

#Determine coordinates
XStart=XCen-BoxSize/2 ; XStop=XCen+BoxSize/2
YStart=YCen-BoxSize/2 ; YStop=YCen+BoxSize/2
 
#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)

##Take the files in along with the offsets
if FileList!='':
   Columns  = asciidata.open(FileList)
   NumFiles = Columns.nrows

##GO THROUGH THE FILES AND PLOT THE PERSISTENCE
mplot.close('all')
RadFig = mplot.figure(0)

##USE THESE SYMBOLS AND COLORS
PSyms = ['r+','b+','g+','m+','c+','orange+']
PlotHandles = []
LegendLabs  = []
LegendSyms  = []
FiltersArr  = []
FiltersStr  = ''

##Get Some Data from the first file
FitsFileName = Columns[0][0]
FitsHDU     = pyfits.open(FitsFileName)
HxRG = HxRG_C(FitsFileName=FitsFileName)
HxRG.Get_Raw_Header(FitsFileName)

##Setup arrays for holding values
if AllReads == 1:
  if TrackStar == 1:
    IntArr = zeros([BoxSize,BoxSize,HxRG.NAxis3,NumFiles],dtype=float32)
  else:
    IntArr = zeros([BoxSize,BoxSize,HxRG.NAxis3,NumFiles-1],dtype=float32)
else:
  if TrackStar == 1:
    IntArr = zeros([BoxSize,BoxSize,NumFiles],dtype=float32)
  else:
    IntArr = zeros([BoxSize,BoxSize,NumFiles-1],dtype=float32)

##Starting from SECOND FILE, so initially keep offsets at 0
XOff = 0 ; YOff = 0
##GO THROUGH THE FILES IN SEQUENCE
for FileNum in range(Columns.nrows):
  print 'FileNum: '+str(FileNum)
  FitsFileName = Columns[0][FileNum]
  FitsHDU     = pyfits.open(FitsFileName)
  HxRG = HxRG_C(FitsFileName=FitsFileName, ObjectName=ObjectName)
  HxRG.Get_Raw_Header(FitsFileName)

  #Get the offsets from the .lst file
  if TrackStar != 1 and FileNum > 0:
    XOff = Columns[1][FileNum-1]
    YOff = Columns[2][FileNum-1]
  else:
    XOff = Columns[1][FileNum]
    YOff = Columns[2][FileNum]

  #Get Offsets for this file
  ThisXStart = XStart+XOff; ThisXStop = XStop+XOff
  ThisYStart = YStart+YOff; ThisYStop = YStop+YOff
 
  #Get the image
  Im  = FitsHDU[0].section[HxRG.NAxis3-1,:,:]
  Im0 = FitsHDU[0].section[0,:,:]

  #Subtract Reference Pixels if necessary
  if UseRef==1:
     Im  = ReturnRSFrame(Im,Im0, HxRG.CFac,BadCols=HxRG.BadCols)
     Im0 = ReturnRSFrame(Im0,Im0, HxRG.CFac,BadCols=HxRG.BadCols)
  Im = Im-Im0
 
  #Obtain the centroid to align the images
  if DoCentroid==1: 
    ThisCen = ReturnCentroid(Im, ThisXStart, ThisXStop,\
	  		     ThisYStart, ThisYStop, TvFlag=TvFlag,\
			     CenSize=CenSize)
    ThisXStart = ThisCen[0]; ThisXStop = ThisCen[1]
    ThisYStart = ThisCen[2]; ThisYStop = ThisCen[3]
 
  #Go through all files and get the reads
  if AllReads == 1:
    for ReadNum in arange(HxRG.NAxis3):
      Im  = FitsHDU[0].section[ReadNum,:,:]
      if UseRef==1:
        Im  = ReturnRSFrame(Im,Im0, HxRG.CFac,BadCols=HxRG.BadCols)
      Im = Im-Im0
      SubIm  = Im[ThisYStart:ThisYStop, ThisXStart:ThisXStop]
      if TrackStar !=1 and FileNum > 0: 
        IntArr[:,:,ReadNum,FileNum-1] = SubIm
      else:
        IntArr[:,:,ReadNum,FileNum] = SubIm
  else:
    Im  = FitsHDU[0].section[ReadNum,:,:]
    if UseRef==1:
      Im  = ReturnRSFrame(Im,Im0, HxRG.CFac,BadCols=HxRG.BadCols)
    Im = Im-Im0
    SubIm  = Im[ThisYStart:ThisYStop, ThisXStart:ThisXStop]
    if TrackStar !=1 and FileNum > 0:
      IntArr[:,:,FileNum-1] = SubIm
    else:
      IntArr[:,:,FileNum] = SubIm

if AllReads == 1:
  StdArr  = IntArr.std(axis=3)
  MeanArr = IntArr.mean(axis=3)
else:    
  StdArr  = IntArr.std(axis=2)
  MeanArr = IntArr.mean(axis=2)
  RadFig.add_axes([0.05,0.05,.4,.85])
  mplot.imshow(StdArr)
  RadFig.add_axes([.5,0.05,.4,.85])
  mplot.imshow(MeanArr)
