def psf(args): """ Calculate the PSF of an image. """ # Read the seeing and sigma of the sky from the header seeing, sigma = utils.get_from_header(args.input, args.FWHM_key, args.sigma) # Do photometry on the image #print "photometry: \n" photfile_name = args.input + ".mag.1" utils.if_exists_remove(photfile_name) iraf.phot(args.input, output=photfile_name, coords=args.stars, wcsin=args.coords, fwhm=seeing, sigma=sigma, datamax=args.maxval, datamin=args.minval, ccdread=args.ron_key, gain=args.gain_key, exposure=args.expt_key, airmass=args.airm_key, annulus=36, dannulus=18, apert=18, verbose="no", verify="no", interac="no") # Select stars on the image #print "pstselect: \n" pstfile_name = args.input + ".pst.1" utils.if_exists_remove(pstfile_name) iraf.pstselect(args.input, photfile=photfile_name, pstfile=pstfile_name, maxnpsf=20, fwhm=seeing, sigma=sigma, datamax=args.maxval, ccdread=args.ron_key, gain=args.gain_key, exposure=args.expt_key, function="auto", nclean=1, psfrad=36, fitrad=18, maxnstar=20, verbose="no", interac="no", verify="no") # Build psf of the stars #print "psf: \n" psffile_table = args.input + ".psf.1.fits" # iraf keeps adding the .fits :( psgfile_name = args.input + ".psg.1" pstfile_name2 = args.input + ".pst.2" utils.if_exists_remove(psffile_table,psgfile_name, pstfile_name2) iraf.psf( args.input, photfile=photfile_name, pstfile=pstfile_name, groupfile=psgfile_name, opstfile=pstfile_name2, psfimage=psffile_table,fwhm=seeing, sigma=sigma, datamax=args.maxval, datamin=args.minval, ccdread=args.ron_key, gain=args.gain_key, exposure=args.expt_key, function="moffat25", nclean=1, psfrad=36, fitrad=18, maxnstar=20, interactive="no", varorder=args.varorder, verbose="no",verify="no") # Use seepsf to build the image of the psf psffile_name = args.input + ".psf.fits" utils.if_exists_remove(psffile_name) iraf.seepsf(psffile_table, psffile_name) return psffile_name
def apply_psf(args, phot_file, pst_file): with tempfile.NamedTemporaryFile(suffix=".psf.1.fits") as fd0: psffile_table = fd0.name with tempfile.NamedTemporaryFile(suffix=".psf") as fd1: psf_name = fd1.name with tempfile.NamedTemporaryFile(suffix=".psg.1") as fd2: psg_name = fd2.name with tempfile.NamedTemporaryFile(suffix=".pst.2") as fd3: pst2_name = fd3.name hdr = args.image.header # for short seeing, sigma = utils.get_from_header(args.image.im_name, hdr.seeingk, hdr.sigmak) iraf.psf( args.image.im_name, photfile=phot_file, pstfile=pst_file, groupfile=psg_name, opstfile=pst2_name, psfimage=psffile_table, fwhm=seeing, sigma=sigma, ccdread=hdr.ccdronk, gain=hdr.gaink, exposure=hdr.exptimek, function="auto", nclean=1, psfrad=6 * seeing, fitrad=5 * seeing, interactive="no", varorder=-1, verbose="no", verify="no", ) # Use seepsf to make an image of the PSF iraf.seepsf(psffile_table, psf_name) print "\n PSF file: ", psf_name return psf_name
def psfphot(image,coords,pststars,refstar,centre=True,vary=False): """PSF photometry. Centering is through phot on refstar. Assume coords is a .als file for now. Recentering is always done for the reference star, never for the targets.""" iraf.dele('temp.mag*') iraf.dele('temp.psf.fits') iraf.dele('temp.als') if centre: xsh,ysh = recentre(image,refstar) print "Fine Centring: ", xsh,ysh else: xsh,ysh = 0,0 if vary: setaperture(image,refstar) shift_file_coords(coords,xsh,ysh,'tempcoords2',sort='als') shift_file_coords(pststars,xsh,ysh,'temppst2',sort='pst') iraf.phot(image,'tempcoords2','temp.mag2',inter="no",calgorithm='none', mode='h',verify='no',update='no',verbose='no') iraf.psf(image,'temp.mag2','temppst2','temp.psf','temp.mag.pst','temp.mag.psg', inter='no',mode='h',verify='no',update='no',verbose='no') iraf.allstar(image,'temp.mag2','temp.psf','temp.als','temp.mag.arj',"default", mode='h',verify='no',update='no',verbose='no') out = iraf.pdump('temp.als','id,mag,merr,msky','yes',Stdout=1) return out
def psffit(img, fwhm, psfstars, hdr, interactive, _datamin, _datamax, psffun='gauss', fixaperture=False): ''' giving an image, a psffile compute the psf using the file _psf.coo ''' import lsc _ron = lsc.util.readkey3(hdr, 'ron') _gain = lsc.util.readkey3(hdr, 'gain') if not _ron: _ron = 1 print 'warning ron not defined' if not _gain: _gain = 1 print 'warning gain not defined' iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) zmag = 0. varord = 0 # -1 analitic 0 - numeric if fixaperture: print 'use fix aperture 5 8 10' hdr = lsc.util.readhdr(img + '.fits') _pixelscale = lsc.util.readkey3(hdr, 'PIXSCALE') a1, a2, a3, a4, = float(5. / _pixelscale), float( 5. / _pixelscale), float(8. / _pixelscale), float(10. / _pixelscale) else: a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5) iraf.fitskypars.annulus = a4 iraf.fitskypars.salgori = 'mean' #mode,mean,gaussian iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4) iraf.datapars.datamin = _datamin iraf.datapars.datamax = _datamax iraf.datapars.readnoise = _ron iraf.datapars.epadu = _gain iraf.datapars.exposure = 'EXPTIME' iraf.datapars.airmass = '' iraf.datapars.filter = '' iraf.centerpars.calgori = 'centroid' iraf.centerpars.cbox = a2 iraf.daopars.recenter = 'yes' iraf.photpars.zmag = zmag psfout = img.replace('.zogypsf', '') + '.psf.fits' iraf.delete('_psf.ma*,_psf.ps*,_psf.gr?,_psf.n*,_psf.sub.fit?,' + psfout, verify=False) iraf.phot(img + '[0]', '_psf.coo', '_psf.mag', interac=False, verify=False, verbose=False) # removes saturated stars from the list (IRAF just issues a warning) with open('_psf.mag') as f: text = f.read() text = re.sub('(.*\n){6}.*BadPixels\* \n', '', text) with open('_psf.mag', 'w') as f: f.write(text) iraf.daopars.psfrad = a4 iraf.daopars.functio = psffun iraf.daopars.fitrad = a1 iraf.daopars.fitsky = 'yes' iraf.daopars.sannulus = a4 iraf.daopars.recenter = 'yes' iraf.daopars.varorder = varord if interactive: # not possible to run pstselect or psf interactively on 64-bit linux (Error 851) os.system('cp _psf.mag _psf.pst') print '_' * 80 print '>>> Mark good stars with "a" or "d"-elete. Then "f"-it,' + \ ' "w"-write and "q"-uit (cursor on ds9)' print '-' * 80 else: iraf.pstselect(img + '[0]', '_psf.mag', '_psf.pst', psfstars, interac=False, verify=False) iraf.psf(img + '[0]', '_psf.mag', '_psf.pst', psfout, '_psf.psto', '_psf.psg', interac=interactive, verify=False, verbose=False) iraf.group(img + '[0]', '_psf.mag', psfout, '_psf.grp', verify=False, verbose=False) iraf.nstar(img + '[0]', '_psf.grp', psfout, '_psf.nst', '_psf.nrj', verify=False, verbose=False) photmag = iraf.txdump("_psf.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1) pst = iraf.txdump("_psf.pst", 'xcenter,ycenter,id', expr='yes', Stdout=1) fitmag = iraf.txdump("_psf.nst", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1) return photmag, pst, fitmag
def psffit(img, fwhm, psfstars, hdr, interactive, _datamax=45000, psffun='gauss', fixaperture=False): import agnkey iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) zmag = 0. varord = 0 # -1 analitic 0 - numeric if fixaperture: print 'use fix aperture 5 8 10' hdr = agnkey.util.readhdr(img+'.fits') _pixelscale = agnkey.util.readkey3(hdr, 'PIXSCALE') a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float( 10. / _pixelscale) # a1, a2, a3, a4, = int(5), int(8), int(10), int(12) else: a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5) _center='no' iraf.fitskypars.annulus = a4 iraf.fitskypars.dannulus = a4 iraf.noao.digiphot.daophot.daopars.sannulus = int(a4) iraf.noao.digiphot.daophot.daopars.wsannul = int(a4) iraf.fitskypars.salgori = 'mean' #mode,mean,gaussian iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4) # iraf.photpars.apertures = '%d,%d,%d'%(a2,a3,a4) iraf.datapars.datamin = -100 iraf.datapars.datamax = _datamax iraf.datapars.readnoise = agnkey.util.readkey3(hdr, 'ron') iraf.datapars.epadu = agnkey.util.readkey3(hdr, 'gain') iraf.datapars.exposure = 'exptime' #agnkey.util.readkey3(hdr,'exptime') iraf.datapars.airmass = 'airmass' iraf.datapars.filter = 'filter2' iraf.centerpars.calgori = 'gauss' iraf.centerpars.cbox = 1 iraf.daopars.recenter = _center iraf.photpars.zmag = zmag iraf.delete('_psf.ma*,' + img + '.psf.fit?,_psf.ps*,_psf.gr?,_psf.n*,_psf.sub.fit?', verify=False) iraf.phot(img+'[0]', '_psf.coo', '_psf.mag', interac=False, verify=False, verbose=False) iraf.daopars.psfrad = a4 iraf.daopars.functio = psffun iraf.daopars.fitrad = a1 iraf.daopars.fitsky = 'yes' iraf.daopars.sannulus = int(a4) iraf.daopars.wsannul = int(a4) iraf.daopars.recenter = _center iraf.daopars.varorder = varord if interactive: shutil.copyfile('_psf.mag', '_psf.pst') print '_' * 80 print '>>> Mark good stars with "a" or "d"-elete. Then "f"-it,' + \ ' "w"-write and "q"-uit (cursor on ds9)' print '-' * 80 else: iraf.pstselect(img+'.fits[0]', '_psf.mag', '_psf.pst', psfstars, interac=False, verify=False) iraf.psf(img+'.fits[0]', '_psf.mag', '_psf.pst', img + '.psf', '_psf.psto', '_psf.psg', interac=interactive, verify=False, verbose=False) # if os.path.isfile(img + '.psf.fits'): # print 'file there' iraf.group(img+'.fits[0]', '_psf.mag', img + '.psf.fits', '_psf.grp', verify=False, verbose=False) iraf.nstar(img+'.fits[0]', '_psf.grp', img + '.psf.fits', '_psf.nst', '_psf.nrj', verify=False, verbose=False) photmag = iraf.txdump("_psf.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1) pst = iraf.txdump("_psf.pst", 'xcenter,ycenter,id', expr='yes', Stdout=1) fitmag = iraf.txdump("_psf.nst", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1) return photmag, pst, fitmag
def performPhotometry(task, logger): #iraf.prcacheOff() [iraf.unlearn(t) for t in ('phot','pstselect','psf','allstar')] iraf.set(imtype="fits,noinherit") # set image output format iraf.set(clobber="yes") hdu=pyfits.open(task['images'])[0] hdr = hdu.header imdata = hdu.data for key,value in task['fits'].iteritems(): task[key] = hdr.get(value,1) #Sextractor to find stars; add an object for the force detect logger.info('Running SExtractor on [%s]' % os.path.basename(task['images'])) sex = sextractor.SExtractor() makeSexConfig(sex,task) sex.run(task['images']) catalog = sex.catalog() #Set up image parameters MIN_X = max(1,int(task['numpixx']*task['filtfactor'])) MIN_Y = max(1,int(task['numpixy']*task['filtfactor'])) MAX_X = int(task['numpixx']*(1-task['filtfactor'])) MAX_Y = int(task['numpixy']*(1-task['filtfactor'])) AREAXY = '[%s:%s,%s:%s]' % (MIN_X, MAX_X, MIN_Y, MAX_Y) AREANO = '[%s:%s,%s:%s]' % (MIN_X, MAX_X-2*MIN_X, MIN_Y, MAX_Y-2*MIN_Y) try: task['pixscale'] = abs(hdr.get('CD1_1'))*3600. except TypeError: task['pixscale'] = abs(hdr.get('CDELT1'))*3600. task['seeing'] = np.median( sorted([i['FWHM_IMAGE'] for i in catalog])[:int(-len(catalog)*0.5)] ) #Take the median of the "bottom" 50% of objects logger.info('--> %s SExtractor detected bright objects in the field' % (len(catalog),) ) logger.info('--> %0.2f median FWHM of bright objects in the field, in arcsec' % (task['seeing']*task['pixscale'],)) task['objects'] = [(i['ALPHA_J2000'],i['DELTA_J2000']) for i in catalog] task['objects'].append(task['objwcs']) task['objectlist'] = open(os.path.join(task['output_directory'],'objectlist'),'w') task['objectlist'].write('\n'.join([' %s %s' % (i[0],i[1]) for i in task['objects']])) task['objectlist'].close() logger.info('Running iraf.imstat') irafoutput = iraf.imstat(images=task['images']+AREANO,fields='midpt,min,max,stddev', format=0, Stdout=1) task['nimgs'] = hdr.get('NIMGS',1) task['gain'] *= task['nimgs']*2/3. task['ron'] *= np.sqrt(task['nimgs'])/task['nimgs']*constants.INTERPSM[task['band']] task['datamean'], task['datamin'], task['datamax'], task['datastdev'] = map(float, irafoutput[0].split()) irafoutput = iraf.imstat(images=task['images'],fields='stddev,midpt',nclip=25,format=0,cache='yes',Stdout=1) task['skynoise'], task['datamean'] = map(float, irafoutput[0].split() ) task['skynoise'] *= constants.INTERPSM[task['band']] task['airmass'] = hdr.get('AIRMASS',1) task['zmag'] -= (float(task['airmass'])-1.0)*constants.extinction_coefficients[task['band']] task['match_proximity'] = 2.5 * task['seeing'] logger.info('--> %5.2f counts: Sky noise, corrected for drizzle imcombine' % task['skynoise']) logger.info('--> %5.2f Median count value, after background subtraction' % task['datamean']) logger.info('--> %5.2f Airmass' % task['airmass']) #prepare temp files that iraf will use for filename in ('photfile','pstfile','psfimg','opstfile','groupfile','allstarfile','rejfile','subimage'): task[filename] = open(os.path.join(task['output_directory'],filename),'w') task[filename].close() #iraf.phot to get APP magnitudes logger.info('Running iraf.apphot.phot') #apsizes = [i*task['faperture']*task['seeing'] for i in (0.4,0.5,0.6,0.8,1.0,1.2,1.5,2.0,2.5,3.0)] #irafapsizes = ','.join(['%.2f' % i for i in apsizes]) irafapsizes = '%0.2f' % (task['faperture']*task['seeing']) kwargs = dict(image=task['images'],coords=task['objectlist'].name, output=task['photfile'].name, interac='no',scale=1, fwhmpsf=task['seeing'], wcsin='world', wcsout='physical', sigma=task['skynoise'], datamin=task['datamin'], datamax=task['datamax'], readnoi=task['ron'], epadu=task['gain'], itime=task['exposure'], xairmass=task['airmass'], ifilter=task['band'], otime=task['dateobs'], aperture= irafapsizes, zmag=task['zmag'], annulus=task['fannulus']*task['seeing'], dannulus=task['fdannulus']*task['seeing'], calgorithm='gauss', cbox = 1.5*task['seeing'], maxshift=2.0*task['seeing'], mode="h",Stdout=1,verify=0) iraf.phot(**kwargs) if task['band'] not in constants.infrared: #iraf.pstselect to choose objects for PSF modelling logger.info('Running iraf.daophot.pstselect') kwargs = dict(image=task['images'], photfile=task['photfile'].name,pstfile=task['pstfile'].name, maxnpsf=task['pstnumber'], wcsin='physical', wcsout='physical', interac="no",verify='no',scale=1, fwhmpsf=task['seeing'], datamin=0, datamax=task['datamax'], psfrad=3.0*task['seeing'], fitrad=1.0*task['seeing'], recente='yes', nclean=task['nclean'], mode="h",Stdout=1) iraf.pstselect(**kwargs) #iraf.psf to model PSF logger.info('Running iraf.daophot.psf') kwargs = dict( image=task['images'], photfile=task['photfile'].name, pstfile=task['pstfile'].name, psfimage=task['psfimg'].name, opstfile=task['opstfile'].name, groupfile=task['groupfile'].name, wcsin='physical',wcsout='physical', interac="no",verify="no",scale=1, fwhmpsf=task['seeing'], sigma=task['skynoise'], datamin=task['datamin'], datamax=task['datamax'], readnoi=task['ron'], epadu=task['gain'], itime=task['exposure'], xairmass=task['airmass'], ifilter=task['band'], otime=task['dateobs'], function=task['func'], varorder=task['varorder'], saturat='no', psfrad=3.0*task['seeing'], fitrad=1.*task['faperture']*task['seeing'], nclean=task['nclean'], mergerad=1.5*task['seeing'], mode='h',Stdout=1) iraf.psf(**kwargs) logger.info('Running iraf.daophot.allstar') #iraf.allstars to compute PSF photometry; recenter with recenter='yes', mergerad=<value> to avoid duplicate detection kwargs = dict(image=task['images'], photfile=task['photfile'].name, wcsin='physical', wcsout='physical', psfimage=task['psfimg'].name, allstarf=task['allstarfile'].name, rejfile=task['rejfile'].name, subimage=task['subimage'].name, verbose=1,verify='no',scale=1, fwhmpsf=task['seeing'], sigma=task['skynoise'], datamin=task['datamin'], datamax=task['datamax'], readnoi=task['ron'], epadu=task['gain'], itime=task['exposure'], xairmass=task['airmass'], ifilter=task['band'], otime=task['dateobs'], function=task['func'], varorder=task['varorder'], psfrad=3.*task['seeing'], fitrad=1.*task['faperture']*task['seeing'], recenter='yes', mergerad=1.5*task['seeing'], mode='h',Stdout=1) iraf.allstar(**kwargs) #Parse both photometry, convert to RA,DEC,MAG,MAGERR logger.info('iraf tasks complete. Parsing results and calibrating') photometry = {} photometry['APP'] = iraf.txdump(textfiles=task['photfile'].name, fields='XCENTER,YCENTER,MAG,MERR',expr='yes', headers='no',Stdout=1) if task['band'] not in constants.infrared: photometry['PSF'] = iraf.txdump(textfiles=task['allstarfile'].name, fields='XCENTER,YCENTER,MAG,MERR',expr='yes', headers='no',Stdout=1) for phototype in photometry: kwargs = dict(input='STDIN', output='STDOUT', insystem='%s physical' % task['images'], outsystem='%s world' % task['images'], ilatuni='physical', ilnguni='physical', olnguni='degrees', olatuni='degrees', ilngfor='%10.7f', ilatfor='%10.7f', olngfor='%10.5f', olatfor='%10.5f', Stdin=photometry[phototype],Stdout=1) photometry[phototype] = [i.split() for i in iraf.skyctran(**kwargs) if i and not i.startswith('#') and 'INDEF' not in i] photometry[phototype] = [map(float,(i[4],i[5],i[2],i[3])) for i in photometry[phototype] ] #Now we have [(ra,dec,'mag','mageerr'),...] results = calibrate((task['objwcs'][0],task['objwcs'][1]),task,photometry,logger) # if 'PSF' not in results: return results
pstfile = open(FitsFileName+'.pst.1', 'a+r') pstfile.write("#N ID XCENTER YCENTER MAG MSKY \\\n") pstfile.write("#U ## pixels pixels magnitudes counts \\\n") pstfile.write("#F %-9d %-10.3f %-10.3f %-12.3f %-15.7g \n") pstfile.write("#\n") np.savetxt(pstfile,g.take([-1,0,1,2,3],axis=1),fmt=['%-9d','%-10.3f','%-10.3f','%-12.3f','%-15.7g']) pstfile.close() iraf.daopars.setParam('matchra',fwhm) iraf.daopars.setParam('psfrad',4*fwhm+1) iraf.daopars.setParam('fitrad',fwhm) iraf.daopars.setParam('sannulu',2*fwhm) iraf.daopars.setParam('wsannul',4*fwhm) iraf.psf.setParam('image',FitsFileName) iraf.psf(mode='h') iraf.seepsf(psfimage=FitsFileName+'.psf.1.fits',image=base+'psfim'+ext+'.fits',magnitu='18.0') iraf.allstar.setParam('image',FitsFileName) iraf.allstar(mode='h',verbose='no') if os.path.exists(base+'allstartmp') == True: os.remove(base+'allstartmp') iraf.txdump(textfile=FitsFileName+'.als.1',fields='xcenter,ycenter,mag,merr,id',expr='mag\ != INDEF && merr != INDEF', Stdout=base+'allstartmp') outmags = np.loadtxt(base+'allstartmp') if os.path.exists(base+'allstartmp') == True: os.remove(base+'allstartmp') if int(ext) <= 4: shifty = 0.0 shiftx = (float(ext) - 1.)*sx else: shifty = sy
def psfphot(image, coofile, ot, wtimage="", varorder=1, clobber=globclob, verbose=globver, pixtol=3.0, maxnpsf=25): """ perform PSF-based photometry on a single target star (SN?) at RA, Dec and also on a set of comparison stars, using daophot. simultaneously perform aperture photometry on all the comparison stars (after subtracting off contributions from neighbors) to enable absolute photometry by comparison to aperture photometry of standard stars observed in other fields """ # Defaults / constants psfmult = 5.0 #standard factor (multiplied by fwhm to get psfradius) psfmultsmall = 3.0 #similar to psfmult, adjusted for nstar and substar # Necessary package iraf.imutil() iraf.digiphot() iraf.daophot() # Detect stars iqobjs("%s.sub.fits" % image[:-5], 1.5, 12000.0, wtimage=wtimage, skyval="0.0") root = image[:-5] [gain, rnoise, fwhm] = get_head(image, ["GAIN", "READN", "SEEPIX"]) fwhm = float(fwhm) rnoise = float(rnoise) iraf.iterstat(image) # Saturation level if not check_head(image, "SATURATE"): saturate = 60000.0 else: saturate = get_head(image, "SATURATE") # Update datapars and daopars iraf.datapars.fwhmpsf = fwhm iraf.datapars.sigma = iraf.iterstat.sigma iraf.datapars.datamin = iraf.iterstat.median - 10 * iraf.iterstat.sigma iraf.datapars.datamax = 0.50 * saturate iraf.datapars.readnoise = rnoise iraf.datapars.epadu = gain iraf.daopars.psfrad = psfmult * fwhm iraf.daopars.fitrad = fwhm iraf.daopars.function = "gauss,moffat15,moffat25,lorentz,penny1" iraf.daopars.varorder = varorder # Reference stars file stars = Starlist(coofile) stars.wcs2pix(image) outf = open("%s.coo.1" % image[:-5], "w") for star in stars: outf.write("%10.3f%10.3f\n" % (star.xval, star.yval)) outf.close() #Aperture photometry iraf.daophot.phot(root, 'default', 'default', apertures=fwhm, verify=no, interac=no, verbose=verbose) iraf.datapars.datamax = 0.50 * saturate iraf.pstselect(root, 'default', 'default', maxnpsf, interactive=no, verify=no, verbose=verbose) iraf.psf(root, 'default', 'default', 'default', 'default', 'default', interactive=no, showplots=no, verify=no, verbose=verbose) iraf.allstar(root, 'default', 'default', 'default', 'default', 'default', verify=no, verbose=verbose) # Prep for subtracted image iraf.iterstat("%s.sub.fits" % root) iraf.datapars.sigma = iraf.iterstat.sigma iraf.datapars.datamin = iraf.iterstat.median - 10 * iraf.iterstat.sigma iraf.datapars.datamax = saturate # Look for source at OT location substars = Starlist("%s.sub.fits.stars" % image[:-5]) otstars = Starlist(ot) otstars.wcs2pix("%s.sub.fits" % image[:-5]) smatch, omatch = substars.match(otstars, tol=pixtol, useflags=no) # Generate coo file otcoo = open("%s.sub.coo.1" % image[:-5], "w") if len(smatch) == 0: otcoo.write("%10.3f%10.3f\n" % (otstars[0].xval, otstars[0].yval)) else: otcoo.write("%10.3f%10.3f\n" % (smatch[0].xval, smatch[0].yval)) otcoo.close() iraf.daophot.phot("%s.sub.fits" % root, "%s.sub.coo.1" % image[:-5], 'default', 'default', apertures=fwhm, calgorithm="none", interac=no, verify=no, verbose=verbose) if len(smatch) == 0: print "No match in subtracted image: %s.sub.fits" % root else: iraf.allstar("%s.sub.fits" % root, 'default', "%s.psf.1.fits" % root, 'default', 'default', 'default', verify=no, verbose=no) return
def psfphot(inlist, ra, dec, reffilt, interact, fwhm, readnoise, gain, threshold,refimage=None,starfile=None,maxnpsf=5, clobber=globclob,verbose=globver,skykey='SKYBKG', filtkey='FILTER',pixtol=3.0): """ perform PSF-based photometry on a single target star (SN?) at RA, Dec and also on a set of comparison stars, using daophot. simultaneously perform aperture photometry on all the comparison stars (after subtracting off contributions from neighbors) to enable absolute photometry by comparison to aperture photometry of standard stars observed in other fields """ # Defaults / constants psfmult=5.0 #standard factor (multiplied by fwhm to get psfradius) psfmultsmall=3.0 #similar to psfmult, adjusted for nstar and substar # Necessary package iraf.imutil() # Parse inputs infiles=iraffiles(inlist) # Which file is reffilt? call it refimage if refimage==None: for image in infiles: if check_head(image, filtkey): try: imgfilt = get_head(image, filtkey) if imgfilt == reffilt: refimage = image break except: pass if not refimage: print "BAD USER! No image corresponds to the filter: %s" % reffilt return else: refroot='s'+refimage.split('.')[0] #first make sure to add back in background of sky iraf.iqsubsky(inlist, sub=no, skykey=skykey) #put reference image first on list infiles.remove(refimage) infiles.insert(0,refimage) #setup for keywords if gain == "!GAIN": try: gainval = float(get_head(image, gain)) except: print "Bad header keyword for gain." else: gainval = float(gain) if readnoise == "!READNOISE": try: readval = float(get_head(image, readnoise)) except: print "Bad header keyword for readnoise." else: readval = float(readnoise) # Process each file in turn for image in infiles: # Check that the image is there check_exist(image,"r") # Grab image root name root=image.split('.')[0] # Map image to reference image if not (image==refimage): [nx,ny]=get_head(image,['NAXIS1','NAXIS2']) stars=Starlist(get_head(image,'STARFILE')) refstars=Starlist(get_head(refimage,'STARFILE')) refstars.pix2wcs(refimage) refstars.wcs2pix(image) match,refmatch=stars.match(refstars,useflags=yes,tol=10.0) nstars=len(match) if not (nstars>2): print 'Could not find star matches between reference and %s' % image infiles.remove(image) continue refmatch.pix2wcs(image) refmatch.wcs2pix(refimage) matchfile=open('%s.match' % root, 'w') for i in range(len(match)): matchfile.write('%10.3f%10.3f%10.3f%10.3f\n' % (refmatch[i].xval,refmatch[i].yval, match[i].xval,match[i].yval)) matchfile.close() check_exist('%s.geodb' % root, 'w', clobber=clobber) iraf.geomap('%s.match' % root,'%s.geodb' % root,1.0,nx,1.0,ny, verbose=no,interactive=no) check_exist('s%s.fits' % root, 'w', clobber=clobber) iraf.geotran(image,'s%s' % root,'%s.geodb' % root, '%s.match' % root,geometry="geometric", boundary="constant",verbose=no) else: iraf.imcopy(image,'s%s' % root) root='s%s' % root #get sky level and calculate sigma #if check_head(image, skykey): # try: # sky=float(get_head(image, skykey)) # except: # print "No sky levels in header." #sigma= (((sky * gainval) + readval**2)**.5) / gainval iraf.iterstat(image) # Saturation level if not check_head(image, "SATURATE"): saturate = 60000.0 else: saturate = get_head(image, "SATURATE") # Update datapars and daopars iraf.datapars.fwhmpsf=fwhm iraf.datapars.sigma=iraf.iterstat.sigma iraf.datapars.datamin=iraf.iterstat.median-10*iraf.iterstat.sigma iraf.datapars.datamax=0.90*saturate iraf.datapars.readnoise=readval iraf.datapars.epadu=gainval iraf.datapars.filter=filtkey iraf.daopars.psfrad=psfmult*fwhm iraf.daopars.fitrad=fwhm iraf.daopars.function="gauss,moffat15,moffat25,lorentz,penny1" #find stars in image unless a starlist is given if image==refimage and starfile==None: iraf.daophot.daofind(root,'refimage.coo.1',threshold=threshold,verify=no, verbose=verbose) elif image==refimage: shutil.copy(starfile,'refimage.coo.1') #initial photometry iraf.daophot.phot(root,'refimage.coo.1','default',aperture=fwhm,verify=no, verbose=verbose) #select stars for psf the first time refstarsfile = "refimage.pst.1" if image == refimage: iraf.pstselect(root,'default',refstarsfile,maxnpsf, interactive=yes,verify=no,verbose=verbose) #fit the psf iraf.psf(root,'default',refstarsfile,'default','default','default', interactive=interact,verify=no,verbose=verbose) #identify neighboring/interfering stars to selected stars groupingfile = root+".psg.1" iraf.nstar(root,groupingfile,'default','default','default', psfrad= psfmultsmall * fwhm,verify=no,verbose=verbose) #subtract out neighboring stars from image iraf.substar(root,'default',refstarsfile,'default','default', psfrad=psfmultsmall*fwhm,verify=no,verbose=verbose) #repeat psf to get better psf model #IRAF's interactive version usually crashes subtractedimage = root+".sub.1" iraf.psf(subtractedimage,root+".nst.1",refstarsfile,'%s.psf.2' % root, '%s.pst.2' % root,'%s.psg.2' % root,interactive=interact, verify=no,verbose=verbose) #Need to make sure SN was detected by daofind stars=Starlist('%s.mag.1' % root) SN=Star(name='SN',radeg=ra,dcdeg=dec,fwhm=2.0,fwhmw=2.0) SNlis=Starlist(stars=[SN]) SNlis.wcs2pix(image) if (len(stars.match(SNlis)[0])==0): #No match - need to add to daofind file print "No match!" coofile=open('refimage.coo.1', 'a+') coofile.write('%10.3f%10.3f%9.3f%8.3f%13.3f%12.3f%8i\n' % (SNlis[0].xval, SNlis[0].yval,99.999,0.500,0.000,0.000,999)) coofile.close() #repeat aperture photometry to get good comparisons to standard fields iraf.daophot.phot(root,'refimage.coo.1','default',aperture=psfmult*fwhm, verify=no,verbose=verbose) # allstar run iraf.allstar(root,'default','default','default','default','default', verify=no,verbose=verbose)
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 psffit(img, fwhm, psfstars, hdr, interactive, _datamax, psffun='gauss', fixaperture=False): ''' giving an image, a psffile compute the psf using the file _psf.coo ''' import lsc _ron = lsc.util.readkey3(hdr, 'ron') _gain = lsc.util.readkey3(hdr, 'gain') if not _ron: _ron = 1 print 'warning ron not defined' if not _gain: _gain = 1 print 'warning ron not defined' iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) zmag = 0. varord = 0 # -1 analitic 0 - numeric if fixaperture: print 'use fix aperture 5 8 10' hdr = lsc.util.readhdr(img+'.fits') _pixelscale = lsc.util.readkey3(hdr, 'PIXSCALE') a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float( 10. / _pixelscale) else: a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5) iraf.fitskypars.annulus = a4 iraf.fitskypars.salgori = 'mean' #mode,mean,gaussian iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4) iraf.datapars.datamin = -100 iraf.datapars.datamax = _datamax iraf.datapars.readnoise = _ron iraf.datapars.epadu = _gain iraf.datapars.exposure = 'EXPTIME' iraf.datapars.airmass = '' iraf.datapars.filter = '' iraf.centerpars.calgori = 'centroid' iraf.centerpars.cbox = a2 iraf.daopars.recenter = 'yes' iraf.photpars.zmag = zmag iraf.delete('_psf.ma*,' + img + '.psf.fit?,_psf.ps*,_psf.gr?,_psf.n*,_psf.sub.fit?', verify=False) iraf.phot(img+'[0]', '_psf.coo', '_psf.mag', interac=False, verify=False, verbose=False) # removes saturated stars from the list (IRAF just issues a warning) with open('_psf.mag') as f: text = f.read() text = re.sub('(.*\n){6}.*BadPixels\* \n', '', text) with open('_psf.mag', 'w') as f: f.write(text) iraf.daopars.psfrad = a4 iraf.daopars.functio = psffun iraf.daopars.fitrad = a1 iraf.daopars.fitsky = 'yes' iraf.daopars.sannulus = a4 iraf.daopars.recenter = 'yes' iraf.daopars.varorder = varord if interactive: # not possible to run pstselect or psf interactively on 64-bit linux (Error 851) shutil.copyfile('_psf.mag', '_psf.pst') print '_' * 80 print '>>> Mark good stars with "a" or "d"-elete. Then "f"-it,' + \ ' "w"-write and "q"-uit (cursor on ds9)' print '-' * 80 else: iraf.pstselect(img+'[0]', '_psf.mag', '_psf.pst', psfstars, interac=False, verify=False) iraf.psf(img + '[0]', '_psf.mag', '_psf.pst', img + '.psf', '_psf.psto', '_psf.psg', interac=interactive, verify=False, verbose=False) iraf.group(img + '[0]', '_psf.mag', img + '.psf', '_psf.grp', verify=False, verbose=False) iraf.nstar(img + '[0]', '_psf.grp', img + '.psf', '_psf.nst', '_psf.nrj', verify=False, verbose=False) photmag = iraf.txdump("_psf.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1) pst = iraf.txdump("_psf.pst", 'xcenter,ycenter,id', expr='yes', Stdout=1) fitmag = iraf.txdump("_psf.nst", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1) return photmag, pst, fitmag
def psffit(img, fwhm, psfstars, hdr, interactive, _datamax=45000, psffun='gauss', fixaperture=False): ''' giving an image, a psffile compute the psf using the file _psf.coo ''' import lsc _ron = lsc.util.readkey3(hdr, 'ron') _gain = lsc.util.readkey3(hdr, 'gain') if not _ron: _ron = 1 print 'warning ron not defined' if not _gain: _gain = 1 print 'warning ron not defined' iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) zmag = 0. varord = 0 # -1 analitic 0 - numeric if fixaperture: print 'use fix aperture 5 8 10' hdr = lsc.util.readhdr(img+'.fits') _pixelscale = lsc.util.readkey3(hdr, 'PIXSCALE') a1, a2, a3, a4, = float(5. / _pixelscale), float(5. / _pixelscale), float(8. / _pixelscale), float( 10. / _pixelscale) else: a1, a2, a3, a4, = int(fwhm + 0.5), int(fwhm * 2 + 0.5), int(fwhm * 3 + 0.5), int(fwhm * 4 + 0.5) iraf.fitskypars.annulus = a4 iraf.fitskypars.salgori = 'mean' #mode,mean,gaussian iraf.photpars.apertures = '%d,%d,%d' % (a2, a3, a4) iraf.datapars.datamin = -100 iraf.datapars.datamax = _datamax iraf.datapars.readnoise = _ron iraf.datapars.epadu = _gain iraf.datapars.exposure = 'EXPTIME' iraf.datapars.airmass = '' iraf.datapars.filter = '' iraf.centerpars.calgori = 'centroid' iraf.centerpars.cbox = a2 iraf.daopars.recenter = 'yes' iraf.photpars.zmag = zmag iraf.delete('_psf.ma*,' + img + '.psf.fit?,_psf.ps*,_psf.gr?,_psf.n*,_psf.sub.fit?', verify=False) iraf.phot(img+'[0]', '_psf.coo', '_psf.mag', interac=False, verify=False, verbose=False) iraf.daopars.psfrad = a4 iraf.daopars.functio = psffun iraf.daopars.fitrad = a1 iraf.daopars.fitsky = 'yes' iraf.daopars.sannulus = a4 iraf.daopars.recenter = 'yes' iraf.daopars.varorder = varord if interactive: shutil.copyfile('_psf.mag', '_psf.pst') print '_' * 80 print '>>> Mark good stars with "a" or "d"-elete. Then "f"-it,' + \ ' "w"-write and "q"-uit (cursor on ds9)' print '-' * 80 else: iraf.pstselect(img+'[0]', '_psf.mag', '_psf.pst', psfstars, interac=False, verify=False) iraf.psf(img + '[0]', '_psf.mag', '_psf.pst', img + '.psf', '_psf.psto', '_psf.psg', interac=interactive, verify=False, verbose=False) iraf.group(img + '[0]', '_psf.mag', img + '.psf', '_psf.grp', verify=False, verbose=False) iraf.nstar(img + '[0]', '_psf.grp', img + '.psf', '_psf.nst', '_psf.nrj', verify=False, verbose=False) photmag = iraf.txdump("_psf.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1) pst = iraf.txdump("_psf.pst", 'xcenter,ycenter,id', expr='yes', Stdout=1) fitmag = iraf.txdump("_psf.nst", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1) return photmag, pst, fitmag
def psfphot(image, clobber=globclob, verbose=globver, pixtol=3.0, maxnpsf=5, interact=yes): """ perform PSF-based photometry on a single target star (SN?) at RA, Dec and also on a set of comparison stars, using daophot. simultaneously perform aperture photometry on all the comparison stars (after subtracting off contributions from neighbors) to enable absolute photometry by comparison to aperture photometry of standard stars observed in other fields """ # Defaults / constants psfmult = 5.0 #standard factor (multiplied by fwhm to get psfradius) psfmultsmall = 3.0 #similar to psfmult, adjusted for nstar and substar # Necessary package iraf.imutil() # Detect stars iqpkg.iqobjs(image, 3.0, 50000.0, wtimage="", skyval="!MEDSKY") root = image[:-5] [gain, rnoise, fwhm] = get_head(image, ["GAIN", "READNOI", "SEEPIX"]) fwhm = float(fwhm) rnoise = float(rnoise) iraf.iterstat(image) # Saturation level if not check_head(image, "SATURATE"): saturate = 60000.0 else: saturate = get_head(image, "SATURATE") # Update datapars and daopars iraf.datapars.fwhmpsf = fwhm iraf.datapars.sigma = iraf.iterstat.sigma iraf.datapars.datamin = iraf.iterstat.median - 10 * iraf.iterstat.sigma iraf.datapars.datamax = 70000.0 iraf.datapars.readnoise = rnoise iraf.datapars.epadu = gain iraf.daopars.psfrad = psfmult * fwhm iraf.daopars.fitrad = fwhm iraf.daopars.function = "gauss,moffat15,moffat25,lorentz,penny1" # coo file stars = Starlist("%s.stars" % image) outf = open("%s.coo.1" % image[:-5], "w") for star in stars: outf.write("%10.3f%10.3f\n" % (star.xval, star.yval)) outf.close() #initial photometry iraf.daophot.phot(root, 'default', 'default', aperture=fwhm, verify=no, verbose=verbose) iraf.datapars.datamax = 30000.0 iraf.pstselect(root, 'default', 'default', maxnpsf, interactive=yes, verify=no, verbose=verbose) iraf.psf(root, 'default', 'default', 'default', 'default', 'default', interactive=interact, verify=no, verbose=verbose) iraf.allstar(root, 'default', 'default', 'default', 'default', 'default', verify=no, verbose=verbose) iraf.iterstat("%s.sub.fits" % root) iraf.datapars.sigma = iraf.iterstat.sigma iraf.datapars.datamin = iraf.iterstat.median - 10 * iraf.iterstat.sigma iraf.datapars.datamax = 70000.0 iraf.daophot.phot("%s.sub.fits" % root, "SN.coo", 'default', 'default', aperture=fwhm, verify=no, verbose=verbose) iraf.datapars.datamax = 30000.0 iraf.daopars.fitrad = fwhm * 2.0 iraf.allstar("%s.sub.fits" % root, 'default', "%s.psf.1.fits" % root, 'default', 'default', 'default', verify=no, verbose=no)
def psfphot(inlist, ra, dec, reffilt, interact, fwhm, readnoise, gain, threshold, refimage=None, starfile=None, maxnpsf=5, clobber=globclob, verbose=globver, skykey='SKYBKG', filtkey='FILTER', pixtol=3.0): """ perform PSF-based photometry on a single target star (SN?) at RA, Dec and also on a set of comparison stars, using daophot. simultaneously perform aperture photometry on all the comparison stars (after subtracting off contributions from neighbors) to enable absolute photometry by comparison to aperture photometry of standard stars observed in other fields """ # Defaults / constants psfmult = 5.0 #standard factor (multiplied by fwhm to get psfradius) psfmultsmall = 3.0 #similar to psfmult, adjusted for nstar and substar # Necessary package iraf.imutil() # Parse inputs infiles = iraffiles(inlist) # Which file is reffilt? call it refimage if refimage == None: for image in infiles: if check_head(image, filtkey): try: imgfilt = get_head(image, filtkey) if imgfilt == reffilt: refimage = image break except: pass if not refimage: print "BAD USER! No image corresponds to the filter: %s" % reffilt return else: refroot = 's' + refimage.split('.')[0] #first make sure to add back in background of sky iraf.iqsubsky(inlist, sub=no, skykey=skykey) #put reference image first on list infiles.remove(refimage) infiles.insert(0, refimage) #setup for keywords if gain == "!GAIN": try: gainval = float(get_head(image, gain)) except: print "Bad header keyword for gain." else: gainval = float(gain) if readnoise == "!READNOISE": try: readval = float(get_head(image, readnoise)) except: print "Bad header keyword for readnoise." else: readval = float(readnoise) # Process each file in turn for image in infiles: # Check that the image is there check_exist(image, "r") # Grab image root name root = image.split('.')[0] # Map image to reference image if not (image == refimage): [nx, ny] = get_head(image, ['NAXIS1', 'NAXIS2']) stars = Starlist(get_head(image, 'STARFILE')) refstars = Starlist(get_head(refimage, 'STARFILE')) refstars.pix2wcs(refimage) refstars.wcs2pix(image) match, refmatch = stars.match(refstars, useflags=yes, tol=10.0) nstars = len(match) if not (nstars > 2): print 'Could not find star matches between reference and %s' % image infiles.remove(image) continue refmatch.pix2wcs(image) refmatch.wcs2pix(refimage) matchfile = open('%s.match' % root, 'w') for i in range(len(match)): matchfile.write('%10.3f%10.3f%10.3f%10.3f\n' % (refmatch[i].xval, refmatch[i].yval, match[i].xval, match[i].yval)) matchfile.close() check_exist('%s.geodb' % root, 'w', clobber=clobber) iraf.geomap('%s.match' % root, '%s.geodb' % root, 1.0, nx, 1.0, ny, verbose=no, interactive=no) check_exist('s%s.fits' % root, 'w', clobber=clobber) iraf.geotran(image, 's%s' % root, '%s.geodb' % root, '%s.match' % root, geometry="geometric", boundary="constant", verbose=no) else: iraf.imcopy(image, 's%s' % root) root = 's%s' % root #get sky level and calculate sigma #if check_head(image, skykey): # try: # sky=float(get_head(image, skykey)) # except: # print "No sky levels in header." #sigma= (((sky * gainval) + readval**2)**.5) / gainval iraf.iterstat(image) # Saturation level if not check_head(image, "SATURATE"): saturate = 60000.0 else: saturate = get_head(image, "SATURATE") # Update datapars and daopars iraf.datapars.fwhmpsf = fwhm iraf.datapars.sigma = iraf.iterstat.sigma iraf.datapars.datamin = iraf.iterstat.median - 10 * iraf.iterstat.sigma iraf.datapars.datamax = 0.90 * saturate iraf.datapars.readnoise = readval iraf.datapars.epadu = gainval iraf.datapars.filter = filtkey iraf.daopars.psfrad = psfmult * fwhm iraf.daopars.fitrad = fwhm iraf.daopars.function = "gauss,moffat15,moffat25,lorentz,penny1" #find stars in image unless a starlist is given if image == refimage and starfile == None: iraf.daophot.daofind(root, 'refimage.coo.1', threshold=threshold, verify=no, verbose=verbose) elif image == refimage: shutil.copy(starfile, 'refimage.coo.1') #initial photometry iraf.daophot.phot(root, 'refimage.coo.1', 'default', aperture=fwhm, verify=no, verbose=verbose) #select stars for psf the first time refstarsfile = "refimage.pst.1" if image == refimage: iraf.pstselect(root, 'default', refstarsfile, maxnpsf, interactive=yes, verify=no, verbose=verbose) #fit the psf iraf.psf(root, 'default', refstarsfile, 'default', 'default', 'default', interactive=interact, verify=no, verbose=verbose) #identify neighboring/interfering stars to selected stars groupingfile = root + ".psg.1" iraf.nstar(root, groupingfile, 'default', 'default', 'default', psfrad=psfmultsmall * fwhm, verify=no, verbose=verbose) #subtract out neighboring stars from image iraf.substar(root, 'default', refstarsfile, 'default', 'default', psfrad=psfmultsmall * fwhm, verify=no, verbose=verbose) #repeat psf to get better psf model #IRAF's interactive version usually crashes subtractedimage = root + ".sub.1" iraf.psf(subtractedimage, root + ".nst.1", refstarsfile, '%s.psf.2' % root, '%s.pst.2' % root, '%s.psg.2' % root, interactive=interact, verify=no, verbose=verbose) #Need to make sure SN was detected by daofind stars = Starlist('%s.mag.1' % root) SN = Star(name='SN', radeg=ra, dcdeg=dec, fwhm=2.0, fwhmw=2.0) SNlis = Starlist(stars=[SN]) SNlis.wcs2pix(image) if (len(stars.match(SNlis)[0]) == 0): #No match - need to add to daofind file print "No match!" coofile = open('refimage.coo.1', 'a+') coofile.write('%10.3f%10.3f%9.3f%8.3f%13.3f%12.3f%8i\n' % (SNlis[0].xval, SNlis[0].yval, 99.999, 0.500, 0.000, 0.000, 999)) coofile.close() #repeat aperture photometry to get good comparisons to standard fields iraf.daophot.phot(root, 'refimage.coo.1', 'default', aperture=psfmult * fwhm, verify=no, verbose=verbose) # allstar run iraf.allstar(root, 'default', 'default', 'default', 'default', 'default', verify=no, verbose=verbose)
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 doaphot_psf_photometry(path, imageFile, extent, extension): data, image, hdulist, size, mid_point = load_image_data( imageFile, extent, extension=extension) # garbage collect data and image data = None image = None # import IRAF packages iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) # dao_params.txt must be created manually using daoedit in iraf/pyraf for 5 stars dao_params = extract_dao_params( imageFile.strip(".fits") + "_dao_params.txt") sky = dao_params[0] sky_sigma = dao_params[1] fwhm = dao_params[2] datamin = dao_params[3] aperature_radius = dao_params[4] annulus_inner_radius = dao_params[5] annulus_outer_radius = dao_params[6] # get datapars datapars = extract_header_info(hdulist) datamax = datapars[0] ccdread = datapars[1] gain = datapars[2] readnoise = datapars[3] epadu = datapars[4] exposure = datapars[5] airmass = datapars[6] filter = datapars[7] obstime = datapars[8] itime = datapars[9] xairmass = datapars[10] ifilter = datapars[11] otime = datapars[12] # set datapars iraf.datapars.unlearn() iraf.datapars.setParam('fwhmpsf', fwhm) iraf.datapars.setParam('sigma', sky_sigma) iraf.datapars.setParam('datamin', datamin) iraf.datapars.setParam('datamax', datamax) iraf.datapars.setParam('ccdread', ccdread) iraf.datapars.setParam('gain', gain) iraf.datapars.setParam('readnoise', readnoise) iraf.datapars.setParam('epadu', epadu) iraf.datapars.setParam('exposure', exposure) iraf.datapars.setParam('airmass', airmass) iraf.datapars.setParam('filter', filter) iraf.datapars.setParam('obstime', obstime) iraf.datapars.setParam('itime', itime) iraf.datapars.setParam('xairmass', xairmass) iraf.datapars.setParam('ifilter', ifilter) iraf.datapars.setParam('otime', otime) # set photpars iraf.photpars.unlearn() iraf.photpars.setParam('apertures', aperature_radius) zp_estimate = iraf.photpars.getParam('zmag') # set centerpars iraf.centerpars.unlearn() iraf.centerpars.setParam('calgorithm', 'centroid') iraf.centerpars.setParam('cbox', 5.) # set fitskypars iraf.fitskypars.unlearn() iraf.fitskypars.setParam('annulus', annulus_inner_radius) iraf.fitskypars.setParam('dannulus', annulus_outer_radius) # run phot run_phot(imageFile, imageFile + ".quality.coo") # set daopars iraf.daopars.unlearn() iraf.daopars.setParam('function', 'auto') iraf.daopars.setParam('psfrad', 2 * int(fwhm) + 1) iraf.daopars.setParam('fitrad', fwhm) # select a psf/prf star # taking whatever the default selection is, can't see a way to pass coords of desired # stars, if could would use those in dao_params.txt # An alternative is to reorder the objects so those in dao_params.txt are at top of # sources table, assuming those are the defaults selected here. iraf.pstselect.unlearn() iraf.pstselect.setParam('maxnpsf', 5) iraf.pstselect(image=imageFile, photfile=imageFile + ".mags.1", pstfile=imageFile + ".pst.1", interactive='no') # fit the psf iraf.psf.unlearn() iraf.psf(image=imageFile, \ photfile=imageFile+".mags.1",\ pstfile=imageFile+".pst.1",\ psfimage=imageFile+".psf.1.fits",\ opstfile=imageFile+".pst.2",\ groupfile=imageFile+".psg.1",\ interactive='no') # check the psf # perhaps pass it through ML and visualise # save visualisation for later manual checks iraf.seepsf.unlearn() iraf.seepsf(psfimage=imageFile + ".psf.1.fits", image=imageFile + ".psf.1s.fits") hdulist_psf = fits.open(imageFile + ".psf.1s.fits") #print "[*] plotting PSF for visual check." #plt.imshow(hdulist_psf[0].data, interpolation="nearest",cmap="hot") #plt.axis("off") #plt.show() # perform photometry run_allstar(imageFile, imageFile + ".psf.1.fits") return zp_estimate, imageFile + ".psf.1.fits"
def doaphot_psf_photometry(path, imageFile, extent, extension): data, image, hdulist, size, mid_point = load_image_data(imageFile, extent, extension=extension) # garbage collect data and image data = None image = None # import IRAF packages iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) # dao_params.txt must be created manually using daoedit in iraf/pyraf for 5 stars dao_params = extract_dao_params(imageFile.strip(".fits")+"_dao_params.txt") sky = dao_params[0] sky_sigma = dao_params[1] fwhm = dao_params[2] datamin = dao_params[3] aperature_radius = dao_params[4] annulus_inner_radius = dao_params[5] annulus_outer_radius = dao_params[6] # get datapars datapars = extract_header_info(hdulist) datamax = datapars[0] ccdread = datapars[1] gain = datapars[2] readnoise = datapars[3] epadu = datapars[4] exposure = datapars[5] airmass = datapars[6] filter = datapars[7] obstime = datapars[8] itime = datapars[9] xairmass = datapars[10] ifilter = datapars[11] otime = datapars[12] # set datapars iraf.datapars.unlearn() iraf.datapars.setParam('fwhmpsf',fwhm) iraf.datapars.setParam('sigma',sky_sigma) iraf.datapars.setParam('datamin',datamin) iraf.datapars.setParam('datamax',datamax) iraf.datapars.setParam('ccdread',ccdread) iraf.datapars.setParam('gain',gain) iraf.datapars.setParam('readnoise',readnoise) iraf.datapars.setParam('epadu',epadu) iraf.datapars.setParam('exposure',exposure) iraf.datapars.setParam('airmass',airmass) iraf.datapars.setParam('filter',filter) iraf.datapars.setParam('obstime',obstime) iraf.datapars.setParam('itime',itime) iraf.datapars.setParam('xairmass',xairmass) iraf.datapars.setParam('ifilter',ifilter) iraf.datapars.setParam('otime',otime) # set photpars iraf.photpars.unlearn() iraf.photpars.setParam('apertures',aperature_radius) zp_estimate = iraf.photpars.getParam('zmag') # set centerpars iraf.centerpars.unlearn() iraf.centerpars.setParam('calgorithm','centroid') iraf.centerpars.setParam('cbox',5.) # set fitskypars iraf.fitskypars.unlearn() iraf.fitskypars.setParam('annulus',annulus_inner_radius) iraf.fitskypars.setParam('dannulus',annulus_outer_radius) # run phot run_phot(imageFile, imageFile+".quality.coo") # set daopars iraf.daopars.unlearn() iraf.daopars.setParam('function','auto') iraf.daopars.setParam('psfrad', 2*int(fwhm)+1) iraf.daopars.setParam('fitrad', fwhm) # select a psf/prf star # taking whatever the default selection is, can't see a way to pass coords of desired # stars, if could would use those in dao_params.txt # An alternative is to reorder the objects so those in dao_params.txt are at top of # sources table, assuming those are the defaults selected here. iraf.pstselect.unlearn() iraf.pstselect.setParam('maxnpsf',5) iraf.pstselect(image=imageFile,photfile=imageFile+".mags.1",pstfile=imageFile+".pst.1",interactive='no') # fit the psf iraf.psf.unlearn() iraf.psf(image=imageFile, \ photfile=imageFile+".mags.1",\ pstfile=imageFile+".pst.1",\ psfimage=imageFile+".psf.1.fits",\ opstfile=imageFile+".pst.2",\ groupfile=imageFile+".psg.1",\ interactive='no') # check the psf # perhaps pass it through ML and visualise # save visualisation for later manual checks iraf.seepsf.unlearn() iraf.seepsf(psfimage=imageFile+".psf.1.fits", image=imageFile+".psf.1s.fits") hdulist_psf = fits.open(imageFile+".psf.1s.fits") #print "[*] plotting PSF for visual check." #plt.imshow(hdulist_psf[0].data, interpolation="nearest",cmap="hot") #plt.axis("off") #plt.show() # perform photometry run_allstar(imageFile,imageFile+".psf.1.fits") return zp_estimate, imageFile+".psf.1.fits"
continue pst1.add_row( (mag1['ID'][ind[0]], mag1['XINIT'][ind[0]], mag1['YINIT'][ind[0]], mag1['MAG'][ind[0]], mag1['MSKY'][ind[0]])) pst1.write(fn[:-5] + '.pst.0', format='ascii.commented_header', delimiter='\t', comment='#N ID XCENTER YCENTER \ MAG MSKY\n#U ## pixels pixels magnitudes counts\n#F %-9d %-10.3f %-10.3f %-12.3f %-15.7g\n#' ) original = sys.stdout try: sys.stdout = open(fn[:-5] + '.psf1.out', 'w') iraf.psf(fn[:-5],photfile=fn[:-5]+'.mag.1',pstfile=fn[:-5]+'.pst.0',psfimage=fn[:-5]+'.psf.1',opstfile=fn[:-5]+'.pst.1', \ groupfile=fn[:-5]+'.psg.1',verify='no',interactive='no',plotfile=fn[:-5]+'.psf1.plots') iraf.allstar(fn[:-5],photfile=fn[:-5]+'.psg.1',psfimage=fn[:-5]+'.psf.1',allstarfile=fn[:-5]+'.als.1',rejfile=fn[:-5]+ \ '.arj.1',subimage=fn[:-5]+'.sub.1',verify='no',verbos='no') iraf.substar(fn[:-5],photfile=fn[:-5]+'.als.1',exfile=fn[:-5]+'.pst.1',psfimage=fn[:-5]+'.psf.1',subimage=fn[:-5]+ \ '.sub.11',verify='no',verbose='no') sys.stdout = open(fn[:-5] + '.psf2.out', 'w') iraf.psf(fn[:-5]+'.sub.11',photfile=fn[:-5]+'.mag.1',pstfile=fn[:-5]+'.pst.1',psfimage=fn[:-5]+'.psf.2', \ opstfile=fn[:-5]+'.pst.2', groupfile=fn[:-5]+'.psg.2',verify='no',interactive='no') iraf.allstar(fn[:-5],photfile=fn[:-5]+'.psg.2',psfimage=fn[:-5]+'.psf.2',allstarfile=fn[:-5]+'.als.2',rejfile=fn[:-5]+ \ '.arj.2',subimage=fn[:-5]+'.sub.2',verify='no',verbos='no') iraf.substar(fn[:-5],photfile=fn[:-5]+'.als.2',exfile=fn[:-5]+'.pst.2',psfimage=fn[:-5]+'.psf.2',subimage=fn[:-5]+ \ '.sub.22',verify='no',verbose='no') sys.stdout = open(fn[:-5] + '.psf3.out', 'w') iraf.psf(fn[:-5]+'.sub.22',photfile=fn[:-5]+'.mag.1',pstfile=fn[:-5]+'.pst.2',psfimage=fn[:-5]+'.psf.3',opstfile \
star_id = prts[0] xpos = prts[1] ypos = prts[2] #print star_id, xpos, ypos pstregfl.write('point(' + str(xpos) + ',' + str(ypos) + ') # point=cross text={' + star_id + '}\n') pstregfl.close() os.system("/usr/local/bin//ds9 " + flnm + '.sub.0.fits' + " -regions " + flnm + ".pst.1.reg" + " &") tfs = time.time() iraf.psf(image=flnm + '.sub.0.fits', photfile=flnm + '.sub.0.mag', pstfile=flnm + '.pst.1', psfimage=flnm + '.psf.fits', opstfile=flnm + '.pst.2', groupfile=flnm + '.grf.psg', interactive="no", showplots="no", verify="no") tfe = time.time() times[runnm + '_psf'] = tfe - tfs iraf.seepsf(psfimage=flnm + '.psf.fits', image=flnm + '.image_of_psf.fits') if i == 0: shutil.copy(runnm + '.mag', runnm + '.tot.mag') # formatting reasons only else: print "=========================================" print "...merging phot lists"
def compute_psf_image(params,g,psf_deg=1,psf_rad=8, star_file='phot.mags',psf_image='psf.fits',edge_dist=5): iraf.digiphot() iraf.daophot() fp = params.loc_output+os.path.sep f_im = g.image*g.mask f = fp+'temp.ref.fits' write_image(f_im,f) g.fw = np.max([1.5,g.fw]) g.fw = np.min([0.5*params.psf_max_radius,g.fw]) logfile = fp+'psf.log' fd = fits.getdata(f) xmax = fd.shape[0] - edge_dist ymax = fd.shape[1] - edge_dist for d in ['temp.stars','temp.phot','temp.phot1','temp.phot2','temp.pst', 'temp.opst','temp.opst2', 'temp.psf.fits','temp.psf1.fits','temp.psf2.fits','temp.psg', 'temp.psg2','temp.psg3','temp.psg5','temp.rej','temp.rej2', 'temp.sub.fits','temp.sub1.fits', 'temp.sub2.fits','temp.opst1','temp.opst3','temp.rej3', 'temp.nst','temp.stars1','ref.mags',psf_image,'temp.als', 'temp.als2']: if os.path.exists(fp+d): os.remove(fp+d) # locate stars iraf.daofind(image=f,output=fp+'temp.stars',interactive='no',verify='no', threshold=3,sigma=params.star_detect_sigma,fwhmpsf=g.fw, datamin=1,datamax=params.pixel_max, epadu=params.gain,readnoise=params.readnoise, noise='poisson') if params.star_file: als_recenter = 'no' all_template_stars = np.genfromtxt(params.star_file) all_new_stars = np.genfromtxt(fp+'temp.stars') if all_new_stars.shape[0] > params.star_file_number_match: new_stars = all_new_stars[all_new_stars[:,2].argsort()][:params.star_file_number_match] else: new_stars = all_new_stars if all_template_stars.shape[0] > params.star_file_number_match: template_stars = all_template_stars[all_template_stars[:,3].argsort()][:params.star_file_number_match] else: template_stars = all_template_stars tx, ty = compute_xy_shift(new_stars,template_stars[:,1:3],0.5, degree=params.star_file_transform_degree) if params.star_file_has_magnitudes: star_positions = all_template_stars[:,1:4] xx = (star_positions[:,0]-np.mean(new_stars[:,0]))/np.mean(new_stars[:,0]) yy = (star_positions[:,1]-np.mean(new_stars[:,1]))/np.mean(new_stars[:,1]) for m in range(params.star_file_transform_degree+1): for n in range(params.star_file_transform_degree+1-m): star_positions[:,0] += tx[m,n]* xx**m * yy**n star_positions[:,1] += ty[m,n]* xx**m * yy**n np.savetxt(fp+'temp.stars.1',star_positions,fmt='%10.3f %10.3f %10.3f') else: star_positions = all_template_stars[:,1:3] xx = (star_positions[:,0]-np.mean(new_stars[:,0]))/np.mean(new_stars[:,0]) yy = (star_positions[:,1]-np.mean(new_stars[:,1]))/np.mean(new_stars[:,1]) for m in range(params.star_file_transform_degree+1): for n in range(params.star_file_transform_degree+1-m): star_positions[:,0] += tx[m,n]* xx**m * yy**n star_positions[:,1] += ty[m,n]* xx**m * yy**n np.savetxt(fp+'temp.stars.1',star_positions,fmt='%10.3f %10.3f') all_template_stars[:,1] = star_positions[:,0] all_template_stars[:,2] = star_positions[:,1] else: als_recenter = 'yes' star_positions = np.genfromtxt(fp+'temp.stars') np.savetxt(fp+'temp.stars.1',star_positions[:,:2],fmt='%10.3f %10.3f') iraf.phot(image=f,output=fp+'temp.phot',coords=fp+'temp.stars.1',interactive='no', verify='no', sigma=params.star_detect_sigma,fwhmpsf=g.fw,apertures=g.fw, datamin=1, datamax=2*params.pixel_max,epadu=params.gain,annulus=3*g.fw, dannulus=3.0, readnoise=params.readnoise,noise='poisson') print 'fw = ',g.fw #fw = np.max([4.0,fw]) #print 'fw = ',fw # select PSF stars iraf.pstselect(image=f,photfile=fp+'temp.phot',pstfile=fp+'temp.pst',maxnpsf=40, interactive='no',verify='no',datamin=1,fitrad=2.0, datamax=params.pixel_max,epadu=params.gain,psfrad=np.max([3.0,g.fw]), readnoise=params.readnoise,noise='poisson') if params.star_file and params.star_file_has_magnitudes: # We don't need to do the photometry - only make the PSF # Initial PSF estimate to generate PSF groups #psfrad=3*np.max([g.fw,1.8]) iraf.psf(image=f,photfile=fp+'temp.phot',pstfile=fp+'temp.pst',psfimage=fp+'temp.psf', function=params.psf_profile_type,opstfile=fp+'temp.opst', groupfile=fp+'temp.psg', interactive='no', verify='no',varorder=0 ,psfrad=2*np.max([g.fw,1.8]), datamin=-10000,datamax=0.95*params.pixel_max, scale=1.0) # construct a file of the psf neighbour stars slist = [] psf_stars = np.loadtxt(fp+'temp.opst',usecols=(0,1,2)) for star in range(psf_stars.shape[0]): xp = psf_stars[star,1] yp = psf_stars[star,2] xmin = np.max([np.int(xp-10*g.fw),0]) xmax = np.min([np.int(xp+10*g.fw),f_im.shape[0]]) ymin = np.max([np.int(yp-10*g.fw),0]) ymax = np.min([np.int(yp+10*g.fw),f_im.shape[1]]) p = star_positions[np.logical_and(np.logical_and(star_positions[:,0]>xmin, star_positions[:,0]<xmax), np.logical_and(star_positions[:,1]>ymin, star_positions[:,1]<ymax))] slist.append(p) group_stars = np.concatenate(slist) np.savetxt(fp+'temp.nst',group_stars,fmt='%10.3f %10.3f %10.3f') # subtract PSF star neighbours iraf.substar(image=f,photfile=fp+'temp.nst',psfimage=fp+'temp.psf', exfile=fp+'temp.opst',fitrad=2.0, subimage=fp+'temp.sub1',verify='no',datamin=1, datamax=params.pixel_max,epadu=params.gain, readnoise=params.readnoise,noise='poisson') # final PSF iraf.psf(image=fp+'temp.sub1',photfile=fp+'temp.phot',pstfile=fp+'temp.opst', psfimage=psf_image,psfrad=2*g.fw, function=params.psf_profile_type,opstfile=fp+'temp.opst2', groupfile=fp+'temp.psg2', interactive='no', verify='no',varorder=0, datamin=1,datamax=0.95*params.pixel_max, scale=1.0) np.savetxt(fp+'ref.mags',all_template_stars,fmt='%7d %10.3f %10.3f %10.3f') stars = all_template_stars else: # initial PSF estimate iraf.psf(image=f,photfile=fp+'temp.phot',pstfile=fp+'temp.pst',psfimage=fp+'temp.psf', function=params.psf_profile_type,opstfile=fp+'temp.opst', groupfile=fp+'temp.psg1', interactive='no', verify='no',varorder=0 ,psfrad=2*g.fw, datamin=1,datamax=0.95*params.pixel_max, scale=1.0) # separation distance of near neighbours separation = np.max([rewrite_psg(fp+'temp.psg1',fp+'temp.psg2'),3]) print 'separation = ',separation # subtract all stars using truncated PSF iraf.allstar(image=f,photfile=fp+'temp.phot',psfimage=fp+'temp.psf', allstarfile=fp+'temp.als',rejfile='', subimage=fp+'temp.sub',verify='no',psfrad=2*g.fw,fitrad=2.0, recenter='yes',groupsky='yes',fitsky='yes',sannulus=7,wsannulus=10, datamin=1,datamax=params.pixel_max, epadu=params.gain,readnoise=params.readnoise, noise='poisson') if params.star_file: os.system('cp '+fp+'temp.phot '+fp+'temp.phot2') else: # locate new stars iraf.daofind(image=fp+'temp.sub',output=fp+'temp.stars1',interactive='no',verify='no', threshold=3,sigma=params.star_detect_sigma,fwhmpsf=2*g.fw, datamin=1,datamax=params.pixel_max, epadu=params.gain,readnoise=params.readnoise, noise='poisson') # magnitudes for new stars iraf.phot(image=fp+'temp.sub',output=fp+'temp.phot1',coords=fp+'temp.stars1', interactive='no', verify='no',sigma=params.star_detect_sigma, fwhmpsf=g.fw,datamin=1, datamax=params.pixel_max,epadu=params.gain, readnoise=params.readnoise,noise='poisson') # join star lists together iraf.pconcat(infiles=fp+'temp.phot,'+fp+'temp.phot1',outfile=fp+'temp.phot2') # new PSF estimate to generate PSF groups iraf.psf(image=f,photfile=fp+'temp.phot2',pstfile=fp+'temp.pst',psfimage=fp+'temp.psf2', function=params.psf_profile_type,opstfile=fp+'temp.opst2', groupfile=fp+'temp.psg3', interactive='no', verify='no',varorder=0 ,psfrad=2*g.fw, datamin=-10000,datamax=0.95*params.pixel_max, scale=1.0) # magnitudes for PSF group stars iraf.nstar(image=f,groupfile=fp+'temp.psg3',psfimage=fp+'temp.psf2', nstarfile=fp+'temp.nst', rejfile='',verify='no',psfrad=2*g.fw,fitrad=2.0, recenter='no', groupsky='yes',fitsky='yes',sannulus=7,wsannulus=10, datamin=1,datamax=params.pixel_max, epadu=params.gain,readnoise=params.readnoise,noise='poisson') # subtract PSF star neighbours iraf.substar(image=f,photfile=fp+'temp.nst',psfimage=fp+'temp.psf2', exfile=fp+'temp.opst2',fitrad=2.0, subimage=fp+'temp.sub1',verify='no',datamin=1, datamax=params.pixel_max,epadu=params.gain, readnoise=params.readnoise,noise='poisson') # final PSF iraf.psf(image=fp+'temp.sub1',photfile=fp+'temp.phot2', pstfile=fp+'temp.opst2', psfimage=psf_image,psfrad=2*g.fw, function=params.psf_profile_type,opstfile=fp+'temp.opst3', groupfile=fp+'temp.psg5', interactive='no', verify='no',varorder=0, datamin=1,datamax=0.95*params.pixel_max, scale=1.0) # final photometry iraf.allstar(image=g.fullname,photfile=fp+'temp.phot2',psfimage=psf_image, allstarfile=fp+'temp.als2',rejfile='', subimage=fp+'temp.sub2',verify='no',psfrad=2*g.fw, recenter=als_recenter,groupsky='yes',fitsky='yes',sannulus=7, wsannulus=10,fitrad=2.0, datamin=params.pixel_min,datamax=params.pixel_max, epadu=params.gain,readnoise=params.readnoise, noise='poisson') psfmag = 10.0 for line in open(fp+'temp.als2','r'): sline = line.split() if sline[1] == 'PSFMAG': psfmag = float(sline[3]) break if params.star_file: iraf.psort(infiles=fp+'temp.als2',field='ID') os.system('cp '+fp+'temp.als2 '+fp+'temp.als3') else: selection = 'XCE >= '+str(edge_dist)+' && XCE <= '+str(xmax)+' && YCE >= '+str(edge_dist)+' && YCE <= '+str(ymax)+' && MAG != INDEF' iraf.pselect(infiles=fp+'temp.als2',outfiles=fp+'temp.als3',expr=selection) iraf.psort(infiles=fp+'temp.als3',field='MAG') iraf.prenumber(infile=fp+'temp.als3') s = iraf.pdump(infiles=fp+'temp.als3',Stdout=1, fields='ID,XCENTER,YCENTER,MAG,MERR,MSKY,SHARPNESS,CHI',expr='yes') sf = [k.replace('INDEF','22.00') for k in s] stars = np.zeros([len(sf),5]) for i, line in enumerate(sf): stars[i,:] = np.array(map(float,sf[i].split()[1:6])) s = iraf.pdump(infiles=fp+'temp.als3',Stdout=1, fields='ID,XCENTER,YCENTER,MAG,MERR,SHARPNESS,CHI,MSKY',expr='yes') sf = [k.replace('INDEF','22.00') for k in s] with open(fp+'ref.mags','w') as fid: for s in sf: fid.write(s+'\n') return stars