##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
##PURPOSE: 
##  To retrieve the values from the LeakyPixel MySQL database
## 
##  Make sure the database is running with the following commands:
##
##  cd /u2/ki/software/mysql-5.0.45-linux-i686-icc-glibc23/
##  sudo ./bin/mysqld_safe -u root &
##
##  The tables in the LeakyPix database have the format of 
##      [DetStr]_[ElecStr]
##  where the DetStr, ElecSTr, and WM are defined as inputs.
##
##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')
##      NReads       - A string with the number of reads in the dark images
##
##KEYWORDS:
##      XStart: int
##	    X start coordinate for region of interest
##      XStop: int     
##	    X stop coordinate for region of interest
##      YStart: int    
##	    Y start coordinate for region of interest
##      YStop: int 
##	    Y stop coordinate for region of interest
##      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
##	MaxRows: int   
##	    The number of rows to be extracted from the database
##	QueryString: string
##	    The stipulation of the MySQL query.  This can be a limit on 
##	    ADU (Single Hit Value) or ADU3 (The sum of all 9 pixels in the box).
##	    Example: "WHERE ADU < 150"    
##	FormMaster: int
##	    0 - Don't write any new tables to the database
##	    1 - Make a new table with the pixels that appear leaky in every
##	        exposure. This will be the master table
##	
##CALLING SEQUENCE:
##  run FE55_Create_Database.py [DetStr] [ElecStr] [
##
##EXAMPLES:
##run LeakyPix_Extract_Database.py 'H2RG-32-147' 'ASIC' '07Dec12' '15.0000' --XStart=200 --XStop=400 --YStart=200 --YStop=400
##
##run LeakyPix_Extract_Database.py 'H2RG-32-147' 'ASIC' '07Dec12' '15.0000' --XStart=200 --XStop=400 --YStart=200 --YStop=400 --QueryString='WHERE ADU < 150'
##
##/////////////////////////////////////////////////////////////////
##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]
NReads       = sys.argv[4]
ObjectName   = 'Fe55'

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

#Now parse the command line keyword arguments 
keywords = ['XStart=', 'XStop=', 'YStart=', 'YStop=', 
	    'FirstFile=', 'LastFile=', 'UseRef=', 'MaxRows=',\
	    'QueryString=','FormMaster=']
XStart      = 4
XStop       = HxRG.FNAxis1-5
YStart      = 4
YStop       = HxRG.FNAxis2-5
FirstFile   = 0
LastFile    = 0
UseRef      = 0
MaxRows     = 0
QueryString = ''
FormMaster  = 0

opts, extraparams = getopt.getopt(sys.argv[5:],'',keywords)
for o,p in opts:
  if o in ['--XStart']:
    XStart =int(p)
  elif o in ['--XStop']:
    XStop  =int(p)
  elif o in ['--YStart']:
    YStart = int(p)
  elif o in ['--YStop']:
    YStop  = 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 ['--MaxRows']: 
    MaxRows  = int(p) 
  elif o in ['--QueryString']:
    QueryString=p
  elif o in ['--FormMaster']:
    FormMaster = int(p)

##CONNECT*****************************************************************
##Open up the connection and include some error handling
try:
        conn=MySQLdb.connect (host="localhost",
                              user="root",
                              passwd="mysql",
                              db="test", 
			      unix_socket="/tmp/mysql.sock")
except MySQLdb.Error, e:
        print "Error $d: $s" % (e.args[0],e.args[1])
        sys.exit

##OPEN DATABASE*************************************************
##Create the cursor object to handle manipulations
cursor=conn.cursor()

TableName = HxRG.DetStrMySQL+'_'+HxRG.ElecStr

try:
        cursor.execute("use LeakyPix")
except MySQLdb.Error, e:
        print "Database does not exist" 

#Get all of the data from the database
conn.query("SELECT X, Y, FILTER, DATE, 4RATIO FROM " + \
	   TableName + " " + QueryString+";")
R=conn.store_result()

if MaxRows == 0: MaxRows = 0
Rows = R.fetch_row(maxrows=MaxRows, how = 1)
NBins        = 800
Threshold    = 60
MaxThreshold = Threshold+NBins

