##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
#! /usr/bin/env python
#
#
#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     = ""- String describing the conditions used for the psf fit
# 
#Example Calling Sequence:
#run ~/python/H4RGAnly/ColorStandardize.py '/nfs/slac/g/ki/ki09/lances/Reduced/07Apr26/M13/M13_G_SlopeFlatFieldedCenteredDitheredMean.fits' '/nfs/slac/g/ki/ki09/lances/Reduced/07Apr26/M13/M13_I_SlopeFlatFieldedCenteredDitheredMean.fits' '/nfs/slac/g/ki/ki09/lances/Reduced/07Apr26/M13/M13_Y_SlopeFlatFieldedCenteredDitheredMean.fits' --TvFlag=1 
import sys,os,getopt
from   pyraf import iraf 
import pyfits
from   pylab import *
import numpy
from   numpy import *
import numdisplay
import matplotlib.pylab as mplot

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

#Now parse the command line keyword arguments 
keywords = ['TvFlag=', 'SkipDAOFind=', 'SkipDAOPhot=', 'SkipPSF=', \
	    'SkipNStar=', 'SkipSubStar=','SkipAllStar=','OverRide=', \
            'SkipPSF2=', 'OverWrite=', 'Comment=' ]
OverWrite   = 0
OverRide    = 0
TvFlag      = 0
SkipDAOFind = 0
SkipDAOPhot = 0
SkipPSF     = 0
SkipPSF2    = 0
SkipNStar   = 0
SkipSubStar = 0
SkipAllStar = 0
Comment     = ''
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 ['--Comment']:
    Comment   = p
#Extract various keys from the full path
GBaseName     = os.path.basename(GFitsFileName)
DirName      = os.path.dirname(GFitsFileName)
GBaseParts    = GBaseName.split('_')
GObjectName   = GBaseParts[0]
GThisFilter   = GBaseParts[1] 
GBaseParts    = GBaseName.split('.')
GBaseKey      = GBaseParts[0]

IBaseName     = os.path.basename(IFitsFileName)
DirName      = os.path.dirname(IFitsFileName)
IBaseParts    = IBaseName.split('_')
IObjectName   = IBaseParts[0]
IThisFilter   = IBaseParts[1]
IBaseParts    = IBaseName.split('.')
IBaseKey      = IBaseParts[0]

#Create a directory for Photometry output
OutDir = DirName+'/Photometry/'
AstDir = DirName+'/Astrometry/'
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/keywords.py'):
  execfile('/afs/slac/u/ki/lances/python/keywords.py');

#Must be in the output dir as the letter 'f' will be prepended to 
#the observations file name
PhotFileName  = OutDir+GObjectName+'PhotometryFiles.txt'
ShiftFileName = OutDir+GObjectName+'GIYShifts.txt'
ImsetFileName = OutDir+GObjectName+'Imsets.txt'
ObsFileName   = 'GIY.obs'
os.chdir(OutDir)

iraf.mkobsfile.setParam('photfiles', PhotFileName)
iraf.mkobsfile.setParam('idfilters','G,I,Y')
iraf.mkobsfile.setParam('imsets', ImsetFileName)
iraf.mkobsfile.setParam('observations',ObsFileName)
iraf.mkobsfile.setParam('shifts', ShiftFileName)
iraf.mkobsfile()
