##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
#! /usr/bin/env python
#
#This script will run DAOPHOT routines on a given fits file (FitsFileName) 
#specified on the command line. The structure of the filename is specific to 
#observations taken with the H4RG detector at the KPNO 2.1m.   
#
#Some of the keywords in the fits header are assumed to be set.  They are
#the following: 
# 
#AIRMASS- the average airmass according to Stetson's formula using Simpson's rule
#ITIME  - the integration time 
#UTMID  - the universal time at the middle of the dither sequence 
#FILTER - the filter used 
#
#KEYWORDS:
#  TvFlag      = 0 - Don't plot or display
#	         1 - Display and offer user imexamine
#  OverRide    = 0 - Don't overwrite the existing .coo or .mag files
#	         1 - Overwrite the files
#  OverWrite   = 0 - Read the paramters from the existing .par files
#		 1 - Rewrite the parameter files with the values in this file
#  SkipPSF     = 1 - Skip the PSF fitting
#  SkipNStar   = 1 - Skip the NStar
#  SkipDAOFind = 1 - Skip the DAOFind routine
#  SkipAllStar = 1 - Skip the AllStar routine
#  SkipPSF     = 1 - Skip the first psf fitting
#  SkipPSF2    = 1 - Skip the fitting of the psf after subtraction of neighbors
#  Comment     = ''- Comment to describe comditions in psf 
#  CoordFile   = ''- The name of the file that contains the coordinates
#		     that will be used for TVMARK
#Example Calling Sequence:
#run ~/python/H4RGAnly/QPhot.py '/nfs/slac/g/ki/ki09/lances/Reduced/07Apr26/M13/M13_G_SlopeFlatFieldedCenteredDitheredMean.fits' --CoordFile='/nfs/slac/g/ki/ki09/lances/Reduced/07Apr26/M13/Astrometry/M13SDSSStandards_G_XY.tab' --TvFlag=2 --SkipDAOFind=0 --SkipDAOPhot=0 --SkipPSF=0 --OverRide=1 --SkipPSF2=0 --SkipAllStar=0 --OverWrite=1 --Comment='' --UsePSFList=1
#
#run ~/python/H4RGAnly/QPhot.py '/nfs/slac/g/ki/ki09/lances/Reduced/07Apr27/Landolt110956/Landolt110956_I_SlopeFlatFieldedPersistReject2_CenteredDitheredMean.fits' --CoordFile='/nfs/slac/g/ki/ki09/lances/Reduced/07Apr27/Landolt110956/Photometry/Landolt110956_I_SlopeFlatFieldedCenteredDitheredMean.coo' --TvFlag=2 --SkipDAOFind=0 --SkipDAOPhot=0 --SkipPSF=0 --OverRide=1 --SkipPSF2=0 --SkipAllStar=0 --OverWrite=1 --Comment='' --UsePSFList=1
#run ~/python/H4RGAnly/QPhot.py '/nfs/slac/g/ki/ki09/lances/Reduced/07Apr28/Landolt/Landolt_G_SlopeFlatFieldedCenteredDitheredMean.fits' --CoordFile='/nfs/slac/g/ki/ki09/lances/Reduced/07Apr27/Landolt110956/Photometry/Landolt110956_G_SlopeFlatFieldedCenteredDitheredMean.coo' --TvFlag=2 --SkipDAOFind=0 --SkipDAOPhot=0 --SkipPSF=0 --OverRide=1 --SkipPSF2=0 --SkipAllStar=0 --OverWrite=1 --Comment='' --UsePSFList=1

import sys,os,getopt
from pyraf import iraf 
import pyfits
from pylab import *
import numpy
import numdisplay

#The filename will be specified as the first argument. These files have
#the form [Object Name]_[Filter Used]_SlopeFlatFielded...fits
FitsFileName = sys.argv[1]

#Now parse the command line keyword arguments 
keywords = ['TvFlag=', 'SkipDAOFind=', 'SkipDAOPhot=', 'SkipPSF=', \
	    'SkipNStar=', 'SkipSubStar=','SkipAllStar=','OverRide=', \
            'SkipPSF2=', 'UsePSFList=','OverWrite=','Comment=', \
	    'CoordFile=']
OverWrite   = 0
OverRide    = 0
TvFlag      = 0
SkipDAOFind = 0
SkipDAOPhot = 0
SkipPSF     = 0
SkipPSF2    = 0
SkipNStar   = 0
SkipSubStar = 0
SkipAllStar = 0
UsePSFList  = 0
Comment     = ''
CoordFile   = ''

