def get_seeing(file, scale, ref): coords = file.replace('fits','dao') log = file.replace('fits','psf') out = file.replace('fits','seeing') iraf.noao() iraf.noao.obsutil() iraf.set(stdgraph="uepsf") data = iraf.psfmeasure(file, coords='markall', wcs='logical', display='no', frame=1, level=0.5, size='FWHM', radius=10.0, sbuffer=1.0, swidth=3.0, iterations=1, logfile=log, imagecur=coords, Stdout=1) fwhm = data.pop().split().pop() fwhm_pix = float(fwhm) fwhm = fwhm_pix*scale seeing = dimm_seeing(fwhm_pix, ref, scale) print "Seeing: ", seeing fp = open(out, 'w') fp.write("%f %f\n" % (fwhm_pix, seeing)) fp.close() os.system("echo \"image;text 85 500 # text={Spot FWHM = %5.2f pixels}\" | xpaset WFS regions" % fwhm_pix) os.system("echo \'image;text 460 500 # text={Seeing = %4.2f\"}\' | xpaset WFS regions" % seeing) os.system("echo \"set wfs_seeing %4.2f\" | nc hacksaw 7666" % seeing) # New function call to insert seeing value into MySQL database. fits2mysql(file, mode, fwhm, seeing)
def avgFWHM(image,coords): '''use the iraf task psfmeasure to compute the average fwhm of a few stars INPUT: image: the fits image you want to measure the fwhm for coords: a text file listing the x,y pixel coordinates of stars in image to use to compute the fwhm. I'd recommend choosing ~10, bright stars, evenly sampled across the frame. OUTPUT: a string of the average fwhm also your graphics terminal will open up with a gross iraf graphics terminal If you are interested you can look at it to see info like elipticity and average fwhm of the image. If you run this in batch mode and consistently are seeing fwhm values >~ 5, it might mean you need to choose different stars in your coords file, or check that the coordinates are accurate. ''' #we need to create a text file that has the letter 'q' in it #this gets fed to psfmeasure as the graphcur input. it forces iraf to quit out of the graphics terminal #this way you can compute the fwhm with out the user having to click out of the graphics terminal anything #I tried to disable to graphics output alltogether, but couldn't. so we use this work around instead with open('graphcur','w') as f: f.write('q') #use the iraf task psfmeasure to get the fwhm #the output is not machine readable friendly, but basically we are after the last thing it prints out #this is the avg fwhm measured stdOutput=iraf.psfmeasure(image,imagecur=coords,display='no', graphcur='graphcur',Stdout=1, logfile='') FWHM=stdOutput[-1].split()[-1] #clean up the graphcur textfile we made iraf.delete('graphcur') return FWHM
def get_seeing(file, scale, ref): # log = file.replace('fits','psf') # never used # Gets called with full path to the fits file # print "daofind get_seeing: ", file iraf.noao() iraf.noao.obsutil() iraf.set(stdgraph="uepsf") # name of file with coordinates of star images. coords = file.replace('fits','dao') data = iraf.psfmeasure(file, coords='markall', wcs='logical', display='no', frame=1, level=0.5, size='FWHM', radius=10.0, sbuffer=1.0, swidth=3.0, iterations=1, logfile=log, imagecur=coords, Stdout=1) # print "daofind psfmeasure data: ", data # last = data.pop() # data.append(last) # print "daofind psfmeasure last line of data: ", last # psfmeasure returns lots of information (data for each spot), but we just # want the last line (the average fwhm), which looks like: # Average full width at half maximum (FWHM) of 6.0163 fwhm = data.pop().split().pop() print "daofind psfmeasure fwhm: ", fwhm fwhm_pix = float(fwhm) fwhm = fwhm_pix*scale seeing = dimm_seeing(fwhm_pix, ref, scale) print "Seeing: ", seeing seeingfile = file.replace('fits','seeing') fp = open(seeingfile, 'w') fp.write("%f %f\n" % (fwhm_pix, seeing)) fp.close() os.system("echo \"image;text 85 500 # text={Spot FWHM = %5.2f pixels}\" | xpaset WFS regions" % fwhm_pix) os.system("echo \'image;text 460 500 # text={Seeing = %4.2f\"}\' | xpaset WFS regions" % seeing) os.system("echo \"set wfs_seeing %4.2f\" | nc hacksaw 7666" % seeing)
def get_seeing(file, scale, ref): coords = file.replace("fits", "dao") log = file.replace("fits", "psf") out = file.replace("fits", "seeing") iraf.noao() iraf.noao.obsutil() iraf.set(stdgraph="uepsf") data = iraf.psfmeasure( file, coords="markall", wcs="logical", display="no", frame=1, level=0.5, size="FWHM", radius=10.0, sbuffer=1.0, swidth=3.0, iterations=1, logfile=log, imagecur=coords, Stdout=1, ) fwhm = data.pop().split().pop() fwhm_pix = float(fwhm) fwhm = fwhm_pix * scale seeing = dimm_seeing(fwhm_pix, ref, scale) print seeing fp = open(out, "w") fp.write("%f %f\n" % (fwhm_pix, seeing)) fp.close() os.system('echo "image;text 85 500 # text={Spot FWHM = %5.2f pixels}" | xpaset WFS regions' % fwhm_pix) os.system("echo 'image;text 460 500 # text={Seeing = %4.2f\"}' | xpaset WFS regions" % seeing) os.system('echo "set wfs_seeing %4.2f" | nc hacksaw 7666' % seeing)
def avgFWHM(image, coords): """ use the iraf task psfmeasure to compute the average fwhm of a few stars INPUT: image: the fits image you want to measure the fwhm for coords: a text file listing the x,y pixel coordinates of stars in image to use to compute the fwhm. I'd recommend choosing ~10, bright stars, evenly sampled across the frame. OUTPUT: a string of the average fwhm also your graphics terminal will open up with a gross iraf graphics terminal If you are interested you can look at it to see info like ellipticity and average fwhm of the image. If you run this in batch mode and consistently are seeing fwhm values >~ 5, it might mean you need to choose different stars in your coords file, or check that the coordinates are accurate. """ # we need to create a text file that has the letter 'q' in it # this gets fed to psfmeasure as the graphcur input. it forces iraf to quit out of the terminal # this way you can compute the fwhm with out the user having to click on anything with open('graphcur', 'w') as f: f.write('q') # use the iraf task psfmeasure to get the fwhm # the output is not machine readable friendly, but basically we are after the last thing it prints out # this is the avg fwhm measured stdOutput = iraf.psfmeasure(image, imagecur=coords, display='no', graphcur='graphcur', Stdout=1, logfile='') #FWHM = stdOutput[-1].split()[-1] psfstarlines = stdOutput[4:-2] linessplitted = [x.split() for x in psfstarlines] FWHM = np.median(np.array([x[3] for x in linessplitted]).astype(float)) # clean up the graphcur textfile we made iraf.delete('graphcur') return FWHM
def ecpsf(img, ofwhm, threshold, psfstars, distance, interactive, ds9, psffun='gauss', fixaperture=False, _catalog='', _datamax=None, show=False): try: import lsc, string hdr = lsc.util.readhdr(img + '.fits') instrument = lsc.util.readkey3(hdr, 'instrume') print 'INSTRUMENT:', instrument if 'PIXSCALE' in hdr: scale = lsc.util.readkey3(hdr, 'PIXSCALE') elif 'CCDSCALE' in hdr: if 'CCDXBIN' in hdr: scale = lsc.util.readkey3(hdr, 'CCDSCALE') * lsc.util.readkey3(hdr, 'CCDXBIN') elif 'CCDSUM' in hdr: scale = lsc.util.readkey3(hdr, 'CCDSCALE') * int(string.split(lsc.util.readkey3(hdr, 'CCDSUM'))[0]) if _datamax is None and 'kb' in instrument: _datamax = 45000 elif _datamax is None: _datamax = 65000 _wcserr = lsc.util.readkey3(hdr, 'wcserr') print _wcserr if float(_wcserr) == 0: if 'L1FWHM' in hdr: seeing = float(lsc.util.readkey3(hdr, 'L1FWHM')) elif 'L1SEEING' in hdr: seeing = float(lsc.util.readkey3(hdr, 'L1SEEING')) * scale else: seeing = 3 else: seeing = 3 if 'PSF_FWHM' in hdr: seeing = float(lsc.util.readkey3(hdr, 'PSF_FWHM')) else: sys.exit('astrometry not good') #except: sys.exit('astrometry not good') fwhm = seeing / scale print 'FWHM[header] ', fwhm, ' in pixel' if ofwhm: fwhm = float(ofwhm) print ' FWHM[input] ', fwhm, ' in pixel' xdim, ydim = iraf.hselect(img+'[0]', 'i_naxis1,i_naxis2', 'yes', Stdout=1)[0].split() print img, fwhm, threshold, scale,xdim ################################################################################# ################### write file to compute psf _psf.coo ############ ################################################################################# if interactive: iraf.display(img, 1, fill=True) iraf.delete('tmp.lo?', verify=False) print '_' * 80 print '>>> Mark reference stars with "a". Then "q"' print '-' * 80 iraf.imexamine(img, 1, wcs='logical', logfile='tmp.log', keeplog=True) xyrefer = iraf.fields('tmp.log', '1,2,6,15', Stdout=1) xns, yns, _fws = [], [], [] ############# write file for PSF ######################### ff = open('_psf.coo', 'w') for i in range(len(xyrefer)): xns.append(float(xyrefer[i].split()[0])) yns.append(float(xyrefer[i].split()[1])) _fws.append(float(xyrefer[i].split()[3])) ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i]))) ff.close() fwhm = np.median(_fws) elif _catalog: print '\n#### use catalog to measure the psf' ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img + '[0]',inwcs='world',outwcs='logical', units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no') ddd=[i for i in ddd if i[0]!='#'] ddd=[' '.join(i.split()[0:3]) for i in ddd] line='' for i in ddd: a,b,c = string.split(i) if float(a) < float(xdim) and float(b) < float(ydim) and float(b) > 0: line = line + '%10s %10s %10s \n' % (a, b, c) if line: ff = open('_psf.coo', 'w') ff.write(line) ff.close() else: sys.exit('error: no catalog objects are in the field') else: ############ run sextractor ##################################### xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale) tot = np.compress(abs(np.array(fluxrad) * 1.6 - fwhm) / fwhm < .5, fluxrad) if len(tot) < 5: print 'warning: fwhm from sexractor different from fwhm computed during pre-reduction' print 'try using option --fwhm xxx' ff = open('tmp.cursor', 'w') image_hdu = fits.open(img + '.fits') for i in range(len(xs)): _xs = np.delete(xs, i) _ys = np.delete(ys, i) dist2 = np.sqrt((_xs - xs[i]) ** 2 + (_ys - ys[i]) ** 2) ########### cut star, not near other object ########################## if abs(fluxrad[i] * 1.6 - fwhm) / fwhm < .5 and min(dist2) > distance * fwhm: x1, x2 = int(xs[i] - fwhm * 3), int(xs[i] + fwhm * 3) y1, y2 = int(ys[i] - fwhm * 3), int(ys[i] + fwhm * 3) if x1 < 1: x1 = 1 if y1 < 1: y1 = 1 if x2 > int(xdim): x2 = int(xdim) if y2 > int(ydim): y2 = int(ydim) fmax = np.max(image_hdu[0].data[y1-1:y2, x1-1:x2]) ########## cut saturated object ######################## if float(fmax) < _datamax: # not saturated ff.write('%10.3f %10.3f 1 m \n' % (xs[i], ys[i])) ff.close() image_hdu.close() iraf.delete('tmp.lo?,tmp.sta?,tmp.gk?', verify=False) iraf.psfmeasure(img+'[0]', imagecur='tmp.cursor', logfile='tmp.log', radius=int(fwhm), iter=3, display=False, StdoutG='tmp.gki') ff = open('tmp.log') righe = ff.readlines() xn = [float(righe[3].split()[1])] yn = [float(righe[3].split()[2])] _fw = [float(righe[3].split()[4])] for r in righe[4:-2]: if len(r) > 0: xn.append(float(r.split()[0])) yn.append(float(r.split()[1])) _fw.append(float(r.split()[3])) print 'FWHM: ', righe[-1].split()[-1] print 80 * "#" ###### ############## eliminate double object identification ########################### xns, yns, _fws = [xn[0]], [yn[0]], [_fw[0]] for i in range(1, len(xn)): if abs(xn[i] - xn[i - 1]) > .2 and abs(yn[i] - yn[i - 1]) > .2: xns.append(xn[i]) yns.append(yn[i]) _fws.append(_fw[i]) ######### write clean file for PSF ######################### fw = [] ff = open('_psf.coo', 'w') for i in range(len(xns)): if abs(_fws[i] - fwhm) / fwhm < .3: ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i]))) fw.append(_fws[i]) ff.close() ## End automatic selection ###################################################################################### ################### write file of object to store in fits table ############# ###################################################################################### if interactive: xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale) ff = open('_psf2.coo', 'w') for i in range(len(xs)): ff.write('%10s %10s %10s \n' % (xs[i], ys[i], fluxrad[i])) ff.close() elif _catalog: ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img + '[0]',inwcs='world',outwcs='logical', units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no') ddd=[i for i in ddd if i[0]!='#'] ddd=[' '.join(i.split()[0:3]) for i in ddd] ff = open('_psf2.coo', 'w') for i in ddd: a,b,c = string.split(i) ff.write('%10s %10s %10s \n' % (a, b, c)) ff.close() else: os.system('cp _psf.coo _psf2.coo') # dflux = fluxrad - np.median(fluxrad) # fstar = np.compress(dflux < np.std(fluxrad), fluxrad) ################################################################################################################# print 80 * "#" photmag, pst, fitmag = psffit(img, fwhm, psfstars, hdr, interactive, _datamax, psffun, fixaperture) photmag2, fitmag2 = psffit2(img, fwhm, psfstars, hdr, _datamax, psffun, fixaperture) radec = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag, Stdout=1, image=img + '[0]', inwcs='logical', outwcs='world', columns="1 2", format='%13.3H %12.2h', min_sig=9, mode='h')[3:] radec2 = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag2, Stdout=1, image=img + '[0]', inwcs='logical', outwcs='world', columns="1 2", format='%13.3H %12.2h', min_sig=9, mode='h')[3:] if ds9 == 0 and (interactive or show): iraf.set(stdimage='imt1024') iraf.display(img, 1, fill=True, Stdout=1) iraf.tvmark(1, coords='STDIN', mark='circle', radii=15, label=True, Stdin=photmag, nxoffset=5, nyoffset=5, txsize=2) iraf.tvmark(1, coords='STDIN', mark='circle', radii=35, label=False, Stdin=pst, color=208) # iraf.tvmark(1, coords='STDIN', mark='cross', length=35, label=False, Stdin=fitmag2, color=204) idpsf = [] for i in range(len(pst)): idpsf.append(pst[i].split()[2]) dmag = [] for i in range(len(radec)): ra, dec, idph, magp2, magp3, magp4, merrp2, merrp3, merrp4 = radec[i].split() dmag.append(9.99) for j in range(len(fitmag)): raf, decf, idf, magf, magerrf = fitmag[j].split() if idph == idf and idph in idpsf and \ magp3 != 'INDEF' and magf != 'INDEF': dmag[i] = float(magp3) - float(magf) break _dmag = np.compress(np.array(dmag) < 9.99, np.array(dmag)) print '>>> Aperture correction (phot) %6.3f +/- %5.3f %3d ' % \ (np.mean(_dmag), np.std(_dmag), len(_dmag)) if len(_dmag) > 3: _dmag = np.compress(np.abs(_dmag - np.median(_dmag)) < 2 * np.std(_dmag), _dmag) print '>>> 2 sigma rejection) %6.3f +/- %5.3f %3d [default]' \ % (np.mean(_dmag), np.std(_dmag), len(_dmag)) print '>>> fwhm %s ' % (str(fwhm)) for i in range(len(dmag)): if dmag[i] == 9.99: dmag[i] = '' else: dmag[i] = '%6.3f' % (dmag[i]) exptime = lsc.util.readkey3(hdr, 'exptime') object = lsc.util.readkey3(hdr, 'object').replace(' ', '') filtro = lsc.util.readkey3(hdr, 'filter') ####################################### rap, decp, magp2, magp3, magp4, smagf, merrp3, smagerrf = [], [], [], [], [], [], [], [] rap0, decp0 = [], [] for i in range(len(radec2)): aa = radec2[i].split() rap.append(aa[0]) decp.append(aa[1]) rap0.append(lsc.lscabsphotdef.deg2HMS(ra=aa[0])) decp0.append(lsc.lscabsphotdef.deg2HMS(dec=aa[1])) idp = aa[2] magp2.append(aa[3]) magp3.append(aa[4]) magp4.append(aa[5]) merrp3.append(aa[7]) _smagf, _smagerrf = 9999, 9999 for j in range(len(fitmag2)): raf, decf, idf, magf, magerrf = fitmag2[j].split() if idf == idp: _smagf = magf _smagerrf = magerrf break smagf.append(_smagf) smagerrf.append(_smagerrf) tbhdu = fits.BinTableHDU.from_columns(fits.ColDefs([fits.Column(name='ra', format='20A', array=np.array(rap)), fits.Column(name='dec', format='20A', array=np.array(decp)), fits.Column(name='ra0', format='E', array=np.array(rap0)), fits.Column(name='dec0', format='E', array=np.array(decp0)), fits.Column(name='magp2', format='E', array=np.array(np.where((np.array(magp2) != 'INDEF'), np.array(magp2), 9999), float)), fits.Column(name='magp3', format='E', array=np.array(np.where((np.array(magp3) != 'INDEF'), np.array(magp3), 9999), float)), fits.Column(name='merrp3', format='E', array=np.array(np.where((np.array(merrp3) != 'INDEF'), np.array(merrp3), 9999), float)), fits.Column(name='magp4', format='E', array=np.array(np.where((np.array(magp4) != 'INDEF'), np.array(magp4), 9999), float)), fits.Column(name='smagf', format='E', array=np.array(np.where((np.array(smagf) != 'INDEF'), np.array(smagf), 9999), float)), fits.Column(name='smagerrf', format='E', array=np.array(np.where((np.array(smagerrf) != 'INDEF'), np.array(smagerrf), 9999), float)), ])) hdu = fits.PrimaryHDU(header=hdr) thdulist = fits.HDUList([hdu, tbhdu]) lsc.util.delete(img + '.sn2.fits') thdulist.writeto(img + '.sn2.fits') lsc.util.updateheader(img + '.sn2.fits', 0, {'APCO': [np.mean(_dmag), 'Aperture correction']}) lsc.util.updateheader(img + '.sn2.fits', 0, {'APCOERR': [np.std(_dmag), 'Aperture correction error']}) lsc.util.updateheader(img + '.sn2.fits', 0, {'XDIM': [lsc.util.readkey3(hdr, 'naxis1'), 'x number of pixels']}) lsc.util.updateheader(img + '.sn2.fits', 0, {'YDIM': [lsc.util.readkey3(hdr, 'naxis2'), 'y number of pixels']}) lsc.util.updateheader(img + '.sn2.fits', 0, {'PSF_FWHM': [fwhm * scale, 'FWHM (arcsec) - computed with daophot']}) os.chmod(img + '.sn2.fits', 0664) os.chmod(img + '.psf.fits', 0664) result = 1 except: result = 0 fwhm = 0.0 traceback.print_exc() return result, fwhm * scale
def ecpsf(img, ofwhm, threshold, psfstars, distance, interactive, psffun='gauss', fixaperture=False, _catalog='', _datamax=None, show=False): try: import lsc, string hdr = lsc.util.readhdr(img + '.fits') instrument = lsc.util.readkey3(hdr, 'instrume') print 'INSTRUMENT:', instrument if 'PIXSCALE' in hdr: scale = lsc.util.readkey3(hdr, 'PIXSCALE') elif 'CCDSCALE' in hdr: if 'CCDXBIN' in hdr: scale = lsc.util.readkey3(hdr, 'CCDSCALE') * lsc.util.readkey3(hdr, 'CCDXBIN') elif 'CCDSUM' in hdr: scale = lsc.util.readkey3(hdr, 'CCDSCALE') * int(string.split(lsc.util.readkey3(hdr, 'CCDSUM'))[0]) if _datamax is None: _datamax = lsc.util.readkey3(hdr, 'datamax') _wcserr = lsc.util.readkey3(hdr, 'wcserr') print _wcserr if float(_wcserr) == 0: if 'L1FWHM' in hdr: seeing = float(lsc.util.readkey3(hdr, 'L1FWHM')) elif 'L1SEEING' in hdr: seeing = float(lsc.util.readkey3(hdr, 'L1SEEING')) * scale else: seeing = 3 else: seeing = 3 if 'PSF_FWHM' in hdr: seeing = float(lsc.util.readkey3(hdr, 'PSF_FWHM')) else: sys.exit('astrometry not good') #except: sys.exit('astrometry not good') fwhm = seeing / scale print 'FWHM[header] ', fwhm, ' in pixel' if ofwhm: fwhm = float(ofwhm) print ' FWHM[input] ', fwhm, ' in pixel' xdim, ydim = iraf.hselect(img+'[0]', 'i_naxis1,i_naxis2', 'yes', Stdout=1)[0].split() print img, fwhm, threshold, scale,xdim ################################################################################# ################### write file to compute psf _psf.coo ############ ################################################################################# if _catalog: print '\n#### use catalog to measure the psf' ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img + '[0]',inwcs='world',outwcs='logical', units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no') ddd=[i for i in ddd if i[0]!='#'] ddd=[' '.join(i.split()[0:3]) for i in ddd] line='' for i in ddd: a,b,c = string.split(i) if float(a) < float(xdim) and float(b) < float(ydim) and float(b) > 0: line = line + '%10s %10s %10s \n' % (a, b, c) if line: ff = open('_psf.coo', 'w') ff.write(line) ff.close() else: sys.exit('error: no catalog objects are in the field') elif interactive: iraf.display(img + '[0]', 1, fill=True) iraf.delete('tmp.lo?', verify=False) print '_' * 80 print '>>> Mark reference stars with "a". Then "q"' print '-' * 80 iraf.imexamine(img, 1, wcs='logical', logfile='tmp.log', keeplog=True) xyrefer = iraf.fields('tmp.log', '1,2,6,15', Stdout=1) xns, yns, _fws = [], [], [] ############# write file for PSF ######################### ff = open('_psf.coo', 'w') for i in range(len(xyrefer)): xns.append(float(xyrefer[i].split()[0])) yns.append(float(xyrefer[i].split()[1])) _fws.append(float(xyrefer[i].split()[3])) ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i]))) ff.close() fwhm = np.median(_fws) else: ############ run sextractor ##################################### xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale) tot = np.compress(abs(np.array(fluxrad) * 1.6 - fwhm) / fwhm < .5, fluxrad) if len(tot) < 5: print 'warning: fwhm from sexractor different from fwhm computed during pre-reduction' print 'try using option --fwhm xxx' ff = open('tmp.cursor', 'w') image_hdu = fits.open(img + '.fits') for i in range(len(xs)): _xs = np.delete(xs, i) _ys = np.delete(ys, i) dist2 = np.sqrt((_xs - xs[i]) ** 2 + (_ys - ys[i]) ** 2) ########### cut star, not near other object ########################## if abs(fluxrad[i] * 1.6 - fwhm) / fwhm < .5 and min(dist2) > distance * fwhm: x1, x2 = int(xs[i] - fwhm * 3), int(xs[i] + fwhm * 3) y1, y2 = int(ys[i] - fwhm * 3), int(ys[i] + fwhm * 3) if x1 < 1: x1 = 1 if y1 < 1: y1 = 1 if x2 > int(xdim): x2 = int(xdim) if y2 > int(ydim): y2 = int(ydim) fmax = np.max(image_hdu[0].data[y1-1:y2, x1-1:x2]) ########## cut saturated object ######################## if float(fmax) < _datamax: # not saturated ff.write('%10.3f %10.3f 1 m \n' % (xs[i], ys[i])) ff.close() image_hdu.close() iraf.delete('tmp.lo?,tmp.sta?,tmp.gk?', verify=False) iraf.psfmeasure(img+'[0]', imagecur='tmp.cursor', logfile='tmp.log', radius=int(fwhm), iter=3, display=False, StdoutG='tmp.gki') ff = open('tmp.log') righe = ff.readlines() xn = [float(righe[3].split()[1])] yn = [float(righe[3].split()[2])] _fw = [float(righe[3].split()[4])] for r in righe[4:-2]: if len(r) > 0: xn.append(float(r.split()[0])) yn.append(float(r.split()[1])) _fw.append(float(r.split()[3])) print 'FWHM: ', righe[-1].split()[-1] print 80 * "#" ###### ############## eliminate double object identification ########################### xns, yns, _fws = [xn[0]], [yn[0]], [_fw[0]] for i in range(1, len(xn)): if abs(xn[i] - xn[i - 1]) > .2 and abs(yn[i] - yn[i - 1]) > .2: xns.append(xn[i]) yns.append(yn[i]) _fws.append(_fw[i]) ######### write clean file for PSF ######################### fw = [] ff = open('_psf.coo', 'w') for i in range(len(xns)): if abs(_fws[i] - fwhm) / fwhm < .3: ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i]))) fw.append(_fws[i]) ff.close() ## End automatic selection ###################################################################################### ################### write file of object to store in fits table ############# ###################################################################################### if _catalog: ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img + '[0]',inwcs='world',outwcs='logical', units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no') ddd=[i for i in ddd if i[0]!='#'] ddd=[' '.join(i.split()[0:3]) for i in ddd] ff = open('_psf2.coo', 'w') for i in ddd: a,b,c = string.split(i) ff.write('%10s %10s %10s \n' % (a, b, c)) ff.close() elif interactive: xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale) ff = open('_psf2.coo', 'w') for i in range(len(xs)): ff.write('%10s %10s %10s \n' % (xs[i], ys[i], fluxrad[i])) ff.close() else: os.system('cp _psf.coo _psf2.coo') # dflux = fluxrad - np.median(fluxrad) # fstar = np.compress(dflux < np.std(fluxrad), fluxrad) ################################################################################################################# print 80 * "#" photmag, pst, fitmag = psffit(img, fwhm, psfstars, hdr, interactive, _datamax, psffun, fixaperture) photmag2, fitmag2 = psffit2(img, fwhm, psfstars, hdr, _datamax, psffun, fixaperture) radec = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag, Stdout=1, image=img + '[0]', inwcs='logical', outwcs='world', columns="1 2", format='%13.3H %12.2h', min_sig=9, mode='h')[3:] radec2 = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag2, Stdout=1, image=img + '[0]', inwcs='logical', outwcs='world', columns="1 2", format='%13.3H %12.2h', min_sig=9, mode='h')[3:] if interactive or show: iraf.set(stdimage='imt1024') iraf.display(img + '[0]', 1, fill=True, Stdout=1) iraf.tvmark(1, coords='STDIN', mark='circle', radii=15, label=True, Stdin=photmag, nxoffset=5, nyoffset=5, txsize=2) iraf.tvmark(1, coords='STDIN', mark='circle', radii=35, label=False, Stdin=pst, color=208) # iraf.tvmark(1, coords='STDIN', mark='cross', length=35, label=False, Stdin=fitmag2, color=204) idpsf = [] for i in range(len(pst)): idpsf.append(pst[i].split()[2]) dmag = [] for i in range(len(radec)): ra, dec, idph, magp2, magp3, magp4, merrp2, merrp3, merrp4 = radec[i].split() dmag.append(9.99) for j in range(len(fitmag)): raf, decf, idf, magf, magerrf = fitmag[j].split() if idph == idf and idph in idpsf and \ magp3 != 'INDEF' and magf != 'INDEF': dmag[i] = float(magp3) - float(magf) break _dmag = np.compress(np.array(dmag) < 9.99, np.array(dmag)) print '>>> Aperture correction (phot) %6.3f +/- %5.3f %3d ' % \ (np.mean(_dmag), np.std(_dmag), len(_dmag)) if len(_dmag) > 3: _dmag = np.compress(np.abs(_dmag - np.median(_dmag)) < 2 * np.std(_dmag), _dmag) print '>>> 2 sigma rejection) %6.3f +/- %5.3f %3d [default]' \ % (np.mean(_dmag), np.std(_dmag), len(_dmag)) print '>>> fwhm %s ' % (str(fwhm)) for i in range(len(dmag)): if dmag[i] == 9.99: dmag[i] = '' else: dmag[i] = '%6.3f' % (dmag[i]) exptime = lsc.util.readkey3(hdr, 'exptime') object = lsc.util.readkey3(hdr, 'object').replace(' ', '') filtro = lsc.util.readkey3(hdr, 'filter') ####################################### rap, decp, magp2, magp3, magp4, smagf, merrp3, smagerrf = [], [], [], [], [], [], [], [] rap0, decp0 = [], [] for i in range(len(radec2)): aa = radec2[i].split() rap.append(aa[0]) decp.append(aa[1]) rap0.append(lsc.lscabsphotdef.deg2HMS(ra=aa[0])) decp0.append(lsc.lscabsphotdef.deg2HMS(dec=aa[1])) idp = aa[2] magp2.append(aa[3]) magp3.append(aa[4]) magp4.append(aa[5]) merrp3.append(aa[7]) _smagf, _smagerrf = 9999, 9999 for j in range(len(fitmag2)): raf, decf, idf, magf, magerrf = fitmag2[j].split() if idf == idp: _smagf = magf _smagerrf = magerrf break smagf.append(_smagf) smagerrf.append(_smagerrf) tbhdu = fits.BinTableHDU.from_columns(fits.ColDefs([fits.Column(name='ra', format='20A', array=np.array(rap)), fits.Column(name='dec', format='20A', array=np.array(decp)), fits.Column(name='ra0', format='E', array=np.array(rap0)), fits.Column(name='dec0', format='E', array=np.array(decp0)), fits.Column(name='magp2', format='E', array=np.array(np.where((np.array(magp2) != 'INDEF'), np.array(magp2), 9999), float)), fits.Column(name='magp3', format='E', array=np.array(np.where((np.array(magp3) != 'INDEF'), np.array(magp3), 9999), float)), fits.Column(name='merrp3', format='E', array=np.array(np.where((np.array(merrp3) != 'INDEF'), np.array(merrp3), 9999), float)), fits.Column(name='magp4', format='E', array=np.array(np.where((np.array(magp4) != 'INDEF'), np.array(magp4), 9999), float)), fits.Column(name='smagf', format='E', array=np.array(np.where((np.array(smagf) != 'INDEF'), np.array(smagf), 9999), float)), fits.Column(name='smagerrf', format='E', array=np.array(np.where((np.array(smagerrf) != 'INDEF'), np.array(smagerrf), 9999), float)), ])) hdu = fits.PrimaryHDU(header=hdr) thdulist = fits.HDUList([hdu, tbhdu]) lsc.util.delete(img + '.sn2.fits') thdulist.writeto(img + '.sn2.fits') lsc.util.updateheader(img + '.sn2.fits', 0, {'APCO': [np.mean(_dmag), 'Aperture correction']}) lsc.util.updateheader(img + '.sn2.fits', 0, {'APCOERR': [np.std(_dmag), 'Aperture correction error']}) lsc.util.updateheader(img + '.sn2.fits', 0, {'XDIM': [lsc.util.readkey3(hdr, 'naxis1'), 'x number of pixels']}) lsc.util.updateheader(img + '.sn2.fits', 0, {'YDIM': [lsc.util.readkey3(hdr, 'naxis2'), 'y number of pixels']}) lsc.util.updateheader(img + '.sn2.fits', 0, {'PSF_FWHM': [fwhm * scale, 'FWHM (arcsec) - computed with daophot']}) os.chmod(img + '.sn2.fits', 0664) os.chmod(img + '.psf.fits', 0664) result = 1 except: result = 0 fwhm = 0.0 traceback.print_exc() return result, fwhm * scale
xycursor = open(imgroot+'_sex_clip1.imgcur','w') if sexdemen==2: for i in range(0,sexcatarr.shape[0]): xycursor.write('%10s %10s %3s %3s' % (sexcatarr[i,0],sexcatarr[i,1],1,'m\n')) if sexdemen==1: xycursor.write('%10s %10s %3s %3s' % (sexcatarr[0],sexcatarr[1],1,'m\n')) xycursor.write(' q') xycursor.close() graphcur = open(imgroot+'_sex_clip1.grphcur','w') graphcur.write(' q') graphcur.close() if os.path.isfile(imgroot+'_psfms.log'): os.remove(imgroot+'_psfms.log') print ' Start iraf.psfmeasure task:\n\n' iraf.psfmeasure(images=image,coords='markall',wcs='logical',frame=1,display=0,level=0.99,size='MFWHM',saturation=65530,ignore_sat=1,iterations=2,logfile=imgroot+'_psfms.log',imagecur=imgroot+'_sex_clip1.imgcur',graphcur=imgroot+'_sex_clip1.grphcur') with open(imgroot+'_psfms.log') as psfms: psmslogn = sum(1 for x in psfms) psfmslog = open(imgroot+'_psfms.log') for anno in (0,1,2): psfmslog.readline() psfmscat = [] imageline = psfmslog.readline() if 50< float(imageline.split(None,5)[2]) < 950: psfmscat.append(imageline.split(None,1)[1].replace('INDEF','99.99')) for p in range(psmslogn-6): psfmsline = psfmslog.readline() if 50< float(psfmsline.split(None,4)[1]) < 950: psfmscat.append(psfmsline.lstrip().replace('INDEF','99.99'))
def ecpsf(img, ofwhm, threshold, psfstars, distance, interactive, ds9, psffun='gauss', fixaperture=False,_catalog='',_datamax=''): try: import agnkey hdr = agnkey.util.readhdr(img + '.fits') instrument = agnkey.util.readkey3(hdr, 'instrume') print 'INSTRUMENT:', instrument if 'PIXSCALE' in hdr: pixelscale = agnkey.util.readkey3(hdr, 'PIXSCALE') elif 'CCDSCALE' in hdr: if 'CCDXBIN' in hdr: pixelscale = agnkey.util.readkey3(hdr, 'CCDSCALE') * agnkey.util.readkey3(hdr, 'CCDXBIN') elif 'CCDSUM' in hdr: pixelscale = agnkey.util.readkey3(hdr, 'CCDSCALE') * int( string.split(agnkey.util.readkey3(hdr, 'CCDSUM'))[0]) if 'kb' in instrument: scale = pixelscale if not _datamax: _datamax = 45000 elif 'fl' in instrument: scale = pixelscale if not _datamax: _datamax = 60000 elif 'fs' in instrument: scale = pixelscale if not _datamax: _datamax = 65000 try: #if 1==1: if 'WCSERR' in hdr: _wcserr = hdr['WCSERR'] elif 'WCS_ERR' in hdr: _wcserr = hdr['WCS_ERR'] print _wcserr if float(_wcserr) == 0: if 'kb' in instrument: seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75 elif 'fl' in instrument: seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75 elif 'fs' in instrument: if 'L1FWHM' in hdr: seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75 elif 'L1SEEING' in hdr: seeing = float(agnkey.util.readkey3(hdr, 'L1SEEING')) * scale else: seeing = 3 else: seeing = 3 else: seeing = float(agnkey.util.readkey3(hdr, 'PSF_FWHM')) sys.exit('astrometry not good') except ValueError: # except: sys.exit('astrometry not good') fwhm = seeing / scale print 'FWHM[header] ', fwhm, ' in pixel' if ofwhm: fwhm = float(ofwhm) print ' FWHM[input] ', fwhm, ' in pixel' xdim, ydim = iraf.hselect(img+'.fits[0]', 'i_naxis1,i_naxis2', 'yes', Stdout=1)[0].split() print img, fwhm, threshold, scale ################################################################################# ################### write file to compute psf _psf.coo ############ ################################################################################# if interactive: iraf.set(stdimage='imt1024') iraf.display(img+'.fits[0]', 1, fill=True) iraf.delete('tmp.lo?', verify=False) print '_' * 80 print '>>> Mark reference stars with "a". Then "q"' print '-' * 80 iraf.imexamine(img+'[0]', 1, wcs='logical', logfile='tmp.log', keeplog=True) xyrefer = iraf.fields('tmp.log', '1,2,6,15', Stdout=1) xns, yns, _fws = [], [], [] ############# write file for PSF ######################### ff = open('_psf.coo', 'w') for i in range(len(xyrefer)): xns.append(float(xyrefer[i].split()[0])) yns.append(float(xyrefer[i].split()[1])) _fws.append(float(xyrefer[i].split()[3])) ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i]))) ff.close() fwhm = np.median(_fws) else: ############ run sextractor ##################################### xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale) _ra1,_dec1,xx11,yy11,_mag,_dist = agnkey.agnastrodef.starsfields(img+'.fits',20,19) if len(_ra1): dist,pos0,pos1 = agnkey.agnastrodef.crossmatchxy(xs,ys,xx11,yy11,10) if len(pos0): xs = xs[pos0] ys = ys[pos0] ran = ran[pos0] decn = decn[pos0] magbest = magbest[pos0] classstar = classstar[pos0] fluxrad = fluxrad[pos0] bkg = bkg[pos0] ff = open('tmp.cursor', 'w') for i in range(len(xs)): x1, x2 = int(xs[i] - fwhm * 3), int(xs[i] + fwhm * 3) y1, y2 = int(ys[i] - fwhm * 3), int(ys[i] + fwhm * 3) sect = '[' + str(x1) + ':' + str(x2) + ',' + str(y1) + ':' + str(y2) + ']' try: fmax = iraf.imstat(img+'.fits[0]' + sect, fields='max', Stdout=1)[1] ########## cut saturated object ######################## if float(fmax) < _datamax: # not saturated ff.write('%10.3f %10.3f 1 m \n' % (xs[i], ys[i])) except: print sect # print 'problem here' # pass ff.close() iraf.delete('tmp.lo?,tmp.sta?,tmp.gk?', verify=False) iraf.psfmeasure(img+'[0]', imagecur='tmp.cursor', logfile='tmp.log', radius=int(fwhm), iter=3, display=False, StdoutG='tmp.gki') ff = open('tmp.log') righe = ff.readlines() xn = [float(righe[3].split()[1])] yn = [float(righe[3].split()[2])] _fw = [float(righe[3].split()[4])] for r in righe[4:-2]: if len(r) > 0: xn.append(float(r.split()[0])) yn.append(float(r.split()[1])) _fw.append(float(r.split()[3])) print 'FWHM: ', righe[-1].split()[-1] print 80 * "#" ###### ############## eliminate double object identification ########################### xns, yns, _fws = [xn[0]], [yn[0]], [_fw[0]] for i in range(1, len(xn)): if abs(xn[i] - xn[i - 1]) > .2 and abs(yn[i] - yn[i - 1]) > .2: xns.append(xn[i]) yns.append(yn[i]) _fws.append(_fw[i]) ######### write clean file for PSF ######################### fw = [] ff = open('_psf.coo', 'w') for i in range(len(xns)): ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i]))) fw.append(_fws[i]) ff.close() ## End automatic selection ###################################################################################### ################### write file of object to store in fits table ############# ###################################################################################### if interactive: xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale) ff = open('_psf2.coo', 'w') for i in range(len(xs)): ff.write('%10s %10s %10s \n' % (xs[i], ys[i], fluxrad[i])) ff.close() elif _catalog: print '\n#### use catalog ' ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img,inwcs='world',outwcs='logical', units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no') ddd=[i for i in ddd if i[0]!='#'] ddd=[' '.join(i.split()[0:3]) for i in ddd] ff = open('_psf2.coo', 'w') for i in ddd: a,b,c = string.split(i) ff.write('%10s %10s %10s \n' % (a, b, c)) ff.close() print 'use catalog' else: os.system('cp _psf.coo _psf2.coo') # print '\n### use sextractor' # xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale) # ff = open('_psf2.coo', 'w') # for i in range(len(xs)): # ff.write('%10s %10s %10s \n' % (xs[i], ys[i], fluxrad[i])) # ff.close() ################################################################################### print 80 * "#" photmag, pst, fitmag = psffit(img, fwhm, psfstars, hdr, interactive, _datamax, psffun, fixaperture) photmag2, fitmag2 = psffit2(img, fwhm, psfstars, hdr, _datamax, psffun, fixaperture) radec = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag, \ Stdout=1, image=img+'.fits[0]', inwcs='logical', outwcs='world', columns="1 2", \ format='%13.3H %12.2h', min_sig=9, mode='h')[3:] radec2 = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag2, \ Stdout=1, image=img+'.fits[0]', inwcs='logical', outwcs='world', columns="1 2", \ format='%13.3H %12.2h', min_sig=9, mode='h')[3:] if ds9 == 0 and interactive: iraf.set(stdimage='imt1024') iraf.display(img, 1, fill=True, Stdout=1) iraf.tvmark(1, coords='STDIN', mark='circle', radii=15, label=False, Stdin=photmag) iraf.tvmark(1, coords='STDIN', mark='rectangle', length=35, label=False, Stdin=pst) iraf.tvmark(1, coords='STDIN', mark='cross', length=35, label=False, Stdin=fitmag2, color=204) idpsf = [] for i in range(len(pst)): idpsf.append(pst[i].split()[2]) dmag = [] for i in range(len(radec)): ra, dec, idph, magp2, magp3, magp4, merrp2, merrp3, merrp4 = radec[i].split() dmag.append(9.99) for j in range(len(fitmag)): raf, decf, idf, magf, magerrf = fitmag[j].split() if idph == idf and idph in idpsf and \ magp3 != 'INDEF' and magf != 'INDEF': dmag[i] = float(magp3) - float(magf) break _dmag = np.compress(np.array(dmag) < 9.99, np.array(dmag)) print '>>> Aperture correction (phot) %6.3f +/- %5.3f %3d ' % \ (np.mean(_dmag), np.std(_dmag), len(_dmag)) if len(_dmag) > 3: _dmag = np.compress(abs(_dmag - np.median(_dmag)) < 2 * np.std(_dmag), _dmag) print '>>> 2 sigma rejection) %6.3f +/- %5.3f %3d [default]' \ % (np.mean(_dmag), np.std(_dmag), len(_dmag)) print '>>> fwhm %s ' % (str(fwhm)) for i in range(len(dmag)): if dmag[i] == 9.99: dmag[i] = '' else: dmag[i] = '%6.3f' % (dmag[i]) exptime = agnkey.util.readkey3(hdr, 'exptime') object = agnkey.util.readkey3(hdr, 'object').replace(' ', '') filtro = agnkey.util.readkey3(hdr, 'filter') ####################################### rap, decp, magp2, magp3, magp4, smagf = [], [], [], [], [], [] merrp2, merrp3, merrp4, smagerrf = [], [], [], [] rap0, decp0 = [], [] for i in range(len(radec2)): aa = radec2[i].split() rap.append(aa[0]) decp.append(aa[1]) rap0.append(agnkey.agnabsphotdef.deg2HMS(ra=aa[0])) decp0.append(agnkey.agnabsphotdef.deg2HMS(dec=aa[1])) idp = aa[2] magp2.append(aa[3]) magp3.append(aa[4]) magp4.append(aa[5]) merrp2.append(aa[6]) merrp3.append(aa[7]) merrp4.append(aa[8]) _smagf, _smagerrf = 9999, 9999 for j in range(len(fitmag2)): raf, decf, idf, magf, magerrf = fitmag2[j].split() if idf == idp: _smagf = magf _smagerrf = magerrf break smagf.append(_smagf) smagerrf.append(_smagerrf) new_cols = pyfits.ColDefs([ pyfits.Column(name='ra', format='20A', array=np.array(rap)), pyfits.Column(name='dec', format='20A', array=np.array(decp)), pyfits.Column(name='ra0', format='E', array=np.array(rap0)), pyfits.Column(name='dec0', format='E', array=np.array(decp0)), pyfits.Column(name='magp2', format='E', array=np.array(np.where((np.array(magp2) != 'INDEF'), np.array(magp2), 9999), float)), pyfits.Column(name='magp3', format='E', array=np.array(np.where((np.array(magp3) != 'INDEF'), np.array(magp3), 9999), float)), pyfits.Column(name='magp4', format='E', array=np.array(np.where((np.array(magp4) != 'INDEF'), np.array(magp4), 9999), float)), pyfits.Column(name='merrp2', format='E', array=np.array(np.where((np.array(merrp2) != 'INDEF'), np.array(merrp2), 9999), float)), pyfits.Column(name='merrp3', format='E', array=np.array(np.where((np.array(merrp3) != 'INDEF'), np.array(merrp3), 9999), float)), pyfits.Column(name='merrp4', format='E', array=np.array(np.where((np.array(merrp4) != 'INDEF'), np.array(merrp4), 9999), float)), pyfits.Column(name='smagf', format='E', array=np.array(np.where((np.array(smagf) != 'INDEF'), np.array(smagf), 9999), float)), pyfits.Column(name='smagerrf', format='E', array=np.array(np.where((np.array(smagerrf) != 'INDEF'), np.array(smagerrf), 9999), float)), ]) tbhdu = pyfits.BinTableHDU.from_columns(new_cols) hdu = pyfits.PrimaryHDU(header=hdr) thdulist = pyfits.HDUList([hdu, tbhdu]) agnkey.util.delete(img + '.sn2.fits') thdulist.writeto(img + '.sn2.fits') agnkey.util.updateheader(img + '.sn2.fits', 0, {'APCO': [np.mean(_dmag), 'Aperture correction']}) agnkey.util.updateheader(img + '.sn2.fits', 0, {'APCOERR': [np.std(_dmag), 'Aperture correction error']}) agnkey.util.updateheader(img + '.sn2.fits', 0, {'XDIM': [agnkey.util.readkey3(hdr, 'naxis1'), 'x number of pixels']}) agnkey.util.updateheader(img + '.sn2.fits', 0, {'YDIM': [agnkey.util.readkey3(hdr, 'naxis2'), 'y number of pixels']}) agnkey.util.updateheader(img + '.sn2.fits', 0, {'PSF_FWHM': [fwhm * scale, 'FWHM (arcsec) - computed with daophot']}) os.chmod(img + '.sn2.fits', 0664) os.chmod(img + '.psf.fits', 0664) result = 1 except IOError as e: print e result = 0 fwhm = 0.0 traceback.print_exc() return result, fwhm * scale
def ecpsf(img, ofwhm, threshold, psfstars, distance, interactive, ds9, psffun='gauss', fixaperture=False,_catalog='',_datamax=''): try: import agnkey hdr = agnkey.util.readhdr(img + '.fits') instrument = agnkey.util.readkey3(hdr, 'instrume') print ('INSTRUMENT:', instrument) if 'PIXSCALE' in hdr: pixelscale = agnkey.util.readkey3(hdr, 'PIXSCALE') elif 'CCDSCALE' in hdr: if 'CCDXBIN' in hdr: pixelscale = agnkey.util.readkey3(hdr, 'CCDSCALE') * agnkey.util.readkey3(hdr, 'CCDXBIN') elif 'CCDSUM' in hdr: pixelscale = agnkey.util.readkey3(hdr, 'CCDSCALE') * int( string.split(agnkey.util.readkey3(hdr, 'CCDSUM'))[0]) if 'kb' in instrument: scale = pixelscale if not _datamax: _datamax = 45000 elif 'fl' in instrument: scale = pixelscale if not _datamax: _datamax = 60000 elif 'fa' in instrument: scale = pixelscale if not _datamax: _datamax = 60000 elif 'fs' in instrument: scale = pixelscale if not _datamax: _datamax = 65000 try: #if 1==1: if 'WCSERR' in hdr: _wcserr = hdr['WCSERR'] elif 'WCS_ERR' in hdr: _wcserr = hdr['WCS_ERR'] print _wcserr if float(_wcserr) == 0: if 'kb' in instrument: seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75 elif 'fl' in instrument: seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75 elif 'fa' in instrument: seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75 elif 'fs' in instrument: if 'L1FWHM' in hdr: seeing = float(agnkey.util.readkey3(hdr, 'L1FWHM')) * .75 elif 'L1SEEING' in hdr: seeing = float(agnkey.util.readkey3(hdr, 'L1SEEING')) * scale else: seeing = 3 else: seeing = 3 else: seeing = float(agnkey.util.readkey3(hdr, 'PSF_FWHM')) sys.exit('astrometry not good') except ValueError: # except: sys.exit('astrometry not good') fwhm = seeing / scale print 'FWHM[header] ', fwhm, ' in pixel' if ofwhm: fwhm = float(ofwhm) print ' FWHM[input] ', fwhm, ' in pixel' xdim, ydim = iraf.hselect(img+'.fits[0]', 'i_naxis1,i_naxis2', 'yes', Stdout=1)[0].split() print img, fwhm, threshold, scale ################################################################################# ################### write file to compute psf _psf.coo ############ ################################################################################# if interactive: iraf.set(stdimage='imt1024') iraf.display(img+'.fits[0]', 1, fill=True) iraf.delete('tmp.lo?', verify=False) print '_' * 80 print '>>> Mark reference stars with "a". Then "q"' print '-' * 80 iraf.imexamine(img+'[0]', 1, wcs='logical', logfile='tmp.log', keeplog=True) xyrefer = iraf.fields('tmp.log', '1,2,6,15', Stdout=1) xns, yns, _fws = [], [], [] ############# write file for PSF ######################### ff = open('_psf.coo', 'w') for i in range(len(xyrefer)): xns.append(float(xyrefer[i].split()[0])) yns.append(float(xyrefer[i].split()[1])) _fws.append(float(xyrefer[i].split()[3])) ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i]))) ff.close() fwhm = np.median(_fws) else: ############ run sextractor ##################################### xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale) _ra1,_dec1,xx11,yy11,_mag,_dist = agnkey.agnastrodef.starsfields(img+'.fits',20,19) if len(_ra1): dist,pos0,pos1 = agnkey.agnastrodef.crossmatchxy(xs,ys,xx11,yy11,10) if len(pos0): xs = xs[pos0] ys = ys[pos0] ran = ran[pos0] decn = decn[pos0] magbest = magbest[pos0] classstar = classstar[pos0] fluxrad = fluxrad[pos0] bkg = bkg[pos0] ff = open('tmp.cursor', 'w') for i in range(len(xs)): x1, x2 = int(xs[i] - fwhm * 3), int(xs[i] + fwhm * 3) y1, y2 = int(ys[i] - fwhm * 3), int(ys[i] + fwhm * 3) sect = '[' + str(x1) + ':' + str(x2) + ',' + str(y1) + ':' + str(y2) + ']' try: fmax = iraf.imstat(img+'.fits[0]' + sect, fields='max', Stdout=1)[1] ########## cut saturated object ######################## if float(fmax) < _datamax: # not saturated ff.write('%10.3f %10.3f 1 m \n' % (xs[i], ys[i])) except: print sect # print 'problem here' # pass ff.close() iraf.delete('tmp.lo?,tmp.sta?,tmp.gk?', verify=False) iraf.psfmeasure(img+'[0]', imagecur='tmp.cursor', logfile='tmp.log', radius=int(fwhm), iter=3, display=False, StdoutG='tmp.gki') ff = open('tmp.log') righe = ff.readlines() xn = [float(righe[3].split()[1])] yn = [float(righe[3].split()[2])] _fw = [float(righe[3].split()[4])] for r in righe[4:-2]: if len(r) > 0: xn.append(float(r.split()[0])) yn.append(float(r.split()[1])) _fw.append(float(r.split()[3])) print 'FWHM: ', righe[-1].split()[-1] print 80 * "#" ###### ############## eliminate double object identification ########################### xns, yns, _fws = [xn[0]], [yn[0]], [_fw[0]] for i in range(1, len(xn)): if abs(xn[i] - xn[i - 1]) > .2 and abs(yn[i] - yn[i - 1]) > .2: xns.append(xn[i]) yns.append(yn[i]) _fws.append(_fw[i]) ######### write clean file for PSF ######################### fw = [] ff = open('_psf.coo', 'w') for i in range(len(xns)): ff.write('%10.3f %10.3f %7.2f \n' % (xns[i], yns[i], float(_fws[i]))) fw.append(_fws[i]) ff.close() ## End automatic selection ###################################################################################### ################### write file of object to store in fits table ############# ###################################################################################### if interactive: xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale) ff = open('_psf2.coo', 'w') for i in range(len(xs)): ff.write('%10s %10s %10s \n' % (xs[i], ys[i], fluxrad[i])) ff.close() elif _catalog: print '\n#### use catalog ' ddd=iraf.wcsctran(input=_catalog,output='STDOUT',Stdout=1,image=img,inwcs='world',outwcs='logical', units='degrees degrees',columns='1 2',formats='%10.1f %10.1f',verbose='no') ddd=[i for i in ddd if i[0]!='#'] ddd=[' '.join(i.split()[0:3]) for i in ddd] ff = open('_psf2.coo', 'w') for i in ddd: a,b,c = string.split(i) ff.write('%10s %10s %10s \n' % (a, b, c)) ff.close() print 'use catalog' else: os.system('cp _psf.coo _psf2.coo') # print '\n### use sextractor' # xs, ys, ran, decn, magbest, classstar, fluxrad, bkg = runsex(img, fwhm, threshold, scale) # ff = open('_psf2.coo', 'w') # for i in range(len(xs)): # ff.write('%10s %10s %10s \n' % (xs[i], ys[i], fluxrad[i])) # ff.close() ################################################################################### print 80 * "#" photmag, pst, fitmag = psffit(img, fwhm, psfstars, hdr, interactive, _datamax, psffun, fixaperture) photmag2, fitmag2 = psffit2(img, fwhm, psfstars, hdr, _datamax, psffun, fixaperture) radec = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag, \ Stdout=1, image=img+'.fits[0]', inwcs='logical', outwcs='world', columns="1 2", \ format='%13.3H %12.2h', min_sig=9, mode='h')[3:] radec2 = iraf.wcsctran(input='STDIN', output='STDOUT', Stdin=photmag2, \ Stdout=1, image=img+'.fits[0]', inwcs='logical', outwcs='world', columns="1 2", \ format='%13.3H %12.2h', min_sig=9, mode='h')[3:] if ds9 == 0 and interactive: iraf.set(stdimage='imt1024') iraf.display(img, 1, fill=True, Stdout=1) iraf.tvmark(1, coords='STDIN', mark='circle', radii=15, label=False, Stdin=photmag) iraf.tvmark(1, coords='STDIN', mark='rectangle', length=35, label=False, Stdin=pst) iraf.tvmark(1, coords='STDIN', mark='cross', length=35, label=False, Stdin=fitmag2, color=204) idpsf = [] for i in range(len(pst)): idpsf.append(pst[i].split()[2]) dmag = [] for i in range(len(radec)): ra, dec, idph, magp2, magp3, magp4, merrp2, merrp3, merrp4 = radec[i].split() dmag.append(9.99) for j in range(len(fitmag)): raf, decf, idf, magf, magerrf = fitmag[j].split() if idph == idf and idph in idpsf and \ magp3 != 'INDEF' and magf != 'INDEF': dmag[i] = float(magp3) - float(magf) break _dmag = np.compress(np.array(dmag) < 9.99, np.array(dmag)) print '>>> Aperture correction (phot) %6.3f +/- %5.3f %3d ' % \ (np.mean(_dmag), np.std(_dmag), len(_dmag)) if len(_dmag) > 3: _dmag = np.compress(abs(_dmag - np.median(_dmag)) < 2 * np.std(_dmag), _dmag) print '>>> 2 sigma rejection) %6.3f +/- %5.3f %3d [default]' \ % (np.mean(_dmag), np.std(_dmag), len(_dmag)) print '>>> fwhm %s ' % (str(fwhm)) for i in range(len(dmag)): if dmag[i] == 9.99: dmag[i] = '' else: dmag[i] = '%6.3f' % (dmag[i]) exptime = agnkey.util.readkey3(hdr, 'exptime') object = agnkey.util.readkey3(hdr, 'object').replace(' ', '') filtro = agnkey.util.readkey3(hdr, 'filter') ####################################### rap, decp, magp2, magp3, magp4, smagf = [], [], [], [], [], [] merrp2, merrp3, merrp4, smagerrf = [], [], [], [] rap0, decp0 = [], [] for i in range(len(radec2)): aa = radec2[i].split() rap.append(aa[0]) decp.append(aa[1]) rap0.append(agnkey.agnabsphotdef.deg2HMS(ra=aa[0])) decp0.append(agnkey.agnabsphotdef.deg2HMS(dec=aa[1])) idp = aa[2] magp2.append(aa[3]) magp3.append(aa[4]) magp4.append(aa[5]) merrp2.append(aa[6]) merrp3.append(aa[7]) merrp4.append(aa[8]) _smagf, _smagerrf = 9999, 9999 for j in range(len(fitmag2)): raf, decf, idf, magf, magerrf = fitmag2[j].split() if idf == idp: _smagf = magf _smagerrf = magerrf break smagf.append(_smagf) smagerrf.append(_smagerrf) new_cols = pyfits.ColDefs([ pyfits.Column(name='ra', format='20A', array=np.array(rap)), pyfits.Column(name='dec', format='20A', array=np.array(decp)), pyfits.Column(name='ra0', format='E', array=np.array(rap0)), pyfits.Column(name='dec0', format='E', array=np.array(decp0)), pyfits.Column(name='magp2', format='E', array=np.array(np.where((np.array(magp2) != 'INDEF'), np.array(magp2), 9999), float)), pyfits.Column(name='magp3', format='E', array=np.array(np.where((np.array(magp3) != 'INDEF'), np.array(magp3), 9999), float)), pyfits.Column(name='magp4', format='E', array=np.array(np.where((np.array(magp4) != 'INDEF'), np.array(magp4), 9999), float)), pyfits.Column(name='merrp2', format='E', array=np.array(np.where((np.array(merrp2) != 'INDEF'), np.array(merrp2), 9999), float)), pyfits.Column(name='merrp3', format='E', array=np.array(np.where((np.array(merrp3) != 'INDEF'), np.array(merrp3), 9999), float)), pyfits.Column(name='merrp4', format='E', array=np.array(np.where((np.array(merrp4) != 'INDEF'), np.array(merrp4), 9999), float)), pyfits.Column(name='smagf', format='E', array=np.array(np.where((np.array(smagf) != 'INDEF'), np.array(smagf), 9999), float)), pyfits.Column(name='smagerrf', format='E', array=np.array(np.where((np.array(smagerrf) != 'INDEF'), np.array(smagerrf), 9999), float)), ]) tbhdu = pyfits.BinTableHDU.from_columns(new_cols) hdu = pyfits.PrimaryHDU(header=hdr) thdulist = pyfits.HDUList([hdu, tbhdu]) agnkey.util.delete(img + '.sn2.fits') thdulist.writeto(img + '.sn2.fits') agnkey.util.updateheader(img + '.sn2.fits', 0, {'APCO': [np.mean(_dmag), 'Aperture correction']}) agnkey.util.updateheader(img + '.sn2.fits', 0, {'APCOERR': [np.std(_dmag), 'Aperture correction error']}) agnkey.util.updateheader(img + '.sn2.fits', 0, {'XDIM': [agnkey.util.readkey3(hdr, 'naxis1'), 'x number of pixels']}) agnkey.util.updateheader(img + '.sn2.fits', 0, {'YDIM': [agnkey.util.readkey3(hdr, 'naxis2'), 'y number of pixels']}) agnkey.util.updateheader(img + '.sn2.fits', 0, {'PSF_FWHM': [fwhm * scale, 'FWHM (arcsec) - computed with daophot']}) os.chmod(img + '.sn2.fits', 0664) os.chmod(img + '.psf.fits', 0664) result = 1 except IOError as e: print e result = 0 fwhm = 0.0 traceback.print_exc() return result, fwhm * scale