def get_tractor_sources_cas_dr9(table, bandname='r', bands=None, extrabands=None, nanomaggies=False): ''' table: filename or astrometry.util.fits.fits_table() object ''' from astrometry.sdss import band_names from astrometry.util.fits import fits_table if isinstance(table, basestring): cas = fits_table(table) else: cas = table # Make it look like a "photoObj" file. cols = cas.get_columns() N = len(cas) T = tabledata() T.ra = cas.ra T.dec = cas.dec # nchild if not 'nchild' in cols: T.nchild = np.zeros(N, int) else: T.nchild = cas.nchild # rowc,colc -- shouldn't be necessary... if not 'objc_flags' in cols: T.objc_flags = np.zeros(N, int) else: T.objc_flags = cas.objc_flags allbands = band_names() nbands = len(allbands) colmap = [ ('phi_dev_deg', 'devphi'), ('phi_exp_deg', 'expphi'), ('theta_dev', 'devrad'), ('theta_exp', 'exprad'), ('ab_dev', 'devab'), ('ab_exp', 'expab'), ('psfflux', 'psfflux'), ('cmodelflux', 'cmodelflux'), ('devflux', 'devflux'), ('expflux', 'expflux'), ('fracdev', 'fracdev'), ('prob_psf', 'probpsf'), ] for c1, c2 in colmap: T.set(c1, np.zeros((N, nbands))) for bi, b in enumerate(allbands): for c1, c2 in colmap: cname = '%s_%s' % (c2, b) if cname in cols: T.get(c1)[:, bi] = cas.get(cname) return _get_sources(-1, -1, -1, release='DR9', objs=T, sdss=DR9(), bandname=bandname, bands=bands, nanomaggies=nanomaggies, extrabands=extrabands)
def get_tractor_sources_cas_dr9(table, bandname='r', bands=None, extrabands=None, nanomaggies=False): ''' table: filename or astrometry.util.fits.fits_table() object ''' from astrometry.sdss import band_names from astrometry.util.fits import fits_table if isinstance(table, basestring): cas = fits_table(table) else: cas = table # Make it look like a "photoObj" file. cols = cas.get_columns() N = len(cas) T = tabledata() T.ra = cas.ra T.dec = cas.dec # nchild if not 'nchild' in cols: T.nchild = np.zeros(N,int) else: T.nchild = cas.nchild # rowc,colc -- shouldn't be necessary... if not 'objc_flags' in cols: T.objc_flags = np.zeros(N,int) else: T.objc_flags = cas.objc_flags allbands = band_names() nbands = len(allbands) colmap = [('phi_dev_deg', 'devphi'), ('phi_exp_deg', 'expphi'), ('theta_dev', 'devrad'), ('theta_exp', 'exprad'), ('ab_dev', 'devab'), ('ab_exp', 'expab'), ('psfflux', 'psfflux'), ('cmodelflux', 'cmodelflux'), ('devflux', 'devflux'), ('expflux', 'expflux'), ('fracdev', 'fracdev'), ('prob_psf', 'probpsf'), ] for c1,c2 in colmap: T.set(c1, np.zeros((N, nbands))) for bi,b in enumerate(allbands): for c1,c2 in colmap: cname = '%s_%s' % (c2, b) if cname in cols: T.get(c1)[:,bi] = cas.get(cname) return _get_sources(-1, -1, -1, release='DR9', objs=T, sdss=DR9(), bandname=bandname, bands=bands, nanomaggies=nanomaggies, extrabands=extrabands)
def _get_sources(run, camcol, field, bandname='r', sdss=None, release='DR7', objs=None, retrieve=True, checkFiles=True, curl=False, roi=None, radecroi=None, radecrad=None, bands=None, badmag=25, nanomaggies=False, getobjs=False, getsourceobjs=False, getobjinds=False, extrabands=None, fixedComposites=False, forcePointSources=False, useObjcType=False, objCuts=True, classmap={}, ellipse=GalaxyShape, cutToPrimary=False): ''' If set, radecrad = (ra,dec,rad) returns sources within "rad" degrees of the given RA,Dec (in degrees) center. WARNING, this method alters the "objs" argument, if given. Consider calling objs.copy() before calling. -"bandname" is the SDSS band used to cut on position, select star/gal/exp/dev, and set galaxy shapes. -"bands" are the bands to include in the returned Source objects; they will be initialized from the SDSS bands. -"extrabands" are also included in the returned Source objects; they will be initialized to the SDSS flux for either the first of "bands", if given, or "bandname". ''' from astrometry.sdss import (DR7, DR8, DR9, band_names, band_index, photo_flags1_map) # brightPointSourceThreshold=0.): if sdss is None: dr = dict(DR7=DR7, DR8=DR8, DR9=DR9)[release] sdss = dr(curl=curl) drnum = sdss.getDRNumber() isdr7 = (drnum == 7) if bands is None: bands = band_names() bandnum = band_index(bandname) bandnums = np.array([band_index(b) for b in bands]) bandnames = bands if extrabands is None: extrabands = [] if objs is None: from astrometry.util.fits import fits_table if isdr7: # FIXME rerun = 0 if checkFiles: _check_sdss_files(sdss, run, camcol, field, bandnum, ['tsObj', 'tsField'], retrieve=retrieve, tryopen=True) tsf = sdss.readTsField(run, camcol, field, rerun) objfn = sdss.getPath('tsObj', run, camcol, field, bandname, rerun=rerun) else: if checkFiles: _check_sdss_files(sdss, run, camcol, field, bandnum, ['photoObj'], retrieve=retrieve, tryopen=True) objfn = sdss.getPath('photoObj', run, camcol, field) objs = fits_table(objfn) if objs is None: print('No sources in SDSS file', objfn) return [] objs.index = np.arange(len(objs)) if getobjs: allobjs = objs.copy() if roi is not None: x0, x1, y0, y1 = roi # FIXME -- we keep only the sources whose centers are within # the ROI box. Should instead do some ellipse-overlaps # geometry. x = objs.colc[:, bandnum] y = objs.rowc[:, bandnum] objs.cut((x >= x0) * (x < x1) * (y >= y0) * (y < y1)) if radecroi is not None: r0, r1, d0, d1 = radecroi objs.cut((objs.ra >= r0) * (objs.ra <= r1) * (objs.dec >= d0) * (objs.dec <= d1)) if radecrad is not None: from astrometry.libkd.spherematch import match_radec (ra, dec, rad) = radecrad I, J, d = match_radec(ra, dec, objs.ra, objs.dec, rad) objs.cut(J) del I del d if objCuts: # Only deblended children; # No BRIGHT sources bright = photo_flags1_map.get('BRIGHT') objs.cut((objs.nchild == 0) * ((objs.objc_flags & bright) == 0)) if cutToPrimary: objs.cut((objs.resolve_status & 256) > 0) if len(objs) == 0: sources = [] if not (getobjs or getobjinds or getsourceobjs): return sources rtn = [sources] if getobjs: rtn.append(None) if getobjinds: rtn.append(None) if getsourceobjs: rtn.append(None) return rtn if isdr7: objs.rename('phi_dev', 'phi_dev_deg') objs.rename('phi_exp', 'phi_exp_deg') objs.rename('r_dev', 'theta_dev') objs.rename('r_exp', 'theta_exp') # SDSS and Tractor have different opinions on which way this rotation goes objs.phi_dev_deg *= -1. objs.phi_exp_deg *= -1. # MAGIC -- minimum size of galaxy. objs.theta_dev = np.maximum(objs.theta_dev, 1. / 30.) objs.theta_exp = np.maximum(objs.theta_exp, 1. / 30.) if forcePointSources: Lstar = np.ones(len(objs), float) Lgal = np.zeros(len(objs), float) Ldev = Lexp = Lgal else: if useObjcType: objs.cut(np.logical_or(objs.objc_type == 6, objs.objc_type == 3)) Lstar = (objs.objc_type == 6) Lgal = (objs.objc_type == 3) else: Lstar = (objs.prob_psf[:, bandnum] == 1) * 1.0 Lgal = (objs.prob_psf[:, bandnum] == 0) * 1.0 if isdr7: fracdev = objs.fracpsf[:, bandnum] else: fracdev = objs.fracdev[:, bandnum] Ldev = Lgal * fracdev Lexp = Lgal * (1. - fracdev) if isdr7: from .sdss_dr7 import _dr7_getBrightness if nanomaggies: raise RuntimeError('Nanomaggies not supported for DR7 (yet)') def lup2bright(lups): counts = [ tsf.luptitude_to_counts(lup, j) for j, lup in enumerate(lups) ] counts = np.array(counts) bright = _dr7_getBrightness(counts, tsf, bandnames, extrabands) return bright flux2bright = lup2bright starflux = objs.psfcounts compflux = objs.counts_model devflux = objs.counts_dev expflux = objs.counts_exp def comp2bright(lups, Ldev, Lexp): counts = [ tsf.luptitude_to_counts(lup, j) for j, lup in enumerate(lups) ] counts = np.array(counts) dcounts = counts * Ldev ecounts = counts * Lexp dbright = _dr7_getBrightness(dcounts, tsf, bands, extrabands) ebright = _dr7_getBrightness(ecounts, tsf, bands, extrabands) return dbright, ebright else: def nmgy2bright(flux): if len(bandnums): flux = flux[bandnums] else: flux = flux[np.array([bandnum])] bb = bandnames + extrabands if nanomaggies: if len(extrabands): if len(bandnums) == 0: # Only "extrabands", no SDSS bands. flux = np.zeros(len(extrabands)) + flux[0] else: flux = np.append(flux, np.zeros(len(extrabands))) bright = NanoMaggies(order=bb, **dict(zip(bb, flux))) else: I = (flux > 0) mag = np.zeros_like(flux) + badmag mag[I] = sdss.nmgy_to_mag(flux[I]) if len(extrabands): mag = np.append(mag, np.zeros(len(extrabands)) + badmag) bright = Mags(order=bb, **dict(zip(bb, mag))) return bright def comp2bright(flux, Ldev, Lexp): dflux = flux * Ldev eflux = flux * Lexp dbright = nmgy2bright(dflux) ebright = nmgy2bright(eflux) return dbright, ebright flux2bright = nmgy2bright starflux = objs.psfflux compflux = objs.cmodelflux devflux = objs.devflux expflux = objs.expflux sources = [] nstars, ndev, nexp, ncomp = 0, 0, 0, 0 isources = [] ptsrcclass = classmap.get(PointSource, PointSource) for i in range(len(objs)): if Lstar[i]: pos = RaDecPos(objs.ra[i], objs.dec[i]) flux = starflux[i, :] bright = flux2bright(flux) # This should work, I just don't feel like testing it now... # if brightPointSourceThreshold: # ps = SdssPointSource(pos, bright, thresh=brightPointSourceThreshold) # else: # ps = PointSource(pos, bright) sources.append(ptsrcclass(pos, bright)) nstars += 1 isources.append(i) continue hasdev = (Ldev[i] > 0) hasexp = (Lexp[i] > 0) iscomp = (hasdev and hasexp) pos = RaDecPos(objs.ra[i], objs.dec[i]) if iscomp: flux = compflux[i, :] elif hasdev: flux = devflux[i, :] elif hasexp: flux = expflux[i, :] else: print( 'Skipping object with Lstar = %g, Ldev = %g, Lexp = %g (fracdev=%g)' % (Lstar[i], Ldev[i], Lexp[i], fracdev[i])) continue isources.append(i) if iscomp: if fixedComposites: bright = flux2bright(flux) fdev = (Ldev[i] / (Ldev[i] + Lexp[i])) else: dbright, ebright = comp2bright(flux, Ldev[i], Lexp[i]) else: bright = flux2bright(flux) if hasdev: re = objs.theta_dev[i, bandnum] ab = objs.ab_dev[i, bandnum] phi = objs.phi_dev_deg[i, bandnum] dshape = ellipse(re, ab, phi) if hasexp: re = objs.theta_exp[i, bandnum] ab = objs.ab_exp[i, bandnum] phi = objs.phi_exp_deg[i, bandnum] eshape = ellipse(re, ab, phi) if iscomp: if fixedComposites: gal = FixedCompositeGalaxy(pos, bright, fdev, eshape, dshape) else: gal = CompositeGalaxy(pos, ebright, eshape, dbright, dshape) ncomp += 1 elif hasdev: gal = DevGalaxy(pos, bright, dshape) ndev += 1 elif hasexp: gal = ExpGalaxy(pos, bright, eshape) nexp += 1 sources.append(gal) print( 'Created', ndev, 'deV,', nexp, 'exp,', ncomp, 'composite', ) print('(total %i) galaxies and %i stars' % (ndev + nexp + ncomp, nstars)) if not (getobjs or getobjinds or getsourceobjs): return sources if nstars + ndev + nexp + ncomp < len(objs): objs = objs[np.array(isources)] rtn = [sources] if getobjs: rtn.append(allobjs) if getobjinds: rtn.append(objs.index if len(objs) else np.array([])) if getsourceobjs: rtn.append(objs) return rtn
def _get_sources(run, camcol, field, bandname='r', sdss=None, release='DR7', objs=None, retrieve=True, curl=False, roi=None, radecroi=None, radecrad=None, bands=None, badmag=25, nanomaggies=False, getobjs=False, getsourceobjs=False, getobjinds=False, extrabands=None, fixedComposites=False, forcePointSources=False, useObjcType=False, objCuts=True, classmap={}, ellipse=GalaxyShape, cutToPrimary=False): ''' If set, radecrad = (ra,dec,rad) returns sources within "rad" degrees of the given RA,Dec (in degrees) center. WARNING, this method alters the "objs" argument, if given. Consider calling objs.copy() before calling. -"bandname" is the SDSS band used to cut on position, select star/gal/exp/dev, and set galaxy shapes. -"bands" are the bands to include in the returned Source objects; they will be initialized from the SDSS bands. -"extrabands" are also included in the returned Source objects; they will be initialized to the SDSS flux for either the first of "bands", if given, or "bandname". ''' from astrometry.sdss import (DR7, DR8, DR9, band_names, band_index, photo_flags1_map) # brightPointSourceThreshold=0.): if sdss is None: dr = dict(DR7=DR7, DR8=DR8, DR9=DR9)[release] sdss = dr(curl=curl) drnum = sdss.getDRNumber() isdr7 = (drnum == 7) if bands is None: bands = band_names() bandnum = band_index(bandname) bandnums = np.array([band_index(b) for b in bands]) bandnames = bands if extrabands is None: extrabands = [] if objs is None: from astrometry.util.fits import fits_table if isdr7: # FIXME rerun = 0 _check_sdss_files(sdss, run, camcol, field, bandnum, ['tsObj', 'tsField'], retrieve=retrieve, tryopen=True) tsf = sdss.readTsField(run, camcol, field, rerun) objfn = sdss.getPath('tsObj', run, camcol, field, bandname, rerun=rerun) else: _check_sdss_files(sdss, run, camcol, field, bandnum, ['photoObj'], tryopen=True, retrieve=retrieve) objfn = sdss.getPath('photoObj', run, camcol, field) objs = fits_table(objfn) if objs is None: print('No sources in SDSS file', objfn) return [] objs.index = np.arange(len(objs)) if getobjs: allobjs = objs.copy() if roi is not None: x0,x1,y0,y1 = roi # FIXME -- we keep only the sources whose centers are within # the ROI box. Should instead do some ellipse-overlaps # geometry. x = objs.colc[:,bandnum] y = objs.rowc[:,bandnum] objs.cut((x >= x0) * (x < x1) * (y >= y0) * (y < y1)) if radecroi is not None: r0,r1,d0,d1 = radecroi objs.cut((objs.ra >= r0) * (objs.ra <= r1) * (objs.dec >= d0) * (objs.dec <= d1)) if radecrad is not None: from astrometry.libkd.spherematch import match_radec (ra,dec,rad) = radecrad I,J,d = match_radec(ra, dec, objs.ra, objs.dec, rad) objs.cut(J) del I del d if objCuts: # Only deblended children; # No BRIGHT sources bright = photo_flags1_map.get('BRIGHT') objs.cut((objs.nchild == 0) * ((objs.objc_flags & bright) == 0)) if cutToPrimary: objs.cut((objs.resolve_status & 256) > 0) if isdr7: objs.rename('phi_dev', 'phi_dev_deg') objs.rename('phi_exp', 'phi_exp_deg') objs.rename('r_dev', 'theta_dev') objs.rename('r_exp', 'theta_exp') # SDSS and Tractor have different opinions on which way this rotation goes objs.phi_dev_deg *= -1. objs.phi_exp_deg *= -1. # MAGIC -- minimum size of galaxy. objs.theta_dev = np.maximum(objs.theta_dev, 1./30.) objs.theta_exp = np.maximum(objs.theta_exp, 1./30.) if forcePointSources: Lstar = np.ones(len(objs), float) Lgal = np.zeros(len(objs), float) Ldev = Lexp = Lgal else: if useObjcType: objs.cut(np.logical_or(objs.objc_type == 6, objs.objc_type == 3)) Lstar = (objs.objc_type == 6) Lgal = (objs.objc_type == 3) else: Lstar = (objs.prob_psf[:,bandnum] == 1) * 1.0 Lgal = (objs.prob_psf[:,bandnum] == 0) if isdr7: fracdev = objs.fracpsf[:,bandnum] else: fracdev = objs.fracdev[:,bandnum] Ldev = Lgal * fracdev Lexp = Lgal * (1. - fracdev) if isdr7: if nanomaggies: raise RuntimeError('Nanomaggies not supported for DR7 (yet)') def lup2bright(lups): counts = [tsf.luptitude_to_counts(lup,j) for j,lup in enumerate(lups)] counts = np.array(counts) bright = _getBrightness(counts, tsf, bandnames, extrabands) return bright flux2bright = lup2bright starflux = objs.psfcounts compflux = objs.counts_model devflux = objs.counts_dev expflux = objs.counts_exp def comp2bright(lups, Ldev, Lexp): counts = [tsf.luptitude_to_counts(lup,j) for j,lup in enumerate(lups)] counts = np.array(counts) dcounts = counts * Ldev ecounts = counts * Lexp dbright = _getBrightness(dcounts, tsf, bands, extrabands) ebright = _getBrightness(ecounts, tsf, bands, extrabands) return dbright, ebright else: def nmgy2bright(flux): if len(bandnums): flux = flux[bandnums] else: flux = flux[np.array([bandnum])] bb = bandnames + extrabands if nanomaggies: if len(extrabands): if len(bandnums) == 0: # Only "extrabands", no SDSS bands. flux = np.zeros(len(extrabands)) + flux[0] else: flux = np.append(flux, np.zeros(len(extrabands))) bright = NanoMaggies(order=bb, **dict(zip(bb, flux))) else: I = (flux > 0) mag = np.zeros_like(flux) + badmag mag[I] = sdss.nmgy_to_mag(flux[I]) if len(extrabands): mag = np.append(mag, np.zeros(len(extrabands)) + badmag) bright = Mags(order=bb, **dict(zip(bb,mag))) return bright def comp2bright(flux, Ldev, Lexp): dflux = flux * Ldev eflux = flux * Lexp dbright = nmgy2bright(dflux) ebright = nmgy2bright(eflux) return dbright, ebright flux2bright = nmgy2bright starflux = objs.psfflux compflux = objs.cmodelflux devflux = objs.devflux expflux = objs.expflux sources = [] nstars, ndev, nexp, ncomp = 0, 0, 0, 0 isources = [] ptsrcclass = classmap.get(PointSource, PointSource) for i in range(len(objs)): if Lstar[i]: pos = RaDecPos(objs.ra[i], objs.dec[i]) flux = starflux[i,:] bright = flux2bright(flux) # This should work, I just don't feel like testing it now... # if brightPointSourceThreshold: # ps = SdssPointSource(pos, bright, thresh=brightPointSourceThreshold) # else: # ps = PointSource(pos, bright) sources.append(ptsrcclass(pos, bright)) nstars += 1 isources.append(i) continue hasdev = (Ldev[i] > 0) hasexp = (Lexp[i] > 0) iscomp = (hasdev and hasexp) pos = RaDecPos(objs.ra[i], objs.dec[i]) if iscomp: flux = compflux[i,:] elif hasdev: flux = devflux[i,:] elif hasexp: flux = expflux[i,:] else: print('Skipping object with Lstar = %g, Ldev = %g, Lexp = %g (fracdev=%g)' % (Lstar[i], Ldev[i], Lexp[i], fracdev[i])) continue isources.append(i) if iscomp: if fixedComposites: bright = flux2bright(flux) fdev = (Ldev[i] / (Ldev[i] + Lexp[i])) else: dbright,ebright = comp2bright(flux, Ldev[i], Lexp[i]) else: bright = flux2bright(flux) if hasdev: re = objs.theta_dev [i,bandnum] ab = objs.ab_dev [i,bandnum] phi = objs.phi_dev_deg[i,bandnum] dshape = ellipse(re, ab, phi) if hasexp: re = objs.theta_exp [i,bandnum] ab = objs.ab_exp [i,bandnum] phi = objs.phi_exp_deg[i,bandnum] eshape = ellipse(re, ab, phi) if iscomp: if fixedComposites: gal = FixedCompositeGalaxy(pos, bright, fdev, eshape, dshape) else: gal = CompositeGalaxy(pos, ebright, eshape, dbright, dshape) ncomp += 1 elif hasdev: gal = DevGalaxy(pos, bright, dshape) ndev += 1 elif hasexp: gal = ExpGalaxy(pos, bright, eshape) nexp += 1 sources.append(gal) print('Created', ndev, 'deV,', nexp, 'exp,', ncomp, 'composite',) print('(total %i) galaxies and %i stars' % (ndev+nexp+ncomp, nstars)) if not (getobjs or getobjinds or getsourceobjs): return sources if nstars + ndev + nexp + ncomp < len(objs): objs = objs[np.array(isources)] rtn = [sources] if getobjs: rtn.append(allobjs) if getobjinds: rtn.append(objs.index if len(objs) else np.array([])) if getsourceobjs: rtn.append(objs) return rtn