def align_combine(fitsdir, myfilter, examine=True): from pyraf import iraf iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) os.chdir(fitsdir) listfiles = glob.glob(myfilter) listfiles.sort() if (examine): print "Opening ",listfiles[0]," to examine." iraf.imexamine(input=listfiles[0], \ logfile="coords.dat", \ keeplog="yes") with open("align.list",'w') as f: for i in listfiles: f.write(i+"\n") print "Aligning with reference:",listfiles[0] iraf.imalign( input = "@align.list", referenc= listfiles[0], coords = "coords.dat", output = "*****@*****.**") listfiles = glob.glob("a_"+myfilter) listfiles.sort() with open("comb.list",'w') as f: for i in listfiles: f.write(i+"\n") print "Combining" iraf.imcombine(input = "@comb.list",\ output = "out.fits",\ combine= "median")
def identify_objects(fnlist,skysiglist,fwhmlist,suffix=".coo"): """Runs the IRAF routine 'daofind' to locate objects in a series of images, creating coordinate files. Inputs: fnlist -> List of strings, each the path to a fits image. skysiglist -> List of floats, each the sky background sigma for an image. fwhmlist -> List of floats, each the FWHM of objects in an image. suffix -> Suffix for the coordinate files. '.coo' by default. Outputs: coolist -> List of strings, each the path to the coordinate files created. """ print "Identifying objects in images..." coolist = [] #Open IRAF packages iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) for i in range(len(fnlist)): coolist.append(fnlist[i]+suffix) iraf.daofind(image=fnlist[i], output=fnlist[i]+suffix, fwhmpsf=fwhmlist[i], sigma=skysiglist[i], threshold=4.0, datamin='INDEF', datamax='INDEF', verify='N') return coolist
def _get_photometry(self): """ Get the photometry for the target. If the target is a standard star, aperture photometry will be performed. For the moment nothing is done with the others, but in due time (TODO) photometry.py will be included here. """ basename = "standards" fd, coords_file = tempfile.mkstemp(prefix=basename, suffix=".coords") os.write(fd, "{0} {1} \n".format(self.RA, self.DEC)) os.close(fd) if self.objtype == "standard": iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) seeing = self.header.hdr[self.header.seeingk] photfile_name = self.header.im_name + ".mag.1" utilities.if_exists_remove(photfile_name) kwargs = dict(output=photfile_name, coords=coords_file, wcsin='world', fwhm=seeing, gain=self.header.gaink, exposure=self.header.exptimek, airmass=self.header.airmassk, annulus=6*seeing, dannulus=3*seeing, apert=2*seeing, verbose="no", verify="no", interac="no") iraf.phot(self.header.im_name, **kwargs) [counts] = iraf.module.txdump(photfile_name, 'FLUX', 'yes', Stdout=subprocess.PIPE) utilities.if_exists_remove(coords_file) return float(counts)
def getSkyMeanSDinAnnulus(ann,delta=5): iraf.noao() iraf.digiphot() iraf.apphot() iraf.photpars.setParam('apertures','1') iraf.phot.setParam('interactive','no') iraf.phot.setParam('image',fitsDir+fitsFile) iraf.phot.setParam('coords',fitsDir+fitsFile+".coo") outmag=".maglim" try: os.remove(fitsDir+fitsFile+outmag) except: print "File does not exist BEFORE running phot, so no need to delete." iraf.phot.setParam('output',fitsDir+fitsFile+outmag) iraf.phot.setParam('interac','no') iraf.fitskypars.setParam('annulus',str(ann)) iraf.fitskypars.setParam('dannulus',str(delta)) ## NO SIGMA CLIPPING! JUST TO BE SAFE: (6/2013) iraf.fitskypars.setParam('sloclip',"0") iraf.fitskypars.setParam('shiclip',"0") iraf.phot(fitsDir+fitsFile,Stdin=cr) aa, nn, xx, ss = ao.readPhotMagFile(fitsDir,fitsFile,outmag) try: os.remove(fitsDir+fitsFile+outmag) except: print "File not found to delete AFTER running phot; that's odd." return xx
def set_default(base): # Loading necessary IRAF packages iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) iraf.apphot(_doprint=0) iraf.reset(min_lenuserarea='200000') iraf.datapars.setParam('datamin','INDEF') iraf.datapars.setParam('datamax','60000') iraf.datapars.setParam('exposure','EXPTIME') iraf.datapars.setParam('airmass', 'AIRMASS') iraf.datapars.setParam('filter', 'FILTER') iraf.findpars.setParam('threshold', 3.0) iraf.findpars.setParam('sharphi', 1) iraf.findpars.setParam('roundhi', 1.3) iraf.findpars.setParam('roundlo', -1.3) iraf.daofind.setParam('verify','no') iraf.daofind.setParam('interactive','no') iraf.photpars.setParam('zmag',25.0) iraf.photpars.setParam('weighti','constant') iraf.photpars.setParam('apertur',3.0) iraf.phot.setParam('output',base+'default') iraf.phot.setParam('coords',base+'default') iraf.phot.setParam('verify','no') iraf.phot.setParam('interactive','no') iraf.fitpsf.setParam('box',10.0) iraf.fitpsf.setParam('verify','no') iraf.fitpsf.setParam('interactive','no') iraf.centerpars.setParam('calgori','none') iraf.fitskypars.setParam('salgorithm','mode') iraf.daopars.setParam('functio','moffat15') iraf.daopars.setParam('varorde','0') iraf.daopars.setParam('nclean','0') iraf.daopars.setParam('saturat','no') iraf.daopars.setParam('fitsky','yes') iraf.daopars.setParam('recenter','yes') iraf.daopars.setParam('groupsk','yes') iraf.daopars.setParam('maxnsta','40000') iraf.psf.setParam('photfile',base+'default') iraf.psf.setParam('pstfile',base+'default') iraf.psf.setParam('psfimage',base+'default') iraf.psf.setParam('opstfile',base+'default') iraf.psf.setParam('groupfil',base+'default') iraf.psf.setParam('interac','no') iraf.psf.setParam('matchby','yes') iraf.psf.setParam('verify','no') iraf.psf.setParam('showplo','no') iraf.allstar.setParam('verify','no')
def get_fake_centroid(filename,x,y,instrument,filt): """ Locate the center of a fake psf INPUTS: The fake-SN psf image in filename, the expected x,y position of the center of the psf, the instrument and filter being modeled. RETURNS: xcentroid, ycentroid, fluxcorr """ from pyraf import iraf iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) iraf.unlearn(iraf.apphot.phot) iraf.unlearn(iraf.datapars) iraf.unlearn(iraf.centerpars) #Use the centroid algorithm right now as it seems more robust to geometric distortion. iraf.centerpars.calgorithm = 'centroid' iraf.centerpars.cbox = 5.0 iraf.unlearn(iraf.fitskypars) iraf.unlearn(iraf.photpars) photparams = { 'interac':False, 'radplot':False, } iraf.datapars.readnoise = 0.0 iraf.datapars.itime = 1.0 iraf.datapars.epadu = 1.0 # iraf.digiphot.apphot.fitskypars : iraf.unlearn(iraf.fitskypars) iraf.fitskypars.salgorithm = 'constant' iraf.fitskypars.skyvalue = 0.0 # iraf.digiphot.apphot.photpars : iraf.unlearn(iraf.photpars) iraf.photpars.weighting = 'constant' iraf.photpars.apertures = 20 # TODO : set this more intelligently ! iraf.photpars.zmag = 25 iraf.photpars.mkapert = False #Write the coordinate file starting as position x and y coxyfile = 'centroid.xycoo' coxy = open(coxyfile, 'w') print >> coxy, "%10.2f %10.2f" % (x,y) coxy.close() if os.path.exists('centroid.mag'): os.remove('centroid.mag') iraf.phot(image=filename, skyfile='', coords=coxyfile, output='centroid.mag', verify=False, verbose=True, Stdout=1, **photparams) f = open('centroid.mag', 'r') maglines = f.readlines() f.close() xcentroid = float(maglines[76].split()[0]) ycentroid = float(maglines[76].split()[1]) return xcentroid,ycentroid
def set_Pairitel_params(): '''Set some daophot parameters, which are typical for my Pairitel images ''' iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) iraf.apphot(_doprint=0) iraf.datapars.fwhmpsf = 4.5 iraf.datapars.datamax=15000. iraf.centerpars.cbox = 9.0 iraf.centerpars.calgorithm = "none" iraf.datapars.datamin = "INDEF" iraf.datapars.ccdread="RDNOISE" # 30. in 2MASS? iraf.datapars.gain = "GAIN" iraf.datapars.exposure = "EXPTIME" iraf.datapars.airmass = "AIRMASS" iraf.datapars.filter = "FILTER" iraf.datapars.obstime="DATE-OBS"
def createMagFile(inputDir, inputFile, useCoordFile, outputFile, fullyAutomatic=False, aperturesString='5,10,15,20', bestSkyAperture='100'): cr=['\n'] iraf.cd(inputDir) iraf.noao() iraf.digiphot() iraf.apphot() iraf.phot.setParam('interactive','no') iraf.centerpars.setParam('cbox','5') iraf.photpars.setParam('apertures',aperturesString) iraf.fitskypars.setParam('annulus',bestSkyAperture) iraf.fitskypars.setParam('dannulus','10') iraf.phot.setParam('image',inputFile) iraf.phot.setParam('coords',useCoordFile) iraf.phot.setParam('output',outputFile) if fullyAutomatic: ## If you use this option, it will not prompt you for any of the aperture/sky settings iraf.phot(mode='h',Stdin=cr) else: # Otherwise, you can change them at will, and get to press enter a bunch iraf.phot(mode='h')
def init_iraf(): """Initializes the pyraf environment. """ # The display of graphics is not used, so skips Pyraf graphics # initialization and run in terminal-only mode to avoid warning messages. os.environ['PYRAF_NO_DISPLAY'] = '1' # Set PyRAF process caching off to avoid errors if spawning multiple # processes. iraf.prcacheOff() # Load iraf packages and does not show any output of the tasks. iraf.digiphot(_doprint = 0) iraf.apphot(_doprint = 0) iraf.images(_doprint = 0) # Set the iraf.phot routine to scripting mode. iraf.phot.interactive = "no" iraf.phot.verify = "no"
def init_iraf(): """Initializes the pyraf environment. """ # The display of graphics is not used, so skips Pyraf graphics # initialization and run in terminal-only mode to avoid warning messages. os.environ['PYRAF_NO_DISPLAY'] = '1' # Set PyRAF process caching off to avoid errors if spawning multiple # processes. iraf.prcacheOff() # Load iraf packages and does not show any output of the tasks. iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) iraf.images(_doprint=0) # Set the iraf.phot routine to scripting mode. iraf.phot.interactive = "no" iraf.phot.verify = "no"
def doPhotometryACS(self, file, inputcoords, apertures, zeropoint, bgsigma, skyann, skydann): ''' This function can be used to do photometry from a given image. Input parameters can be varied, however, the task assumes that photometry is done from an ACS image and the exposure time is found from the header of the file. Object recentering is done with centroid algorithm and shifts up to 6 pixels are possible. For skyfitting algorithm mode is adopted. ''' #load packages, should supress the output I.digiphot() I.apphot() #setting up for ACS I.datapar(sigma = bgsigma, exposure = 'exptime', gain = 'ccdgain') I.centerpars(calgorithm = 'centroid', cbox = 10., maxshift = 6.) I.fitskypars(salgorithm = 'mode', annulus = skyann, \ dannulus = skydann, skyvalue = 0.) I.photpars(apertures = apertures, zmag = zeropoint) I.phot(file, coords=inputcoords, verify='no', verbose = 'no')
def doPhotometryACS(self, file, inputcoords, apertures, zeropoint, bgsigma, skyann, skydann): ''' This function can be used to do photometry from a given image. Input parameters can be varied, however, the task assumes that photometry is done from an ACS image and the exposure time is found from the header of the file. Object recentering is done with centroid algorithm and shifts up to 6 pixels are possible. For skyfitting algorithm mode is adopted. ''' #load packages, should supress the output I.digiphot() I.apphot() #setting up for ACS I.datapar(sigma=bgsigma, exposure='exptime', gain='ccdgain') I.centerpars(calgorithm='centroid', cbox=10., maxshift=6.) I.fitskypars(salgorithm = 'mode', annulus = skyann, \ dannulus = skydann, skyvalue = 0.) I.photpars(apertures=apertures, zmag=zeropoint) I.phot(file, coords=inputcoords, verify='no', verbose='no')
def do_phot(fnlist,coofile,fwhmlist,skysiglist): """Runs the IRAF 'phot' routine on a series of images to determine their centroid positions and magnitudes. It is very likely you don't want to call this routine on its own, but rather have align_norm do it for you. Inputs: fnlist -> List of strings, each the path to a fits image coofile -> Path to the file containing the coordinate list of stars fwhmlist -> List of the PSF FWHM's for each image skysiglist -> List of the sky background sigmas for each image Outputs: photlist -> List of strings, the paths to the photometry outputs """ iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) photlist = [] for i in range(len(fnlist)): print "Performing photometry on the stars in image "+fnlist[i] photlist.append(fnlist[i]+".mag") iraf.phot(image=fnlist[i], skyfile="", coords=coofile, output=photlist[i], interactive="N", sigma=skysiglist[i], datamin='INDEF', datamax='INDEF', calgorithm='centroid', cbox=2*fwhmlist[i], salgorithm='centroid', annulus=4*fwhmlist[i], dannulus=6*fwhmlist[i], apertures=4*fwhmlist[i], verify='N') return photlist
def calcFluxMulti(imageName, coords, rMin, rMax, step=1, centerBox=5.0): nCoords = coords.shape[0] results = [] coordsFN = tempfile.NamedTemporaryFile(delete=False) for xx, yy in coords: print >>coordsFN, xx, yy coordsFN.close() digiphot = iraf.digiphot(_doprint=0) apphot = iraf.apphot(_doprint=0) qphot = iraf.qphot pdump = iraf.pdump qphot.coords = coordsFN.name #if (iraf.hedit(image=imageName,fields='EXPTIME',value='.')): #^this line prints the value of EXPTIME which is not needed #need to check for EXPTIME or qphot will raise an error try: pyfits.open(imageName)[0].header['EXPTIME'] qphot.exposure = 'EXPTIME' except: pass qphot.airmass = '' qphot.interactive = False qphot.radplots = False qphot.verbose = False apertures = ','.join([str(nn) for nn in range(int(rMin), int(rMax+1))]) annulus = rMax dannulus = rMax + 1 qphot.output = 'qPhotOutput.tmp' if os.path.exists(qphot.output): os.remove(qphot.output) qphot(imageName, centerBox, annulus, dannulus, apertures, Stdout=1) for nn in range(nCoords): areas = np.array(map(float, pdump(qphot.output, 'AREA', 'yes', Stdout=1)[nn].split())) fluxes = np.array(map(float, pdump(qphot.output, 'SUM', 'yes', Stdout=1)[nn].split())) newXX = np.array(map(float, pdump(qphot.output, 'XCENTER', 'yes', Stdout=1)[nn].split()))[0] newYY = np.array(map(float, pdump(qphot.output, 'YCENTER', 'yes', Stdout=1)[nn].split()))[0] results.append([nn, coords[nn][0], coords[nn][1], newXX, newYY, areas, fluxes]) #if os.path.exists(qphot.output): os.remove(qphot.output) return results
def get_star_data(asteroid_id, mag, expnum, header): """ From ossos psf fitted image, calculate mean of the flux of each row of the rotated PSF """ # calculate mean psf uri = storage.get_uri(expnum.strip('p'), header[_CCD].split('d')[1]) ossos_psf = '{}.psf.fits'.format(uri.strip('.fits')) local_psf = '{}{}.psf.fits'.format(expnum, header[_CCD].split('d')[1]) local_file_path = '{}/{}'.format(_STAMPS_DIR, local_psf) storage.copy(ossos_psf, local_file_path) # pvwcs = wcs.WCS(header) # x, y = pvwcs.sky2xy(asteroid_id['ra'].values, asteroid_id['dec'].values) x = asteroid_id[_XMID_HEADER].values[0] y = asteroid_id[_YMID_HEADER].values[0] # run seepsf on the mean psf image iraf.set(uparm="./") iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) iraf.daophot(_doprint=0) iraf.seepsf(local_file_path, local_psf, xpsf=x, ypsf=y, magnitude=mag) with fits.open(local_psf) as hdulist: data = hdulist[0].data th = math.degrees(asteroid_id[_THETA_HEADER].values[0]) data_rot = rotate(data, th) data_rot = np.ma.masked_where(data_rot == 0, data_rot) data_mean = np.ma.mean(data_rot, axis=1) os.unlink(local_psf) os.unlink(local_file_path) return data_mean[np.nonzero(np.ma.fix_invalid(data_mean, fill_value=0))[0]]
def measurephot(xpos, ypos, image): coords = open("noisecoords.dat", 'w') for i in range(len(xpos)): coords.write("%8.1f %8.1f \n" % (xpos[i], ypos[i])) sky = open("sky", 'w') for i in range(npoints): sky.write("0.0 \n") sky.close() aps = open("apertures", 'w') aps.write("1,2,3,4,5,6,7,8,9,10,11,12,13,14,15") aps.close() os.system("rm noise.dat") iraf.digiphot() iraf.daophot() print image image = "j-sky.fits" image = "n-sky.fits" iraf.digiphot.daophot.phot(image, coords="noisecoords.dat", output="noise.dat", skyfile="sky", salgori="file", aperture="apertures", interactive="no", wcsin='logical') iraf.apphot() iraf.phot('check.fits', coords='noisecoords.dat', output='noise.dat', skyfile='sky', salgori='file', aperture='apertures', interactive='no') test = raw_input("enter anything")
def calcFlux(imageName, xx, yy, rr): coordsFN = tempfile.NamedTemporaryFile(delete=False) print >>coordsFN, xx, yy coordsFN.close() digiphot = iraf.digiphot(_doprint=0) apphot = iraf.apphot(_doprint=0) qphot = iraf.qphot pdump = iraf.pdump qphot.coords = coordsFN.name #if (iraf.hedit(image=imageName,fields='EXPTIME',value='.')): #^this line prints the value of EXPTIME which is not needed #need to check for EXPTIME or qphot will raise an error try: pyfits.open(imageName)[0].header['EXPTIME'] qphot.exposure = 'EXPTIME' except: pass qphot.airmass = '' qphot.interactive = False qphot.radplots = False qphot.verbose = False annulus = rr dannulus = 1.0 qphot.output = 'qPhotOutput.tmp' if os.path.exists(qphot.output): os.remove(qphot.output) qphot(imageName, 1.0, annulus, dannulus, annulus, Stdout=1) areas = np.array(map(float, pdump(qphot.output, 'AREA', 'yes', Stdout=1)[0].split())) fluxes = np.array(map(float, pdump(qphot.output, 'SUM', 'yes', Stdout=1)[0].split())) if os.path.exists(qphot.output): os.remove(qphot.output) return [np.float64(areas[0]), np.float64(fluxes[0])]
def skySub(imageName): #Set up iraf packages and variables for filenames skyIm = imageName+'.sky' outfile = 'mSky'+imageName+'.file' avgValLog = 'mSkyLog'+imageName+'.file' stDevLog = 'stDevLog'+imageName+'.file' outputIm = imageName+'.sky' iraf.apphot(_doprint=0) #Delete exisiting files for f in (skyIm,outfile,'mSky.file',outputIm+'.fits',avgValLog): silentDelete(f) #Configure fitsky parameters iraf.fitsky.coords="coordslist" iraf.fitsky.interactive='no' iraf.fitsky.verify='no' iraf.datapars.datamax=150000 iraf.datapars.datamin=-200 iraf.fitskypars.salgorithm="mode" iraf.fitskypars.annulus=0 iraf.fitskypars.dannulus=40 iraf.fitskypars.shireject=2 iraf.fitskypars.sloreject=2 iraf.fitsky.verbose='no' #Call fitsky to compute the mean of the sky iraf.fitsky(imageName,output=outfile) #Use 'PDUMP" to extract only the mean sky values sys.stdout=open(avgValLog,"w") iraf.pdump(outfile,'mSky',"yes") sys.stdout=sys.__stdout__ #Use pdump to get the standard deviation from the sky sys.stdout=open(stDevLog,"w") iraf.pdump(outfile,'stDev',"yes") sys.stdout=sys.__stdout__ #Read in the mSky values file skyValues = open(avgValLog,'r') valueList = skyValues.readlines() #convert the value array to a numpy array valueListNp = np.asfarray(valueList) #compute average average = np.mean(valueListNp) #Print output print(str(average)+" is the average") #Read in the stDev log, take the average stDevs = open(stDevLog,'r') stDevList = stDevs.readlines() stDevListNp = np.asfarray(stDevList) stDevAvg = np.mean(stDevListNp) #Configure imarith iraf.imarith.pixtype="real" iraf.imarith.calctype="real" #Use imarith to subtract the average sky value from the image iraf.imarith(imageName,"-",average,outputIm) #Add the average sky value and stDev to the image header iraf.hedit(outputIm,"SKY",average,add='yes',verify='no') iraf.hedit(outputIm,"SKYSIGMA",stDevAvg,add='yes',verify='no') #Print an output image print("Subtracted "+ str(average)+ " to create a new image called "+outputIm) #Delete acillary output files silentDelete(outfile)
def escut(image, pos_file, fwhm, peak): # input image file name, file name with matched source positions, **np.array of fwhm measurements for each source import numpy as np import matplotlib.pyplot as plt from scipy import stats from pyraf import iraf # import sewpy import os from matplotlib.path import Path iraf.images(_doprint=0) iraf.tv(_doprint=0) iraf.ptools(_doprint=0) iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.photcal(_doprint=0) iraf.apphot(_doprint=0) iraf.imutil(_doprint=0) iraf.unlearn(iraf.phot, iraf.datapars, iraf.photpars, iraf.centerpars, iraf.fitskypars) iraf.apphot.phot.setParam('interactive', "no") iraf.apphot.phot.setParam('verify', "no") iraf.datapars.setParam('datamax', 50000.) iraf.datapars.setParam('gain', "gain") iraf.datapars.setParam('ccdread', "rdnoise") iraf.datapars.setParam('exposure', "exptime") iraf.datapars.setParam('airmass', "airmass") iraf.datapars.setParam('filter', "filter") iraf.datapars.setParam('obstime', "time-obs") # iraf.datapars.setParam('obstime',"date-obs") iraf.datapars.setParam('sigma', "INDEF") iraf.photpars.setParam('zmag', 0.) iraf.centerpars.setParam('cbox', 9.) iraf.centerpars.setParam('maxshift', 3.) iraf.fitskypars.setParam('salgorithm', "median") iraf.fitskypars.setParam('dannulus', 10.) # clean up the indefs so we can actually do stats, but reassign them to 99999 so we don't lose track of things # keep a separate list without them to do the median (we need floats) indefs = np.where(fwhm == 'INDEF') good = np.where(fwhm != 'INDEF') fwhm[indefs] = 99.999 fwhm = fwhm.astype(float) fwhm_good = fwhm[good].astype(float) indefs = np.where(peak == 'INDEF') peak[indefs] = -999.999 peak = peak.astype(float) peak_good = peak[good].astype(float) if not os.path.isfile(image[0:-5] + '.txdump'): # findavgfwhm = sewpy.SEW( # params = ["X_IMAGE", "Y_IMAGE", "FWHM_IMAGE", "FLAGS"], # config = {"DETECT_THRESH":200.0}, # sexpath = "sex" # ) # # out = findavgfwhm(image)["table"] # # fwhms = out['FWHM_IMAGE'] # This is an astropy table. # flags = out['FLAGS'] # get a really rough estimate of the stellar FWHM in the image to set apertures # use the input fwhm measurement # ap1x = fwhm_est # xpos = datatable['X_IMAGE'] # ypos = datatable['Y_IMAGE'] # fwhm = datatable['FWHM_IMAGE'] # flags = datatable['FLAGS'] # idno = datatable['NUMBER'] ap1x = np.median( fwhm_good ) # only use isolated detections of stars, this is the 1x aperture # print ap1x ap2x = 2.0 * ap1x # these = [ i for i,id in enumerate(idno) if (flags[i] == 0)] # with open(image[0:-5]+'.escut.pos','w+') as f: # for j in range(len(xpos)): # print >> f, xpos[j], ypos[j], fwhm[j], idno[j] iraf.datapars.setParam('fwhmpsf', ap1x) iraf.photpars.setParam('apertures', repr(ap1x) + ', ' + repr(ap2x)) iraf.fitskypars.setParam('annulus', 4. * ap1x) iraf.apphot.phot(image=image, coords=pos_file, output=image[0:-5] + '.phot') with open(image[0:-5] + '.txdump', 'w+') as txdump_out: iraf.ptools.txdump( textfiles=image[0:-5] + '.phot', fields= "id,mag,merr,msky,stdev,rapert,xcen,ycen,ifilter,xairmass,image", expr= 'MAG[1] != INDEF && MERR[1] != INDEF && MAG[2] != INDEF && MERR[2] != INDEF', headers='no', Stdout=txdump_out) mag1x, mag2x = np.loadtxt(image[0:-5] + '.txdump', usecols=(1, 2), unpack=True) iraf_id = np.loadtxt(image[0:-5] + '.txdump', usecols=(0, ), dtype=int, unpack=True) # idno = np.loadtxt(image[0:-5]+'.escut.pos', usecols=(3,), dtype=int, unpack=True) xpos, ypos = np.loadtxt(pos_file, usecols=(0, 1), unpack=True) keepIndex = iraf_id - 1 xpos, ypos, fwhm, peak = xpos[keepIndex], ypos[keepIndex], fwhm[ keepIndex], peak[keepIndex] # print idno.size, iraf_id.size, xpos.size diff = mag2x - mag1x diffCut = diff magCut = mag2x xCut = xpos #[good] yCut = ypos #[good] idCut = iraf_id fwhmCut = fwhm #_good peakCut = peak print(peakCut.size, magCut.size, diffCut.size) print(diffCut.size, 0, np.median(diffCut), diffCut.std()) nRemoved = 1 # plt.clf() # plt.scatter(peakCut, magCut, edgecolor='none') # plt.savefig('peaktest.pdf') plt.clf() # plt.hlines(bin_edges, -2, 1, colors='red', linestyle='dashed') plt.scatter(diff, mag2x, edgecolor='none', facecolor='black', s=4) # plt.scatter(diffCut, magCut, edgecolor='none', facecolor='blue', s=4) magdiff = list( zip(magCut.tolist(), diffCut.tolist(), peakCut.tolist(), idCut.tolist())) dtype = [('mag', float), ('diff', float), ('peak', float), ('id', int)] magdiff = np.array(magdiff, dtype=dtype) magSort = np.sort(magdiff, order='peak') peakRange = (magSort['peak'] > 20000.0) & (magSort['peak'] < 40000.0) peakVal = np.median((magSort['diff'])[np.where(peakRange)]) # peakVal = np.median(diffCut) print(peakVal) plt.scatter((magSort['diff'])[np.where(peakRange)], (magSort['mag'])[np.where(peakRange)], edgecolor='none', facecolor='blue', s=4) while nRemoved != 0: nBefore = diffCut.size diffCheck = np.where( abs(peakVal - diffCut) < 2.0 * diffCut.std()) #[i for i,d in enumerate(diff) if (-0.5 < d < 0.0)] # diffCut = diffCut[diffCheck] nRemoved = nBefore - diffCut.size magCut = magCut[diffCheck] xCut = xCut[diffCheck] yCut = yCut[diffCheck] idCut = idCut[diffCheck] fwhmCut = fwhmCut[diffCheck] print(diffCut.size, nRemoved, np.median(diffCut), diffCut.std()) if 0.05 < diffCut.std() < 0.06: nRemoved = 0 # plt.fill_betweenx(bin_centers, bin_meds+3.0*bin_stds, bin_meds-3.0*bin_stds, facecolor='red', edgecolor='none', alpha=0.4, label='2x RMS sigma clipping region') # with open('escutSTD_i.pos','w+') as f: # for i,blah in enumerate(xCut): # print >> f, xCut[i], yCut[i], diffCut[i] bin_meds, bin_edges, binnumber = stats.binned_statistic(magCut, diffCut, statistic='median', bins=24, range=(-12, 0)) bin_stds, bin_edges, binnumber = stats.binned_statistic(magCut, diffCut, statistic=np.std, bins=24, range=(-12, 0)) bin_width = (bin_edges[1] - bin_edges[0]) bin_centers = bin_edges[1:] - bin_width / 2 # print bin_meds, bin_stds bin_hw = np.zeros_like(bin_stds) for i, bin_std in enumerate(bin_stds): if bin_std > 0.025: bin_hw[i] = 3.0 * bin_std else: bin_hw[i] = 0.075 # print len(binnumber) # for i,bin_hwi in enumerate(bin_hw): left_edge = np.array(list(zip(peakVal - bin_hw, bin_centers))) right_edge = np.flipud(np.array(list(zip(peakVal + bin_hw, bin_centers)))) # print left_edge, right_edge verts = np.vstack((left_edge, right_edge)) # print verts # verts = np.delete(verts, np.array([0,1,2,22,23,24,25,45,46,47]), axis=0) # DON'T USE A PATH BECAUSE APPARENTLY IT CAN SELECT THE INVERSE SET!! WTF # print verts esRegion = Path(verts) sources = esRegion.contains_points(list(zip(diff, mag2x))) # print sources with open('escutREG_i.pos', 'w+') as f: for i, blah in enumerate(xpos[sources]): print((xpos[sources])[i], (ypos[sources])[i], (diff[sources])[i], file=f) magCut2 = mag2x[sources] magCut1 = mag1x[sources] fwhmCut = fwhm[sources] xCut = xpos[sources] yCut = ypos[sources] diffCut = diff[sources] # find the sources that are in the std method but not the region method # print idCut, idno[sources] # extrasSTD = np.setdiff1d(idno[sources], idCut) # print extrasSTD.size # print extrasSTD # with open('escutUNIQUE.pos','w+') as f: # for i,blah in enumerate(extrasSTD): # print >> f, xpos[blah-1], ypos[blah-1] # fwhmcheck = np.loadtxt('testfwhmREG.log', usecols=(10,), unpack=True) fwhmchk2 = np.where((magCut2 < -4) & (fwhmCut < 90.0)) print(np.median(fwhmCut[fwhmchk2]), np.std(fwhmCut[fwhmchk2])) fwchk = np.where( np.abs(fwhmCut - np.median(fwhmCut[fwhmchk2])) > 10.0 * np.std(fwhmCut[fwhmchk2])) drop = np.abs(fwhmCut - np.median(fwhmCut[fwhmchk2])) > 10.0 * np.std( fwhmCut[fwhmchk2]) keep = np.abs(fwhmCut - np.median(fwhmCut[fwhmchk2])) <= 10.0 * np.std( fwhmCut[fwhmchk2]) with open('escutVBAD_i.pos', 'w+') as f: for i, blah in enumerate(xCut[fwchk]): print((xCut[fwchk])[i], (yCut[fwchk])[i], file=f) with open('escut_i.pos', 'w+') as f: for i, blah in enumerate(xCut): if not drop[i]: print(xCut[i], yCut[i], magCut2[i], fwhmCut[i], magCut1[i], file=f) with open('escut_g.pos', 'w+') as f: for i, blah in enumerate(xCut): if not drop[i]: print(xCut[i], yCut[i], magCut2[i], fwhmCut[i], magCut1[i], file=f) plt.fill_betweenx(bin_centers, peakVal + bin_hw, peakVal - bin_hw, facecolor='red', edgecolor='none', alpha=0.4, label='2x RMS sigma clipping region') plt.scatter(diffCut[fwchk], magCut2[fwchk], edgecolor='none', facecolor='red', s=4) plt.ylim(0, -12) plt.xlabel('$m_{2x} - m_{1x}$') plt.ylabel('$m_{2x}$') plt.xlim(-2, 1) plt.savefig('testmagiraf.pdf') plt.clf() plt.scatter(magCut2, fwhmCut, edgecolor='none', facecolor='black') plt.scatter(magCut2[fwchk], fwhmCut[fwchk], edgecolor='none', facecolor='red') plt.hlines([np.median(fwhmCut)], -12, 0, colors='red', linestyle='dashed') plt.hlines([ np.median(fwhmCut) + fwhmCut.std(), np.median(fwhmCut) - fwhmCut.std() ], -12, 0, colors='red', linestyle='dotted') plt.ylim(0, 20) plt.xlim(-12, 0) plt.ylabel('fwhm') plt.xlabel('$m_{2x}$') plt.savefig('fwhmcheck.pdf') return fwhmCut[keep]
def get_app_phot(coords, image, plot_only=False, store=True, wcsin="world", fwhm=2, plotdir=".", box=15): ''' coords: files: wcsin: can be "world", "logic" ''' # Load packages; splot is in the onedspec package, which is in noao. # The special keyword _doprint=0 turns off displaying the tasks # when loading a package. if (not plot_only): iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) iraf.unlearn("apphot") imdir = os.path.dirname(image) imname = os.path.basename(image) plotdir = os.path.join(imdir, "photometry") if not os.path.isdir(plotdir): os.makedirs(plotdir) out_name = os.path.join(plotdir, imname + ".seq.mag") clean_name = os.path.join(plotdir, imname + ".app.mag") # Read values from .ec file ecfile= image+".ec" filter_value=''.join(ecfile).split('.',1)[0] fwhm_value = fwhm if (fitsutils.has_par(image, 'FWHM')): fwhm_value = fitsutils.get_par(image, 'FWHM') if (fitsutils.has_par(image, 'AIRMASS')): airmass_value = fitsutils.get_par(image, 'AIRMASS') else: airmass_value = 1.3 exptime = fitsutils.get_par(image, 'EXPTIME') gain = fitsutils.get_par(image, 'GAIN') try: with open(''.join(ecfile),'r') as f: for line in f: if "airmass" in line: airmass_value = line.split('=',1)[1] else: airmass_value = 1 if "FWHM" in line: print line fwhm_value = line.split('FWHM=',1)[1] fwhm_value = fwhm_value.rsplit("aperture")[0] except: pass print "FWHM", fwhm_value aperture_rad = math.ceil(float(fwhm_value)*2) # Set aperture radius to three times the PSF radius sky_rad= math.ceil(aperture_rad)*5 print aperture_rad, sky_rad if (not plot_only): if os.path.isfile(out_name): os.remove(out_name) if os.path.isfile(clean_name): os.remove(clean_name) # Check if files in list, otherwise exit if not ecfile: print "No .ec files in directory, exiting" sys.exit() iraf.noao.digiphot.apphot.qphot(image = image,\ cbox = box ,\ annulus = sky_rad ,\ dannulus = 15. ,\ aperture = str(aperture_rad),\ coords = coords ,\ output = out_name ,\ plotfile = "" ,\ zmag = 0. ,\ exposure = "exptime" ,\ airmass = "airmass" ,\ filter = "filters" ,\ obstime = "DATE" ,\ epadu = gain ,\ interactive = "no" ,\ radplots = "yes" ,\ verbose = "no" ,\ graphics = "stdgraph" ,\ display = "stdimage" ,\ icommands = "" ,\ wcsin = wcsin, wcsout = "logical", gcommands = "") #iraf.noao.digiphot.apphot.phot(image=image, cbox=5., annulus=12.4, dannulus=10., salgori = "centroid", aperture=9.3,wcsin="world",wcsout="tv", interac = "no", coords=coords, output=out_name) iraf.txdump(out_name, "id,image,xcenter,ycenter,xshift,yshift,fwhm,msky,stdev,mag,merr", "yes", Stdout=clean_name) ma = np.genfromtxt(clean_name, comments="#", dtype=[("id","<f4"), ("image","|S20"), ("X","<f4"), ("Y","<f4"), ("Xshift","<f4"), ("Yshift","<f4"),("fwhm","<f4"), ("ph_mag","<f4"), ("stdev","<f4"), ("fit_mag","<f4"), ("fiterr","<f4")]) if (ma.size > 0): m = ma[~np.isnan(ma["fit_mag"])] else: print "Only one object found!" m = np.array([ma]) hdulist = pf.open(image) prihdr = hdulist[0].header img = hdulist[0].data * 1. nx, ny = img.shape dimX = int(4) dimY = int(np.ceil(len(m)*1./4)) outerrad = sky_rad+10 cutrad = outerrad + 15 plt.suptitle("FWHM="+str(fwhm_value)) k = 0 for i in np.arange(dimX): for j in np.arange(dimY): if ( k < len(m)): ax = plt.subplot2grid((dimX,dimY),(i, j)) y1, y2, x1, x2 = m[k]["X"]-cutrad, m[k]["X"]+cutrad, m[k]["Y"]-cutrad, m[k]["Y"]+cutrad y1, y2, x1, x2 = int(y1), int(y2), int(x1), int(x2) try: zmin, zmax = zscale.zscale(img[x1:x2,y1:y2], nsamples=1000, contrast=0.25) except: sh= img[x1:x2,y1:y2].shape if sh[0]>0 and sh[1]>0: zmin = np.nanmin(img[x1:x2,y1:y2]) zmax = np.nanmax(img[x1:x2,y1:y2]) continue else: continue ax.imshow(img[x1:x2,y1:y2], aspect="equal", extent=(-cutrad, cutrad, -cutrad, cutrad), origin="lower", cmap=matplotlib.cm.gray_r, interpolation="none", vmin=zmin, vmax=zmax) c1 = plt.Circle( (0, 0), edgecolor="r", facecolor="none", radius=5.) c2 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=sky_rad) c3 = plt.Circle( (0, 0), edgecolor="yellow", facecolor="none", radius=sky_rad+10) plt.gca().add_artist(c1) plt.gca().add_artist(c2) plt.gca().add_artist(c3) ax.set_xticks([]) ax.set_yticks([]) plt.text(+5, +5, "%d"%m[k]["id"]) plt.text(-cutrad, -cutrad, "%.2f$\pm$%.2f"%(m[k]["fit_mag"], m[k]["fiterr"]), color="b") k = k+1 plt.savefig(os.path.join(plotdir, imname + "plot.png")) plt.clf()
if list_images["type"][index] in ["cig","standards","clusters"]: find_sky.main(arguments=[list_images["filename"][index]]) print "Detecting objects for images of CIG(s), standard(s) and cluster(s)" # This part follows the example in the webpage: #http://www.lancesimms.com/programs/Python/pyraf/daofind.py for index, image in enumerate(list_images["filename"]): if list_images["type"][index] in ["cig","standards","clusters"]: outfile = utilities.replace_extension(image, ".cat") if os.path.isfile(outfile): os.remove(outfile) # Prepare and run daofind hdr = fits.getheader(image) iraf.noao(_doprint=0) # Load noao iraf.digiphot(_doprint=0) # Load digiphot iraf.apphot(_doprint=0) # Load apphot iraf.daofind.setParam('image',image) FWHM = min(max_FWHM,float(hdr["LEMON FWHM"])) iraf.daofind.setParam('fwhmpsf', FWHM) iraf.daofind.setParam('output', outfile) iraf.daofind.setParam('sigma', float(hdr["SKY_STD"])) iraf.daofind.setParam('gain', gaink) iraf.daofind.setParam('readnoise', float(hdr[read_noisek])) iraf.daofind.setParam('roundlo', -0.3) # Minimal roundness (0 is round) iraf.daofind.setParam('roundhi', 0.3 ) iraf.daofind.setParam('airmass', "airmass") iraf.daofind.setParam('filter', filterk) iraf.daofind.setParam('exposure', exptimek) iraf.daofind.setParam('obstime', "date-obs") iraf.daofind.setParam('verify', "no") iraf.daofind.setParam('datamin', 500)
def run(imfile, coordlist, WCS=False, outroot=None, poisson=True, verbose=True, debug=False, abmags=False, decliner=False, smallskyann=False, calgorithm='gauss', upperlim=None, snanadat=False, cbox=5.0): """ Use iraf.digiphot.apphot to collect aperture photometry in Vega magnitudes. Required Arguments: imfile (str): fits image file name (cannot be fpacked) coord : the coordinate list for photometry as a str, this is the name of a text file in two columns giving x,y coordinates. as a python list or numpy array, this gives coordinate pairs for each target e.g. coord=[ [1024,440], [502,680] ] Optional arguments: WCS : If input coordinates are in RA and DEC (degrees), user must set WCS=True. outroot : root of the output file names (outroot.mag, outroot.full, outroot.out) if unspecified, the root of imfile is used. poisson: Use a poisson noise model or not. If set to false, a constant noise model is used. Default is True calgorithm: Centering algorithm to pass to iraf.apphot. Choices are "gauss","centroid", "none", or "ofilter". Default is "gauss" cbox: Size of centering box to pass to iraf.apphot. Default is 5.0 upperlim: None = when flux<3-sigma then report 3-sig upper limit True = force computation of 3-sigma upper limit False = disallow upper limits (i.e. always report measured flux) snanadat: True = Report the mjd, flux and mag in a SNANA-style OBS: line False = report the filename, mjd, source position, mag and errors. verbose: Default is True. debug: Start pdb. Default is False. Output products : phot.out, phot.mag, phot.full phot.out : the raw output from apphot phot.full : a detailed photometry file phot.mag : header+one line summary output file : #image name, filter, xpos, ypos, magnitude, and errors Requires : numpy, pyfits (astLib is required if WCS=True) """ if debug: import pdb pdb.set_trace() from math import sqrt from numpy import nan, log10 from pyraf import iraf iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) if outroot == None: outroot = os.path.splitext(os.path.basename(imfile))[0] # Get info from header hdr = pyfits.getheader(imfile) exptime = hdr['EXPTIME'] if 'EXPSTART' in hdr: mjdobs = "%.1f" % hdr['EXPSTART'] else: mjdobs = '0.0' if 'DATE-OBS' in hdr: dateobs = hdr['DATE-OBS'] elif 'DATE' in hdr: dateobs = hdr['DATE'] else: dateobs = 'unknown' if 'D001OUUN' in hdr: if hdr['D001OUUN'].upper() == 'CPS': gain = exptime exptime = 1.0 elif hdr['D001OUUN'].upper() == 'COUNTS': gain = 1.0 elif hdr['BUNIT'].upper() == 'ELECTRONS/S': gain = exptime exptime = 1.0 elif hdr['BUNIT'].upper() == 'ELECTRONS': gain = 1.0 else: print 'Problem determining units, check image' return if 'FILTER' in hdr: filt = hdr['FILTER'] elif 'FILTER1' in hdr: filt = hdr['FILTER1'] if filt.startswith('CLEAR'): filt = hdr['FILTER2'] instrument = hdr['INSTRUME'] if instrument == 'WFC3': instrument = instrument + '_' + hdr['DETECTOR'] if abmags: if instrument == 'WFC3_IR': ZPT = ZPT_WFC3_IR_AB[filt] elif instrument == 'WFC3_UVIS': ZPT = ZPT_WFC3_UVIS_AB[filt] elif instrument == 'ACS': ZPT = ZPT_ACS_AB[filt] else: print "Can't handle instrument: %s; check the fits header." % instrument #Give up and die return else: if instrument == 'WFC3_IR': ZPT = ZPT_WFC3_IR_VEGA[filt] elif instrument == 'WFC3_UVIS': ZPT = ZPT_WFC3_UVIS_VEGA[filt] elif instrument == 'ACS': ZPT = ZPT_ACS_VEGA[filt] else: print "Can't handle instrument: %s; check the fits header." % instrument #Give up and die return # format for coord list: # coordlist= [ [x0,y0], [x1,y1], ... ] # or a two-column text file if type(coordlist) == str: # user provided name of a coordinate file fin = open(coordlist, 'r') coordlines = fin.readlines() fin.close() coordvals = array([cline.split() for cline in coordlines], dtype=float) elif len(shape(coordlist)) == 1: # user provided something like coord=[x,y] coordvals = array([coordlist]) else: # user provided something like coord= [[x1,y1],[x2,y2],[x3,y3]] coordvals = array(coordlist) # how many objects do we have ? numcoo = len(coordvals) if WCS: # If coords in wcs instead of x,y, # get wcs information from imfile for converting to xy. from astLib import astWCS wcsfits = astWCS.WCS(imfile) # (re)write a list of x,y positions coxyfile = '%s.xycoo' % outroot coxy = open(coxyfile, 'w') for coord in coordvals: if WCS: #Convert from RA and Dec to xy # NOTE: wcsfits returns values based on a 0,0 origin, but the iraf phot # packages expect a 1,1 origin. So we add 1 to each value xy = wcsfits.wcs2pix(coord[0], coord[1]) xy[0] += 1 xy[1] += 1 else: xy = coord print >> coxy, "%10.2f %10.2f" % (float(xy[0]), float(xy[1])) coxy.close() if verbose > 1: print("XY coords written to %s" % coxyfile) """ iraf.digiphot.apphot.datapars : 2013.09.06 SR: updated to use Poisson noise model (which includes sky noise) as the default, instead of 'constant' """ iraf.unlearn(iraf.apphot.phot) iraf.unlearn(iraf.datapars) iraf.datapars.scale = 1.0 iraf.datapars.fwhmpsf = psffwhm[instrument] iraf.datapars.emission = not decliner iraf.datapars.sigma = 'INDEF' iraf.datapars.datamin = 'INDEF' iraf.datapars.datamax = 'INDEF' if poisson: iraf.datapars.noise = 'poisson' else: iraf.datapars.noise = 'constant' iraf.datapars.ccdread = '' #iraf.datapars.gain = '' iraf.datapars.readnoise = 0.0 #iraf.datapars.exposure = ' ' #iraf.datapars.airmass = '' #iraf.datapars.obstime = '' iraf.datapars.itime = exptime iraf.datapars.epadu = gain iraf.datapars.xairmass = 'INDEF' iraf.datapars.ifilter = 'INDEF' iraf.datapars.otime = 'INDEF' # iraf.digiphot.apphot.centerpars : iraf.unlearn(iraf.centerpars) iraf.centerpars.calgorithm = calgorithm iraf.centerpars.cbox = cbox iraf.centerpars.cthreshold = 0.0 iraf.centerpars.minsnratio = 1.0 iraf.centerpars.cmaxiter = 10.0 iraf.centerpars.maxshift = 1.0 iraf.centerpars.clean = False iraf.centerpars.rclean = 1.0 iraf.centerpars.rclip = 2.0 iraf.centerpars.kclean = 3.0 iraf.centerpars.mkcenter = False # iraf.digiphot.apphot.fitskypars : iraf.unlearn(iraf.fitskypars) iraf.fitskypars.salgorithm = 'median' if smallskyann: iraf.fitskypars.annulus = 8.0 iraf.fitskypars.dannulus = 12.0 else: iraf.fitskypars.annulus = 25.0 iraf.fitskypars.dannulus = 40.0 iraf.fitskypars.skyvalue = 0.0 iraf.fitskypars.smaxiter = 10.0 iraf.fitskypars.sloclip = 0.0 iraf.fitskypars.shiclip = 0.0 iraf.fitskypars.snreject = 50.0 iraf.fitskypars.sloreject = 3.0 iraf.fitskypars.shireject = 3.0 iraf.fitskypars.khist = 3.0 iraf.fitskypars.binsize = 0.1 iraf.fitskypars.smooth = False iraf.fitskypars.rgrow = 0.0 iraf.fitskypars.mksky = False # iraf.digiphot.apphot.photpars : iraf.unlearn(iraf.photpars) iraf.photpars.weighting = 'constant' iraf.photpars.apertures = APLIST iraf.photpars.zmag = ZPT iraf.photpars.mkapert = False photparams = { 'interac': False, 'radplot': False, } magfile_out = outroot + '.out' magfile_full = outroot + '.full' magfile_phot = outroot + '.mag' if os.path.exists(magfile_out): os.remove(magfile_out) # run photometry using the newly created coxyfile for providing input coordinates try: iraf.phot(image=imfile, skyfile='', coords=coxyfile, output=magfile_out, verify=False, verbose=True, Stdout=1, **photparams) except iraf.IrafError, e: print("phot failed on %s with IRAF error :\n%s" % (imfile, e))
from types import * from numpy import matrix from numpy import linalg from iqpkg import * import p60photutils # Necessary packages iraf.images() iraf.immatch() iraf.imfilter() iraf.noao() iraf.imred() iraf.ccdred() iraf.digiphot() iraf.apphot() yes = iraf.yes no = iraf.no INDEF = iraf.INDEF hedit = iraf.hedit imgets = iraf.imgets imcombine = iraf.imcombine pyrafdir = "python/pyraf/" pyrafdir_key = 'PYRAFPARS' if os.environ.has_key(pyrafdir_key): pardir = os.environ[pyrafdir_key] else: pardir = os.environ['HOME'] + '/' + pyrafdir
def runApperturePhotometryOnObject(self, photObject, write=False): # Aperture sizes #fap = 1.0 #fdan = 2.0 #fan = 3.0 # To take photometry of a given object using DAOPHOT/APPHOT.PHOT # Check we have an object list coordfilename = self._Name.replace(".fits", "objectID_%s.coo" % photObject._id) coordfilename2 = self._Name.replace(".fits", "_apphot_%s.reg" % photObject._Name.replace(" ","")) try: regionOut = open(coordfilename2, "w") regionOut.write("image; circle(%s, %s, %s) # color = red\n"% (photObject._pixelx, photObject._pixely, photObject._fap)) regionOut.write("image; circle(%s, %s, %s) # color = blue\n" % (photObject._pixelx, photObject._pixely, photObject._fdan)) regionOut.write("image; circle(%s, %s, %s) # color = green\n" % (photObject._pixelx, photObject._pixely, photObject._fan)) regionOut.close() except: print "ERROR opening region file to write" print sys.exc_info()[0] try: objectList = open(coordfilename,"r") objectList.close() except: objectList = open(coordfilename,"w") objectList.write("%s %s" % (photObject._pixelx, photObject._pixely)) objectList.close() # Load as in IRAF and neglect output: # NOAO # DIGIPHOT # APPHOT # DAOFIND iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) # DAOPHOT/APPHOT - PHOT Package # Parameters: # # image # skyfile??? # coords - input file # output - output file # datapars # centerpars # fitskypars # photpars cwd = os.getcwd() # datapars: datapars = "%s/uparm/datapars.par" % cwd #datapars = "uparm/datapars.par" #iraf.datapars.saveParList(filename=datapars) # centerpars: centerpars = "%s/uparm/centerpars.par" % cwd #centerpars = "uparm/centerpars.par" # iraf.centerpars.saveParList(filename=centerpars) # fitskypars: fitskypars = "%s/uparm/fitskypars.par" % cwd #fitskypars = "uparm/fitskypars.par" # iraf.fitskypars.saveParList(filename=fitskypars) # photpars: photpars = "%s/uparm/photpars.par" % cwd #photpars = "uparm/photpars.par" # iraf.photpar.saveParList(filename=photpars) #iraf.datapar(sigma=1.5, exposure='IMGEXP', gain='GAIN', ccdread='RON') # standard #iraf.phot.setParam('image', self._Name) #iraf.phot.setParam("output", self._Name.replace(".fits", "_phot.mag")) #iraf.phot.setParam("datapar", datapars) #iraf.phot.setParam("centerp", centerpars) #iraf.phot.setParam("fitskyp", fitskypars) #iraf.phot.setParam("photpar", photpars) #iraf.phot.setParam("interac", "no") #iraf.phot.setParam("verify", "yes") # co-ordinate file iraf.phot.setParam("coords", coordfilename) # run phot apphotpar = "%s/uparm/phot.par" % cwd # iraf.phot.saveParList(filename=apphotpar) output = self._Name.replace(".fits", "_objectID%s_phot.mag" % photObject._id) try: self.cleanOutputFiles(output) except: print "Error cleaning failed" print sys.exc_info()[0] #apsizes= (.4*fap*self._MEDFWHM,.5*fap*self._MEDFWHM,.6*fap*self._MEDFWHM,.8*fap*self._MEDFWHM, # 1.*fap*self._MEDFWHM,1.2*fap*self._MEDFWHM, 1.5*fap*self._MEDFWHM,2.*fap*self._MEDFWHM, # 2.5*fap*self._MEDFWHM,3.*fap*self._MEDFWHM # ) #irafapsizes = '%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f,%.2f' % apsizes #irafapsizes = '%.2f' % (5*apsizes[4]) self.loadHeader() if self._Band in ["J", "H", "K"]: ifilter = self.getHeader("FILTER") else: ifilter = self.getHeader("SUBSET") print "FILTER: %s" % ifilter print "RON: %s" % self.getHeader("RON") print "GAIN: %s" % self.getHeader("GAIN") print "EXPTIME: %s" % self.getHeader("EXPTIME") print "AIRMASS: %s" % self.getHeader("AIRMASS") print "DATE: %s" % self.getHeader("DATE-OBS") print "ZP: %s" % self._ZPDICT[self.getHeader("FILTER")] try: EPADU = (2/3)*self.getHeader("NIMGS")*self.getHeader("GAIN") print "Keyword used: NIMGS" except: EPADU = (2/3)**self.getHeader("NCOMBINE")*self.getHeader("GAIN") print "Keyword uparmsed: NCOMBINE" iraf.phot(image=self._Name, coords=coordfilename, output=output, verify=0, verbose=1, interac="no", scale=1, fwhmpsf=self._MEDFWHM, sigma=self._skySTDEV, wcsin="logical", wcsout="logical", datamin=-100, datamax="INDEF", zmag=self._ZPDICT[self.getHeader("FILTER")], # to do: make function to calculate annulus=photObject._fan, dannulus=photObject._fdan, calgorithm="none", aperture=photObject._fap, cbox=1.5*self._MEDFWHM, maxshift=15, mode="ql", Stdout=1, readnoi=self.getHeader("RON"), epadu=EPADU, itime=self.getHeader("EXPTIME"), xairmass=self.getHeader("AIRMASS"), ifilter=ifilter, otime=self.getHeader("DATE-OBS"), salgori="mode", #skyvalu=0, smaxite=1 ) # Parse the output photout = open(output, "r") photoObjectProperties = photout.readlines() photout.close() propList = [] for Properties in photoObjectProperties: if Properties[0] != "#": Property = [i for i in Properties.replace("\n","").replace("\\","").split(" ") if i != ""] propList.append(Property) try: photObject._skyFlux = float(propList[3][0]) except: photObject._skyFlux = (propList[3][0]) try: photObject._flux = float(propList[4][1]) except: photObject._flux = (propList[4][1]) try: photObject._appMag = float(propList[4][4]) except: photObject._appMag = (propList[4][4]) try: photObject._appMagErr = float(propList[4][5]) except: photObject._appMagErr = (propList[4][5]) photObject._midMJD, photObject._midMJDErr = self.getMidMJD()#seconds=True,zero=55822.89371528)
def build(f): ### is this an MEF file current_ext = 0 NEXTEND = 0 EXTEND = f[0].header["EXTEND"] if EXTEND == "T": NEXTEND = f[0].header["NEXTEND"] current_ext = 1 ### create the name of the output MEF psf file if not f[0].header.has_key("FILENAME"): os.unlink(opt.filename) sys.exit("The fits file " + opt.filename + " has no EXPNUM keyword\n") sexp = f[0].header["FILENAME"] mef_psf = sexp + "p_psf_iraf.fits" ### create an MEF file that will contian the PSF(s) import pyfits fitsobj = pyfits.HDUList() prihdu = pyfits.PrimaryHDU() import re prihdu.header.update("FILENAME", sexp, comment="CFHT Exposure Numebr") prihdu.header.update("NEXTEND", NEXTEND, comment="number of extensions") version = re.match(r"\$Rev.*: (\d*.\d*) \$", __Version__).group(1) prihdu.header.update("MKPSF_V", float(version), comment="Version number of mkpsf") fitsobj.append(prihdu) if os.access(mef_psf, os.F_OK): os.unlink(mef_psf) fitsobj.writeto(mef_psf) fitsobj.close() outfits = pyfits.open(mef_psf, "append") prihdr = outfits[0].header import jjkmode ### Get my python routines from pyraf import iraf from pyraf.irafpar import IrafParList ### keep all the parameters locally cached. iraf.set(uparm="./") ### Load the required IRAF packages iraf.digiphot() iraf.apphot() iraf.daophot() ### temp file name hash. tfile = {} while current_ext <= NEXTEND: ### this is a psf SCRIPT so the showplots and interactive are off by force print "Working on image section " + str(current_ext) iraf.findpars.sharplo = 0 iraf.findpars.sharphi = 0.7 iraf.findpars.roundlo = -0.7 iraf.findpars.roundhi = 0.7 iraf.datapars.datamax = 20000 iraf.datapars.airmass = "AIRMASS" iraf.datapars.filter = "FILTER" iraf.datapars.obstime = "TIME-OBS" iraf.datapars.exposure = "EXPTIME" iraf.datapars.gain = "GAIN" iraf.datapars.ccdread = "RDNOISE" iraf.datapars.fwhmpsf = opt.fwhm iraf.daopars.nclean = 2 iraf.daopars.psfrad = 5.0 * opt.fwhm iraf.daopars.fitrad = 0.85 * opt.fwhm iraf.daopars.function = "gauss" iraf.centerpars.calgorithm = "centroid" zero_mag = 26.19 iraf.photpars.zmag = zero_mag iraf.photpars.apertures = int(0.85 * opt.fwhm) iraf.fitskypars.annulus = 2 + int(opt.fwhm * 4.00) iraf.fitskypars.dannulus = int(opt.fwhm * 2.0) iraf.daophot.verbose = no iraf.daophot.verify = no iraf.daophot.update = no iraf.psf.interactive = no iraf.pstselect.interactive = no iraf.datapars.saveParList() iraf.fitskypars.saveParList() iraf.centerpars.saveParList() iraf.findpars.saveParList() iraf.photpars.saveParList() tfiles = [ "coo_bright", "coo_ok", "coo_faint", "mag_all", "mag_bright", "mag_ok", "mag_good", "mag_best", "pst_in", "pst_out", "pst_out2", "prf", "psg_org", "psg", "psf_1.fits", "psf_2.fits", "psf_final.fits", "psf_3.fits", "psf_4.fits", "mag_pst", "coo_pst", "nst", "nrj", "seepsf.fits", "sub.fits", "fwhm", "apcor", ] for file in tfiles: extname = "chip" + str(f[current_ext].header.get("IMAGEID", str(current_ext))).zfill(2) tfile[file] = sexp + "_" + extname + "." + file if os.access(tfile[file], os.F_OK): os.unlink(tfile[file]) if EXTEND == "T": this_image = opt.filename + "[" + extname + "]" else: this_image = opt.filename gain = f[current_ext].header.get("GAIN", 1.0) #### set sky/sigma parameters specific to this frame. (skyvalue, sigma) = jjkmode.stats(f[current_ext].data) import math sigma = math.sqrt(skyvalue / gain) datamin = float(skyvalue) - 8.0 * float(sigma) print "Determined sky level to be " + str(skyvalue) + " +/-" + str(sigma) iraf.datapars.datamin = datamin iraf.datapars.sigma = float(sigma) iraf.datapars.saveParList() iraf.fitskypars.skyvalue = skyvalue iraf.fitskypars.saveParList() ### find the bright stars in the image. print "sextracting for stars in " + this_image ###iraf.daophot.daofind(image=this_image, ### output=tfile['coo_bright'],threshold=4.0) os.system( "sex -c /home/cadc/kavelaar/12kproc/config/default.sex -SATUR_LEVEL 25000 -CATALOG_NAME " + tfile["coo_bright"] + " " + this_image ) ### print "finding stellar locus in sround/ground space" print "clipping using star_class > 0.85 " fcoo = open(tfile["coo_bright"], "r") lines = fcoo.readlines() fcoo.close() import numarray, math fout = open(tfile["coo_ok"], "w") for line in lines: if re.match(r"^#", line) or re.search(r"INDEF", line): continue values = line.split() star_class = float(values[2]) if star_class > 0.75: fout.write(line) fout.close() print "Measuring photometry for psf candidate stars in " + tfile["coo_ok"] iraf.daophot.phot(image=this_image, coords=tfile["coo_ok"], output=tfile["mag_bright"]) ### do this selection in 2 steps because of the way IRAF handles INDEFs print "Selecting stars that have good centroids and magnitudes " iraf.pselect( tfile["mag_bright"], tfile["mag_ok"], "(CIER==0)&&(PIER==0)&&(SIER==0)&&(MSKY>0)&&(MSKY<2e5)&&(MSKY!=INDEF)" ) print "Selecting stars that have normal sky levels" condition = "(abs(MSKY -" + str(skyvalue) + ") < 5.0*" + str(sigma) + ")" iraf.pselect(tfile["mag_ok"], tfile["mag_good"], condition) a = iraf.txdump(tfile["mag_good"], "SSKEW", iraf.yes, Stdout=1) aa = [] for v in a: aa.append(float(v)) a = numarray.array(aa) mean = a.mean() aa = a * a stddev = math.sqrt(aa.sum() / len(aa) - mean ** 2) limit = mean + 2 * stddev os.unlink(tfile["mag_good"]) condition = condition + " && SSKEW < " + str(limit) iraf.pselect(tfile["mag_ok"], tfile["mag_good"], condition) print "Choosing the psf stars" iraf.pstselect(image=this_image, photfile=tfile["mag_good"], pstfile=tfile["pst_in"], maxnpsf=25) ## construct an initial PSF image print "computing psf with neighbor stars based on complete star list" iraf.psf.mode = "a" iraf.psf( image=this_image, photfile=tfile["mag_bright"], pstfile=tfile["pst_in"], psfimage=tfile["psf_1.fits"], opstfile=tfile["pst_out"], groupfile=tfile["psg_org"], varorder=0, ) try: print "subtracting the psf neighbors and placing the results in " + tfile["sub.fits"] iraf.daophot.nstar( image=this_image, groupfile=tfile["psg_org"], psfimage=tfile["psf_1.fits"], nstarfile=tfile["nst"], rejfile=tfile["nrj"], ) iraf.daophot.substar( image=this_image, photfile=tfile["nst"], exfile=tfile["pst_in"], psfimage=tfile["psf_1.fits"], subimage=tfile["sub.fits"], ) a = iraf.daophot.txdump(tfile["nst"], "chi", "yes", Stdout=1) aa = [] for v in a: aa.append(float(v)) a = numarray.array(aa) mean = a.mean() aa = a * a stddev = math.sqrt(aa.sum() / len(aa) - mean ** 2) limit = mean + 2.5 * stddev print "Selecting those psf stars with CHI^2 <" + str(limit) + " after fitting with trial psf" iraf.pselect(tfile["nst"], tfile["mag_best"], "CHI < " + str(limit)) os.unlink(tfile["pst_out"]) ## os.unlink(tfile['psg']) ## rebuild the PSF file with the psf stars that fit well.. ## using the neighbor subtracted image print "Rebuilding the PSF" iraf.daophot.psf( image=tfile["sub.fits"], photfile=tfile["mag_best"], pstfile=tfile["pst_in"], psfimage=tfile["psf_2.fits"], opstfile=tfile["pst_out"], groupfile=tfile["psg"], varorder=0, ) print "re-subtracting with rebuilt psf" os.unlink(tfile["nst"]) os.unlink(tfile["nrj"]) iraf.daophot.nstar( image=this_image, groupfile=tfile["psg"], psfimage=tfile["psf_2.fits"], nstarfile=tfile["nst"], rejfile=tfile["nrj"], ) os.unlink(tfile["sub.fits"]) iraf.daophot.substar( image=this_image, photfile=tfile["nst"], exfile=tfile["pst_in"], psfimage=tfile["psf_2.fits"], subimage=tfile["sub.fits"], ) os.unlink(tfile["psg"]) os.unlink(tfile["pst_out"]) iraf.daophot.psf( image=tfile["sub.fits"], photfile=tfile["mag_best"], pstfile=tfile["pst_in"], psfimage=tfile["psf_3.fits"], opstfile=tfile["pst_out"], groupfile=tfile["psg"], varorder=0, ) os.unlink(tfile["nrj"]) os.unlink(tfile["nst"]) iraf.daophot.nstar( image=this_image, groupfile=tfile["psg"], psfimage=tfile["psf_3.fits"], nstarfile=tfile["nst"], rejfile=tfile["nrj"], ) a = iraf.daophot.txdump(tfile["nst"], "chi", "yes", Stdout=1) aa = [] for v in a: aa.append(float(v)) a = numarray.array(aa) mean = a.mean() aa = a * a stddev = math.sqrt(aa.sum() / len(aa) - mean ** 2) limit = mean + 2 * stddev limit = 2.0 # print "Selecting those psf stars with CHI^2 < "+str(limit)+" after fit with GOOD psf" os.unlink(tfile["mag_best"]) iraf.pselect(tfile["nst"], tfile["mag_best"], "CHI < " + str(limit)) print "Building final PSF.... " os.unlink(tfile["sub.fits"]) iraf.daophot.substar( image=this_image, photfile=tfile["nst"], exfile=tfile["pst_in"], psfimage=tfile["psf_3.fits"], subimage=tfile["sub.fits"], ) os.unlink(tfile["psg"]) os.unlink(tfile["pst_out"]) iraf.daophot.psf( image=tfile["sub.fits"], photfile=tfile["mag_best"], pstfile=tfile["pst_in"], psfimage=tfile["psf_final.fits"], opstfile=tfile["pst_out"], groupfile=tfile["psg"], varorder=0, ) print "building an analytic psf for the FWHM calculations" os.unlink(tfile["pst_out"]) os.unlink(tfile["psg"]) iraf.daophot.psf( image=tfile["sub.fits"], photfile=tfile["mag_best"], pstfile=tfile["pst_in"], psfimage=tfile["psf_4.fits"], opstfile=tfile["pst_out"], groupfile=tfile["psg"], varorder=-1, ) except: print sys.exc_info()[1] print "ERROR: Reverting to first pass psf" tfile["psf_final.fits"] = tfile["psf_1.fits"] iraf.daophot.psf( image=this_image, photfile=tfile["mag_best"], pstfile=tfile["pst_in"], psfimage=tfile["psf_4.fits"], opstfile=tfile["pst_out2"], groupfile=tfile["psg"], varorder=-1, ) psf_ap = iraf.photpars.apertures ap1 = int(psf_ap) ap2 = int(4.0 * opt.fwhm) apcor = "INDEF" aperr = "INDEF" if 0: # try ### now that we have the psf use the output list of psf stars ### to compute the aperature correction lines = iraf.txdump(tfile["pst_out"], "xcen,ycen,mag,id", iraf.yes, Stdout=tfile["coo_pst"]) ## set the lower ap value for the COG (normally set to 2) if ap1 < 3: smallap = 1 else: smallap = 2 - ap1 + 1 ap1 = 2 ap2 = int(math.floor(4.0 * opt.fwhm)) naperts = ap2 - ap1 + 1 iraf.photpars.apertures = str(ap1) + ":" + str(ap2) + ":1" iraf.photpars.saveParList() iraf.daophot.phot(image=this_image, coords=tfile["coo_pst"], output=tfile["mag_pst"]) iraf.photcal() iraf.photcal.mkapfile( tfile["mag_pst"], naperts=naperts, apercors=tfile["apcor"], smallap=smallap, verify="no", gcommands="", interactive=0, ) fin = open(tfile["apcor"], "r") lines = fin.readlines() values = lines[2].split() apcor = values[1] aperr = values[2] # except: ## compute the FWHM of the PSF image using the analytic PSF (VarOrd=-1) psf_file = pyfits.open(tfile["psf_4.fits"]) fwhm = psf_file[0].header.get("PAR1", 99.0) + psf_file[0].header.get("PAR2", 99.0) psf_file.close() # ## Open the psf.fits infits = pyfits.open(tfile["psf_final.fits"]) hdu = infits[0] inhdu = hdu.header inhdu.update("XTENSION", "IMAGE", before="SIMPLE") inhdu.update("PCOUNT", 0, after="NAXIS2") inhdu.update("GCOUNT", 1, after="PCOUNT") del hdu.header["SIMPLE"] del hdu.header["EXTEND"] inhdu.update("EXTNAME", extname, comment="image extension identifier") # inhdu.update("SLOW",slow,comment="SROUND low cutoff") # inhdu.update("SIGH",sigh,comment="SROUND high cutoff") inhdu.update("PFWHM", fwhm, comment="FWHM of stars based on PSF fitting") inhdu.update("ZMAG", zero_mag, comment="ZMAG of PSF ") inhdu.update("BCKG", skyvalue, comment="Mean sky level in counts") inhdu.update("BCKG_STD", sigma, comment="standard deviation of sky in counts") inhdu.update("AP1", psf_ap, comment="Apperture used for PSF flux") inhdu.update("AP2", ap2, comment="Full Flux aperture") inhdu.update("APCOR", apcor, comment="Apperture correction (ap1->ap2)") inhdu.update("APERR", apcor, comment="Uncertainty in APCOR") # ### append this psf to the output images.... print "Sticking this PSF onto the output file" f[current_ext].header.update("PFWHM", fwhm, comment="FWHM of stars based on PSF fitting") f[current_ext].header.update("BCKG", skyvalue, comment="Mean sky level in counts") f[current_ext].header.update("BCKG_STD", sigma, comment="Standard deviation of sky in counts") f.flush() outfits.append(hdu) outfits.flush() infits.close() ### remove the temp file we used for this computation. for tf in tfile.keys(): if os.access(tfile[tf], os.F_OK): os.unlink(tfile[tf]) current_ext = current_ext + 1 outfits.close() return mef_psf
def phot(fits_filename, x_in, y_in, aperture=15, sky=20, swidth=10, apcor=0.3, maxcount=30000.0, exptime=1.0, zmag=None, extno=0, centroid=True): """ Compute the centroids and magnitudes of a bunch sources on fits image. :rtype : astropy.table.Table :param fits_filename: Name of fits image to measure source photometry on. :type fits_filename: str :param x_in: x location of source to measure :type x_in: float, numpy.array :param y_in: y location of source to measure :type y_in: float, numpy.array :param aperture: radius of circular aperture to use. :type aperture: float :param sky: radius of inner sky annulus :type sky: float :param swidth: width of the sky annulus :type swidth: float :param apcor: Aperture correction to take aperture flux to full flux. :type apcor: float :param maxcount: maximum linearity in the image. :type maxcount: float :param exptime: exposure time, relative to zmag supplied :type exptime: float :param zmag: zeropoint magnitude :param extno: extension of fits_filename the x/y location refers to. """ if not hasattr(x_in, '__iter__'): x_in = [x_in, ] if not hasattr(y_in, '__iter__'): y_in = [y_in, ] if (not os.path.exists(fits_filename) and not fits_filename.endswith(".fits")): # For convenience, see if we just forgot to provide the extension fits_filename += ".fits" try: input_hdulist = fits.open(fits_filename) except Exception as err: logger.debug(str(err)) raise TaskError("Failed to open input image: %s" % err.message) # get the filter for this image filter_name = input_hdulist[extno].header.get('FILTER', 'DEFAULT') # Some nominal CFHT zeropoints that might be useful zeropoints = {"I": 25.77, "R": 26.07, "V": 26.07, "B": 25.92, "DEFAULT": 26.0, "g.MP9401": 32.0, 'r.MP9601': 31.9, 'gri.MP9603': 33.520} if zmag is None: logger.warning("No zmag supplied to daophot, looking for header or default values.") zmag = input_hdulist[extno].header.get('PHOTZP', zeropoints[filter_name]) logger.warning("Setting zmag to: {}".format(zmag)) # check for magic 'zeropoint.used' files for zpu_file in ["{}.zeropoint.used".format(os.path.splitext(fits_filename)[0]), "zeropoint.used"]: if os.access(zpu_file, os.R_OK): with open(zpu_file) as zpu_fh: zmag = float(zpu_fh.read()) logger.warning("Using file {} to set zmag to: {}".format(zpu_file, zmag)) break photzp = input_hdulist[extno].header.get('PHOTZP', zeropoints.get(filter_name, zeropoints["DEFAULT"])) if zmag != photzp: logger.warning(("zmag sent to daophot: ({}) " "doesn't match PHOTZP value in image header: ({})".format(zmag, photzp))) # setup IRAF to do the magnitude/centroid measurements iraf.set(uparm="./") iraf.digiphot() iraf.apphot() iraf.daophot(_doprint=0) iraf.photpars.apertures = aperture iraf.photpars.zmag = zmag iraf.datapars.datamin = 0 iraf.datapars.datamax = maxcount iraf.datapars.exposur = "" iraf.datapars.itime = exptime iraf.fitskypars.annulus = sky iraf.fitskypars.dannulus = swidth iraf.fitskypars.salgorithm = "mode" iraf.fitskypars.sloclip = 5.0 iraf.fitskypars.shiclip = 5.0 if centroid: iraf.centerpars.calgori = "centroid" iraf.centerpars.cbox = 5. iraf.centerpars.cthreshold = 0. iraf.centerpars.maxshift = 2. iraf.centerpars.clean = 'no' else: iraf.centerpars.calgori = "none" iraf.phot.update = 'no' iraf.phot.verbose = 'no' iraf.phot.verify = 'no' iraf.phot.interactive = 'no' # Used for passing the input coordinates coofile = tempfile.NamedTemporaryFile(suffix=".coo", delete=False) for i in range(len(x_in)): coofile.write("%f %f \n" % (x_in[i], y_in[i])) coofile.flush() # Used for receiving the results of the task # mag_fd, mag_path = tempfile.mkstemp(suffix=".mag") magfile = tempfile.NamedTemporaryFile(suffix=".mag", delete=False) # Close the temp files before sending to IRAF due to docstring: # "Whether the name can be used to open the file a second time, while # the named temporary file is still open, varies across platforms" coofile.close() magfile.close() os.remove(magfile.name) iraf.phot(fits_filename+"[{}]".format(extno), coofile.name, magfile.name) pdump_out = ascii.read(magfile.name, format='daophot') logging.debug("PHOT FILE:\n"+str(pdump_out)) if not len(pdump_out) > 0: mag_content = open(magfile.name).read() raise TaskError("photometry failed. {}".format(mag_content)) # apply the aperture correction pdump_out['MAG'] -= apcor # if pdump_out['PIER'][0] != 0 or pdump_out['SIER'][0] != 0 or pdump_out['CIER'][0] != 0: # raise ValueError("Photometry failed:\n {}".format(pdump_out)) # Clean up temporary files generated by IRAF os.remove(coofile.name) os.remove(magfile.name) logger.debug("Computed aperture photometry on {} objects in {}".format(len(pdump_out), fits_filename)) del input_hdulist return pdump_out
def photom(images,anillo,danillo,apertura,outfile=None,path=None,coords='coords.in'): ''' Dada una lista de imágenes, realiza la fotometría de apertura con la tarea phot. INPUT images : Lista de imágenes .in o archivo .fits (coords) : Archivo con las coordenadas de los objetos, por defecto es "coords.in" anillo : radio annulus danillo : radio dannulus apertura : Apretura de los radios, ingresada como un string ej: r1,r2:rn-1,rn (outfile): Nombre de archivo(s) de salida. por defecto es el nombre de images en extension phot (path) : Línea que indica el camino al directorio de archivos. ------------------------------------------------------------------ Given a image list, performs aperture photometry via phot task. INPUT images : .in image list or .fits file. coords : file with objects' coordinates. anillo : ? danillo : ?? (outfile): Name of the output file(s). (path) : String that indicates the path to the files. ''' if path is not None: originalpath=aux.chdir(path,save=True) iraf.noao() iraf.digiphot() iraf.apphot() iraf.unlearn(iraf.datapars) iraf.unlearn(iraf.fitskypars) iraf.unlearn(iraf.photpars) iraf.unlearn(iraf.phot) #eterno resplandor de una tarea sin recuerdos # serie de comandos que indican el campo del header donde ir a buscar info iraf.datapars.ccdread="rdnoise" iraf.datapars.gain="gain" iraf.datapars.exposure="exptime" iraf.datapars.airmass="airmass" iraf.datapars.filter="filtnew" iraf.datapars.obstime="time-obs" # setea parámetros de la fotometría iraf.fitskypars.annulus=anillo # radio del anillo interior (cuentas obj - cuentas d cielo) iraf.fitskypars.dannulus=danillo # radio del anillo exterior(cuentas cielo) # iraf.photpars.apertures="0.5,1:30:1" iraf.photpars.apertures= apertura # intervalo de radios a tomar iraf.phot.interac='no' iraf.phot.verify='no' iraf.phot.verbose='no' if (os.path.splitext(images)[-1] == '.fit' or os.path.splitext(images)[-1] == '.fits'): iraf.phot.image=images iraf.phot.coords=coords outfile=aux.default(outfile,os.path.splitext(images)[0]+'.phot',borrar=True) iraf.phot.output=outfile else: iraf.phot.image='@'+images iraf.phot.coords='@'+coords outfile=aux.default(outfile,os.path.splitext(images)[0]+'_phots.in',borrar=True) imagelista=np.genfromtxt(images,dtype=None) f=open(outfile,'a+') for im in imagelista : f.write(os.path.splitext(im)[0]+'.phot'+'\n') aux.rm(os.path.splitext(im)[0]+'.phot') f.close() iraf.phot.output='@'+outfile iraf.phot() #%% # contador de cantidad de filas en un archivo -sin contar los comentarios- # if (os.path.splitext(images)[-1] == '.fit' or # os.path.splitext(images)[-1] == '.fits'): # with open(coords) as fp: # for curline in fp: # check if the current line # starts with "#" # if curline.startswith("#"): #if it's a commented line # continue #do nothing # else: # line = fp.readline() #initialize reading # cnt = 1 # while line: #print("Line {}: {}".format(cnt, line.strip())) #eso puede servir para comprimir los dos for en #uno solo, pero no sé como jeje... pronto pronto... # line = fp.readline() # cnt += 1 #count how many lines -stars- in coords file. # fp.close() if path is not None: aux.chdir(originalpath)
def iraf_phot(f,x,y,zmag=26.5,apin=10,skyin=15,skywidth=10): """Compute the magnitude of the star at location x/y""" import pyfits import re infits=pyfits.open(f,'update') f=re.sub(r'.fits$','',f) ### Get my python routines from pyraf import iraf from pyraf.irafpar import IrafParList ### keep all the parameters locally cached. iraf.set(uparm="./") iraf.set(imtype="fits") ### Load the required IRAF packages iraf.digiphot() iraf.apphot() iraf.daophot() ### temp file name hash. tfile={} iraf.datapars.datamax=60000 iraf.datapars.datamin=-1000 iraf.datapars.airmass='AIRMASS' iraf.datapars.filter='FILTER' iraf.datapars.obstime='TIME-OBS' iraf.datapars.exposure='EXPTIME' iraf.datapars.gain='GAIN' iraf.datapars.ccdread='RDNOISE' iraf.datapars.fwhmpsf=5.0 iraf.centerpars.calgorithm='centroid' iraf.photpars.zmag=zmag iraf.photpars.apertures=apin iraf.fitskypars.annulus=skyin iraf.fitskypars.dannulus=skywidth iraf.daophot.verbose=iraf.no iraf.daophot.verify=iraf.no iraf.daophot.update=iraf.no iraf.psf.interactive=iraf.no iraf.pstselect.interactive=iraf.no iraf.datapars.saveParList() iraf.fitskypars.saveParList() iraf.centerpars.saveParList() iraf.findpars.saveParList() iraf.photpars.saveParList() tfiles = ['coo','mag'] for file in tfiles: extname=f tfile[file]=extname+"."+file if ( os.access(tfile[file],os.F_OK) ): os.unlink(tfile[file]) this_image=f fd = open(tfile['coo'],'w') fd.write('%f %f\n' % ( x, y) ) fd.close() print "Measuring photometry psf star in "+tfile['coo'] iraf.daophot.phot(image=this_image, coords=tfile['coo'], output=tfile['mag']) import string a=iraf.txdump(tfile['mag'],"MAG,XCEN,YCEN",iraf.yes,Stdout=1) (mag,x,y)=string.split(a[0]) inhdu=infits[0].header inhdu.update("PSFMAG",float(mag),comment="PSF Magnitude") inhdu.update("PSF_X",float(x),comment="PSF Magnitude") inhdu.update("PSF_Y",float(y),comment="PSF Magnitude") inhdu.update("ZMAG",zmag,comment="ZMAG of PSF ") ## now measure using a smaller aperture to get aper correction iraf.photpars.apertures=apin*3.0 os.unlink(tfile['mag']) iraf.daophot.phot(image=this_image, coords=tfile['coo'], output=tfile['mag']) a=iraf.txdump(tfile['mag'],"MAG,XCEN,YCEN",iraf.yes,Stdout=1) (magout,x,y)=string.split(a[0]) inhdu.update("APCOR",float(magout)-float(mag),comment="AP_OUT - AP_IN") inhdu.update("AP_IN",apin*3.0,comment="Small aperature") inhdu.update("AP_OUT",apin,comment="Large aperture") # ### append this psf to the output images.... infits.close() ### remove the temp file we used for this computation. #for tf in tfile.keys(): # if os.access(tfile[tf],os.F_OK): # os.unlink(tfile[tf]) return 0
def plant_kbos(filename, psf, kbos, shifts, prefix): """ Add KBOs to an image :param filename: name of the image to add KBOs to :param psf: the Point Spread Function in IRAF/DAOPHOT format to be used by ADDSTAR :param kbos: list of KBOs to add, has format as returned by KBOGenerator :param shifts: dictionary with shifts to transfer to coordinates to reference frame. :param prefix: an estimate FWHM of the image, used to determine trailing. :return: None """ iraf.set(uparm="./") iraf.digiphot() iraf.apphot() iraf.daophot(_doprint=0) iraf.images() if shifts['nmag'] < 4: logging.warning("Mag shift based on fewer than 4 common stars.") fd = open("plant.WARNING", 'a') fd.write("Mag shift based on fewer than 4 common stars.") fd.close() if shifts['emag'] > 0.05: logging.warning("Mag shift has large uncertainty.") fd = open("plant.WARNING", 'a') fd.write("Mag shift hsa large uncertainty.") fd.close() addstar = tempfile.NamedTemporaryFile(suffix=".add") # transform KBO locations to this frame using the shifts provided. w = get_wcs(shifts) header = fits.open(filename)[0].header # set the rate of motion in units of pixels/hour instead of ''/hour scale = header['PIXSCAL1'] rate = kbos['sky_rate']/scale # compute the location of the KBOs in the current frame. # offset magnitudes from the reference frame to the current one. mag = kbos['mag'] - shifts['dmag'] angle = radians(kbos['angle']) # Move the x/y locations to account for the sky motion of the source. x = kbos['x'] - rate*24.0*shifts['dmjd']*cos(angle) y = kbos['y'] - rate*24.0*shifts['dmjd']*sin(angle) x, y = w.wcs_world2pix(x, y, 1) # Each source will be added as a series of PSFs so that a new PSF is added for each pixel the source moves. itime = float(header['EXPTIME'])/3600.0 npsf = fabs(rint(rate * itime)) + 1 mag += 2.5*log10(npsf) dt_per_psf = itime/npsf # Build an addstar file to be used in the planting of source. idx = 0 for record in transpose([x, y, mag, npsf, dt_per_psf, rate, angle]): x = record[0] y = record[1] mag = record[2] npsf = record[3] dt = record[4] rate = record[5] angle = record[6] for i in range(int(npsf)): idx += 1 x += dt*rate*math.cos(angle) y += dt*rate*math.sin(angle) addstar.write("{} {} {} {}\n".format(x, y, mag, idx)) addstar.flush() fk_image = prefix+filename try: os.unlink(fk_image) except OSError as err: if err.errno == errno.ENOENT: pass else: raise # add the sources to the image. iraf.daophot.addstar(filename, addstar.name, psf, fk_image, simple=True, verify=False, verbose=False) # convert the image to short integers. iraf.images.chpix(fk_image, fk_image, 'ushort')
def escut(image, pos_file, fwhm, peak): # input image file name, file name with matched source positions, **np.array of fwhm measurements for each source import numpy as np import matplotlib.pyplot as plt from scipy import stats from pyraf import iraf # import sewpy import os from matplotlib.path import Path iraf.images(_doprint=0) iraf.tv(_doprint=0) iraf.ptools(_doprint=0) iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.photcal(_doprint=0) iraf.apphot(_doprint=0) iraf.imutil(_doprint=0) iraf.unlearn(iraf.phot,iraf.datapars,iraf.photpars,iraf.centerpars,iraf.fitskypars) iraf.apphot.phot.setParam('interactive',"no") iraf.apphot.phot.setParam('verify',"no") iraf.datapars.setParam('datamax',50000.) iraf.datapars.setParam('gain',"gain") iraf.datapars.setParam('ccdread',"rdnoise") iraf.datapars.setParam('exposure',"exptime") iraf.datapars.setParam('airmass',"airmass") iraf.datapars.setParam('filter',"filter") iraf.datapars.setParam('obstime',"time-obs") # iraf.datapars.setParam('obstime',"date-obs") iraf.datapars.setParam('sigma',"INDEF") iraf.photpars.setParam('zmag',0.) iraf.centerpars.setParam('cbox',9.) iraf.centerpars.setParam('maxshift',3.) iraf.fitskypars.setParam('salgorithm',"median") iraf.fitskypars.setParam('dannulus',10.) # clean up the indefs so we can actually do stats, but reassign them to 99999 so we don't lose track of things # keep a separate list without them to do the median (we need floats) indefs = np.where(fwhm=='INDEF') good = np.where(fwhm!='INDEF') fwhm[indefs] = 99.999 fwhm = fwhm.astype(float) fwhm_good = fwhm[good].astype(float) indefs = np.where(peak=='INDEF') peak[indefs] = -999.999 peak = peak.astype(float) peak_good = peak[good].astype(float) if not os.path.isfile(image[0:-5]+'.txdump'): # findavgfwhm = sewpy.SEW( # params = ["X_IMAGE", "Y_IMAGE", "FWHM_IMAGE", "FLAGS"], # config = {"DETECT_THRESH":200.0}, # sexpath = "sex" # ) # # out = findavgfwhm(image)["table"] # # fwhms = out['FWHM_IMAGE'] # This is an astropy table. # flags = out['FLAGS'] # get a really rough estimate of the stellar FWHM in the image to set apertures # use the input fwhm measurement # ap1x = fwhm_est # xpos = datatable['X_IMAGE'] # ypos = datatable['Y_IMAGE'] # fwhm = datatable['FWHM_IMAGE'] # flags = datatable['FLAGS'] # idno = datatable['NUMBER'] ap1x = np.median(fwhm_good) # only use isolated detections of stars, this is the 1x aperture # print ap1x ap2x = 2.0*ap1x # these = [ i for i,id in enumerate(idno) if (flags[i] == 0)] # with open(image[0:-5]+'.escut.pos','w+') as f: # for j in range(len(xpos)): # print >> f, xpos[j], ypos[j], fwhm[j], idno[j] iraf.datapars.setParam('fwhmpsf',ap1x) iraf.photpars.setParam('apertures',repr(ap1x)+', '+repr(ap2x)) iraf.fitskypars.setParam('annulus',4.*ap1x) iraf.apphot.phot(image=image, coords=pos_file, output=image[0:-5]+'.phot') with open(image[0:-5]+'.txdump','w+') as txdump_out : iraf.ptools.txdump(textfiles=image[0:-5]+'.phot', fields="id,mag,merr,msky,stdev,rapert,xcen,ycen,ifilter,xairmass,image", expr='MAG[1] != INDEF && MERR[1] != INDEF && MAG[2] != INDEF && MERR[2] != INDEF', headers='no', Stdout=txdump_out) mag1x, mag2x = np.loadtxt(image[0:-5]+'.txdump', usecols=(1,2), unpack=True) iraf_id = np.loadtxt(image[0:-5]+'.txdump', usecols=(0,), dtype=int, unpack=True) # idno = np.loadtxt(image[0:-5]+'.escut.pos', usecols=(3,), dtype=int, unpack=True) xpos, ypos = np.loadtxt(pos_file, usecols=(0,1), unpack=True) keepIndex = iraf_id - 1 xpos, ypos, fwhm, peak = xpos[keepIndex], ypos[keepIndex], fwhm[keepIndex], peak[keepIndex] # print idno.size, iraf_id.size, xpos.size diff = mag2x - mag1x diffCut = diff magCut = mag2x xCut = xpos#[good] yCut = ypos#[good] idCut = iraf_id fwhmCut = fwhm#_good peakCut = peak print peakCut.size, magCut.size, diffCut.size print diffCut.size, 0, np.median(diffCut), diffCut.std() nRemoved = 1 # plt.clf() # plt.scatter(peakCut, magCut, edgecolor='none') # plt.savefig('peaktest.pdf') plt.clf() # plt.hlines(bin_edges, -2, 1, colors='red', linestyle='dashed') plt.scatter(diff, mag2x, edgecolor='none', facecolor='black', s=4) # plt.scatter(diffCut, magCut, edgecolor='none', facecolor='blue', s=4) magdiff = zip(magCut.tolist(), diffCut.tolist(), peakCut.tolist(), idCut.tolist()) dtype = [('mag',float), ('diff', float), ('peak', float), ('id', int)] magdiff = np.array(magdiff, dtype=dtype) magSort = np.sort(magdiff, order='peak') peakRange = (magSort['peak'] > 20000.0) & (magSort['peak'] < 40000.0) peakVal = np.median((magSort['diff'])[np.where(peakRange)]) # peakVal = np.median(diffCut) print peakVal plt.scatter((magSort['diff'])[np.where(peakRange)], (magSort['mag'])[np.where(peakRange)], edgecolor='none', facecolor='blue', s=4) while nRemoved != 0: nBefore = diffCut.size diffCheck = np.where(abs(peakVal-diffCut) < 2.5*diffCut.std())#[i for i,d in enumerate(diff) if (-0.5 < d < 0.0)] # diffCut = diffCut[diffCheck] nRemoved = nBefore - diffCut.size magCut = magCut[diffCheck] xCut = xCut[diffCheck] yCut = yCut[diffCheck] idCut = idCut[diffCheck] fwhmCut = fwhmCut[diffCheck] print diffCut.size, nRemoved, np.median(diffCut), diffCut.std() if 0.05 < diffCut.std() <0.06: nRemoved=0 # plt.fill_betweenx(bin_centers, bin_meds+3.0*bin_stds, bin_meds-3.0*bin_stds, facecolor='red', edgecolor='none', alpha=0.4, label='2x RMS sigma clipping region') # with open('escutSTD_i.pos','w+') as f: # for i,blah in enumerate(xCut): # print >> f, xCut[i], yCut[i], diffCut[i] bin_meds, bin_edges, binnumber = stats.binned_statistic(magCut, diffCut, statistic='median', bins=24, range=(magCut.min(),magCut.max())) bin_stds, bin_edges, binnumber = stats.binned_statistic(magCut, diffCut, statistic=np.std, bins=24, range=(magCut.min(),magCut.max())) bin_width = (bin_edges[1] - bin_edges[0]) bin_centers = bin_edges[1:] - bin_width/2 # print bin_meds, bin_stds left_edge = np.array(zip(peakVal-2.5*bin_stds, bin_centers)) right_edge = np.flipud(np.array(zip(peakVal+2.5*bin_stds, bin_centers))) # print left_edge, right_edge verts = np.vstack((left_edge, right_edge)) # print verts # verts = np.delete(verts, np.array([0,1,2,22,23,24,25,45,46,47]), axis=0) # print verts esRegion = Path(verts) sources = esRegion.contains_points(zip(diff,mag2x)) # print sources with open('escutREG_i.pos','w+') as f: for i,blah in enumerate(xpos[sources]): print >> f, (xpos[sources])[i], (ypos[sources])[i], (diff[sources])[i] magCut = mag2x[sources] fwhmCut = fwhm[sources] xCut = xpos[sources] yCut = ypos[sources] diffCut = diff[sources] # find the sources that are in the std method but not the region method # print idCut, idno[sources] # extrasSTD = np.setdiff1d(idno[sources], idCut) # print extrasSTD.size # print extrasSTD # with open('escutUNIQUE.pos','w+') as f: # for i,blah in enumerate(extrasSTD): # print >> f, xpos[blah-1], ypos[blah-1] # fwhmcheck = np.loadtxt('testfwhmREG.log', usecols=(10,), unpack=True) fwhmchk2 = np.where((magCut<-4) & (fwhmCut<90.0)) print np.median(fwhmCut[fwhmchk2]), np.std(fwhmCut[fwhmchk2]) fwchk = np.where(np.abs(fwhmCut-np.median(fwhmCut[fwhmchk2])) > 10.0*np.std(fwhmCut[fwhmchk2])) drop = np.abs(fwhmCut-np.median(fwhmCut[fwhmchk2])) > 10.0*np.std(fwhmCut[fwhmchk2]) # fwmag = mag2x[sources] with open('escutVBAD_i.pos','w+') as f: for i,blah in enumerate(xCut[fwchk]): print >> f, (xCut[fwchk])[i], (yCut[fwchk])[i] with open('escut_r.pos','w+') as f: for i,blah in enumerate(xCut): if not drop[i]: print >> f, xCut[i], yCut[i] with open('escut_g.pos','w+') as f: for i,blah in enumerate(xCut): if not drop[i]: print >> f, xCut[i], yCut[i] plt.fill_betweenx(bin_centers, peakVal+2.5*bin_stds, peakVal-2.5*bin_stds, facecolor='red', edgecolor='none', alpha=0.4, label='2x RMS sigma clipping region') plt.scatter(diffCut[fwchk], magCut[fwchk], edgecolor='none', facecolor='red', s=4) plt.ylim(0,-12) plt.xlabel('$m_{2x} - m_{1x}$') plt.ylabel('$m_{2x}$') plt.xlim(-2,1) plt.savefig('testmagiraf.pdf') plt.clf() plt.scatter(magCut, fwhmCut, edgecolor='none', facecolor='black') plt.scatter(magCut[fwchk], fwhmCut[fwchk], edgecolor='none', facecolor='red') plt.hlines([np.median(fwhmCut)], -12, 0, colors='red', linestyle='dashed') plt.hlines([np.median(fwhmCut)+fwhmCut.std(), np.median(fwhmCut)-fwhmCut.std()], -12, 0, colors='red', linestyle='dotted') plt.ylim(0,20) plt.xlim(-12,0) plt.ylabel('fwhm') plt.xlabel('$m_{2x}$') plt.savefig('fwhmcheck.pdf') return True
def get_app_phot_target(image, ra=None, dec=None, plot=True, store=True, wcsin="logical", fwhm=None, box=15, arcsecpix=0.394, app=2): ''' coords: files: wcsin: can be "world", "logic" fwhm: in arcsec ''' # Load packages; splot is in the onedspec package, which is in noao. # The special keyword _doprint=0 turns off displaying the tasks # when loading a package. with warnings.catch_warnings(): warnings.simplefilter("ignore") fxn() iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) iraf.unlearn("apphot") impf = pf.open(image) wcs = WCS(impf[0].header) #Check that actually the object is within this frame. if (ra is None or dec is None): if (fitsutils.has_par(image, "OBJRA") and fitsutils.has_par(image, "OBJRA")): ra, dec = cc.hour2deg(fitsutils.get_par(image, 'OBJRA'), fitsutils.get_par(image, 'OBJDEC')) else: ra, dec = cc.hour2deg(fitsutils.get_par(image, 'RA'), fitsutils.get_par(image, 'DEC')) print "Assuming ra=%.5f, dec=%.5f"%(ra, dec) pra, pdec = get_xy_coords(image, ra, dec) else: if("logic" in wcsin): pra, pdec = ra, dec else: #Using new method to derive the X, Y pixel coordinates, as wcs module does not seem to be working well. try: #pra, pdec = wcs.wcs_sky2pix(ra, dec, 1) #print "Retrieved the pixel number" pra, pdec = get_xy_coords(image, ra, dec) except IndexError: print "Error with astrometry.net. trying the rudimentary method." pra, pdec = wcs.wcs_sky2pix(ra, dec, 1) #pra, pdec = wcs.wcs_sky2pix(np.array([ra, dec], ndmin=2), 1)[0] shape = impf[0].data.shape if (pra > 0) and (pra < shape[0]) and (pdec > 0) and (pdec < shape[1]): pass else: print image, "ERROR! Object coordinates are outside this frame. Skipping any aperture photometry!!" print pra, pdec, shape return imdir = os.path.dirname(image) imname = os.path.basename(image) plotdir = os.path.join(imdir, "photometry") if not os.path.isdir(plotdir): os.makedirs(plotdir) out_name = os.path.join(plotdir, imname + ".seq.mag") clean_name = os.path.join(plotdir, imname + ".objapp.mag") if (not fwhm is None): fwhm_value = fwhm elif (fitsutils.has_par(image, 'FWHM')): fwhm_value = fitsutils.get_par(image, 'FWHM') else: #Put some default value for Palomar fwhm_value=1.5 if (wcsin == 'logical'): fwhm_value = fwhm_value / arcsecpix if (fitsutils.has_par(image, 'AIRMASS')): airmass_value = fitsutils.get_par(image, 'AIRMASS') else: airmass_value = 1.3 if (not fitsutils.has_par(image, "EXPTIME")): if (fitsutils.has_par(image, "ITIME") and fitsutils.has_par(image, "COADDS")): exptime = fitsutils.get_par(image, "ITIME")*fitsutils.get_par(image, "COADDS") fitsutils.update_par(image, "EXPTIME", exptime) exptime = fitsutils.get_par(image, 'EXPTIME') gain = fitsutils.get_par(image, 'GAIN') #print "FWHM", fwhm_value aperture_rad = math.ceil(float(fwhm_value)*app) # Set aperture radius to two times the PSF radius sky_rad= math.ceil(aperture_rad*app*2) #print aperture_rad, sky_rad print "Saving coodinates for the object in pixels",pra,pdec coords = "/tmp/coords.dat" np.savetxt("/tmp/coords.dat", np.array([[pra, pdec]]), fmt="%.4f %.4f") if os.path.isfile(out_name): os.remove(out_name) if os.path.isfile(clean_name): os.remove(clean_name) iraf.noao.digiphot.apphot.qphot(image = image,\ cbox = box ,\ annulus = sky_rad ,\ dannulus = 20. ,\ aperture = str(aperture_rad),\ coords = coords ,\ output = out_name ,\ plotfile = "" ,\ zmag = 0. ,\ exposure = "exptime" ,\ airmass = "airmass" ,\ filter = "filter" ,\ obstime = "DATE" ,\ epadu = gain ,\ interactive = "no" ,\ radplots = "yes" ,\ verbose = "no" ,\ graphics = "stdgraph" ,\ display = "stdimage" ,\ icommands = "" ,\ wcsin = "logical", wcsout = "logical", gcommands = "") #iraf.noao.digiphot.apphot.phot(image=image, cbox=5., annulus=12.4, dannulus=10., salgori = "centroid", aperture=9.3,wcsin="world",wcsout="tv", interac = "no", coords=coords, output=out_name) iraf.txdump(out_name, "id,image,xcenter,ycenter,xshift,yshift,fwhm,msky,stdev,cier,rapert,sum,area,nsky,flux,itime,mag,merr", "yes", Stdout=clean_name) ma = np.genfromtxt(clean_name, comments="#", dtype=[("id","<f4"), ("image","|S20"), ("X","<f4"), ("Y","<f4"), ("Xshift","<f4"), ("Yshift","<f4"),("fwhm","<f4"), ("msky","<f4"), \ ("stdev","<f4"), ("flags", np.int), ("rapert", "<f4"), ("sum", "<f4"), ("area", "<f4"), ("nsky","<f4") , ("flux", "<f4"), ("itime", "<f4"), ("fit_mag","<f4"), ("fiterr","<f4")]) if (ma.size > 0): ma = np.array([ma]) m = ma[~np.isnan(ma["fit_mag"])] else: print "Only one object found!" m = np.array([ma]) insmag = np.round(ma['fit_mag'][0] , 3) insmagerr = np.round(ma['fiterr'][0], 3) if (fitsutils.has_par(image, "ZEROPT") and fitsutils.has_par(image, "ZEROPTU")): mag = insmag + float(fitsutils.get_par(image, "ZEROPT")) magerr = np.sqrt(insmagerr**2+ float(fitsutils.get_par(image, "ZEROPTU"))**2) else: mag = 0 magerr = 0 if np.isnan(mag): mag, magerr = 0, 0 insmag, insmagerr = 0,0 fitsutils.update_par(image, "INSMAG", "%.3f"%insmag ) fitsutils.update_par(image, "INSMAGER", "%.3f"%insmagerr) fitsutils.update_par(image, "APPMAG", np.round(mag, 3) ) fitsutils.update_par(image, "APPMAGER", np.round(magerr, 3)) if (plot): #zmin, zmax = zscale.zscale(impf[0].data.T[pra-50:pra+50,pdec-50:pdec+50]) #zmin, zmax = zscale.zscale(impf[0].data) #im = plt.imshow(impf[0].data, vmin=zmin, vmax=zmax, origin="bottom") print np.percentile(impf[0].data, 5), np.percentile(impf[0].data, 95) impf[0].data[np.isnan(impf[0].data)] = np.nanmedian(impf[0].data) print np.percentile(impf[0].data, 5), np.percentile(impf[0].data, 95) im = plt.imshow(impf[0].data, vmin=np.percentile(impf[0].data, 5), vmax=np.percentile(impf[0].data, 95), origin="bottom") X = int(ma["X"][0]) Y = int(ma["Y"][0]) pra = int(pra) pdec = int(pdec) plt.scatter(X, Y, marker="o", s=100, facecolor="none", edgecolor="red") plt.colorbar(im) plt.savefig(os.path.join(plotdir, imname+".png"), dpi=200) plt.clf() zmin, zmax = zscale.zscale(impf[0].data.T[X-50:X+50,Y-50:Y+50].T) im = plt.imshow(impf[0].data.T[pra-50:pra+50,pdec-50:pdec+50].T, vmin=zmin, vmax=zmax, interpolation="none", origin="bottom", extent=(-50,50,-50,50)) c1 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=aperture_rad, label="Initial position") c11 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=sky_rad) c2 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=aperture_rad, label="Adjusted centroid") c22 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=sky_rad) plt.gca().add_artist(c1) plt.gca().add_artist(c11) plt.gca().add_artist(c2) plt.gca().add_artist(c22) plt.colorbar(im) myhandles = [] markers = ["o", "o"] labels = ["Initial position", "Adjusted centroid"] cols = ["k", "orange"] for i in np.arange(len(markers)): myhandles.append(mlines.Line2D([], [], mec=cols[i], mfc="none", marker=markers[i], ls="None", markersize=10, label=labels[i])) plt.legend(handles=myhandles, loc="lower left", labelspacing=0.3, fontsize=11, numpoints=1, frameon=False, ncol=5, bbox_to_anchor=(0.0, 0.00), fancybox=False, shadow=True) plt.title("MIN: %.0f MAX: %.0f"%(np.nanmin(impf[0].data.T[X-50:X+50,Y-50:Y+50]), np.nanmax(impf[0].data.T[X-50:X+50,Y-50:Y+50]))) plt.savefig(os.path.join(plotdir, imname+"_zoom.png")) plt.clf()
def get_app_phot(coords, image, plot_only=False, store=True, wcsin="world", fwhm=2.5, plotdir=None, box=15, arcsecpix=0.394): ''' coords: files: wcsin: can be "world", "logic" ''' # Load packages; splot is in the onedspec package, which is in noao. # The special keyword _doprint=0 turns off displaying the tasks # when loading a package. from pyraf import iraf if (not plot_only): iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) iraf.unlearn("apphot") imdir = os.path.dirname(image) imname = os.path.basename(image) if (plotdir is None): plotdir = os.path.join(imdir, "photometry") if not os.path.isdir(plotdir): os.makedirs(plotdir) out_name = os.path.join(plotdir, imname + ".seq.mag") clean_name = os.path.join(plotdir, imname + ".app.mag") print "Will create output files", out_name, clean_name # Read values from .ec file fwhm_value = fwhm/arcsecpix if (fitsutils.has_par(image, 'FWHM')): fwhm_value = fitsutils.get_par(image, 'FWHM')/arcsecpix elif (fwhm is None): fwhm_value=3.5/arcsecpix if (fitsutils.has_par(image, 'AIRMASS')): airmass_value = fitsutils.get_par(image, 'AIRMASS') else: airmass_value = 1.3 exptime = fitsutils.get_par(image, 'EXPTIME') gain = fitsutils.get_par(image, 'GAIN') noise = fitsutils.get_par(image, 'RDNOISE') print "FWHM: %.1f pixels, %.1f arcsec"%(fwhm_value, fwhm_value*arcsecpix) aperture_rad = math.ceil(float(fwhm_value)*1.5) # Set aperture radius to three times the PSF radius sky_rad= math.ceil(aperture_rad)*4 if (not plot_only): iraf.noao.digiphot.apphot.qphot(image = image,\ cbox = box ,\ annulus = sky_rad ,\ dannulus = 20. ,\ aperture = str(aperture_rad),\ coords = coords ,\ output = out_name ,\ plotfile = "" ,\ zmag = 0. ,\ exposure = "exptime",\ airmass = "airmass" ,\ filter = "filter" ,\ obstime = "DATE" ,\ #fwhm = fwhm_value,\ epadu = gain ,\ interactive = "no" ,\ radplots = "yes" ,\ verbose = "no" ,\ graphics = "stdgraph" ,\ display = "stdimage" ,\ icommands = "" ,\ wcsin = wcsin, wcsout = "logical", gcommands = "") #iraf.noao.digiphot.apphot.phot(image=image, cbox=5., annulus=12.4, dannulus=10., salgori = "centroid", aperture=9.3,wcsin="world",wcsout="tv", interac = "no", coords=coords, output=out_name) iraf.txdump(out_name, "id,image,xcenter,ycenter,xshift,yshift,fwhm,msky,stdev,cier,rapert,sum,area,nsky,flux,itime,mag,merr", "yes", Stdout=clean_name) ma = np.genfromtxt(clean_name, comments="#", dtype=[("id","<f4"), ("image","|S20"), ("X","<f4"), ("Y","<f4"), ("Xshift","<f4"), ("Yshift","<f4"),("fwhm","<f4"), ("msky","<f4"), ("stdev","<f4"),\ ("flags", np.int), ("rapert", "<f4"), ("sum", "<f4"), ("area", "<f4"), ("nsky","<f4") , ("flux", "<f4"), ("itime", "<f4"), ("fit_mag","<f4"), ("fiterr","<f4")]) if (ma.size > 0): m = ma[~np.isnan(ma["fit_mag"])] else: print "Only one object found!" m = np.array([ma]) hdulist = pf.open(image) prihdr = hdulist[0].header img = hdulist[0].data * 1. nx, ny = img.shape dimX = int(np.floor(np.sqrt(len(m)))) dimY = int(np.ceil(len(m)*1./dimX)) outerrad = sky_rad+10 cutrad = outerrad + 15 print "Cutrad %.1f"%cutrad plt.suptitle("FWHM=%.2f arcsec. %d stars"%(fwhm_value*arcsecpix, len(m))) for i in np.arange(dimX): for j in np.arange(dimY): if ( i*dimY + j < len(m)): k = i*dimY + j #print dimX, dimY, i, j, k ax = plt.subplot2grid((dimX,dimY),(i, j)) y1, y2, x1, x2 = m[k]["X"]-cutrad, m[k]["X"]+cutrad, m[k]["Y"]-cutrad, m[k]["Y"]+cutrad y1, y2, x1, x2 = int(y1), int(y2), int(x1), int(x2) y1 = np.maximum(y1, 0); y2=np.maximum(y2, 0); x1=np.maximum(x1, 0); x2 = np.maximum(x2, 0) try: zmin, zmax = zscale.zscale(img[x1:x2,y1:y2], nsamples=1000, contrast=0.25) except ValueError: print y1, y2, x1, x2 print img[x1:x2,y1:y2] sh= img[x1:x2,y1:y2].shape if sh[0]>0 and sh[1]>0: zmin = np.nanmin(img[x1:x2,y1:y2]) zmax = np.nanmax(img[x1:x2,y1:y2]) ax.imshow(img[x1:x2,y1:y2], aspect="equal", extent=(-cutrad, cutrad, -cutrad, cutrad), origin="lower", cmap=matplotlib.cm.gray_r, interpolation="none", vmin=zmin, vmax=zmax) c1 = plt.Circle( (0, 0), edgecolor="r", facecolor="none", radius=aperture_rad) c2 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=sky_rad) c3 = plt.Circle( (0, 0), edgecolor="yellow", facecolor="none", radius=sky_rad+20) plt.gca().add_artist(c1) plt.gca().add_artist(c2) plt.gca().add_artist(c3) ax.set_xticks([]) ax.set_yticks([]) plt.text(+5, +5, "%d"%m[k]["id"]) plt.text(-cutrad, -cutrad, "%.2f$\pm$%.2f"%(m[k]["fit_mag"], m[k]["fiterr"]), color="b") plt.tight_layout() plt.savefig(os.path.join(plotdir, imname + "plot.png")) plt.clf()
def get_fake_centroid_and_fluxcorr(filename, x, y, instrument, filt): """ Locate the center of a fake psf and determine the flux correction needed to bring it into alignment with the hstsnphot zero points. INPUTS: The fake-SN psf image in filename, the expected x,y position of the center of the psf, the instrument and filter being modeled. RETURNS: xcentroid, ycentroid, fluxcorr """ from pyraf import iraf if instrument == 'WFC3_IR': ZPT = ZPT_WFC3_IR_VEGA[filt] elif instrument == 'WFC3_UVIS': ZPT = ZPT_WFC3_UVIS_VEGA[filt] elif instrument.startswith('ACS'): ZPT = ZPT_ACS_VEGA[filt] iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) iraf.unlearn(iraf.apphot.phot) iraf.unlearn(iraf.datapars) iraf.unlearn(iraf.centerpars) #Use the centroid algorithm right now as it seems more robust to geometric distortion. iraf.centerpars.calgorithm = 'centroid' iraf.centerpars.cbox = 5.0 iraf.unlearn(iraf.fitskypars) iraf.unlearn(iraf.photpars) photparams = { 'interac': False, 'radplot': False, } iraf.datapars.readnoise = 0.0 iraf.datapars.itime = 1.0 iraf.datapars.epadu = 1.0 # iraf.digiphot.apphot.fitskypars : iraf.unlearn(iraf.fitskypars) iraf.fitskypars.salgorithm = 'constant' iraf.fitskypars.skyvalue = 0.0 # iraf.digiphot.apphot.photpars : iraf.unlearn(iraf.photpars) iraf.photpars.weighting = 'constant' iraf.photpars.apertures = APDEFAULTSIZE[instrument] * PIXSIZE[ instrument] / NATIVE_PIXSIZE[instrument] iraf.photpars.zmag = ZPT iraf.photpars.mkapert = False #Write the coordinate file starting as position x and y coxyfile = 'centroid.xycoo' coxy = open(coxyfile, 'w') print >> coxy, "%10.2f %10.2f" % (x, y) coxy.close() if os.path.exists('centroid.mag'): os.remove('centroid.mag') iraf.phot(image=filename, skyfile='', coords=coxyfile, output='centroid.mag', verify=False, verbose=True, Stdout=1, **photparams) f = open('centroid.mag', 'r') maglines = f.readlines() f.close() xcentroid = float(maglines[76].split()[0]) ycentroid = float(maglines[76].split()[1]) #Calculate the aperture correction necessary to match the observed aperture correction. meas_flux_frac = float(maglines[79].split()[1]) expected_flux_frac = 10.0**( -0.4 * APCOR[filt][APSIZE.index('%1i' % APDEFAULTSIZE[instrument])]) flux_corr = expected_flux_frac / meas_flux_frac return xcentroid, ycentroid, flux_corr
def phot(fits_filename, x_in, y_in, aperture=15, sky=20, swidth=10, apcor=0.3, maxcount=30000.0, exptime=1.0, zmag=None): """ Compute the centroids and magnitudes of a bunch sources detected on CFHT-MEGAPRIME images. Args: fits_filename: str The name of the file containing the image to be processed. Returns a MOPfiles data structure. """ if not hasattr(x_in, '__iter__'): x_in = [ x_in, ] if not hasattr(y_in, '__iter__'): y_in = [ y_in, ] if (not os.path.exists(fits_filename) and not fits_filename.endswith(".fits")): # For convenience, see if we just forgot to provide the extension fits_filename += ".fits" try: input_hdulist = fits.open(fits_filename) except Exception as err: logger.debug(str(err)) raise TaskError("Failed to open input image: %s" % err.message) ## get the filter for this image filter = input_hdulist[0].header.get('FILTER', 'DEFAULT') ### Some nominal CFHT zeropoints that might be useful zeropoints = { "I": 25.77, "R": 26.07, "V": 26.07, "B": 25.92, "DEFAULT": 26.0, "g.MP9401": 26.4 } if zmag is None: zmag = input_hdulist[0].header.get('PHOTZP', zeropoints[filter]) ### check for magic 'zeropoint.used' files zpu_file = "zeropoint.used" if os.access(zpu_file, os.R_OK): with open(zpu_file) as zpu_fh: zmag = float(zpu_fh.read()) else: zpu_file = "%s.zeropoint.used" % (fits_filename[0:-5]) if os.access(zpu_file, os.R_OK): with open(zpu_file) as zpu_fh: zmag = float(zpu_fh.read()) photzp = input_hdulist[0].header.get( 'PHOTZP', zeropoints.get(filter, zeropoints["DEFAULT"])) if zmag != photzp: logger.warning( "ZEROPOINT {} sent to DAOPHOT doesn't match PHOTZP {} in header". format(zmag, photzp)) ### setup IRAF to do the magnitude/centroid measurements iraf.set(uparm="./") iraf.digiphot() iraf.apphot() iraf.daophot(_doprint=0) iraf.photpars.apertures = aperture iraf.photpars.zmag = zmag iraf.datapars.datamin = 0 iraf.datapars.datamax = maxcount iraf.datapars.exposur = "" iraf.datapars.itime = exptime iraf.fitskypars.annulus = sky iraf.fitskypars.dannulus = swidth iraf.fitskypars.salgorithm = "mode" iraf.fitskypars.sloclip = 5.0 iraf.fitskypars.shiclip = 5.0 iraf.centerpars.calgori = "centroid" iraf.centerpars.cbox = 5. iraf.centerpars.cthreshold = 0. iraf.centerpars.maxshift = 2. iraf.centerpars.clean = 'no' iraf.phot.update = 'no' iraf.phot.verbose = 'no' iraf.phot.verify = 'no' iraf.phot.interactive = 'no' # Used for passing the input coordinates coofile = tempfile.NamedTemporaryFile(suffix=".coo", delete=False) for i in range(len(x_in)): coofile.write("%f %f \n" % (x_in[i], y_in[i])) # Used for receiving the results of the task # mag_fd, mag_path = tempfile.mkstemp(suffix=".mag") magfile = tempfile.NamedTemporaryFile(suffix=".mag", delete=False) # Close the temp files before sending to IRAF due to docstring: # "Whether the name can be used to open the file a second time, while # the named temporary file is still open, varies across platforms" coofile.close() magfile.close() os.remove(magfile.name) iraf.phot(fits_filename, coofile.name, magfile.name) pdump_out = ascii.read(magfile.name, format='daophot') if not len(pdump_out) > 0: mag_content = open(magfile.name).read() raise TaskError("photometry failed. {}".format(mag_content)) # Clean up temporary files generated by IRAF os.remove(coofile.name) os.remove(magfile.name) return pdump_out
def phot(fits_filename, x_in, y_in, aperture=15, sky=20, swidth=10, apcor=0.3, maxcount=30000.0, exptime=1.0, zmag=None, extno=0, centroid=True): """ Compute the centroids and magnitudes of a bunch sources on fits image. :rtype : astropy.table.Table :param fits_filename: Name of fits image to measure source photometry on. :type fits_filename: str :param x_in: x location of source to measure :type x_in: float, numpy.array :param y_in: y location of source to measure :type y_in: float, numpy.array :param aperture: radius of circular aperture to use. :type aperture: float :param sky: radius of inner sky annulus :type sky: float :param swidth: width of the sky annulus :type swidth: float :param apcor: Aperture correction to take aperture flux to full flux. :type apcor: float :param maxcount: maximum linearity in the image. :type maxcount: float :param exptime: exposure time, relative to zmag supplied :type exptime: float :param zmag: zeropoint magnitude :param extno: extension of fits_filename the x/y location refers to. """ if not hasattr(x_in, '__iter__'): x_in = [ x_in, ] if not hasattr(y_in, '__iter__'): y_in = [ y_in, ] if (not os.path.exists(fits_filename) and not fits_filename.endswith(".fits")): # For convenience, see if we just forgot to provide the extension fits_filename += ".fits" try: input_hdulist = fits.open(fits_filename) except Exception as err: logger.debug(str(err)) raise TaskError("Failed to open input image: %s" % err.message) # get the filter for this image filter_name = input_hdulist[extno].header.get('FILTER', 'DEFAULT') # Some nominal CFHT zeropoints that might be useful zeropoints = { "I": 25.77, "R": 26.07, "V": 26.07, "B": 25.92, "DEFAULT": 26.0, "g.MP9401": 32.0, 'r.MP9601': 31.9, 'gri.MP9603': 33.520 } if zmag is None: logger.warning( "No zmag supplied to daophot, looking for header or default values." ) zmag = input_hdulist[extno].header.get('PHOTZP', zeropoints[filter_name]) logger.warning("Setting zmag to: {}".format(zmag)) # check for magic 'zeropoint.used' files for zpu_file in [ "{}.zeropoint.used".format(os.path.splitext(fits_filename)[0]), "zeropoint.used" ]: if os.access(zpu_file, os.R_OK): with open(zpu_file) as zpu_fh: zmag = float(zpu_fh.read()) logger.warning("Using file {} to set zmag to: {}".format( zpu_file, zmag)) break photzp = input_hdulist[extno].header.get( 'PHOTZP', zeropoints.get(filter_name, zeropoints["DEFAULT"])) if zmag != photzp: logger.warning( ("zmag sent to daophot: ({}) " "doesn't match PHOTZP value in image header: ({})".format( zmag, photzp))) # setup IRAF to do the magnitude/centroid measurements iraf.set(uparm="./") iraf.digiphot() iraf.apphot() iraf.daophot(_doprint=0) iraf.photpars.apertures = aperture iraf.photpars.zmag = zmag iraf.datapars.datamin = 0 iraf.datapars.datamax = maxcount iraf.datapars.exposur = "" iraf.datapars.itime = exptime iraf.fitskypars.annulus = sky iraf.fitskypars.dannulus = swidth iraf.fitskypars.salgorithm = "mode" iraf.fitskypars.sloclip = 5.0 iraf.fitskypars.shiclip = 5.0 if centroid: iraf.centerpars.calgori = "centroid" iraf.centerpars.cbox = 5. iraf.centerpars.cthreshold = 0. iraf.centerpars.maxshift = 2. iraf.centerpars.clean = 'no' else: iraf.centerpars.calgori = "none" iraf.phot.update = 'no' iraf.phot.verbose = 'no' iraf.phot.verify = 'no' iraf.phot.interactive = 'no' # Used for passing the input coordinates coofile = tempfile.NamedTemporaryFile(suffix=".coo", delete=False) for i in range(len(x_in)): coofile.write("%f %f \n" % (x_in[i], y_in[i])) coofile.flush() # Used for receiving the results of the task # mag_fd, mag_path = tempfile.mkstemp(suffix=".mag") magfile = tempfile.NamedTemporaryFile(suffix=".mag", delete=False) # Close the temp files before sending to IRAF due to docstring: # "Whether the target_name can be used to open the file a second time, while # the named temporary file is still open, varies across platforms" coofile.close() magfile.close() os.remove(magfile.name) iraf.phot(fits_filename + "[{}]".format(extno), coofile.name, magfile.name) pdump_out = ascii.read(magfile.name, format='daophot') logging.debug("PHOT FILE:\n" + str(pdump_out)) if not len(pdump_out) > 0: mag_content = open(magfile.name).read() raise TaskError("photometry failed. {}".format(mag_content)) # apply the aperture correction pdump_out['MAG'] -= apcor # if pdump_out['PIER'][0] != 0 or pdump_out['SIER'][0] != 0 or pdump_out['CIER'][0] != 0: # raise ValueError("Photometry failed:\n {}".format(pdump_out)) # Clean up temporary files generated by IRAF os.remove(coofile.name) os.remove(magfile.name) logger.debug("Computed aperture photometry on {} objects in {}".format( len(pdump_out), fits_filename)) del input_hdulist return pdump_out
def Photometry(path): print "Starting Photometry..." print "This can take a long time. Go drink some coffee!" filename = path #raw_input("Path to the directory of the images: ") os.chdir(filename) iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) #Parameter settings iraf.centerpars.setParam('calgorithm', 'centroid', check=0, exact=0) iraf.centerpars.setParam('cbox', '10.0', check=0, exact=0) iraf.centerpars.saveParList(filename="center.par") iraf.centerpars.setParList(ParList="center.par") iraf.fitskypars.setParam('salgorithm', 'mode', check=0, exact=0) iraf.fitskypars.setParam('annulus', '7.0') iraf.fitskypars.setParam('dannulus', '2.0', check=0, exact=0) iraf.fitskypars.saveParList(filename='fits.par') iraf.fitskypars.setParList(ParList='fits.par') iraf.photpars.setParam('apertures', '4.5', check=0, exact=0) iraf.photpars.saveParList(filename='phot.par') iraf.photpars.setParList(ParList='phot.par') iraf.datapars.setParam('fwhm', 'INDEF', check=0, exact=0) iraf.datapars.setParam('sigma', 'INDEF', check=0, exact=0) iraf.datapars.setParam('datamin', 'INDEF', check=0, exact=0) iraf.datapars.setParam('datamax','INDEF', check=0, exact=0) iraf.datapars.setParam('ccdread','RESPONSE', check=0, exact=0) iraf.datapars.setParam('gain','EGAIN', check=0, exact=0) iraf.datapars.setParam('exposur','EXPTIME', check=0, exact=0) iraf.datapars.setParam('airmass','AIRMASS', check=0, exact=0) #iraf.datapars.setParam('xairmas','1.', check=0, exact=0) iraf.datapars.setParam('obstime','UTSHUT', check=0, exact=0) iraf.datapars.setParam('filter','FILTER', check=0, exact=0) iraf.datapars.saveParList(filename='data.par') iraf.datapars.setParList(ParList='data.par') #Takes the necessary parameters iraf.centerpars.getParam('calgorithm', native=1, mode="h", exact=0, prompt=0) iraf.centerpars.getParam('cbox', native=1, exact=0, prompt=0) iraf.fitskypars.getParam('salgorithm', native=1, mode="h", exact=0, prompt=0) iraf.fitskypars.getParam('annulus', native=1, exact=0, prompt=0) iraf.fitskypars.getParam('dannulus', native=1, exact=0, prompt=0) iraf.photpars.getParam('apertures', native=1, mode="h", exact=0, prompt=0) iraf.datapars.getParam('fwhm', native=0, mode='h', exact=0, prompt=0) iraf.datapars.getParam('sigma', native=0, mode="h", exact=0, prompt=0) iraf.datapars.getParam('datamin', native=0, mode="h", exact=0, prompt=0) iraf.datapars.getParam('datamax', native=0, mode="h", exact=0, prompt=0) iraf.datapars.getParam('ccdread', native=0, mode="h", exact=0, prompt=0) iraf.datapars.getParam('gain', native=0, mode="h", exact=0, prompt=0) iraf.datapars.getParam('exposur', native=0, mode="h", exact=0, prompt=0) iraf.datapars.getParam('airmass', native=0, mode="h", exact=0, prompt=0) iraf.datapars.getParam('xairmas', native=0, mode="h", exact=0, prompt=0) iraf.datapars.getParam('obstime', native=0, mode="h", exact=0, prompt=0) iraf.datapars.getParam('filter', native=0, mode="h", exact=0, prompt=0) #Removing existing files filelist1 = glob.glob("*.mag") filelist2 = glob.glob("*.magnitude") for f in filelist1: os.remove(f) for g in filelist2: os.remove(g) #Photometry print "Extracting magnitude and magnitude error from the files created by Pyraf" images = glob.glob("*.fits") for i in images: tr_coord = i + '.out.xy.done' iraf.phot(image = i, output = i + '.mag', coords = tr_coord, interactive = 'no' , wcsin = 'tv', skyfile = '', verify='no', verbose='no' ) #uses IRAF's pdump to take MAG values from the PHOT's output file photometry = glob.glob("*.mag") for j in photometry: iraf.pdump(j, 'MAG, MERR', 'yes', Stdout = j + 'nitude')
def main(argv): home_root = os.environ['HOME'] iraf.images(_doprint=0) iraf.tv(_doprint=0) iraf.ptools(_doprint=0) iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.photcal(_doprint=0) iraf.apphot(_doprint=0) iraf.imutil(_doprint=0) for file_ in os.listdir("./"): if file_.endswith("_i.fits"): fits_file_i = file_ title_string = file_[0:9] if file_.endswith("_g.fits"): fits_file_g = file_ fits_h_i = fits.open(fits_file_i) fits_h_g = fits.open(fits_file_g) fwhm_i = fits_h_i[0].header['FWHMPSF'] fwhm_g = fits_h_g[0].header['FWHMPSF'] print 'Image FWHM :: g = {0:5.3f} : i = {1:5.3f}'.format(fwhm_g,fwhm_i) # if not os.path.isdir('psf'): # make a psf subfolder if it doesn't exist # os.mkdir('psf') # # and then copy in the data files if they don't already exist as files # if not os.path.isfile('psf/'+fits_file_i): # iraf.images.imcopy(fits_file_i,'psf/'+fits_file_i,verbose="yes") # # if not os.path.isfile('psf/'+fits_file_g) : # iraf.images.imcopy(fits_file_g,'psf/'+fits_file_g,verbose="yes") # # # also copy in the apcor star lists # if not (os.path.isfile('psf/apcor_stars_i.txt') or os.path.islink('psf/apcor_stars_i.txt')) : # shutil.copyfile('apcor_stars_i.txt','psf/apcor_stars_i.txt') # # if not (os.path.isfile('psf/apcor_stars_g.txt') or os.path.islink('psf/apcor_stars_g.txt')) : # shutil.copyfile('apcor_stars_g.txt','psf/apcor_stars_g.txt') # # # and change to the psf folder # os.chdir('psf') iraf.unlearn(iraf.phot,iraf.datapars,iraf.photpars,iraf.centerpars,iraf.fitskypars) iraf.apphot.phot.setParam('interactive',"no") iraf.apphot.phot.setParam('verify',"no") iraf.datapars.setParam('datamax',"INDEF") iraf.datapars.setParam('gain',"gain") iraf.datapars.setParam('ccdread',"rdnoise") iraf.datapars.setParam('exposure',"exptime") iraf.datapars.setParam('airmass',"airmass") iraf.datapars.setParam('filter',"filter") iraf.datapars.setParam('obstime',"time-obs") iraf.datapars.setParam('sigma',"INDEF") iraf.photpars.setParam('zmag',0.) iraf.centerpars.setParam('cbox',9.) iraf.centerpars.setParam('maxshift',3.) iraf.fitskypars.setParam('salgorithm',"median") iraf.fitskypars.setParam('dannulus',10.) iraf.datapars.setParam('fwhmpsf',fwhm_g) iraf.photpars.setParam('apertures',iraf.nint(4.*fwhm_g)) iraf.fitskypars.setParam('annulus',(6.*fwhm_g)) iraf.apphot.phot(image=fits_file_g, coords='apcor_stars_g.txt', output="g_psfstars.mag.1") iraf.datapars.setParam('fwhmpsf',fwhm_i) iraf.photpars.setParam('apertures',iraf.nint(4.*fwhm_i)) iraf.fitskypars.setParam('annulus',(6.*fwhm_i)) iraf.apphot.phot(image=fits_file_i, coords='apcor_stars_i.txt', output="i_psfstars.mag.1") with open("apcor_stars_i.txt") as foo: lines = len(foo.readlines()) iraf.daophot(_doprint=0) iraf.pstselect.setParam('image',fits_file_i) iraf.pstselect.setParam('photfile',"i_psfstars.mag.1") iraf.pstselect.setParam('pstfile',"default") iraf.pstselect.setParam('maxnpsf',lines) iraf.pstselect.setParam('plottype',"mesh") iraf.daophot.pstselect(verify='no', mode='h') with open("apcor_stars_g.txt") as foo: lines = len(foo.readlines()) iraf.pstselect.setParam('image',fits_file_g) iraf.pstselect.setParam('photfile',"g_psfstars.mag.1") iraf.pstselect.setParam('pstfile',"default") iraf.pstselect.setParam('maxnpsf',lines) iraf.pstselect.setParam('plottype',"mesh") iraf.daophot.pstselect() iraf.datapars.setParam('datamax',"INDEF") iraf.datapars.setParam('gain',"gain") iraf.datapars.setParam('ccdread',"rdnoise") iraf.datapars.setParam('exposure',"exptime") iraf.datapars.setParam('airmass',"airmass") iraf.datapars.setParam('filter',"filter") iraf.datapars.setParam('obstime',"time-obs") iraf.datapars.setParam('sigma',"INDEF") iraf.daopars.setParam('psfrad',iraf.nint(3.*fwhm_i)) iraf.daopars.setParam('fitrad',fwhm_i) iraf.psf.setParam('image',fits_file_i) iraf.psf.setParam('photfile',"i_psfstars.mag.1") iraf.psf.setParam('pstfile',fits_file_i+".pst.1") iraf.psf.setParam('interactive', 'no') iraf.daophot.psf() iraf.psf.setParam('image',fits_file_g) iraf.psf.setParam('photfile',"g_psfstars.mag.1") iraf.psf.setParam('pstfile',fits_file_g+".pst.1") iraf.psf.setParam('interactive', 'no') iraf.daophot.psf()
def phot(fits_filename, x_in, y_in, aperture=15, sky=20, swidth=10, apcor=0.3, maxcount=30000.0, exptime=1.0): """ Compute the centroids and magnitudes of a bunch sources detected on CFHT-MEGAPRIME images. Args: fits_filename: str The name of the file containing the image to be processed. Returns a MOPfiles data structure. """ if (not os.path.exists(fits_filename) and not fits_filename.endswith(".fits")): # For convenience, see if we just forgot to provide the extension fits_filename += ".fits" try: input_hdulist = fits.open(fits_filename) except Exception as err: raise TaskError("Failed to open input image: %s" % err.message) ## get the filter for this image filter = input_hdulist[0].header.get('FILTER', 'DEFAULT') ### Some CFHT zeropoints that might be useful zeropoints = {"I": 25.77, "R": 26.07, "V": 26.07, "B": 25.92, "DEFAULT": 26.0, "g.MP9401": 26.4 } ### load the if not filter in zeropoints: filter = "DEFAULT" zmag = input_hdulist[0].header.get('PHOTZP', zeropoints[filter]) ### setup IRAF to do the magnitude/centroid measurements iraf.set(uparm="./") iraf.digiphot() iraf.apphot() iraf.daophot(_doprint=0) ### check for the magical 'zeropoint.used' file zpu_file = "zeropoint.used" if os.access(zpu_file, os.R_OK): with open(zpu_file) as zpu_fh: zmag = float(zpu_fh.read()) iraf.photpars.apertures = int(aperture) iraf.photpars.zmag = zmag iraf.datapars.datamin = 0 iraf.datapars.datamax = maxcount #iraf.datapars.exposur="EXPTIME" iraf.datapars.exposur = "" iraf.datapars.itime = exptime iraf.fitskypars.annulus = sky iraf.fitskypars.dannulus = swidth iraf.centerpars.calgori = "centroid" iraf.centerpars.cbox = 5. iraf.centerpars.cthreshold = 0. iraf.centerpars.maxshift = 2. iraf.centerpars.clean = 'no' iraf.phot.update = 'no' iraf.phot.verbose = 'no' iraf.phot.verify = 'no' iraf.phot.interactive = 'no' # Used for passing the input coordinates coofile = tempfile.NamedTemporaryFile(suffix=".coo", delete=False) coofile.write("%f %f \n" % (x_in, y_in)) # Used for receiving the results of the task # mag_fd, mag_path = tempfile.mkstemp(suffix=".mag") magfile = tempfile.NamedTemporaryFile(suffix=".mag", delete=False) # Close the temp files before sending to IRAF due to docstring: # "Whether the name can be used to open the file a second time, while # the named temporary file is still open, varies across platforms" coofile.close() magfile.close() os.remove(magfile.name) iraf.phot(fits_filename, coofile.name, magfile.name) pdump_out = iraf.pdump(magfile.name, "XCENTER,YCENTER,MAG,MERR,ID,XSHIFT,YSHIFT,LID", "MERR < 0.4 && MERR != INDEF && MAG != INDEF && PIER==0", header='no', parameters='yes', Stdout=1) os.remove(coofile.name) os.remove(magfile.name) ### setup the mop output file structure hdu = {} hdu['header'] = {'image': input_hdulist, 'aper': aperture, 's_aper': sky, 'd_s_aper': swidth, 'aper_cor': apcor, 'zeropoint': zmag} hdu['order'] = ['X', 'Y', 'MAG', 'MERR', 'ID', 'XSHIFT', 'YSHIFT', 'LID'] hdu['format'] = {'X': '%10.2f', 'Y': '%10.2f', 'MAG': '%10.2f', 'MERR': '%10.2f', 'ID': '%8d', 'XSHIFT': '%10.2f', 'YSHIFT': '%10.2f', 'LID': '%8d'} hdu['data'] = {} for col in hdu['order']: hdu['data'][col] = [] for line in pdump_out: values = line.split() for col in hdu['order']: if re.match('\%.*f', hdu['format'][col]): if col == 'MAG': values[0] = float(values[0]) - float(apcor) hdu['data'][col].append(float(values.pop(0))) elif re.match('\%.*d', hdu['format'][col]): hdu['data'][col].append(int(values.pop(0))) else: hdu['data'][col].append(values.pop(0)) # Clean up temporary files generated by IRAF os.remove("datistabe.par") os.remove("datpdump.par") return hdu
def get_app_phot_target(image, plot=False, store=True, wcsin="logical", fwhm=2, box=4, ra=None, dec=None): ''' coords: files: wcsin: can be "world", "logic" ''' # Load packages; splot is in the onedspec package, which is in noao. # The special keyword _doprint=0 turns off displaying the tasks # when loading a package. with warnings.catch_warnings(): warnings.simplefilter("ignore") fxn() iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) iraf.unlearn("apphot") impf = pf.open(image) wcs = pywcs.WCS(impf[0].header) #Check that actually the object is within this frame. if (ra is None or dec is None): if (fitsutils.has_par(image, "OBJRA") and fitsutils.has_par(image, "OBJRA")): ra, dec = cc.hour2deg(fitsutils.get_par(image, 'OBJRA'), fitsutils.get_par(image, 'OBJDEC')) else: ra, dec = cc.hour2deg(fitsutils.get_par(image, 'RA'), fitsutils.get_par(image, 'DEC')) pra, pdec = get_xy_coords(image, ra, dec) else: if(wcsin == "logical"): pra, pdec = ra, dec else: #Using new method to derive the X, Y pixel coordinates, as pywcs does not seem to be working well. pra, pdec = get_xy_coords(image, ra, dec) #pra, pdec = wcs.wcs_sky2pix(ra, dec, 1) #pra, pdec = wcs.wcs_sky2pix(np.array([ra, dec], ndmin=2), 1)[0] shape = impf[0].data.shape if (pra > 0) and (pra < shape[0]) and (pdec > 0) and (pdec < shape[1]): pass else: print image, "ERROR! Object coordinates are outside this frame. Skipping any aperture photometry!!" print pra, pdec, shape return imdir = os.path.dirname(image) imname = os.path.basename(image) plotdir = os.path.join(imdir, "photometry") if not os.path.isdir(plotdir): os.makedirs(plotdir) out_name = os.path.join(plotdir, imname + ".seq.mag") clean_name = os.path.join(plotdir, imname + ".objapp.mag") fwhm_value = fwhm nsrc, fwhm_value, ellip = sextractor.get_image_pars(image) if np.isnan(fwhm_value): fwhm_value=99 fitsutils.update_par(image, 'FWHM', fwhm_value) if (fitsutils.has_par(image, 'AIRMASS')): airmass_value = fitsutils.get_par(image, 'AIRMASS') else: airmass_value = 1.3 exptime = fitsutils.get_par(image, 'EXPTIME') gain = fitsutils.get_par(image, 'GAIN') #print "FWHM", fwhm_value aperture_rad = math.ceil(float(fwhm_value)*3) # Set aperture radius to three times the PSF radius sky_rad= math.ceil(aperture_rad*4) #print aperture_rad, sky_rad print "Saving coodinates for the object in pixels",pra,pdec coords = "/tmp/coords.dat" np.savetxt("/tmp/coords.dat", np.array([[pra, pdec]]), fmt="%.4f %.4f") if (plot): zmin, zmax = zscale.zscale(impf[0].data) im = plt.imshow(impf[0].data, vmin=zmin, vmax=zmax, origin="bottom") plt.scatter(pra, pdec, marker="o", s=100, facecolor="none") plt.savefig(os.path.join(plotdir, imname+".png")) plt.clf() if os.path.isfile(out_name): os.remove(out_name) if os.path.isfile(clean_name): os.remove(clean_name) iraf.noao.digiphot.apphot.qphot(image = image,\ cbox = box ,\ annulus = sky_rad ,\ dannulus = 15. ,\ aperture = str(aperture_rad),\ coords = coords ,\ output = out_name ,\ plotfile = "" ,\ zmag = 0. ,\ exposure = "exptime" ,\ airmass = "airmass" ,\ filter = "filter" ,\ obstime = "DATE" ,\ epadu = gain ,\ interactive = "no" ,\ radplots = "yes" ,\ verbose = "no" ,\ graphics = "stdgraph" ,\ display = "stdimage" ,\ icommands = "" ,\ wcsin = "logical", wcsout = "logical", gcommands = "") #iraf.noao.digiphot.apphot.phot(image=image, cbox=5., annulus=12.4, dannulus=10., salgori = "centroid", aperture=9.3,wcsin="world",wcsout="tv", interac = "no", coords=coords, output=out_name) iraf.txdump(out_name, "id,image,xcenter,ycenter,xshift,yshift,fwhm,msky,stdev,mag,merr", "yes", Stdout=clean_name) ma = np.genfromtxt(clean_name, comments="#", dtype=[("id","<f4"), ("image","|S20"), ("X","<f4"), ("Y","<f4"), ("Xshift","<f4"), ("Yshift","<f4"),("fwhm","<f4"), ("ph_mag","<f4"), ("stdev","<f4"), ("fit_mag","<f4"), ("fiterr","<f4")]) if (ma.size > 0): if (ma.size==1): ma = np.array([ma]) m = ma[~np.isnan(ma["fit_mag"])] else: print "Only one object found!" m = np.array([ma]) insmag = np.round(ma['fit_mag'][0] , 3) insmagerr = np.round(ma['fiterr'][0], 3) if (fitsutils.has_par(image, "ZEROPT")): mag = insmag + float(fitsutils.get_par(image, "ZEROPT")) magerr = np.sqrt(insmagerr**2+ float(fitsutils.get_par(image, "ZEROPTU"))**2) if np.isnan(mag): mag, magerr = 0, 0 insmag, insmagerr = 0,0 fitsutils.update_par(image, "INSMAG", "%.3f"%insmag ) fitsutils.update_par(image, "INSMAGER", "%.3f"%insmagerr) fitsutils.update_par(image, "APPMAG", np.round(mag, 3) ) fitsutils.update_par(image, "APPMAGER", np.round(magerr, 3)) if (plot): X = int(ma["X"][0]) Y = int(ma["Y"][0]) pra = int(pra) pdec = int(pdec) plt.scatter(X, Y, marker="o", s=100, facecolor="none", edgecolor="red") plt.colorbar(im) plt.savefig(os.path.join(plotdir, imname+".png")) plt.clf() zmin, zmax = zscale.zscale(impf[0].data.T[X-50:X+50,Y-50:Y+50].T) im = plt.imshow(impf[0].data.T[pra-50:pra+50,pdec-50:pdec+50].T, vmin=zmin, vmax=zmax, interpolation="none", origin="bottom", extent=(-50,50,-50,50)) c1 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=aperture_rad, label="Initial position") c11 = plt.Circle( (pra-X, pdec-Y), edgecolor="k", facecolor="none", radius=sky_rad) c2 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=aperture_rad, label="Adjusted centroid") c22 = plt.Circle( (0, 0), edgecolor="orange", facecolor="none", radius=sky_rad) plt.gca().add_artist(c1) plt.gca().add_artist(c11) plt.gca().add_artist(c2) plt.gca().add_artist(c22) plt.colorbar(im) myhandles = [] markers = ["o", "o"] labels = ["Initial position", "Adjusted centroid"] cols = ["k", "orange"] for i in np.arange(len(markers)): myhandles.append(mlines.Line2D([], [], mec=cols[i], mfc="none", marker=markers[i], ls="None", markersize=10, label=labels[i])) plt.legend(handles=myhandles, loc="lower left", labelspacing=0.3, fontsize=11, numpoints=1, frameon=False, ncol=5, bbox_to_anchor=(0.0, 0.00), fancybox=False, shadow=True) plt.title("MIN: %.0f MAX: %.0f"%(np.nanmin(impf[0].data.T[X-50:X+50,Y-50:Y+50]), np.nanmax(impf[0].data.T[X-50:X+50,Y-50:Y+50]))) plt.savefig(os.path.join(plotdir, imname+"_zoom.png")) plt.clf()
#! /usr/local/bin/python import os, sys import numpy as np from pyraf import iraf from odi_calibrate import download_sdss, js_calibrate from sdss_fit import getVabs from collections import OrderedDict iraf.images(_doprint=0) iraf.tv(_doprint=0) iraf.ptools(_doprint=0) iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.photcal(_doprint=0) iraf.apphot(_doprint=0) iraf.imutil(_doprint=0) def getHImass(object, dm): # print object, mpc uchvcdb = os.path.dirname(os.path.abspath(__file__))+'/predblist.sort.csv' name, mass = np.loadtxt(uchvcdb, usecols=(1,6), dtype=str, delimiter=',', unpack=True) # find the right row coord = [i for i,this in enumerate(mass) if object.upper() in name[i]][0] # print 'the HI mass of', name[coord], 'is', mass[coord], 'at 1 Mpc' # mpc = mpc/1000. mpc = pow(10,((dm + 5.)/5.))/1000000. logm = float(mass[coord]) mass = mpc*mpc*10**logm # make sure to scale by the distance in Mpc^2 print '{:3.1f}'.format(np.log10(mass))
def build(f): ### is this an MEF file current_ext = 0 NEXTEND = 0 EXTEND = f[0].header['EXTEND'] if (EXTEND == "T"): NEXTEND = f[0].header['NEXTEND'] current_ext = 1 ### create the name of the output MEF psf file if not f[0].header.has_key('FILENAME'): os.unlink(opt.filename) sys.exit('The fits file ' + opt.filename + ' has no EXPNUM keyword\n') sexp = f[0].header['FILENAME'] mef_psf = sexp + "p_psf_iraf.fits" ### create an MEF file that will contian the PSF(s) import pyfits fitsobj = pyfits.HDUList() prihdu = pyfits.PrimaryHDU() import re prihdu.header.update('FILENAME', sexp, comment='CFHT Exposure Numebr') prihdu.header.update('NEXTEND', NEXTEND, comment='number of extensions') version = re.match(r'\$Rev.*: (\d*.\d*) \$', __Version__).group(1) prihdu.header.update('MKPSF_V', float(version), comment="Version number of mkpsf") fitsobj.append(prihdu) if os.access(mef_psf, os.F_OK): os.unlink(mef_psf) fitsobj.writeto(mef_psf) fitsobj.close() outfits = pyfits.open(mef_psf, "append") prihdr = outfits[0].header import jjkmode ### Get my python routines from pyraf import iraf from pyraf.irafpar import IrafParList ### keep all the parameters locally cached. iraf.set(uparm="./") ### Load the required IRAF packages iraf.digiphot() iraf.apphot() iraf.daophot() ### temp file name hash. tfile = {} while (current_ext <= NEXTEND): ### this is a psf SCRIPT so the showplots and interactive are off by force print "Working on image section " + str(current_ext) iraf.findpars.sharplo = 0 iraf.findpars.sharphi = 0.7 iraf.findpars.roundlo = -0.7 iraf.findpars.roundhi = 0.7 iraf.datapars.datamax = 20000 iraf.datapars.airmass = 'AIRMASS' iraf.datapars.filter = 'FILTER' iraf.datapars.obstime = 'TIME-OBS' iraf.datapars.exposure = 'EXPTIME' iraf.datapars.gain = 'GAIN' iraf.datapars.ccdread = 'RDNOISE' iraf.datapars.fwhmpsf = opt.fwhm iraf.daopars.nclean = 2 iraf.daopars.psfrad = 5.0 * opt.fwhm iraf.daopars.fitrad = 0.85 * opt.fwhm iraf.daopars.function = "gauss" iraf.centerpars.calgorithm = 'centroid' zero_mag = 26.19 iraf.photpars.zmag = zero_mag iraf.photpars.apertures = int(0.85 * opt.fwhm) iraf.fitskypars.annulus = 2 + int(opt.fwhm * 4.00) iraf.fitskypars.dannulus = int(opt.fwhm * 2.0) iraf.daophot.verbose = no iraf.daophot.verify = no iraf.daophot.update = no iraf.psf.interactive = no iraf.pstselect.interactive = no iraf.datapars.saveParList() iraf.fitskypars.saveParList() iraf.centerpars.saveParList() iraf.findpars.saveParList() iraf.photpars.saveParList() tfiles = [ 'coo_bright', 'coo_ok', 'coo_faint', 'mag_all', 'mag_bright', 'mag_ok', 'mag_good', 'mag_best', 'pst_in', 'pst_out', 'pst_out2', 'prf', 'psg_org', 'psg', 'psf_1.fits', 'psf_2.fits', 'psf_final.fits', 'psf_3.fits', 'psf_4.fits', 'mag_pst', 'coo_pst', 'nst', 'nrj', 'seepsf.fits', 'sub.fits', 'fwhm', 'apcor' ] for file in tfiles: extname = "chip" + str(f[current_ext].header.get( 'IMAGEID', str(current_ext))).zfill(2) tfile[file] = sexp + "_" + extname + "." + file if (os.access(tfile[file], os.F_OK)): os.unlink(tfile[file]) if (EXTEND == "T"): this_image = opt.filename + "[" + extname + "]" else: this_image = opt.filename gain = f[current_ext].header.get('GAIN', 1.0) #### set sky/sigma parameters specific to this frame. (skyvalue, sigma) = jjkmode.stats(f[current_ext].data) import math sigma = math.sqrt(skyvalue / gain) datamin = float(skyvalue) - 8.0 * float(sigma) print "Determined sky level to be " + str(skyvalue) + " +/-" + str( sigma) iraf.datapars.datamin = datamin iraf.datapars.sigma = float(sigma) iraf.datapars.saveParList() iraf.fitskypars.skyvalue = skyvalue iraf.fitskypars.saveParList() ### find the bright stars in the image. print "sextracting for stars in " + this_image ###iraf.daophot.daofind(image=this_image, ### output=tfile['coo_bright'],threshold=4.0) os.system( "sex -c /home/cadc/kavelaar/12kproc/config/default.sex -SATUR_LEVEL 25000 -CATALOG_NAME " + tfile['coo_bright'] + " " + this_image) ### print "finding stellar locus in sround/ground space" print "clipping using star_class > 0.85 " fcoo = open(tfile['coo_bright'], 'r') lines = fcoo.readlines() fcoo.close() import numarray, math fout = open(tfile['coo_ok'], 'w') for line in lines: if re.match(r'^#', line) or re.search(r'INDEF', line): continue values = line.split() star_class = float(values[2]) if star_class > 0.75: fout.write(line) fout.close() print "Measuring photometry for psf candidate stars in " + tfile[ 'coo_ok'] iraf.daophot.phot(image=this_image, coords=tfile['coo_ok'], output=tfile['mag_bright']) ### do this selection in 2 steps because of the way IRAF handles INDEFs print "Selecting stars that have good centroids and magnitudes " iraf.pselect( tfile['mag_bright'], tfile['mag_ok'], "(CIER==0)&&(PIER==0)&&(SIER==0)&&(MSKY>0)&&(MSKY<2e5)&&(MSKY!=INDEF)" ) print "Selecting stars that have normal sky levels" condition = "(abs(MSKY -" + str(skyvalue) + ") < 5.0*" + str( sigma) + ")" iraf.pselect(tfile['mag_ok'], tfile['mag_good'], condition) a = iraf.txdump(tfile['mag_good'], "SSKEW", iraf.yes, Stdout=1) aa = [] for v in a: aa.append(float(v)) a = numarray.array(aa) mean = a.mean() aa = a * a stddev = math.sqrt(aa.sum() / len(aa) - mean**2) limit = mean + 2 * stddev os.unlink(tfile['mag_good']) condition = condition + " && SSKEW < " + str(limit) iraf.pselect(tfile['mag_ok'], tfile['mag_good'], condition) print "Choosing the psf stars" iraf.pstselect(image=this_image, photfile=tfile['mag_good'], pstfile=tfile['pst_in'], maxnpsf=25) ## construct an initial PSF image print "computing psf with neighbor stars based on complete star list" iraf.psf.mode = 'a' iraf.psf(image=this_image, photfile=tfile['mag_bright'], pstfile=tfile['pst_in'], psfimage=tfile['psf_1.fits'], opstfile=tfile['pst_out'], groupfile=tfile['psg_org'], varorder=0) try: print "subtracting the psf neighbors and placing the results in " + tfile[ 'sub.fits'] iraf.daophot.nstar(image=this_image, groupfile=tfile['psg_org'], psfimage=tfile['psf_1.fits'], nstarfile=tfile['nst'], rejfile=tfile['nrj']) iraf.daophot.substar(image=this_image, photfile=tfile['nst'], exfile=tfile['pst_in'], psfimage=tfile['psf_1.fits'], subimage=tfile['sub.fits']) a = iraf.daophot.txdump(tfile['nst'], 'chi', 'yes', Stdout=1) aa = [] for v in a: aa.append(float(v)) a = numarray.array(aa) mean = a.mean() aa = a * a stddev = math.sqrt(aa.sum() / len(aa) - mean**2) limit = mean + 2.5 * stddev print "Selecting those psf stars with CHI^2 <" + str( limit) + " after fitting with trial psf" iraf.pselect(tfile['nst'], tfile['mag_best'], "CHI < " + str(limit)) os.unlink(tfile['pst_out']) ## os.unlink(tfile['psg']) ## rebuild the PSF file with the psf stars that fit well.. ## using the neighbor subtracted image print "Rebuilding the PSF" iraf.daophot.psf(image=tfile['sub.fits'], photfile=tfile['mag_best'], pstfile=tfile['pst_in'], psfimage=tfile['psf_2.fits'], opstfile=tfile['pst_out'], groupfile=tfile['psg'], varorder=0) print "re-subtracting with rebuilt psf" os.unlink(tfile['nst']) os.unlink(tfile['nrj']) iraf.daophot.nstar(image=this_image, groupfile=tfile['psg'], psfimage=tfile['psf_2.fits'], nstarfile=tfile['nst'], rejfile=tfile['nrj']) os.unlink(tfile['sub.fits']) iraf.daophot.substar(image=this_image, photfile=tfile['nst'], exfile=tfile['pst_in'], psfimage=tfile['psf_2.fits'], subimage=tfile['sub.fits']) os.unlink(tfile['psg']) os.unlink(tfile['pst_out']) iraf.daophot.psf(image=tfile['sub.fits'], photfile=tfile['mag_best'], pstfile=tfile['pst_in'], psfimage=tfile['psf_3.fits'], opstfile=tfile['pst_out'], groupfile=tfile['psg'], varorder=0) os.unlink(tfile['nrj']) os.unlink(tfile['nst']) iraf.daophot.nstar(image=this_image, groupfile=tfile['psg'], psfimage=tfile['psf_3.fits'], nstarfile=tfile['nst'], rejfile=tfile['nrj']) a = iraf.daophot.txdump(tfile['nst'], 'chi', 'yes', Stdout=1) aa = [] for v in a: aa.append(float(v)) a = numarray.array(aa) mean = a.mean() aa = a * a stddev = math.sqrt(aa.sum() / len(aa) - mean**2) limit = mean + 2 * stddev limit = 2.0 #print "Selecting those psf stars with CHI^2 < "+str(limit)+" after fit with GOOD psf" os.unlink(tfile['mag_best']) iraf.pselect(tfile['nst'], tfile['mag_best'], "CHI < " + str(limit)) print "Building final PSF.... " os.unlink(tfile['sub.fits']) iraf.daophot.substar(image=this_image, photfile=tfile['nst'], exfile=tfile['pst_in'], psfimage=tfile['psf_3.fits'], subimage=tfile['sub.fits']) os.unlink(tfile['psg']) os.unlink(tfile['pst_out']) iraf.daophot.psf(image=tfile['sub.fits'], photfile=tfile['mag_best'], pstfile=tfile['pst_in'], psfimage=tfile['psf_final.fits'], opstfile=tfile['pst_out'], groupfile=tfile['psg'], varorder=0) print "building an analytic psf for the FWHM calculations" os.unlink(tfile['pst_out']) os.unlink(tfile['psg']) iraf.daophot.psf(image=tfile['sub.fits'], photfile=tfile['mag_best'], pstfile=tfile['pst_in'], psfimage=tfile['psf_4.fits'], opstfile=tfile['pst_out'], groupfile=tfile['psg'], varorder=-1) except: print sys.exc_info()[1] print "ERROR: Reverting to first pass psf" tfile['psf_final.fits'] = tfile['psf_1.fits'] iraf.daophot.psf(image=this_image, photfile=tfile['mag_best'], pstfile=tfile['pst_in'], psfimage=tfile['psf_4.fits'], opstfile=tfile['pst_out2'], groupfile=tfile['psg'], varorder=-1) psf_ap = iraf.photpars.apertures ap1 = int(psf_ap) ap2 = int(4.0 * opt.fwhm) apcor = "INDEF" aperr = "INDEF" if (0): #try ### now that we have the psf use the output list of psf stars ### to compute the aperature correction lines = iraf.txdump(tfile['pst_out'], 'xcen,ycen,mag,id', iraf.yes, Stdout=tfile['coo_pst']) ## set the lower ap value for the COG (normally set to 2) if (ap1 < 3): smallap = 1 else: smallap = 2 - ap1 + 1 ap1 = 2 ap2 = int(math.floor(4.0 * opt.fwhm)) naperts = ap2 - ap1 + 1 iraf.photpars.apertures = str(ap1) + ":" + str(ap2) + ":1" iraf.photpars.saveParList() iraf.daophot.phot(image=this_image, coords=tfile['coo_pst'], output=tfile['mag_pst']) iraf.photcal() iraf.photcal.mkapfile(tfile['mag_pst'], naperts=naperts, apercors=tfile['apcor'], smallap=smallap, verify='no', gcommands='', interactive=0) fin = open(tfile['apcor'], 'r') lines = fin.readlines() values = lines[2].split() apcor = values[1] aperr = values[2] #except: ## compute the FWHM of the PSF image using the analytic PSF (VarOrd=-1) psf_file = pyfits.open(tfile['psf_4.fits']) fwhm = (psf_file[0].header.get('PAR1', 99.0) + psf_file[0].header.get('PAR2', 99.0)) psf_file.close() # ## Open the psf.fits infits = pyfits.open(tfile['psf_final.fits']) hdu = infits[0] inhdu = hdu.header inhdu.update('XTENSION', 'IMAGE', before='SIMPLE') inhdu.update('PCOUNT', 0, after='NAXIS2') inhdu.update('GCOUNT', 1, after='PCOUNT') del hdu.header['SIMPLE'] del hdu.header['EXTEND'] inhdu.update("EXTNAME", extname, comment="image extension identifier") #inhdu.update("SLOW",slow,comment="SROUND low cutoff") #inhdu.update("SIGH",sigh,comment="SROUND high cutoff") inhdu.update("PFWHM", fwhm, comment="FWHM of stars based on PSF fitting") inhdu.update("ZMAG", zero_mag, comment="ZMAG of PSF ") inhdu.update("BCKG", skyvalue, comment="Mean sky level in counts") inhdu.update("BCKG_STD", sigma, comment="standard deviation of sky in counts") inhdu.update("AP1", psf_ap, comment="Apperture used for PSF flux") inhdu.update("AP2", ap2, comment="Full Flux aperture") inhdu.update("APCOR", apcor, comment="Apperture correction (ap1->ap2)") inhdu.update("APERR", apcor, comment="Uncertainty in APCOR") # ### append this psf to the output images.... print "Sticking this PSF onto the output file" f[current_ext].header.update( "PFWHM", fwhm, comment="FWHM of stars based on PSF fitting") f[current_ext].header.update("BCKG", skyvalue, comment="Mean sky level in counts") f[current_ext].header.update( "BCKG_STD", sigma, comment="Standard deviation of sky in counts") f.flush() outfits.append(hdu) outfits.flush() infits.close() ### remove the temp file we used for this computation. for tf in tfile.keys(): if os.access(tfile[tf], os.F_OK): os.unlink(tfile[tf]) current_ext = current_ext + 1 outfits.close() return mef_psf
def runApphot(imfile, coofile=None, magfile=None): """ use iraf.digiphot.apphot to collect aperture photometry """ import pyraf from pyraf import iraf import os import pyfits if not coofile: coofile = imfile.replace('.fits', '.coo') if not magfile: magfile = imfile.replace('.fits', '.mag') iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) if os.path.exists(magfile): os.remove(magfile) hdr = pyfits.getheader(imfile) if 'FILTER' in hdr: filt = hdr['FILTER'] filtkey = 'FILTER' elif 'FILTER1' in hdr: filt = hdr['FILTER1'] filtkey = 'FILTER1' if filt.startswith('CLEAR'): filt = hdr['FILTER2'] filtkey = 'FILTER2' if filt not in APCOR.keys(): return ([]) # iraf.digiphot.apphot.datapars : iraf.datapars.scale = 1.0 iraf.datapars.fwhmpsf = 2.5 iraf.datapars.emission = True iraf.datapars.sigma = 'INDEF' iraf.datapars.datamin = 'INDEF' iraf.datapars.datamax = 'INDEF' iraf.datapars.noise = 'constant' iraf.datapars.ccdread = '' iraf.datapars.gain = '' iraf.datapars.readnoise = 0.0 iraf.datapars.epadu = 1.0 iraf.datapars.exposure = '' iraf.datapars.airmass = '' iraf.datapars.filter = filt iraf.datapars.obstime = '' iraf.datapars.itime = 1.0 iraf.datapars.xairmass = 'INDEF' iraf.datapars.ifilter = 'INDEF' iraf.datapars.otime = 'INDEF' # iraf.digiphot.apphot.centerpars : iraf.unlearn(iraf.centerpars) iraf.centerpars.calgorithm = 'centroid' iraf.centerpars.cbox = 3.0 iraf.centerpars.cthreshold = 0.0 iraf.centerpars.minsnratio = 1.0 iraf.centerpars.cmaxiter = 10.0 iraf.centerpars.maxshift = 1.0 iraf.centerpars.clean = False iraf.centerpars.rclean = 1.0 iraf.centerpars.rclip = 2.0 iraf.centerpars.kclean = 3.0 iraf.centerpars.mkcenter = False # iraf.digiphot.apphot.fitskypars : iraf.unlearn(iraf.fitskypars) iraf.fitskypars.salgorithm = 'median' iraf.fitskypars.annulus = 25.0 iraf.fitskypars.dannulus = 40.0 iraf.fitskypars.skyvalue = 0.0 iraf.fitskypars.smaxiter = 10.0 iraf.fitskypars.sloclip = 0.0 iraf.fitskypars.shiclip = 0.0 iraf.fitskypars.snreject = 50.0 iraf.fitskypars.sloreject = 3.0 iraf.fitskypars.shireject = 3.0 iraf.fitskypars.khist = 3.0 iraf.fitskypars.binsize = 0.1 iraf.fitskypars.smooth = False iraf.fitskypars.rgrow = 0.0 iraf.fitskypars.mksky = False # iraf.digiphot.apphot.photpars : iraf.unlearn(iraf.photpars) iraf.photpars.weighting = 'constant' iraf.photpars.apertures = '2,3,4,5' iraf.photpars.zmag = ZPT[filt] iraf.photpars.mkapert = False iraf.unlearn(iraf.phot) iraf.gflush() iraf.gflush() iraf.flpr() iraf.flpr() iraf.flpr() photparams = { 'interac': False, 'radplot': False, } outputstuff = iraf.phot(image=imfile, skyfile='', coords=coofile, output=magfile, verify=False, verbose=True, Stdout=1, **photparams) sourcelist = apphotOutput(magfile) return (sourcelist)
def unisf(image, fwhm, sigma, thold, zmin='INDEF', zmax='INDEF', path=None, outfile=None): ''' Dada una lista de imágenes, identifica las estrellas de campo y genera un archivo de coordenadas. INPUT image : Archivo .in con los nombres de las imágenes, o la imagen simple. fwmh : Ancho a mitad de intensidad máxima de las estrellas. sigma : Desviación estándar del cielo. thold : Amplitud mínima por encima del valor de fondo. (zmin) : Valor mínimo de cuentas a considerar. (zmax) : Valor máximo de cuentas a considerar. (path) : String que indica el camino a la imagen. (outfile) : Nombre del archivo de salida. Por defecto usa el nombre de la imagen y agrega un .coo . --------------------------------------- Given a images list, identifies field stars and generates a coordinates file. INPUT image : .in file with the name of each image, or a single image. fwmh : Full width half maximum of intensity of stars. sigma : Standard deviation of the sky. thold : Minimum amplitude above background level. (zmin) : Minimum counts value to consider. (zmax) : Maximum counts value to consider. (path) : String that indicates the path do the images. (outfile) : Name of the output file, including .coo. By default, uses image name and adds .coo extension. ''' if path is not None: originalpath = aux.chdir(path, save=True) iraf.noao() #loads noao iraf.digiphot() #loads digiphot iraf.apphot() #loads apphot iraf.unlearn(iraf.daofind) iraf.unlearn(iraf.datapars) iraf.unlearn(iraf.findpars) #unlearns every task required iraf.datapars.fwhmpsf = fwhm iraf.datapars.sigma = sigma iraf.datapars.datamin = zmin iraf.datapars.datamax = zmax #sets meaningful parameters iraf.findpars.thresho = thold #sets meaningful threshold iraf.daofind.verify = "no" #avoid checking parameters given aux.default(outfile, image + '.coo', borrar=True) iraf.daofind.output = outfile iraf.daofind.image = image iraf.daofind() #execute iraf task 'daofind' if path is not None: aux.chdir(originalpath)
def plant_kbos(filename, psf, kbos, shifts, prefix): """ Add KBOs to an image :param filename: name of the image to add KBOs to :param psf: Point Spread Function in IRAF/DAOPHOT format, used by ADDSTAR :param kbos: list of KBOs to add, has format as returned by KBOGenerator :param shifts: dictionary to transform coordinates to reference frame. :param prefix: an estimate FWHM of the image, used to determine trailing. :return: None """ iraf.set(uparm="./") iraf.digiphot() iraf.apphot() iraf.daophot(_doprint=0) iraf.images() if shifts['nmag'] < 4: logging.warning("Mag shift based on fewer than 4 common stars.") fd = open("plant.WARNING", 'a') fd.write("Mag shift based on fewer than 4 common stars.") fd.close() if shifts['emag'] > 0.05: logging.warning("Mag shift has large uncertainty.") fd = open("plant.WARNING", 'a') fd.write("Mag shift hsa large uncertainty.") fd.close() addstar = tempfile.NamedTemporaryFile(suffix=".add", mode='w') # transform KBO locations to this frame using the shifts provided. w = get_wcs(shifts) header = fits.open(filename)[0].header # set the rate of motion in units of pixels/hour instead of ''/hour scale = header['PIXSCAL1'] rate = kbos['sky_rate'] / scale # compute the location of the KBOs in the current frame. # offset magnitudes from the reference frame to the current one. mag = kbos['mag'] - shifts['dmag'] angle = radians(kbos['angle']) # Move the x/y locations to account for the sky motion of the source. x = kbos['x'] - rate * 24.0 * shifts['dmjd'] * cos(angle) y = kbos['y'] - rate * 24.0 * shifts['dmjd'] * sin(angle) x, y = w.wcs_world2pix(x, y, 1) # Each source will be added as a series of PSFs so that a new PSF is # added for each pixel the source moves. itime = float(header['EXPTIME']) / 3600.0 npsf = fabs(rint(rate * itime)) + 1 mag += 2.5 * log10(npsf) dt_per_psf = itime / npsf # Build an addstar file to be used in the planting of source. idx = 0 for record in transpose([x, y, mag, npsf, dt_per_psf, rate, angle]): x = record[0] y = record[1] mag = record[2] npsf = record[3] dt = record[4] rate = record[5] angle = record[6] for i in range(int(npsf)): idx += 1 x += dt * rate * math.cos(angle) y += dt * rate * math.sin(angle) addstar.write("{} {} {} {}\n".format(x, y, mag, idx)) addstar.flush() fk_image = prefix + filename try: os.unlink(fk_image) except OSError as err: if err.errno == errno.ENOENT: pass else: raise # add the sources to the image. if os.access(f'{fk_image}.art', os.R_OK): os.unlink(f'{fk_image}.art') iraf.daophot.addstar(filename, addstar.name, psf, fk_image, simple=True, verify=False, verbose=False) # convert the image to short integers. iraf.images.chpix(fk_image, fk_image, 'ushort')
def findStars(self): # To look for the stars that exist in the image specified. # It will: # Utilise the IRAF package daofind # be used for this image subtraction package # Gives ouput of co-ords, "#imagename_daofind.coo" output = self._Name.replace(".fits", "_daofind.coo") try: self.cleanOutputFiles(output) except: print "Error cleaning output files" print sys.exc_info()[0] # Load as in IRAF and neglect output: # NOAO # DIGIPHOT # APPHOT # DAOFIND iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.apphot(_doprint=0) # Set the parameter list # image: input image # output: output co-ordinates # verify: don't know # threshold: sigma above which stars are determined (fixed in this script) # datapar: point to a file with the user input # # ccdread: CCD readout noise (image header keyword) # gain: CCD gain (image header keyword) # readnoi: CCD readout noise in e # epadu: gain in e- per count # # findpar: point to a file with the user input # # threshold: in units of sigma # # centerpars: # # Save the parameter list # Pull information from the header of the FITS image #self.loadHeader() #datapars = iraf.datapars.getParList() #findpars = iraf.findpars.getParList() #ceneterpars = iraf.centerpars.getParList() #for par in datapars: #print par ## Print to user #print "Information on images:" #print "Gain: %s" % self.getHeader('RON') #print "RON: %s" % self.getHeader('RON') # datapar: iraf.datapars.setParam('ccdread', 'RON') iraf.datapars.setParam('gain', 'GAIN') iraf.datapars.setParam('exposur','EXPTIME') iraf.datapars.setParam('airmass','AIRMASS') iraf.datapars.setParam('filter','SUBSET') iraf.datapars.setParam('obstime','HIERARCH ESO OBS START') iraf.datapars.setParam('datamin', '-100') iraf.datapars.setParam('datamax', '60000') # iraf.datapars.saveParList(filename="%s/uparm/datapars.par" % os.getcwd()) # findpar: #threshold = 4.0 #iraf.findpars.setParam('threshold', str(threshold)) #iraf.findpars.saveParList(filename="uparm/findpars.par") #print glob.glob(output) # daofind: iraf.daofind(image=self._Name, output=output, verify=0, verbose=1, sigma=self._skySTDEV, nsigma=1, scale=1, fwhmpsf=self._MEDFWHM, sharplo='0.0', sharphi='1.0', roundlo='-0.9', roundhi='0.9', thresho='6.0', wcsout="logical", Stdout=1, mode='h' ) # 1. Open the .coo file # 2. Pasre the input # 1. take input file filename = "%s" % (self._Name.replace(".fits", "_daofind.coo")) coords = open(filename) coord = coords.readlines() coords.close() # 2. parse out the lines beginning with "#" filename = "%s" % (self._Name.replace(".fits", "_daofind.reg")) coordregfile = open(filename, "w") coobject = [] for co in coord: if co[0] != "#": # 2b. parse each element newCoordObject = imObject() coitem = [i for i in co.replace("\n","").split(" ") if i!= ""] newCoordObject._id = self.giveObjectID() newCoordObject._pixelx = float(coitem[0]) newCoordObject._pixely = float(coitem[1]) newCoordObject._mag = float(coitem[2]) #newCoordObject.printInfo() coordregfile.write("image; circle(%f,%f,4) # color = red\n" % (newCoordObject._pixelx, newCoordObject._pixely)) coobject.append(newCoordObject) coordregfile.close() self._ObjectList = coobject
fwhm = float(coordLines[9].split()[3]) sigma = float(coordLines[19].split()[3]) threshold = float(coordLines[27].split()[3]) coordFile.close() print(objname, filter_, fwhm, sigma, threshold) print('creating psf...') iraf.images(_doprint=0) iraf.tv(_doprint=0) iraf.ptools(_doprint=0) iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.photcal(_doprint=0) iraf.apphot(_doprint=0) iraf.imutil(_doprint=0) iraf.daophot(_doprint=0) iraf.tables(_doprint=0) iraf.unlearn(iraf.ptools.pselect, iraf.tables.tcreate, iraf.tables.tmatch, iraf.tables.tinfo, iraf.tables.tdump) # first measure the psf for the image iraf.unlearn(iraf.phot, iraf.datapars, iraf.photpars, iraf.centerpars, iraf.fitskypars) iraf.apphot.phot.setParam('interactive', "no") iraf.apphot.phot.setParam('verify', "no") iraf.datapars.setParam('datamax', "INDEF") iraf.datapars.setParam('gain', "gain")
import pyfits from types import * from mx.DateTime import * from iqpkg import * import ephem # Necessary packages iraf.images() iraf.immatch() iraf.imfilter() iraf.noao() iraf.imred() iraf.ccdred() iraf.digiphot() iraf.apphot() yes=iraf.yes no=iraf.no INDEF=iraf.INDEF hedit=iraf.hedit imgets=iraf.imgets imcombine=iraf.imcombine pyrafdir="python/pyraf/" pyrafdir_key='PYRAFPARS' if os.environ.has_key(pyrafdir_key): pardir=os.environ[pyrafdir_key] else: pardir=os.environ['HOME']+'/'+pyrafdir
def skySub(imageName): #Set up iraf packages and variables for filenames skyIm = imageName + '.sky' outfile = 'mSky' + imageName + '.file' avgValLog = 'mSkyLog' + imageName + '.file' stDevLog = 'stDevLog' + imageName + '.file' outputIm = imageName + '.sky' iraf.apphot(_doprint=0) #Delete exisiting files for f in (skyIm, outfile, 'mSky.file', outputIm + '.fits', avgValLog): silentDelete(f) #Configure fitsky parameters iraf.fitsky.coords = "coordslist" iraf.fitsky.interactive = 'no' iraf.fitsky.verify = 'no' iraf.datapars.datamax = 150000 iraf.datapars.datamin = -200 iraf.fitskypars.salgorithm = "mode" iraf.fitskypars.annulus = 0 iraf.fitskypars.dannulus = 40 iraf.fitskypars.shireject = 2 iraf.fitskypars.sloreject = 2 iraf.fitsky.verbose = 'no' #Call fitsky to compute the mean of the sky iraf.fitsky(imageName, output=outfile) #Use 'PDUMP" to extract only the mean sky values sys.stdout = open(avgValLog, "w") iraf.pdump(outfile, 'mSky', "yes") sys.stdout = sys.__stdout__ #Use pdump to get the standard deviation from the sky sys.stdout = open(stDevLog, "w") iraf.pdump(outfile, 'stDev', "yes") sys.stdout = sys.__stdout__ #Read in the mSky values file skyValues = open(avgValLog, 'r') valueList = skyValues.readlines() #convert the value array to a numpy array valueListNp = np.asfarray(valueList) #compute average average = np.mean(valueListNp) #Print output print(str(average) + " is the average") #Read in the stDev log, take the average stDevs = open(stDevLog, 'r') stDevList = stDevs.readlines() stDevListNp = np.asfarray(stDevList) stDevAvg = np.mean(stDevListNp) #Configure imarith iraf.imarith.pixtype = "real" iraf.imarith.calctype = "real" #Use imarith to subtract the average sky value from the image iraf.imarith(imageName, "-", average, outputIm) #Add the average sky value and stDev to the image header iraf.hedit(outputIm, "SKY", average, add='yes', verify='no') iraf.hedit(outputIm, "SKYSIGMA", stDevAvg, add='yes', verify='no') #Print an output image print("Subtracted " + str(average) + " to create a new image called " + outputIm) #Delete acillary output files silentDelete(outfile)