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

##This script will take a list of stars in Catalog, that have 
##the RA and DEC in Columns cRA and cDEC.  I.e. if the RA is column
##1 and DEC is column 2, then Columns='c1,c2'.
##
##It will display the image and mark the coordinate points using the display
##and tvmark IRAF routines.  It assumes the WCS keywords in the fits image
##are properly written.
##
#CALLING SEQUENCE:
#  TvCatalog.py FileName CatalogName RAColumn DECColumn [Keywords]
#
#EXAMPLES
#
#M13
#run ~/python/python_HxRG/TvCatalog.py '/nfs/slac/g/ki/ki09/lances/Reduced/07Apr26/M13/M13_G_SlopeFlatFieldedCenteredDitheredMeanWCS.fits' '/nfs/slac/g/ki/ki09/lances/Reduced/07Apr26/M13/Photometry/M13CCDPhotometry2001.txt' 10 11 4 --TvFlag=1 
#
#run ~/python/python_HxRG/TvCatalog.py '/nfs/slac/g/ki/ki09/lances/Reduced/07Apr26/M13/M13_G_SlopeFlatFieldedCenteredDitheredMeanWCS.fits' '/nfs/slac/g/ki/ki09/lances/Reduced/07Apr26/M13/Photometry/M13SDSSPhotometricTargetsNoHeader.txt' 7 8 10 --TvFlag=1 
#
#-----------------------
#NGC2395
#run ~/python/python_HxRG/TvCatalog.py '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/ASIC/Reduced/07Dec18/NGC2395/SaturationRamp_I_H2RG_SIPIN_20_Reads_Dec19_2007_04_44_16_I_RPS_SlopeFlatFielded.fits[0]' '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/ASIC/Reduced/07Dec18/NGC2395/Astrometry/NGC2935_UCAC2_Catalog_RA_DEC_Only.txt' 2 3 4 --TvFlag=1
#
#Main Reference Stars
#run ~/python/python_HxRG/TvCatalog.py '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/ASIC/Reduced/07Dec18/NGC2395/SaturationRamp_I_H2RG_SIPIN_20_Reads_Dec19_2007_04_44_16_I_RPS_SlopeFlatFielded.fits[0]' '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/ASIC/Reduced/07Dec18/NGC2395/Astrometry/NGC2935_ReferenceStars_I_H2RG_SIPIN_20_Reads_Dec19_2007_04_44_16_I.txt' 1 2
#
#run ~/python/python_HxRG/TvCatalog.py '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/ASIC/Reduced/07Dec18/NGC2395/SaturationRamp_I_H2RG_SIPIN_20_Reads_Dec19_2007_04_44_16_I_RPS_SlopeFlatFielded.fits[0]' '/nfs/slac/g/ki/ki04/lances/H2RG-32-147/ASIC/Reduced/07Dec18/NGC2395/Astrometry/SaturationRamp_I_H2RG_SIPIN_20_Reads_Dec19_2007_04_44_16_I_RPS_SlopeFlatFieldedTvPoints.txt' 3 4
##
import sys,os, getopt, TableIO
from pyraf import iraf
import pyfits
import numpy
from numpy import *
from pylab import *

#Now parse the command line keyword arguments 
keywords = ['TvFlag=']
TvFlag   = 0

opts, extraparams = getopt.getopt(sys.argv[5:],'',keywords)
for o,p in opts:
  if o in ['--TvFlag']:
    TvFlag=int(p)

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

#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]

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

#Now display them on an image if so desired
Im = pyfits.getdata(FitsFileName.replace('[0]',''))
MaxDisp = Im.mean()+3*Im.std()
if long(MaxDisp) == 0: MaxDisp = 50
iraf.set(stdimage='imt2048')
iraf.display.setParam('zrange', 'No')
iraf.display(FitsFileName,1,z1=1,z2=MaxDisp)

#Set up the coordinate transformation algorithm to take from STDIN
iraf.wcsctran.setParam('input', CoordFileName)
iraf.wcsctran.setParam('output','STDOUT')
iraf.wcsctran.setParam('image',FitsFileName)
iraf.wcsctran.setParam('inwcs','world')
iraf.wcsctran.setParam('outwcs','tv') 
iraf.wcsctran.setParam('columns', str(RACol)+','+str(DECCol))
XYCoordList = iraf.wcsctran(Stdout=1, mode = 'h')

iraf.tvmark.setParam('coords', 'STDIN')
iraf.tvmark.setParam('mark','circle')
iraf.tvmark.setParam('radii', 1)
iraf.tvmark(1,Stdin=XYCoordList,col=205, mode='h')
      