opts, extraparams = getopt.getopt(sys.argv[2:],'',keywords)
for o,p in opts:
  if o in ['--TvFlag']:
    TvFlag=int(p)
  elif o in ['--OverRide']:
    OverRide=int(p)
  elif o in ['--SkipDAOFind']:
    SkipDAOFind = int(p)
  elif o in ['--SkipDAOPhot']:
    SkipDAOPhot = int(p)
  elif o in ['--SkipPSF']:
    SkipPSF = int(p)
  elif o in ['--SkipNStar']:
    SkipNStar = int(p)
  elif o in ['--SkipSubStar']:
    SkipSubStar = int(p)
  elif o in ['--SkipAllStar']:
    SkipAllStar = int (p)
  elif o in ['--SkipPSF2']:
    SkipPSF2 = int(p)
  elif o in ['--OverWrite']:
    OverWrite = int(p)
  elif o in ['--UsePSFList']:
    UsePSFList = int(p)
  elif o in ['--Comment']:
    Comment = p
  elif o in ['--CoordFile']:
    CoordFile = p

#Extract various keys from the full path
BaseName     = os.path.basename(FitsFileName)
DirName      = os.path.dirname(FitsFileName)
BaseParts    = BaseName.split('_')
ObjectName   = BaseParts[0]
ThisFilter   = BaseParts[1] 
BaseParts    = BaseName.split('.')
BaseKey      = BaseParts[0]

#Create a directory for Photometry output
OutDir = DirName+'/Photometry/'
if not os.path.exists(OutDir): os.mkdir(OutDir)

#Get some of the keywords from the keywords file 
if os.path.isfile('/afs/slac/u/ki/lances/python/H4RGAnly/keywords.py'):
  execfile('/afs/slac/u/ki/lances/python/H4RGAnly/keywords.py');

#GET THE DATE
Fullpath=os.getcwd()

#Read in the array
Im = pyfits.getdata(FitsFileName)

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

#PARAMETERS in the various files
datapars   = iraf.datapars.getParList()
daopars    = iraf.daopars.getParList()
findpars   = iraf.findpars.getParList()
centerpars = iraf.centerpars.getParList()
skypars    = iraf.fitskypars.getParList()
photpars   = iraf.photpars.getParList()

#KEY PARAMETERS Paramters to use
FWHM      = 12.0		  #Full Width at Half Max 
GAIN      = 5.0 		  #e-/ADU
READNOISE = 0.45		  #Readnoise for a single
NUMREADS  = 15			  #Number of reads in the ramp
NUMFILES  = 9.0 		  #Number of files used to make dither 
GAIN      = NUMFILES*GAIN         #If N images are averaged, GAIN=N*GAIN
print 'Using FWHM of ' + str(FWHM) 

#Aperture sizes to use in qphot
Apertures = str(FWHM)+','+str(1.5*FWHM)+','+str(2.*FWHM)+','+str(2.5*FWHM)+','+\
	    str(3.*FWHM)+','+str(3.5*FWHM)+','+str(4*FWHM)
#Parameters to use
Annulus  = 4*FWHM
DAnnulus = 2*FWHM

#PHOTPARS #############################################################
PhotParsFileName = OutDir+ObjectName+'_'+ThisFilter+Comment+'PhotPar.par'
if OverWrite == 1 or not os.path.exists(PhotParsFileName) :
  iraf.photpars.setParam('apertures',Apertures)
  iraf.photpars.saveParList(filename=PhotParsFileName)
else :
  iraf.photpars.setParList(ParList = PhotParsFileName)

#CENTERPARS #############################################################
CenterParsFileName = OutDir+ObjectName+'_'+ThisFilter+Comment+\
	'CenterPar.par'
if OverWrite == 1 or not os.path.exists(CenterParsFileName) :
  iraf.centerpars.setParam('calgorithm','none') #Recommended for first pass
  iraf.centerpars.setParam('cbox',4.5*FWHM)     #Centroiding box
  iraf.centerpars.setParam('cthreshold','0')
  iraf.centerpars.saveParList(filename=CenterParsFileName)
else :
  iraf.centerpars.setParList(ParList=CenterParsFileName)

#SKYPARS ################################################################
FitSkyParsFileName = OutDir+ObjectName+'_'+ThisFilter+Comment+\
	'FitSkyPar.par'
if OverWrite == 1 or not os.path.exists(FitSkyParsFileName) :
  iraf.fitskypars.setParam('salgorithm','centroid')
  iraf.fitskypars.setParam('skyvalue',2.1)
  iraf.fitskypars.setParam('annulus', Annulus)
  iraf.fitskypars.setParam('dannulus',DAnnulus)
  iraf.fitskypars.saveParList(filename=FitSkyParsFileName)
