##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
#! /usr/bin/env python

##This script will execute the IRAF DAOFIND program on all of the files in
##the current working directory [.../2005-05-1*/ShackHartman/Raw] and 
##put the results in a MySQL database.
##
##The Database name is DAOFIND and the table for each date is given by
##	0[Month]_[Date]
##
##The entries in the DATABASE ARE 
###N XCENTER   YCENTER   MAG      SHARPNESS   SROUND      GROUND      ID         
import sys,os
from pyraf import iraf 
import pyfits
from numarray import *
from pylab import *
import MySQLdb
import get_date

#GET THE DATE
Date=get_date.get_date1()
Year=Date[0]
Month=Date[1]
Day=Date[2]

#GET ALL OF THE RAW FILENAMES IN CWD
raw_filenames=iraf.files('*.fits',Stdout=1)
num_files=len(raw_filenames)
exam_date='1_31'
##Open up the connection and include some error handling
try:
        conn=MySQLdb.connect (host="localhost",
                              user="root",
                              passwd="mysql",
                              db="my_test")
except MySQLdb.Error, e:
        print "Error $d: $s" % (e.args[0],e.args[1])
        sys.exit
                                                                                
##CREATE CURSOR OBJECT
cursor=conn.cursor()
                                                                                
try:
        cursor.execute("CREATE DATABASE DAOFIND")
        print "Database Created"
except MySQLdb.Error, e:
        print e
	print "Database exists: Moving ON"

try:
        cursor.execute("use DAOFIND");
        cursor.execute('CREATE TABLE '+exam_date+'_'+Month+'_'+Day+
                                               '(Filename varchar(20),'+
                                               'XCOORD float,'+
                                               'YCOORD float,'+
                                               'MAG float,'+
                                               'SHARPNESS float,'+
                                               'SROUND float,'+
					       'GROUND float,'+
					       'ID mediumint);')
except MySQLdb.Error, e:
        print e
                                      

#Import the good packages from IRAF
iraf.digiphot(_doprint=0)
iraf.daophot(_doprint=0)

#DATAPARS
datapars=iraf.datapars.getParList()
findpars=iraf.findpars.getParList()
centerpars=iraf.centerpars.getParList()

#SET SOME PARTICULAR 
iraf.centerpars.setParam('cbox','10.0')
iraf.datapars.setParam('fwhmpsf','4.0')
iraf.datapars.setParam('datamax','10000')
iraf.datapars.setParam('datamin','1')
iraf.findpars.setParam('threshold','10')
iraf.centerpars.saveParList(filename='~/iraf/uparm/datcentes.par')
iraf.datapars.saveParList(filename='~/iraf/uparm/datdataps.par')

#Set up daofind to go without prompting for input
iraf.daofind.setParam('image','@all_fits_files.txt')	#Set filename
iraf.daofind.setParam('output','../coo_'+exam_date+'/')
iraf.daofind.setParam('verify','no')			#Don't verify
iraf.daofind.saveParList(filename='daofind.par')	#Save values


for i in range(num_files):
	iraf.daofind.setParam('image',raw_filenames[i])
	daostring=iraf.daofind(mode='h',Stdout=1)	#Run DAOFIND hidden
	lendaostring=len(daostring)			#Get Number of Spots
	print lendaostring

	filename=daostring[1].split()[1]		#Get Filename


	for spot in range(3,lendaostring-40):
		cursor.execute("INSERT INTO "+exam_date+'_'+Month+'_'+Day+
			       " VALUES('"+
			       filename+"','"+      
			       daostring[spot].split()[0]+"','"+
                               daostring[spot].split()[1]+"','"+
                               daostring[spot].split()[2]+"','"+
                               daostring[spot].split()[3]+"','"+
                               daostring[spot].split()[4]+"','"+
                               daostring[spot].split()[5]+"','"+
                               daostring[spot].split()[6]+
			       "')")


