def create_source_table_from_fields(RCF, ra, dec, cutToPrimary, srcband, sdss): # gather rows from sources within a specified radius TT = [] for ifield,(run,camcol,field) in enumerate(RCF): # Retrieve SDSS catalog sources in the field srcs,objs = tsdss.get_tractor_sources_dr9( run, camcol, field, bandname = srcband, sdss = sdss, # cache is in scratch/ radecrad = (ra, dec, radius*np.sqrt(2.)), nanomaggies = True, cutToPrimary = cutToPrimary, getsourceobjs = True, useObjcType = True) print 'Got sources:' for src in srcs: print ' ', src # Write out the sources T = aufits.fits_table() T.ra = [src.getPosition().ra for src in srcs] T.dec = [src.getPosition().dec for src in srcs] # same objects, same order assert(len(objs) == len(srcs)) assert(np.all(T.ra == objs.ra)) # r-band bandnum = 2 T.primary = ((objs.resolve_status & 256) > 0) T.run = objs.run T.camcol = objs.camcol T.field = objs.field T.is_star = (objs.objc_type == 6) T.frac_dev = objs.fracdev[:,bandnum] T.theta_dev = objs.theta_dev[:,bandnum] T.theta_exp = objs.theta_exp[:,bandnum] T.phi_dev = objs.phi_dev_deg[:,bandnum] T.phi_exp = objs.phi_exp_deg[:,bandnum] T.ab_dev = objs.ab_dev[:,bandnum] T.ab_exp = objs.ab_exp[:,bandnum] for band in bands: bi = tsdss.band_index(band) T.set('psfflux_%s' % band, objs.psfflux[:,bi]) T.set('devflux_%s' % band, objs.devflux[:,bi]) T.set('expflux_%s' % band, objs.expflux[:,bi]) T.set('cmodelflux_%s' % band, objs.cmodelflux[:,bi]) TT.append(T) T = tsdss.merge_tables(TT) return T
def get_sdss_sources(bands, targetwcs, photoobjdir=None, local=True, extracols=[], ellipse=None): ''' Finds SDSS catalog sources within the given `targetwcs` region, returning FITS table and Tractor Source objects. Returns ------- cat : Tractor Catalog object Tractor Source objects for the SDSS catalog entries objs : fits_table object FITS table object for the sources. Row-by-row parallel to `cat`. ''' from astrometry.sdss import DR9, band_index, AsTransWrapper from astrometry.sdss.fields import read_photoobjs_in_wcs from tractor.sdss import get_tractor_sources_dr9 # FIXME? margin = 0. if ellipse is None: from tractor.ellipses import EllipseESoft ellipse = EllipseESoft.fromRAbPhi sdss = DR9(basedir=photoobjdir) if local: local = (local and ('BOSS_PHOTOOBJ' in os.environ) and ('PHOTO_RESOLVE' in os.environ)) if local: sdss.useLocalTree() cols = ['objid', 'ra', 'dec', 'fracdev', 'objc_type', 'theta_dev', 'theta_deverr', 'ab_dev', 'ab_deverr', 'phi_dev_deg', 'theta_exp', 'theta_experr', 'ab_exp', 'ab_experr', 'phi_exp_deg', 'resolve_status', 'nchild', 'flags', 'objc_flags', 'run','camcol','field','id', 'psfflux', 'psfflux_ivar', 'cmodelflux', 'cmodelflux_ivar', 'modelflux', 'modelflux_ivar', 'devflux', 'expflux', 'extinction'] + extracols # If we have a window_flist file cut to primary objects, use that. # This file comes from svn+ssh://astrometry.net/svn/trunk/projects/wise-sdss-phot # cut-window-flist.py, and used resolve/2013-07-29 (pre-DR13) as input. wfn = 'window_flist-cut.fits' if not os.path.exists(wfn): # default to the usual window_flist.fits file. wfn = None objs = read_photoobjs_in_wcs(targetwcs, margin, sdss=sdss, cols=cols, wfn=wfn) if objs is None: print('No photoObjs in wcs') return None,None print('Got', len(objs), 'photoObjs') print('Bands', bands, '->', list(bands)) # It can be string-valued objs.objid = np.array([int(x) if len(x) else 0 for x in objs.objid]) srcs = get_tractor_sources_dr9( None, None, None, objs=objs, sdss=sdss, bands=list(bands), nanomaggies=True, fixedComposites=True, useObjcType=True, ellipse=ellipse) print('Created', len(srcs), 'Tractor sources') # record coordinates in target brick image ok,objs.tx,objs.ty = targetwcs.radec2pixelxy(objs.ra, objs.dec) objs.tx -= 1 objs.ty -= 1 W,H = targetwcs.get_width(), targetwcs.get_height() objs.itx = np.clip(np.round(objs.tx), 0, W-1).astype(int) objs.ity = np.clip(np.round(objs.ty), 0, H-1).astype(int) cat = Catalog(*srcs) return cat, objs
NUM_SHAPE = 4 def loss(th0, th1, images): l1, pixel_grid = calc_total_prob_galaxy(images, th0[:NUM_BANDS], th0[NUM_BANDS:NUM_BANDS + NUM_LOC], th0[NUM_BANDS + NUM_LOC:NUM_BANDS + NUM_LOC + NUM_SHAPE]) l2 = calc_total_prob_point_source(pixel_grid, images, th0[:NUM_BANDS], th0[NUM_BANDS:NUM_BANDS + NUM_LOC]) return (l1 - l2) * (l1 - l2) # read in image and corresponding source print "read in images and sources" run = 125 camcol = 1 field = 17 tsrcs = sdss.get_tractor_sources_dr9(run, camcol, field) imgfits = make_fits_images(run, camcol, field) # list of images, list of celeste sources imgs = [imgfits[b] for b in BANDS] srcs = [tractor_src_to_celestepy_src(s) for s in tsrcs] # do gradient descent # 1, 9, 10 galaxies print [src.a for src in srcs] for src in [srcs[10]]: if src.a == 0: continue print "looking at source", src shape_params = np.array([src.theta, src.phi, src.sigma, src.rho])
def get_sdss_sources(bands, targetwcs, photoobjdir=None, local=True, extracols=[], ellipse=None): ''' Finds SDSS catalog sources within the given `targetwcs` region, returning FITS table and Tractor Source objects. Returns ------- cat : Tractor Catalog object Tractor Source objects for the SDSS catalog entries objs : fits_table object FITS table object for the sources. Row-by-row parallel to `cat`. ''' from astrometry.sdss import DR9, band_index, AsTransWrapper from astrometry.sdss.fields import read_photoobjs_in_wcs from tractor.sdss import get_tractor_sources_dr9 # FIXME? margin = 0. if ellipse is None: from tractor.ellipses import EllipseESoft ellipse = EllipseESoft.fromRAbPhi sdss = DR9(basedir=photoobjdir) if local: local = (local and ('BOSS_PHOTOOBJ' in os.environ) and ('PHOTO_RESOLVE' in os.environ)) if local: sdss.useLocalTree() cols = [ 'objid', 'ra', 'dec', 'fracdev', 'objc_type', 'theta_dev', 'theta_deverr', 'ab_dev', 'ab_deverr', 'phi_dev_deg', 'theta_exp', 'theta_experr', 'ab_exp', 'ab_experr', 'phi_exp_deg', 'resolve_status', 'nchild', 'flags', 'objc_flags', 'run', 'camcol', 'field', 'id', 'psfflux', 'psfflux_ivar', 'cmodelflux', 'cmodelflux_ivar', 'modelflux', 'modelflux_ivar', 'devflux', 'expflux', 'extinction' ] + extracols # If we have a window_flist file cut to primary objects, use that. # This file comes from svn+ssh://astrometry.net/svn/trunk/projects/wise-sdss-phot # cut-window-flist.py, and used resolve/2013-07-29 (pre-DR13) as input. wfn = 'window_flist-cut.fits' if not os.path.exists(wfn): # default to the usual window_flist.fits file. wfn = None objs = read_photoobjs_in_wcs(targetwcs, margin, sdss=sdss, cols=cols, wfn=wfn) if objs is None: print('No photoObjs in wcs') return None, None print('Got', len(objs), 'photoObjs') print('Bands', bands, '->', list(bands)) # It can be string-valued objs.objid = np.array([int(x) if len(x) else 0 for x in objs.objid]) srcs = get_tractor_sources_dr9(None, None, None, objs=objs, sdss=sdss, bands=list(bands), nanomaggies=True, fixedComposites=True, useObjcType=True, ellipse=ellipse) print('Created', len(srcs), 'Tractor sources') # record coordinates in target brick image ok, objs.tx, objs.ty = targetwcs.radec2pixelxy(objs.ra, objs.dec) objs.tx -= 1 objs.ty -= 1 W, H = targetwcs.get_width(), targetwcs.get_height() objs.itx = np.clip(np.round(objs.tx), 0, W - 1).astype(int) objs.ity = np.clip(np.round(objs.ty), 0, H - 1).astype(int) cat = Catalog(*srcs) return cat, objs
import seaborn as sns sns.set_style("white") plt.ion() from CelestePy.util.data import make_fits_images, tractor_src_to_celestepy_src import numpy as np import pandas as pd import pyprind if __name__ == '__main__': ######################################################## # subselect stripe field 367 - get existing sources ######################################################## run, camcol, field = 4263, 4, 367 from tractor import sdss as st tsrcs = st.get_tractor_sources_dr9(run, camcol, field) # grab fits images imgfits = make_fits_images(run, camcol, field) ####################################################################### # find a bright Exp galaxy, inspect celeste params and tractor params # ####################################################################### import tractor.galaxy as tg import tractor.pointsource as ps def is_gal(s): return (type(s) == tg.DevGalaxy) or \ (type(s) == tg.ExpGalaxy) or \ (type(s) == tg.CompositeGalaxy)
# rd2 is four pixels above. rd1 = RaDecPos(225.67954, 11.265948) rd2 = RaDecPos(225.67991, 11.265852) lvl = logging.INFO logging.basicConfig(level=lvl, format='%(message)s', stream=sys.stdout) bandname = band sdssprefix = '%06i-%s%i-%04i' % (run, bandname, camcol, field) timg, info = st.get_tractor_image_dr9(run, camcol, field, bandname, roi=roi) sources = st.get_tractor_sources_dr9(run, camcol, field, bandname, roi=roi) wcs = timg.getWcs() for source in sources: x, y = wcs.positionToPixel(source.getPosition(), src=source) print(' (%.2f, %.2f):' % (x + x0, y + y0), source) tractor = Tractor([timg], sources) lnlorig = tractor.getLogLikelihood() zr = np.array([-5., +20.]) * info['skysig'] save(sdssprefix + '-orig', tractor, zr) # find closest source mini = -1 mind2 = 1e6