else:
  iraf.fitskypars.setParList(ParList=FitSkyParsFileName)

#FINDPARS ##############################################################
FindParsFileName =  OutDir+ObjectName+'_'+ThisFilter+Comment+\
	'FindPar.par'
if OverWrite == 1 or not os.path.exists(FindParsFileName) :
  iraf.findpars.setParam('threshold',4.0)
  iraf.findpars.setParam('sharphi',  1)
  iraf.findpars.setParam('roundhi', 1.3)
  iraf.findpars.setParam('roundlo', -1.3)
  iraf.findpars.saveParList(filename=FindParsFileName)
else :
  iraf.findpars.setParList(ParList = FindParsFileName)

#DATAPARS - http://stsdas.stsci.edu/cgi-bin/gethelp.cgi?datapars.hlp
DataParsFileName =  OutDir+ObjectName+'_'+ThisFilter+Comment+\
	'DataPar.par'
if OverWrite == 1 or not os.path.exists(DataParsFileName) :
  iraf.datapars.setParam('fwhmpsf',FWHM)   #FWHM of biggest star
  iraf.datapars.setParam('sigma','0.38')   #Std. Dev of sky pixels
  iraf.datapars.setParam('datamin','1.0')  #Min good data value ~Sky-3Sigma
  iraf.datapars.setParam('datamax','600')  #Max good data value
  iraf.datapars.setParam('epadu',GAIN)     #e- per ADU; calculated above
  iraf.datapars.setParam('readnoise','.43')#readnoise
  #KEYWORDS IN FITSHEADER
  iraf.datapars.setParam('exposure','ITIME')	#Integration time
  iraf.datapars.setParam('airmass', 'AIRMASS')	#Airmass at middle
  iraf.datapars.setParam('filter', 'FILTER')	#Filter used
  iraf.datapars.setParam('obstime','UTMID')	#UT at middle
  iraf.datapars.saveParList(filename=DataParsFileName)
else:
  iraf.datapars.setParList(ParList=DataParsFileName)

#DAOPARS ###############################################################
DAOParsFileName =  OutDir+ObjectName+'_'+ThisFilter+Comment+\
	'DAOPar.par'
if OverWrite == 1 or not os.path.exists(DAOParsFileName) :
  iraf.daopars.setParam('psfrad',4.*FWHM)
  iraf.daopars.setParam('fitrad',1.*FWHM)
  iraf.daopars.setParam('function','auto')
  iraf.daopars.setParam('nclean',2)
  iraf.daopars.setParam('sannulus',3.5*FWHM)
  iraf.daopars.setParam('wsannulus',2*FWHM)
  iraf.daopars.setParam('fitsky','no')    #Refit the sky
  iraf.daopars.setParam('varorder',0)      #Use an analytic model
  iraf.daopars.setParam('recenter','yes')
  iraf.daopars.saveParList(filename=DAOParsFileName)
else: 
  iraf.daopars.setParList(ParList = DAOParsFileName)

#The coordinate file should already exist
DaoFindFile = OutDir+BaseKey+Comment+'.coo'
QPhotFile   = OutDir+BaseKey+Comment+'.qphot.mag'
if CoordFile == '' : CoordFile = DaoFindFile

#Mark the centroids on the image if TvFlag = 1
if TvFlag >= 1:
  MaxDisp = Im.mean()+3*Im.std()
  iraf.set(stdimage='imt4096')
  iraf.display(FitsFileName,1,z1=1,z2=MaxDisp,zrange='no')
  if os.path.exists(CoordFile):
     TvMarkFile = OutDir+BaseKey+'TvMark.fits'
     if os.path.exists(TvMarkFile): os.remove(TvMarkFile)
     iraf.tvmark(1, CoordFile, outimage=TvMarkFile)
iraf.qphot.setParam('image',FitsFileName)
iraf.qphot.setParam('cbox',4.5*FWHM)
iraf.qphot.setParam('annulus', Annulus)
iraf.qphot.setParam('dannulus', DAnnulus)
iraf.qphot.setParam('coords', CoordFile)
iraf.qphot.setParam('exposure','ITIME')    
iraf.qphot.setParam('airmass', 'AIRMASS') 
iraf.qphot.setParam('filter', 'FILTER')   
iraf.qphot.setParam('obstime','UTMID')    
iraf.qphot.setParam('output', QPhotFile)
iraf.qphot.setParam('apertures', Apertures)
iraf.qphot()
