##^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^Lance Simms, Stanford University 2009
#find_fwhm.py
#
#PURPOSE:
#Script to examine image and get an idea of the fwhm
#
#CALLING SEQUENCE:
#run ~/python/H4RGAnly/examine_fwhm '/nfs/slac/g/ki/ki09/lances/Reduced/07Apr26/M13/M13_G_SlopeFlatFieldedCenteredDitheredMean.fits' 1

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

#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');

#Get the filename and options from command line
FitsFileName = sys.argv[1]
TvFlag	     = sys.argv[2]

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]

#Get the data array from the file
Im = pyfits.getdata(FitsFileName)

#Make sure ds9 is already open and display the image
#This imt4096 will make sure all of the pixels are shown
numdisplay.close()
numdisplay.open()
numdisplay.display(Im, bufname = 'imt4096')

#Now move to the imexamine.  Can't figure out how to display full size
#image; automatically set to 512x512.
iraf.imexamine(FitsFileName)

#Display the image along with the coordinates found
iraf.set(stdimage='imt4096')
iraf.display(FitsFileName,1)
CentroidFile = DirName+'/Photometry/'+BaseKey+'.coo'
if os.path.exists(CentroidFile): iraf.tvmark(1, CentroidFile )
#Commands:
#Go to a star on the dispaly and click 'r' to get radial profile
#To make the fit radius bigger, type
#:rplot 20
#Go to a star on the display and click 'j' to fit a gaussian profile FWHM ~ 10.

