##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
##Persistence_Extract_Database.py
##
##PURPOSE: 
##To extract the persistence quanitities from the databases created with the
##Persistence_Create_Database.py script 
##
## Table 1:  HxRG.ObjectName+'_Saturation_'+str(HxRG.NReads)+'_'+HxRG.Filter
##	PixNum, X, Y, MaxSlope in Reads, MaxSlope in Seconds,
##      MaxCounts, Well Percentage, Number of Saturated Pixels, Number of 
##      Leaky pixels 
## Table 2:  HxRG.ObjectName+'_DarksAfter_'+str(HxRG.NReads)+'_'+HxRG.Filter
##
##INPUTS: 
##	ParFile: string
##	    The full path to the file that contains the parameters used
##	    for extracting the information from the database.  This file
##	    is located in a subdirectory of ./ and contains the following
##
##          DetStr          - 'H1RG-022','H2RG-32-147', or 'H4RG-10-007' 
##          ElecSt          - 'ASIC' or 'LEACH'
##          Dates           - A string array containing the dates in the path 
##          NReads          - A list with the number of reads in the images
##	    NDarkReads      - A listwith the number of 
##	    SlopeTableNames - A string array containing the names of the
##                            databases with the saturation parameters 
##                            for each star
##          DarkTableNames  - A string array containing the names of the 
##                            database with the dark parameters for each star
##
##	    SlopeFiles      - A string array containing the paths to the slope
##                            files reduced from the saturation exposures
##	    FileLists       - A string array containing the paths to the dark
##                            exposures taken after saturation
##
##      Mode: int
##	    The mode to run the script in
##
##KEYWORDS:
##	CheckStars: int
##	    0 - Trust the .coo file
##	    1 - Go through the .coo file and examine all of the stars
##      FirstFile: int 
##          The index of the first read to be plotted (starting at zero)
##      LastFile: int
##          The index of the last read to be plotted
##      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
##	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)
##	RadMode: int
##	    0 - Take average values at given radius
##	    1 - Return values for all radii
##	TvFlag: int
##	    0 - Don't plot
##	    1 - Plot quantities as script runs
##	TvPPT: int
##	    0 - Plot everything
##	    1 - Only plot the centroids
##	Verbosity: int
##	    0 - don't print much
##	    1 - print the values
##      AttFit: int
##          0 - Don't try to fit the radial function
##          1 - Try to fit the radial function with a function of the form 
##                
##              A*Cos(B*r^C)*Exp(-r^2/D)
##
##CALLING SEQUENCE:
##	run Persistence_Extract_Database.py [SlopeFileName] [FileList] [Mode] 
##
##EXAMPLES:
##run Persistence_Extract_Database.py './PersistenceExpI/NGC956_5Reads_I.py' 0
##
##////////////////////////////////////////////////////////////////////////
##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 *
from ReadCol import *
import TableIO
import csv
import pdb
import time

#Get the filename and options from command line
ParList    = sys.argv[1]
Mode       = int(sys.argv[2])

#Get the parameters from the ParList
execfile(ParList)

##PARSE KEYWORDS
keywords    = ['CheckStars=', 'FirstFile=','LastFile=','UseRef=',\
	       'DarkSub=','PlotMode=','FileList=','DoCentroid=','XCen=',\
	       'YCen=', 'BoxSize=', 'RadMode=','TvFlag=', 'AttFit=', 'TvPPT=',\
               'Verbosity=']
CheckStars  = 0
FirstFile   = 0
LastFile    = 0
UseRef      = 0
DarkSub     = 0
PlotMode    = 0
DoCentroid  = 1
XCen        = 100
YCen        = 100
BoxSize	    = 50
RadMode     = 1
TvFlag	    = 0
TvPPT       = 0
AttFit      = 0
Verbosity     = 0

opts, extraparams = getopt.getopt(sys.argv[4:],'',keywords)
for o,p in opts:
  if o in ['--CheckStars']:
    CheckStars = int(p)
  elif 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 ['--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 ['--RadMode']:
    RadMode = int(p)
  elif o in ['--TvFlag']:
    TvFlag  = int(p)
  elif o in ['--TvPPT']:
    TvPPT   = int(p)
  elif o in ['--AttFit']:
    AttFit  = int(p)
  elif o in ['--Verbosity']:
    Verbosity = int(p)

##OPEN THE DATABASE
try:
   conn=MySQLdb.connect (host="localhost",
                         user="root",
                         passwd="mysql",
                         db=DataBaseName,
                         unix_socket="/tmp/mysql.sock")
except MySQLdb.Error, e:
   print "Error $d: $s" % (e.args[0],e.args[1])
   sys.exit

cursor=conn.cursor()

##############################################################################
#MODE 0 - Plot the maximum value in the halo as a function of saturation for
#         one table
##############################################################################
if Mode == 0:
   #Set up for the sequence used to fill the database
   FileNum = 0
   DarkFiles=ReadFileList(FileLists[FileNum])
   NumDarkFiles = len(DarkFiles)

   conn.query("SELECT * FROM "+ SlopeTableNames[0]+";")
   SatRes     = conn.store_result()
   SatRows    = SatRes.fetch_row(maxrows=0, how=1)
   NumSatRows = SatRes.num_rows() 
   PixIDs   = zeros(NumSatRows,dtype=Int32)
   WellPcts = zeros(NumSatRows,dtype=float32)
   Maxs     = zeros(NumSatRows,dtype=float32) 
   Mins     = zeros(NumSatRows,dtype=float32)
   MinRads  = zeros(NumSatRows,dtype=float32)
   MaxRads  = zeros(NumSatRows,dtype=float32)
   FZeros   = zeros(NumSatRows,dtype=float32)
   SZeros   = zeros(NumSatRows,dtype=float32)

   #Get values from primary table and then access Dark table for each Pixel
   for RowNum in arange(NumSatRows):
     PixIDs[RowNum]   = SatRows[RowNum]['PIXID']
     WellPcts[RowNum] = SatRows[RowNum]['WELL_PERCENT']
     conn.query("SELECT * FROM " + DarkTableNames[0] + " WHERE PIXID="+\
              str(PixIDs[RowNum])+" AND READNUM="+ str(NDarkReads[FileNum]-1)+\
              " AND FILENUM=1;")
     DarkRes  = conn.store_result()
     DarkRow  = DarkRes.fetch_row(maxrows=0,how=1)
     Maxs[RowNum] = DarkRow[0]['MAX']
     Mins[RowNum] = DarkRow[0]['MIN']
     MinRads[RowNum] = DarkRow[0]['MINRAD']
     MaxRads[RowNum] = DarkRow[0]['MAXRAD']
     FZeros[RowNum] = DarkRow[0]['FIRSTZ']
     SZeros[RowNum] = DarkRow[0]['SECONDZ']
 
