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 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 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 daophot(inp, fwhm): iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) iraf.datapars.fwhmpsf = float(fwhm) iraf.datapars.datamin = 10 iraf.datapars.datamax = 60000 iraf.datapars.sigma = 50 iraf.datapars.readnoise = 2.3 iraf.datapars.epadu = 0.8 iraf.centerpars.calgorithm = 'centroid' iraf.centerpars.cbox = 6 iraf.fitskypars.annulus = 20 iraf.fitskypars.dannulus = 10 app = str(fwhm) + ":" + str(4 * float(fwhm)) + ":" + "1" iraf.photpars.apertures = app iraf.photpars.zmag = 25 iraf.phot(image=inp, output='default', verify='No', verbose='No', coords='default')
def doiraf(ximage, yimage, xmax, ymax): (xpos, ypos) = getpositions(ximage, yimage, xmax, ymax) coords = open("noisecoords.dat", 'w') for i in range(len(xpos)): coords.write("%8.1f %8.1f \n" % (xpos[i], ypos[i])) coords.close() sky = open("sky", 'w') for i in range(npoints): sky.write("0.0 \n") sky.close() aps = open("apertures", 'w') aps.write("1,1.5,2,2.6,3,3.5,4,4.5,5,5.5") aps.close() #runiraf() iraf.digiphot() iraf.daophot() ##iraf.apphot() image = 'mosaic_minus_median_extract.fits' #print image os.system("rm noise.dat") iraf.digiphot.daophot.phot(image, coords="noisecoords.dat", output="noise.dat", calgorithm='none', skyfile="sky", salgori="file", aperture="apertures", interactive="no", verify='no', verbose='no')
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 psffit2(img, fwhm, psfstars, hdr, _datamax=45000, psffun='gauss', fixaperture=False): ''' giving an image, a psffile, calculate the magnitudes of strs in 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.photpars.zmag = zmag iraf.centerpars.calgori = 'centroid' iraf.centerpars.cbox = a2 iraf.daopars.recenter = 'yes' iraf.delete('_psf2.ma*', verify=False) iraf.phot(img+'[0]', '_psf2.coo', '_psf2.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 iraf.delete("_als2,_psf.grp,_psf.nrj", verify=False) iraf.group(img + '[0]', '_psf2.mag', img + '.psf', '_psf.grp', verify=False, verbose=False) iraf.nstar(img + '[0]', '_psf.grp', img + '.psf', '_als2', '_psf.nrj', verify=False, verbose=False) photmag = iraf.txdump("_psf2.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1) fitmag = iraf.txdump("_als2", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1) return photmag, fitmag
def psffit2(img, fwhm, psfstars, hdr, _datamax, psffun='gauss', fixaperture=False): ''' giving an image, a psffile, calculate the magnitudes of strs in 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.photpars.zmag = zmag iraf.centerpars.calgori = 'centroid' iraf.centerpars.cbox = a2 iraf.daopars.recenter = 'yes' iraf.delete('_psf2.ma*', verify=False) iraf.phot(img+'[0]', '_psf2.coo', '_psf2.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 iraf.delete("_als2,_psf.grp,_psf.nrj", verify=False) iraf.group(img + '[0]', '_psf2.mag', img + '.psf', '_psf.grp', verify=False, verbose=False) iraf.nstar(img + '[0]', '_psf.grp', img + '.psf', '_als2', '_psf.nrj', verify=False, verbose=False) photmag = iraf.txdump("_psf2.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1) fitmag = iraf.txdump("_als2", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1) return photmag, fitmag
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 findStars(frame,fwhm,sigma,thresh,extra="",overwrite=True,interactive=True): if overwrite: if os.path.isfile(frame+".fullcoo"): os.system("rm "+frame+".fullcoo") iraf.noao() iraf.digiphot() iraf.daophot() iraf.datapars.setParam("fwhmpsf",fwhm) iraf.datapars.setParam("sigma",sigma) iraf.findpars.setParam("thresh",thresh) foo = iraf.daofind(image=frame+extra,output=frame+".fullcoo",Stdin=cr,Stdout=1)
def psf(args): """ Calculate the PSF of an image. """ # Load iraf packages: phot, pstselect, psf will be needed iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.module.daophot(_doprint=0) # 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.module.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=6*seeing, dannulus=3*seeing, apert=2*seeing, 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.module.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=2*seeing, fitrad=seeing, maxnstar=20, verbose="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.module.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=12, fitrad=seeing, maxnstar=20, interactive="no", varorder=-1, 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.module.seepsf(psffile_table, psffile_name) return psffile_name
def psffit2(img, fwhm, psfstars, hdr, _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) 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('_psf2.ma*', verify=False) iraf.phot(img+'[0]', '_psf2.coo', '_psf2.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 iraf.delete("_als,_psf.grp,_psf.nrj", verify=False) iraf.group(img+'[0]', '_psf2.mag', img + '.psf', '_psf.grp', verify=False, verbose=False) iraf.nstar(img+'[0]', '_psf.grp', img + '.psf', '_als', '_psf.nrj', verify=False, verbose=False) photmag = iraf.txdump("_psf2.mag", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1) fitmag = iraf.txdump("_als", 'xcenter,ycenter,id,mag,merr', expr='yes', Stdout=1) return photmag, fitmag
def aperture_photometry(image,photfilesuffix,threshold=4.,wcs='logical',mode='small', telescope='Pairitel'): '''perform aperture photometry on a given image This procedure perfrom IRAF/DAOPHOT aperture phtometry on (all extensions of) a fits image. It sets some global daophot parameters, then perfroms daophot.daofind and daophot.phot The output .mag file is filtered for valid magnitudes and a mag.sex file computed, where all coordinates are transformed to sexagesimal coordinates. input: image: filename keywords: threshold=4 :daofind detection threshold in sigma_sky wcs='logical' :working wcs system, can be 'logical','physical' or 'tv' in any case an additional sexagesimal output file will be computed mode='small' : 'standard' = 'large' or 'psf' = 'small' choses a pre-defined aperture size for aperture photometry of standards or just as starting point for psf photometry ''' print 'Performing aperture photometry on '+image if telescope == 'Pairitel': set_Pairitel_params() elif telescope == 'FLWO': set_FLWO_params() else: print 'No parameters found for this telescope!' iraf.mscred(_doprint=0) iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) iraf.daophot.verify = False iraf.daophot.wcsin = wcs iraf.daophot.wcsout = wcs iraf.daophot.wcspsf = wcs iraf.centerpars.cbox=2.*iraf.datapars.fwhmpsf iraf.fitskypars.annulus=20 # usually one would use 5.*iraf.datapars.fwhmpsf #annulus must be wide enough to have good sky statistics iraf.fitskypars.dannulus=10. # this is standard if 'standard'.find(mode.lower()) != -1 or 'large'.find(mode.lower()) != -1: iraf.photpars.aperture = 4.*iraf.datapars.fwhmpsf iraf.centerpars.calgorithm = 'centroid' elif 'small'.find(mode.lower()) != -1 or 'psf'.find(mode.lower()) != -1: iraf.photpars.aperture = 4. # close in for crowded regions iraf.centerpars.calgorithm = 'none' else: print '''use: aperture_photometry(image,threshold=4.,wcs="logical",mode="small") The following mode keywords are implemented: standard, large, psf, small''' raise NotImplementedError #for imagebi in fits_ext(image): sky,skydev=get_sky(image) iraf.datapars.datamin=sky-5*skydev iraf.datapars.sigma=skydev #this could be changed to use Stdin and Stdout so that fewer files are written (saves space and time), but for debug purposes better keep all that iraf.daofind(image, output='default', verbose=False, verify=False, threshold=threshold) iraf.daophot.phot(image, coords='default', output=image+photfilesuffix, verbose=False, verify=False, interactive=False, wcsout=wcs)
def set_FLWO_params(): '''Set some daophot parameters, which are typical for my FLWO images ''' iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) iraf.datapars.fwhmpsf = 4.5 iraf.datapars.datamax=40000. iraf.datapars.ccdread = "rdnoise" iraf.datapars.gain = "gain" iraf.datapars.exposure = "exptime" iraf.datapars.airmass = "air" iraf.datapars.filter = "filter" iraf.datapars.obstime="date-obs"
def run_fitparams(weight=True): if weight: weighting = 'photometric' else: weighting = 'uniform' ir.delete('standards.transParams') ir.digiphot() ir.photcal() ir.unlearn('fitparams') ir.fitparams('standards.instMag', 'photcal_ukirt_faint.dat', 'standards.config', 'standards.transParams', weighting=weighting)
def _dao_setup(fwhmpsf, threshold, sigma): iraf.digiphot(_doprint=0) iraf.digiphot.apphot(_doprint=0) datapars = iraf.noao.digiphot.apphot.datapars findpars = iraf.noao.digiphot.apphot.findpars datapars.scale = 1.0 datapars.sigma = sigma * (3.0 / 4.0) datapars.fwhmpsf = fwhmpsf # not important for source finding datapars.readnoise = 0.0 datapars.epadu = 1.0 findpars.threshold = threshold # only care about brightest
def marksn2(img,fitstab,frame=1,fitstab2='',verbose=False): from pyraf import iraf from numpy import array #,log10 import lsc iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) iraf.images(_doprint=0) iraf.imcoords(_doprint=0) iraf.proto(_doprint=0) iraf.set(stdimage='imt1024') hdr=lsc.util.readhdr(fitstab) _filter=lsc.util.readkey3(hdr,'filter') column=lsc.lscabsphotdef.makecatalogue([fitstab])[_filter][fitstab] rasex=array(column['ra0'],float) decsex=array(column['dec0'],float) if fitstab2: hdr=lsc.util.readhdr(fitstab2) _filter=lsc.util.readkey3(hdr,'filter') _exptime=lsc.util.readkey3(hdr,'exptime') column=lsc.lscabsphotdef.makecatalogue([fitstab2])[_filter][fitstab2] rasex2=array(column['ra0'],float) decsex2=array(column['dec0'],float) iraf.set(stdimage='imt1024') iraf.display(img + '[0]',frame,fill=True,Stdout=1) vector=[] for i in range(0,len(rasex)): vector.append(str(rasex[i])+' '+str(decsex[i])) xy = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\ formats='%10.1f %10.1f',verbose='yes')[3:] iraf.tvmark(frame,'STDIN',Stdin=list(xy),mark="circle",number='yes',label='no',radii=10,nxoffse=5,nyoffse=5,color=207,txsize=2) if verbose: # print 2.5*log10(_exptime) for i in range(0,len(column['ra0'])): print xy[i],column['ra0'][i],column['dec0'][i],column['magp3'][i],column['magp4'][i],column['smagf'][i],column['magp2'][i] if fitstab2: vector2=[] for i in range(0,len(rasex2)): vector2.append(str(rasex2[i])+' '+str(decsex2[i])) xy1 = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector2,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\ formats='%10.1f %10.1f',verbose='yes')[3:] iraf.tvmark(frame,'STDIN',Stdin=list(xy1),mark="cross",number='yes',label='no',radii=10,nxoffse=5,nyoffse=5,color=205,txsize=2)
def detect_stars(f,params): print 'Detecting stars in',f.name print 'Current directory is', os.getcwd() fp = params.loc_output+os.path.sep fn = f.fullname iraf.digiphot() iraf.daophot() print 'FWHM = ',f.fw nstars = 0 thresh = 100 while (nstars < 2*params.nstamps) and (thresh > 1.5): print 'thresh = ',thresh for d in ['temp.stars','temp.phot']: if os.path.exists(fp+d): os.system('/bin/rm '+fp+d) iraf.daofind(image=fn,output=fp+'temp.stars',interactive='no',verify='no', threshold=thresh,sigma=30,fwhmpsf=f.fw, datamin=params.pixel_min,datamax=params.pixel_max, epadu=params.gain,readnoise=params.readnoise, noise='poisson') iraf.phot(image=fn,output=fp+'temp.phot',coords=fp+'temp.stars',interactive='no', verify='no', sigma=30,fwhmpsf=f.fw,datamin=params.pixel_min, datamax=params.pixel_max,epadu=params.gain, readnoise=params.readnoise,noise='poisson',Stdout='/dev/null') nstars = 0 if os.path.exists(fp+'temp.phot'): iraf.psort(infiles=fp+'temp.phot',field='MAG') iraf.prenumber(infile=fp+'temp.phot') s = iraf.pdump(infiles=fp+'temp.phot',Stdout=1,fields='ID,XCENTER,YCENTER,MAG', expr='yes') stars = np.zeros([len(s),3]) i = 0 for line in s: mag = line.split()[3] if not(mag == 'INDEF'): stars[i,:] = np.array(map(float,line.split()[1:4])) i += 1 nstars = i thresh = thresh*0.5 if nstars == 0: print 'Error: could not detect stars in',fn return None stars = stars[:i,:].copy() sys.old_stdout = sys.stdout return stars
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 make_iraf_psf(fname): with fits.open(fname) as stp: # Read in image info placed in header previously header = stp[0].header ra = header["RA"] dec = header["DEC"] for ext in range(len(stp)): # Look in each header section try: header = stp[ext].header expnum = header["EXPNUM"] except: continue stamp = fits.open(fname) for ext in range( len(stamp) ): # Establish size of image and location of source in image (source should be centered in cutout) try: world_coords = wcs.WCS(stamp[ext].header) image_coords = world_coords.sky2xy(float(ra), float(dec)) image_size = stamp[ext].data.shape except: continue # Some cutouts are split over multiple CCDs, meaning the source may not actually be in some of these small image 'pieces' if 0 < image_coords[0] < image_size[0] and 0 < image_coords[1] < image_size[1]: ccdnum = stamp[ext].header.get("EXTNO", None) psf_file = storage.get_file(expnum, ccd=ccdnum, ext="psf.fits") # Get psf file for image from storage iraf.noao() iraf.digiphot() iraf.daophot(_doprint=0) psf_image = psf_file.replace("psf", "seepsf") # Seepsf creates psf_image iraf.seepsf(psf_file, psf_image, xpsf=image_coords[0], ypsf=image_coords[1]) os.unlink(psf_file) return psf_image else: return None
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 doPhot(): print 'Running doPhot' try: mapFile = open('map.dat') except: print 'map.dat not found. Exiting.' sys.exit() try: changeFile = open('change.dat') except: print 'change.dat not found. Exiting.' sys.exit() coordList = list() for line in mapFile: coords = map(float,line.split()) coordList.append(coords) coordList = np.array(coordList) iraf.noao() iraf.digiphot() iraf.daophot() iraf.ptools() iraf.set(clobber='yes') photFile = open('phot.dat', 'w') for line in changeFile.readlines(): elms = line.split() imageName = elms[0] changeCoords = np.array([float(elms[1]),float(elms[2])]) newCoords = coordList + changeCoords print 'Image: '+imageName coordFile = makeCoordFile(newCoords) iraf.noao.digiphot.daophot.phot(image=imagesDir+imageName, coords=coordFile, output='.temp-phot', skyfile='', verify='no', fwhmpsf=fwhmpsf, sigma=sigma, readnoise=readnoise, epadu=epadu, exposure=exposureHeader, obstime=obstimeHeader, calgorithm=calgorithm, cbox=cbox, apertures=apertures, annulus=annulus, dannulus=dannulus) result = iraf.noao.digiphot.ptools.txdump(Stdout=1, textfiles='.temp-phot', fields='mag, merr, otime', expr='yes') writeString = result[0].split()[-1] +' '+ ' '.join([' '.join(x.split()[:2]) for x in result]) photFile.write(writeString+"\n") photFile.close() raw_input('doPhot Done. Hit return key to continue.')
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 getnoise(image, catalog): #sky-subtracted image (ximage, yimage, isoarea, fluxerriso) = readcatalog(catalog) (xpos, ypos) = getpositions(ximage, yimage, isoarea, image) measurephot(xpos, ypos, image) iraf.digiphot() iraf.daophot() iraf.phot('check.fits', coords='noisecoords.dat', output='noise.dat', skyfile='sky', salgori='file', aperture='apertures', interactive='no', calgorith='none', verify='no') (aveap, aveaperr, avearea, aveareaerr) = calcavesky() (a, b) = solveforab(aveaperr, avearea) print "a,b", a, b s = str(image) (file, post) = s.split('.') #plotnoisepylab(aveap,aveaperr,avearea,aveareaerr,a,b,file,isoarea,fluxerriso) return a, b
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 init(iraf_dir=DEFAULT_IRAF_DIR): '''Initialize IRAF for use in photometry @param iraf_dir The home directory for iraf, i.e. where login.cl and uparm are located, slightly annoying that we need this, but not much we can do about it''' global _initialized if _initialized: #already initialized no need to run again return global iraf global yes global no with workingDirectory(iraf_dir): from pyraf import iraf yes = iraf.yes no = iraf.no #now load the packages we need iraf.noao() iraf.digiphot() iraf.daophot() iraf.obsutil() _initialized = True return
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 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 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))
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 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 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 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
else: _show = True if option.redo == False: redo = False else: redo = True arterr = '' if _interactive: _show = True print _show, _interactive # ####################### SET IRAF PARAMETERS ####################### from pyraf import iraf iraf.astcat(_doprint=0) iraf.imcoords(_doprint=0) iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) zmag = 0 iraf.digiphot.daophot.photpars.zmag = zmag ################################# CHECK HEADER ###################################### warn = '##################################\n' for imglong in imglist: if imglong: if imglong[-5:] == '.fits': img = imglong[:-5] else: img = imglong ###### try: print string.split(img, '/')[-1] + '.fits' agnkey.agnsqldef.updatevalue(
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 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
import copy, os, shutil, glob, sys, string, re, math, operator, time 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:
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 organizeFiles(): # Create files that are usually generated by mkimset. # We have more information about how to properly group our # files together. imsetsFilename = 'standards.imageSets' obsparFilename = 'standards.obsParams' shiftsFilename = 'standards.shifts' iMagniFilename = 'standards.instMag' imsets = open(imsetsFilename, 'w') obspar = open(obsparFilename, 'w') shifts = open(shiftsFilename, 'w') for star in stars: roots = [] for filter in filters: dir = star.lower() + '_' + filter.lower() if filter == 'Lp': dir += '2' images = glob.glob(dataDir + dir + '/c????.fits') imageRoots = [] for ii in images: fileRoot = ii.split('/')[-1] imageRoots.append(fileRoot.replace('.fits', '')) roots.append(imageRoots) print star, filter, len(imageRoots) longCount = np.array([len(r) for r in roots]).max() print 'Long Count for %s = %d' % (star, longCount) for ii in range(longCount): imsets.write('%5s : ' % star) for ff in range(len(roots)): if ii < len(roots[ff]): # Write obsParams ir.txdump(roots[ff][ii] + '.phot.mag', 'IMAGE,IFILTER,ITIME,XAIRMASS,OTIME', 'yes', Stdout=obspar) # Write shifts posInfo = ir.txdump(roots[ff][ii] + '.phot.mag', 'IMAGE,XCENTER,YCENTER', 'yes', Stdout=1) posFields = posInfo[0].split() shiftX = -1.0 * float(posFields[1]) shiftY = -1.0 * float(posFields[2]) shifts.write('%-10s %7.2f %7.2f\n' % (posFields[0], shiftX, shiftY)) # Write imageSet imsets.write('%-10s ' % (roots[ff][ii] + '.fits')) else: imsets.write('%-10s ' % ('INDEF')) imsets.write('\n') imsets.close() obspar.close() shifts.close() # Now run mknobsfile ir.delete(iMagniFilename) ir.digiphot() ir.photcal() ir.unlearn('mknobsfile') ir.mknobsfile.obsparams = obsparFilename ir.mknobsfile.shifts = shiftsFilename ir.mknobsfile('*.phot.mag', ','.join(filters), imsetsFilename, iMagniFilename, verbose='no', wrap='no') # Run mkconfig ir.delete('standards.config') ir.unlearn('mkconfig') ir.mkconfig('standards.config', 'fphotcal_ukirt_faint.dat', 'fstandards.instMag.dat', 'tphotcal_ukirt_faint.dat', check='no', edit='no')
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 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
def fitsn(img, imgpsf, coordlist, _recenter, fwhm0, original, sn, residual, _show, _interactive, z11='', z22='', midpt='', size=7, apco0=0, dmax=51000, dmin=-500): import lsc lsc.util.delete("apori") lsc.util.delete(img + ".sn.mag") ################################# from pyraf import iraf import string iraf.imcoords(_doprint=0) iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) from iraf import digiphot from iraf import daophot from iraf import ptools a1 = int(fwhm0) a2 = int(2. * fwhm0 + .5) a3 = int(3. * fwhm0 + .5) a4 = int(4. * fwhm0 + .5) ap = str(a1) + "," + str(a2) + "," + str(a3) ######################################## if _recenter: answ = 'yes' else: answ = 'no' ######################################### hdr = lsc.util.readhdr(img + '.fits') _gain = lsc.util.readkey3(hdr, 'gain') _ron = lsc.util.readkey3(hdr, 'ron') _exptime = lsc.util.readkey3(hdr, 'exptime') _datamin = dmin _datamax = dmax iraf.noao.digiphot.daophot.photpars.zmag = 0 iraf.noao.digiphot.daophot.datapars.readnoi = _gain #1.4 #_ron iraf.noao.digiphot.daophot.datapars.epadu = _ron # 13 #_gain iraf.noao.digiphot.daophot.datapars.datamin = _datamin # -100 #_datamin iraf.noao.digiphot.daophot.datapars.datamax = _datamax iraf.noao.daophot.fitskypars.annulus = a3 iraf.noao.daophot.photpars.apertures = ap iraf.noao.digiphot.daophot.datapars.exposure = 'exptime' iraf.noao.digiphot.daophot.datapars.airmass = 'airmass' iraf.noao.digiphot.daophot.datapars.filter = 'filter2' iraf.noao.digiphot.daophot.daopars.psfrad = a4 # modify fitrad to 3 fwhm to see if works better iraf.noao.digiphot.daophot.daopars.fitrad = fwhm0 #* 3 iraf.noao.digiphot.daophot.daopars.sannulus = int(a4) iraf.noao.digiphot.daophot.daopars.recenter = answ iraf.noao.digiphot.daophot.daopars.fitsky = 'yes' # iraf.noao.digiphot.daophot.centerpars.cbox = 0 iraf.noao.digiphot.daophot.centerpars.cbox = 4 iraf.noao.digiphot.daophot.centerpars.calgori = 'gauss' # fitskypars.salgorithm = "constant" # fitskypars.skyvalue = 0 print '\n### recentering: ' + str(answ) if _show: iraf.noao.digiphot.daophot.phot(original, coordlist, "apori", veri='no') iraf.noao.digiphot.daophot.phot(sn, coordlist, img + ".sn.mag", veri='no') else: iraf.noao.digiphot.daophot.phot(original, coordlist, "apori", veri='no', verb='no') iraf.noao.digiphot.daophot.phot(sn, coordlist, img + ".sn.mag", veri='no', verb='no') lsc.util.delete(img + ".sn.als") print sn, imgpsf, img iraf.allstar(sn, img + ".sn.mag", imgpsf, img + ".sn.als", "", residual, veri='no', verb='no') lsc.util.delete("snfit.fits") iraf.imarith(sn + '.fits', "-", residual + '.fits', "snfit.fits") lsc.util.delete("skyfit.fits") iraf.imarith(original + '.fits', "-", "snfit.fits", "skyfit.fits") iraf.txsort(img + ".sn.als", "ID") tmptbl = iraf.txdump(img + ".sn.als", "mag,merr,xcenter,ycenter", expr='yes', Stdout=1) magerr, fitmag, centx, centy = [], [], [], [] for i in tmptbl: try: fitmag.append(float(string.split(i)[0])) #-2.5*log10(_exptime)) except: fitmag.append(string.split(i)[0]) try: magerr.append(float(string.split(i)[1])) except: magerr.append(string.split(i)[1]) centx.append(float(string.split(i)[2])) centy.append(float(string.split(i)[3])) tmptbl = iraf.txdump("apori", "mag", expr='yes', Stdout=1) apori1, apori2, apori3 = [], [], [] for i in tmptbl: try: apori1.append(float(string.split(i)[0])) #-2.5*log10(_exptime)) except: apori1.append(string.split(i)[0]) try: apori2.append(float(string.split(i)[1])) #-2.5*log10(_exptime)) except: apori2.append(string.split(i)[1]) try: apori3.append(float(string.split(i)[2])) #-2.5*log10(_exptime)) except: apori3.append(string.split(i)[2]) iraf.txsort(img + ".sn.mag", "YCENTER") tmptbl = iraf.txdump(img + ".sn.mag", "mag,magerr", expr='yes', Stdout=1) if _show: print "********************************************************************" print "ID <apmag on original> <apmag on bgsubt> fitmag truemag err_fit" print " ", a1, " ", a2, " ", a3, " ", a1, " ", a2, " ", a3 apmag1, apmag2, apmag3, truemag = [], [], [], [] for i in range(len(tmptbl)): try: apmag1.append(float(string.split( tmptbl[i])[0])) #-2.5*log10(_exptime)) except: apmag1.append(9999) try: apmag2.append(float(string.split( tmptbl[i])[1])) #-2.5*log10(_exptime)) except: apmag2.append(9999) try: apmag3.append(float(string.split( tmptbl[i])[2])) #-2.5*log10(_exptime)) except: apmag3.append(9999) try: truemag.append(fitmag[i] + float(apco0)) except: truemag.append('INDEF') if _show: print i, apori1[i], apori2[i], apori3[i], apmag1[i], apmag2[ i], apmag3[i], fitmag[i], truemag[i], magerr[i] if _show: print "********************************************************************" if _show: print midpt, z11, z22 _tmp1, _tmp2, goon = lsc.util.display_image(original + '.fits', 1, z11, z22, False, _xcen=.25, _ycen=.25, _xsize=.3, _ysize=.3) z01 = float(z11) - float(midpt) z02 = float(z22) - float(midpt) s1 = 1 s2 = -int(fwhm0) lsc.util.delete("tmptbl") ff = open('tmptbl', 'w') ff.write(str(s1) + ' ' + str(s2) + " ORIGINAL") ff.close() iraf.tvmark(1, "tmptbl", autol='no', mark="none", inter='no', label='yes', txsize=2) _tmp1, _tmp2, goon = lsc.util.display_image('snfit.fits', 1, z01, z02, False, _xcen=.25, _ycen=.75, _xsize=.3, _ysize=.3, _erase='no') lsc.util.delete("tmptbl") tmptbl0 = iraf.txdump(img + ".sn.als", "xcen,ycen", expr='yes', Stdout=1) ff = open('tmptbl', 'w') for i in tmptbl0: ff.write(i + '\n') ff.close() lra = int((2 * float(size) * float(fwhm0)) * 2) iraf.tvmark(1, "tmptbl", autol='no', mark="circle", number='yes', nyoffset=lra, radi=a2, txsize=2, inter='no') s1 = 1 s2 = -1 * int(fwhm0) lsc.util.delete("tmptbl") ff = open('tmptbl', 'w') ff.write(str(s1) + ' ' + str(s2) + " FITTED") ff.close() iraf.tvmark(1, "tmptbl", autol='no', mark="none", inter='no', label='yes', txsize=2) _tmp1, _tmp2, goon = lsc.util.display_image('skyfit.fits', 1, z11, z22, False, _xcen=.75, _ycen=.25, _xsize=.3, _ysize=.3, _erase='no') s1 = 1 s2 = -1 * int(fwhm0) lsc.util.delete("tmptbl") ff = open('tmptbl', 'w') ff.write(str(s1) + ' ' + str(s2) + " RESIDUAL") ff.close() iraf.tvmark(1, "tmptbl", autol='no', mark="none", inter='no', label='yes', txsize=2) return apori1, apori2, apori3, apmag1, apmag2, apmag3, fitmag, truemag, magerr, centx, centy
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 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 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
def marksn2(img, fitstab, frame=1, fitstab2='', verbose=False): from pyraf import iraf from numpy import array #,log10 import lsc iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) iraf.images(_doprint=0) iraf.imcoords(_doprint=0) iraf.proto(_doprint=0) iraf.set(stdimage='imt1024') hdr = lsc.util.readhdr(fitstab) _filter = lsc.util.readkey3(hdr, 'filter') column = lsc.lscabsphotdef.makecatalogue([fitstab])[_filter][fitstab] rasex = array(column['ra0'], float) decsex = array(column['dec0'], float) if fitstab2: hdr = lsc.util.readhdr(fitstab2) _filter = lsc.util.readkey3(hdr, 'filter') _exptime = lsc.util.readkey3(hdr, 'exptime') column = lsc.lscabsphotdef.makecatalogue([fitstab2])[_filter][fitstab2] rasex2 = array(column['ra0'], float) decsex2 = array(column['dec0'], float) iraf.set(stdimage='imt1024') iraf.display(img + '[0]', frame, fill=True, Stdout=1) vector = [] for i in range(0, len(rasex)): vector.append(str(rasex[i]) + ' ' + str(decsex[i])) xy = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\ formats='%10.1f %10.1f',verbose='yes')[3:] iraf.tvmark(frame, 'STDIN', Stdin=list(xy), mark="circle", number='yes', label='no', radii=10, nxoffse=5, nyoffse=5, color=207, txsize=2) if verbose: # print 2.5*log10(_exptime) for i in range(0, len(column['ra0'])): print xy[i], column['ra0'][i], column['dec0'][i], column['magp3'][ i], column['magp4'][i], column['smagf'][i], column['magp2'][i] if fitstab2: vector2 = [] for i in range(0, len(rasex2)): vector2.append(str(rasex2[i]) + ' ' + str(decsex2[i])) xy1 = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector2,Stdout=1,image=img+'[0]',inwcs='world',units='degrees degrees',outwcs='logical',\ formats='%10.1f %10.1f',verbose='yes')[3:] iraf.tvmark(frame, 'STDIN', Stdin=list(xy1), mark="cross", number='yes', label='no', radii=10, nxoffse=5, nyoffse=5, color=205, txsize=2)
iraf.imarith('Ha_final', "-", imageName + "_scaledplus", "Hacs_finalplus") iraf.hedit("Hacs_finalplus", 'Rscale', avgScaleFactor + stDev) #minus one stdev iraf.imarith('R_final', "*", avgScaleFactor - stDev, imageName + "_scaledminus") iraf.imarith('Ha_final', "-", imageName + "_scaledminus", "Hacs_finalminus") iraf.hedit("Hacs_finalminus", 'Rscale', avgScaleFactor - stDev) #clear up superfluous images for f in (imageName + "_scaledminus.fits", imageName + "_scaledplus.fits", imageName + "_scaled.fits", 'rIn.fits', 'haIn.fits'): silentDelete(f) print( 'The continuum subtracted images are Hacs_final.fits, Hacs_finalminus.fits, and Hacs_finalplus.fits' ) #This bit just takes the arguments from the command line amd runs the above #two functions on them. filename_R = sys.argv[1] filename_Ha = sys.argv[2] iraf.digiphot() print('Aligning images...') alignImages(filename_R, filename_Ha) print('Subtracting continuum...') continuumReduce(filename_R)
from scipy import interpolate from scipy.optimize import leastsq from astropy import wcs from astropy.coordinates import ICRS, Galactic from astropy import units from astropy.io import fits,ascii from astropy.modeling import models, fitting from astropy.table import Table, Column from astLib import astCoords import matplotlib.pyplot as plt import pyfits import psycopg2 from pyraf import iraf iraf.obsutil(_doprint=0) iraf.digiphot(_doprint=0) iraf.digiphot.daophot(_doprint=0) import traceback if ndmode in ['d','D','database','Database']: conn_string = "host='10.36.2.113' dbname='postgres' user='******' password='******'" print "Connecting to database\n ->%s" % (conn_string) conn = psycopg2.connect(conn_string) cursor = conn.cursor() errlogname = 'ErrorPhot_'+time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime())+'.log' logerror = open(errlogname,'wa')
def absphot(img,_field,_catalogue,_fix,_color,rejection,_interactive,_type='fit',redo=False,show=False,cutmag=-1,database='dataredulco',_calib='sloan'): from astropy.io import fits import lsc import math import sys,re,string,os from lsc.util import readkey3, readhdr from numpy import array, compress, zeros, median, std, asarray, isfinite,mean from pyraf import iraf if show: from pylab import ion,plot,draw,clf import time iraf.noao(_doprint=0) iraf.digiphot(_doprint=0) iraf.daophot(_doprint=0) iraf.images(_doprint=0) iraf.imcoords(_doprint=0) iraf.proto(_doprint=0) t = fits.open(img) tbdata = t[1].data hdr2=t[1].header hdr=lsc.util.readhdr(img) _cat=readkey3(hdr,'catalog') _telescope=lsc.util.readkey3(hdr,'telescop') _instrume=lsc.util.readkey3(hdr,'instrume') _filter=lsc.util.readkey3(hdr,'filter') _airmass=lsc.util.readkey3(hdr,'airmass') _exptime=lsc.util.readkey3(hdr,'exptime') _date=lsc.util.readkey3(hdr,'date-obs') _object=lsc.util.readkey3(hdr,'object') _ra=lsc.util.readkey3(hdr,'RA') _dec=lsc.util.readkey3(hdr,'DEC') print _filter if _telescope in ['lsc','1m0-04','1m0-05','1m0-09']: kk=lsc.sites.extintion('ctio') elif _telescope in ['elp','1m0-08']: kk=lsc.sites.extintion('mcdonald') elif _telescope in ['cpt','1m0-12','1m0-10','1m0-13']: kk=lsc.sites.extintion('southafrica') elif _telescope in ['ftn','Faulkes Telescope North']: kk=lsc.sites.extintion('mauna') elif _telescope in ['1m0-03','1m0-11','coj','fts','Faulkes Telescope South']: kk=lsc.sites.extintion('siding') if _calib not in ['sloan','sloanprime','natural','apass','']: colorefisso=lsc.sites.colfix(_instrume) else: colorefisso=lsc.sites.colfix(_instrume,_calib) print redo print _cat if _cat and not redo: print 'already calibrated' else: try: lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]): lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) except: print 'module mysqldef not found' column=makecatalogue([img])[_filter][img] rasex=array(column['ra0'],float) decsex=array(column['dec0'],float) if _type=='fit': magsex=array(column['smagf'],float) magerrsex=array(column['smagerrf'],float) elif _type=='ph': magsex=array(column['magp3'],float) magerrsex=array(column['merrp3'],float) else: sys.exit(_type+' not valid (ph or fit)') print len(rasex) if not cutmag: cutmag=99 #else: cutmag= cutmag-2.5*math.log10(float(_exptime)) if len(compress( array(magsex) < float(cutmag) , magsex)) < 5 : cutmag=99 # not cut if only few object rasex = compress(array(magsex,float)<=cutmag,rasex) decsex = compress(array(magsex,float)<=cutmag,decsex) magerrsex = compress(array(magsex,float)<=cutmag,magerrsex) magsex = compress(array(magsex,float)<=cutmag,array(magsex)) print len(rasex) if _interactive: iraf.set(stdimage='imt1024') iraf.display(re.sub('.sn2','',img),1,fill=True,Stdout=1) vector=[] for i in range(0,len(rasex)): vector.append(str(rasex[i])+' '+str(decsex[i])) xy = iraf.wcsctran('STDIN',output="STDOUT",Stdin=vector,Stdout=1,image=img,inwcs='world',units='degrees degrees',outwcs='logical',\ formats='%10.1f %10.1f',verbose='yes')[3:] iraf.tvmark(1,'STDIN',Stdin=list(xy),mark="circle",number='yes',label='no',radii=10,nxoffse=5,nyoffse=5,color=207,txsize=2) # raw_input('here') if _catalogue: ######## use external catalogue if _catalogue[0]=='/': stdcooC=lsc.lscastrodef.readtxt(_catalogue) else: stdcooC=lsc.lscastrodef.readtxt(lsc.__path__[0]+'/standard/cat/'+_catalogue) rastdC,decstdL=array(stdcooC['ra'],float),array(stdcooC['dec'],float) lsc.util.delete('tmp.stdL.pix') colonne=str(stdcooC['rapos'])+' '+str(stdcooC['decpos']) if _catalogue[0]=='/': iraf.wcsctran(_catalogue,'tmp.stdL.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',\ columns=colonne,formats='%10.1f %10.1f',verbose='no') else: iraf.wcsctran(lsc.__path__[0]+'/standard/cat/'+_catalogue,'tmp.stdL.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',\ columns=colonne,formats='%10.1f %10.1f',verbose='no') standardpixC=lsc.lscastrodef.readtxt('tmp.stdL.pix') if _interactive: iraf.tvmark(1,'tmp.stdL.pix',mark="circle",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=204,txsize=2) xstdC=standardpixC['ra'] ystdC=standardpixC['dec'] idstdC=standardpixC['id'] xstdC=compress((array(xstdC,float)<readkey3(hdr,'XDIM'))&(array(xstdC,float)>0)&(array(ystdC,float)>0)&(array(ystdC,float)<readkey3(hdr,'YDIM')),xstdC) xstdL=xstdLL=xstdS=xstdC standardpixL=standardpixLL=standardpixS=standardpixC stdcooL=stdcooLL=stdcooS=stdcooC else: ######## check if it is landolt field stdcooL=lsc.lscastrodef.readtxt(lsc.__path__[0]+'/standard/cat/landolt.cat') rastdL,decstdL=array(stdcooL['ra'],float),array(stdcooL['dec'],float) lsc.util.delete('tmp.stdL.pix') iraf.wcsctran(lsc.__path__[0]+'/standard/cat/landolt.cat','tmp.stdL.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',\ columns='1 2',formats='%10.1f %10.1f',verbose='no') standardpixL=lsc.lscastrodef.readtxt('tmp.stdL.pix') if _interactive: iraf.tvmark(1,'tmp.stdL.pix',mark="circle",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=204,txsize=2) print 'yelow circles sextractor' xstdL=standardpixL['ra'] ystdL=standardpixL['dec'] idstdL=standardpixL['id'] xstdL=compress((array(xstdL,float)<readkey3(hdr,'XDIM'))&(array(xstdL,float)>0)&(array(ystdL,float)>0)&(array(ystdL,float)<readkey3(hdr,'YDIM')),xstdL) ######## check if it is Stetson field stdcooLL=lsc.lscastrodef.readtxt(lsc.__path__[0]+'/standard/cat/StetsonCat.dat') ww=asarray([i for i in range(len(stdcooLL['ra'])) if ( abs(float(stdcooLL['ra'][i])-float(_ra))<.2 and abs(float(stdcooLL['dec'][i])-_dec)<.2 )]) if len(ww)>0: for hh in stdcooLL.keys(): if type(stdcooLL[hh])!=int: if hh not in ['id','ra','dec']: stdcooLL[hh]=array(array(stdcooLL[hh])[ww],float) else: stdcooLL[hh]=array(stdcooLL[hh])[ww] lll=[] for i in range(0,len(stdcooLL['ra'])): lll.append(stdcooLL['ra'][i]+' '+stdcooLL['dec'][i]) rastdLL,decstdLL=array(stdcooLL['ra'],float),array(stdcooLL['dec'],float) lsc.util.delete('tmp.stdLL.pix') iraf.wcsctran('STDIN','tmp.stdLL.pix',img,inwcs='world',Stdin=lll,units='degrees degrees',outwcs='logical',\ columns='1 2',formats='%10.1f %10.1f',verbose='no') if _interactive: iraf.tvmark(1,'tmp.stdLL.pix',mark="cross",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=204,txsize=2) print 'red crosses Stetson' standardpixLL={} for ii in stdcooLL.keys(): standardpixLL[ii]=stdcooLL[ii] standardpixLL['ra']=array(iraf.proto.fields('tmp.stdLL.pix',fields='1',Stdout=1),float) #standardpixLL['ra'] standardpixLL['dec']=array(iraf.proto.fields('tmp.stdLL.pix',fields='2',Stdout=1),float) #standardpixLL['dec'] xstdLL=array(iraf.proto.fields('tmp.stdLL.pix',fields='1',Stdout=1),float) #standardpixLL['ra'] ystdLL=array(iraf.proto.fields('tmp.stdLL.pix',fields='2',Stdout=1),float) #standardpixLL['dec'] idstdLL=standardpixLL['id'] xstdLL=compress((array(xstdLL,float)<readkey3(hdr,'XDIM'))&(array(xstdLL,float)>0)&(array(ystdLL,float)>0)&(array(ystdLL,float)<readkey3(hdr,'YDIM')),xstdLL) ######## check if it is sloan field magsel0,magsel1=12,18 _ids=lsc.lscastrodef.sloan2file(_ra,_dec,20,float(magsel0),float(magsel1),'_tmpsloan.cat') ascifile='_tmpsloan.cat' stdcooS=lsc.lscastrodef.readtxt(ascifile) rastdS,decstdS=array(stdcooS['ra'],float),array(stdcooS['dec'],float) lsc.util.delete('tmp.stdS.pix') iraf.wcsctran(ascifile,'tmp.stdS.pix',img,inwcs='world',units='degrees degrees',outwcs='logical',columns='1 2',formats='%10.1f %10.1f',verbose='no') standardpixS=lsc.lscastrodef.readtxt('tmp.stdS.pix') if _interactive: iraf.tvmark(1,'tmp.stdS.pix',mark="cross",number='yes',label='no',radii=8,nxoffse=5,nyoffse=5,color=205,txsize=2) print 'green cross sloan' xstdS=standardpixS['ra'] ystdS=standardpixS['dec'] idstdS=standardpixS['id'] xstdS=compress((array(xstdS,float)<readkey3(hdr,'XDIM'))&(array(xstdS,float)>0)&(array(ystdS,float)>0)&(array(ystdS,float)<readkey3(hdr,'YDIM')),xstdS) ##############################################3 if not _catalogue and len(xstdLL)>0: xstdL=xstdLL standardpixL=standardpixLL stdcooL=stdcooLL if _filter in ['U', 'B', 'V', 'R','I','Bessell-B','Bessell-V','Bessell-R','Bessell-I']: filters={'U':'U', 'B':'B', 'V':'V', 'R':'R', 'I':'I','Bessell-B':'B','Bessell-V':'V','Bessell-R':'R','Bessell-I':'I'} if _color: colors=lsc.myloopdef.chosecolor(_color,False) if not colors: colors={'U':['UB'],'B':['UB','BV'],'V':['BV','VR'],'R':['VR','RI'],'I':['RI']} else: colors={'U':['UB'],'B':['UB','BV'],'V':['BV','VR'],'R':['VR','RI'],'I':['RI']} if _field=='sloan': standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]} print 'filters and field selected do not match' else: _field='landolt' if len(xstdL)>=1: standardpix=standardpixL stdcoo=stdcooL if not _catalogue: if len(xstdLL)>0: _catalogue='StetsonCat.dat' else: _catalogue='landolt.dat' elif len(xstdS)>=1: if not _catalogue: _catalogue='sdss8' standardpix=standardpixS stdcoo=stdcooS stdcoo=lsc.lscastrodef.transformsloanlandolt(stdcoo) if not _catalogue: _catalogue='sdss8' print '\n### transform sloan in landolt' else: print 'landolt, but catalogue not found' standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]} elif _filter in ['up','gp','rp','ip','zs','SDSS-G','SDSS-R','SDSS-I','Pan-Starrs-Z']: filters={'up':'u','ip':'i','gp':'g','rp':'r','zs':'z','SDSS-G':'g','SDSS-R':'r','SDSS-I':'i','Pan-Starrs-Z':'z'} if _color: colors=lsc.myloopdef.chosecolor(_color,False) if not colors: colors={'i':['ri','iz'],'r':['gr','ri'],'g':['ug','gr'],'z':['iz'],'u':['ug']} else: colors={'i':['ri','iz'],'r':['gr','ri'],'g':['ug','gr'],'z':['iz'],'u':['ug']} if _field=='landolt': standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]} print 'filters and field selected do not match' else: _field='sloan' if len(xstdS)>=1: if not _catalogue: _catalogue='sdss8' standardpix=standardpixS stdcoo=stdcooS elif len(xstdL)>=1: standardpix=standardpixL stdcoo=stdcooL stdcoo=lsc.lscastrodef.transformlandoltsloan(stdcoo) if not _catalogue: _catalogue='landolt.dat' print '\n### transform landolt to sloan' else: print 'sloan, but not in the sdss footprint' standardpix,stdcoo={'ra':[9999],'dec':[9999],'id':[1]},{'ra':[9999],'dec':[9999]} xstd=standardpix['ra'] ystd=standardpix['dec'] idstd=standardpix['id'] rastd,decstd=array(stdcoo['ra'],float),array(stdcoo['dec'],float) xstd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)&(array(ystd,float)<readkey3(hdr,'YDIM')),xstd) if len(xstd0)>1: ######## go only if standard stars are in the field ########## magstd0={} airmass0={} result={} fileph={} print '\n### standard field: '+str(_field) ystd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\ &(array(ystd,float)<readkey3(hdr,'YDIM')),ystd) rastd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\ &(array(ystd,float)<readkey3(hdr,'YDIM')),rastd) decstd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\ &(array(ystd,float)<readkey3(hdr,'YDIM')),decstd) idstd0=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\ &(array(ystd,float)<readkey3(hdr,'YDIM')),idstd) stdcoo0={} for key in stdcoo.keys(): if key in 'ugrizUBVRI': stdcoo0[key]=compress((array(xstd,float)<readkey3(hdr,'XDIM'))&(array(xstd,float)>0)&(array(ystd,float)>0)\ &(array(ystd,float)<readkey3(hdr,'YDIM')),stdcoo[key]) ############################################################### # pos0 = standard pos1 = sextractor distvec,pos0,pos1=lsc.lscastrodef.crossmatch(array(rastd0),array(decstd0),array(rasex),array(decsex),10) for key in stdcoo0.keys(): stdcoo0[key]=stdcoo0[key][pos0] rastd0=rastd0[pos0] decstd0=decstd0[pos0] idstd0=idstd0[pos0] rasex=rasex[pos1] decsex=decsex[pos1] # after change in may 2013 mag in sn2.fits file are already at 1s magsex=magsex[pos1]-kk[filters[_filter]]*float(_airmass) # - K x airmass # magsex=magsex[pos1]+2.5*math.log10(float(_exptime))-kk[filters[_filter]]*float(_airmass) # mag exptime - K x airmass ################################################################################# if _field=='landolt': print '\n### landolt system' for _filtlandolt in 'UBVRI': if _filtlandolt==filters[_filter]: airmass0[_filtlandolt]= 0 #_airmass else: airmass0[_filtlandolt]= 0 magstd0[_filtlandolt]=stdcoo0[_filtlandolt] fileph['mU']=zeros(len(rastd0))+999 fileph['mB']=zeros(len(rastd0))+999 fileph['mV']=zeros(len(rastd0))+999 fileph['mR']=zeros(len(rastd0))+999 fileph['mI']=zeros(len(rastd0))+999 fileph['V']=magstd0['V'] fileph['BV']=array(array(magstd0['B'],float)-array(magstd0['V'],float),str) fileph['UB']=array(array(magstd0['U'],float)-array(magstd0['B'],float),str) fileph['VR']=array(array(magstd0['V'],float)-array(magstd0['R'],float),str) fileph['RI']=array(array(magstd0['R'],float)-array(magstd0['I'],float),str) elif _field=='sloan': for _filtsloan in 'ugriz': if _filtsloan==filters[_filter]: airmass0[_filtsloan]= 0 # _airmass else: airmass0[_filtsloan]=0 magstd0[_filtsloan]=stdcoo0[_filtsloan] fileph['mu']=zeros(len(rastd0))+999 fileph['mg']=zeros(len(rastd0))+999 fileph['mr']=zeros(len(rastd0))+999 fileph['mi']=zeros(len(rastd0))+999 fileph['mz']=zeros(len(rastd0))+999 fileph['r']=magstd0['r'] fileph['gr']=array(array(magstd0['g'],float)-array(magstd0['r'],float),str) fileph['ri']=array(array(magstd0['r'],float)-array(magstd0['i'],float),str) fileph['ug']=array(array(magstd0['u'],float)-array(magstd0['g'],float),str) fileph['iz']=array(array(magstd0['i'],float)-array(magstd0['z'],float),str) ######################################################################################## zero=[] magcor=[] fil = open(re.sub('.fits','.ph',img),'w') fil.write(str(_instrume)+' '+str(_date)+'\n') fil.write('*** '+_object+' '+str(len(magsex))+'\n') if _field=='landolt': fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(1),str(1),str(1),str(1),str(1))) # exptime fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(airmass0['U']),str(airmass0['B']),str(airmass0['V']),str(airmass0['R']),str(airmass0['I']))) elif _field=='sloan': fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(1),str(1),str(1),str(1),str(1))) # exptime fil.write('%6.6s\t%6.6s\t%6.6s\t%6.6s\t%6.6s\n' % (str(airmass0['u']),str(airmass0['g']),str(airmass0['r']),str(airmass0['i']),str(airmass0['z']))) for i in range(0,len(magsex)): fileph['m'+filters[_filter]][i]=magsex[i] # instrumental mangitude of std in pos0[i] if _field=='landolt': stringastandard='%12.12s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s' % (idstd0[i],fileph['V'][i],fileph['BV'][i],fileph['UB'][i],\ fileph['VR'][i],fileph['RI'][i]) fil.write('%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%60.60s\n' \ % (str(fileph['mU'][i]),str(fileph['mB'][i]),str(fileph['mV'][i]),str(fileph['mR'][i]),str(fileph['mI'][i]),str(stringastandard))) elif _field=='sloan': stringastandard='%12.12s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s' % (idstd0[i],fileph['r'][i],fileph['gr'][i],fileph['ug'][i],\ fileph['ri'][i],fileph['iz'][i]) fil.write('%7.7s\t%7.7s\t%7.7s\t%7.7s\t%7.7s\t%60.60s\n' \ % (str(fileph['mu'][i]),str(fileph['mg'][i]),str(fileph['mr'][i]),str(fileph['mi'][i]),str(fileph['mz'][i]),str(stringastandard))) zero.append(float(float(magstd0[filters[_filter]][i]))-float(magsex[i])) magcor.append(magsex[i]) fil.close() if show: import time from pylab import ion,plot,draw ion() aa=mean(compress(abs(array(zero))<99,zero)) xxx=compress((abs(array(magcor))<99)&(abs(array(zero))<99),magcor) yyy=compress((abs(array(zero))<99)&(abs(array(magcor))<99),zero) plot(xxx,yyy,'or') plot([min(compress(abs(array(magcor))<99,magcor)),max(compress(abs(array(magcor))<99,magcor))],[aa,aa],'-b') draw() print std(compress(abs(array(zero))<99,zero)) time.sleep(5) colorvec=colors[filters[_filter]] for col in colorvec: col0=magstd0[col[0]] col1=magstd0[col[1]] colstd0=array(col0,float)-array(col1,float) ################## sex ###################### colore=[] for i in range(0,len(pos1)): colore.append(colstd0[i]) colore1=compress(abs(array(zero))<50,array(colore)) zero1=compress(abs(array(zero))<50,array(zero)) zero2=compress(abs(array(colore1))<2,array(zero1)) colore2=compress(abs(array(colore1))<2,array(colore1)) if _fix: fisso=colorefisso[filters[_filter]+col] else: fisso='' if len(colore2)==0: print 'no calibration, '+_filter+' '+_field b,a,sa,sb=9999,9999,0,0 else: if _interactive: a,sa,b,sb=fitcol(colore2,zero2,_filter,col,fisso) else: a,sa,b,sb=fitcol2(colore2,zero2,_filter,col,fisso,show,rejection) print a,sa,b,sb result[filters[_filter]+col]=[a,sa,b,sb] if result: print '\n### zeropoint ..... done at airmass 0' if _catalogue: lsc.util.updateheader(img,0,{'CATALOG':[str(string.split(_catalogue,'/')[-1]),'catalogue source']}) stringa='' for ll in result: for kk in range(0,len(result[ll])): if not isfinite(result[ll][kk]): result[ll][kk]=0.0 valore='%3.3s %6.6s %6.6s %6.6s %6.6s' % (str(ll),str(result[ll][0]),str(result[ll][2]),str(result[ll][1]),str(result[ll][3])) print '### ',valore lsc.util.updateheader(img,0,{'zp'+ll:[str(valore),'a b sa sb in y=a+bx']}) if ll[0]==ll[2]: num=2 elif ll[0]==ll[1]: num=1 else: sys.exit('somthing wrong with color '+ll) print ll,num try: print 'zcol'+str(num),ll[1:],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1] lsc.mysqldef.updatevalue(database,'zcol'+str(num),ll[1:],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'z'+str(num),result[ll][0],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'c'+str(num),result[ll][2],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'dz'+str(num),result[ll][1],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'dc'+str(num),result[ll][3],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]): lsc.mysqldef.updatevalue(database,'zcol'+str(num),ll[1:],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'z'+str(num),result[ll][0],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'c'+str(num),result[ll][2],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'dz'+str(num),result[ll][1],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) lsc.mysqldef.updatevalue(database,'dc'+str(num),result[ll][3],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) if result[ll][0]!=9999: print _catalogue lsc.mysqldef.updatevalue(database,'zcat',string.split(_catalogue,'/')[-1],string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]): lsc.mysqldef.updatevalue(database,'zcat',string.split(_catalogue,'/')[-1],string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) else: lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.sn2.fits','.fits',img),'/')[-1]) if os.path.isfile(string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]): lsc.mysqldef.updatevalue(database,'zcat','X',string.split(re.sub('.diff.sn2.fits','.fits',img),'/')[-1]) except: print 'module mysqldef not found'
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')