#Extract the data from the tuple of dictionaries
if MaxRows == 0: MaxRows = len(Rows)
Xs = zeros(MaxRows,dtype=float32)
Ys = zeros(MaxRows,dtype=float32)
Xs      = []
Ys      = []
Ratio4  = []
Filters = []
Dates   = []
LPTuple   = []
for RowNum in arange(MaxRows):
  #Xs[RowNum]     = Rows[RowNum]['X']
  #Ys[RowNum]     = Rows[RowNum]['Y']
  Xs.append(Rows[RowNum]['X'])
  Ys.append(Rows[RowNum]['Y'])
  Filters.append(Rows[RowNum]['FILTER'])
  Dates.append(Rows[RowNum]['DATE'])
  Ratio4.append(Rows[RowNum]['4RATIO'])
  LPTuple.append([Xs[RowNum],Ys[RowNum],Filters[RowNum],Dates[RowNum],\
	        Ratio4[RowNum]])

#Sort the Arrays based upon pixel
#Ys.sort()
Xs.sort(key=lambda x, p=iter(Ys).next: p())
Dates.sort(key=lambda x, p=iter(Ys).next: p())
Filters.sort(key=lambda x, p=iter(Ys).next: p())

#Sort the tuple by x and y coordinates
LPTuple.sort(key=lambda x:x[0])
LPTuple.sort(key=lambda x:x[1])

ThisX = 0
ThisY = 0
ThisRatio = 0
SingX = []
SingY = []
AvgRa = []
NumXY = []
MaxXY = 12

for LPTupleRow in arange(LPTuple.__len__()):
  if LPTuple[LPTupleRow][0] == ThisX and LPTuple[LPTupleRow][1] == ThisY:
        ThisRatio.append(LPTuple[LPTupleRow][4])
	NumThisXY+=1
  else:
     if ThisX !=0 and ThisY !=0:
	#A new pixel, append old values
        if NumThisXY == MaxXY:
          SingX.append(ThisX)
          SingY.append(ThisY) 
          AvgRa.append(mean(ThisRatio))
	  NumXY.append(NumThisXY)

	#Form a new pixel ratio array
        NumThisXY = 1
	del ThisRatio
        ThisRatio = [LPTuple[LPTupleRow][4]]

     else:
        del ThisRatio
        ThisRatio = [LPTuple[LPTupleRow][4]]
	NumThisXY = 1
     ThisX = LPTuple[LPTupleRow][0] ; ThisY = LPTuple[LPTupleRow][1]

########################################################################
##FORM THE MASTER TABLE IF REQUESTED
if FormMaster == 1:
  MasterMask = zeros([HxRG.FNAxis1, HxRG.FNAxis2],dtype=float32)
  MasterMaskName = HxRG.LeakyDir+'LeakyMask.fits'
  MasterTableName = HxRG.DetStrMySQL+'_Master'

  try:
     cursor.execute('CREATE TABLE '+ MasterTableName +\
                    '(PIXID MEDIUMINT NOT NULL PRIMARY KEY,'+\
                     'X SMALLINT,'+\
                     'Y SMALLINT,'+\
                     'RATIO FLOAT);')    
  except MySQLdb.Error, e:
     print "Table exists: Moving On"
  
  #Add the values to the master table
  for LeakyNum in arange(NumXY.__len__()):
     PixID = SingY[LeakyNum]*HxRG.FNAxis1+SingX[LeakyNum]
     MasterMask[SingY[LeakyNum], SingX[LeakyNum]] = AvgRa[LeakyNum]
     cursor.execute("INSERT INTO "+MasterTableName+" VALUES("+\
                           str(PixID)+","+\
                           str(SingX[LeakyNum])  +","+\
                           str(SingY[LeakyNum])  +","+\
                           str(AvgRa[LeakyNum])  +")")

  #Write the .fits file
  HDU = pyfits.PrimaryHDU(MasterMask)
  HDU.writeto(MasterMaskName)
  
#Now do the histograms
N,Bins = histogram(AvgRa, 50)


