def _buildtrees(x1, x2): (fx1, fx2) = _cleaninputs(x1, x2) kd1 = spherematch_c.kdtree_build(fx1) if fx2 is fx1: kd2 = kd1 else: kd2 = spherematch_c.kdtree_build(fx2) return (kd1, kd2)
def __call__(self, ra, dec, spherematch=True, radius=0, contains=False): T = self.tab # HACK - magic 13x9 +1 arcmin. if radius == 0: radius = sqrt(14.**2 + 10.**2) / 2. d2 = arcmin2distsq(radius) if self.sdssxyz is None: self.sdssxyz = radectoxyz(T.ra, T.dec) if not spherematch: rcfs = [] for r, d in broadcast(ra, dec): xyz = radectoxyz(r, d) dist2s = sum((xyz - self.sdssxyz)**2, axis=1) I = flatnonzero(dist2s < d2) rcfs.append( zip(T[I].run, T[I].camcol, T[I].field, T[I].ra, T[I].dec)) else: from astrometry.libkd import spherematch from astrometry.libkd import spherematch_c if self.kd is None: self.kd = spherematch_c.kdtree_build(self.sdssxyz) rds = array([x for x in broadcast(ra, dec)]) xyz = radectoxyz(rds[:, 0], rds[:, 1]).astype(double) kd2 = spherematch_c.kdtree_build(xyz) notself = False inds, D = spherematch_c.match(self.kd, kd2, np.sqrt(d2), notself, True) if len(inds) == 0: return [] spherematch_c.kdtree_free(kd2) I = np.argsort(D[:, 0]) inds = inds[I] rcfs = [[] for i in range(len(rds))] cols = T.columns() gotem = False if contains: if ('ramin' in cols and 'ramax' in cols and 'decmin' in cols and 'decmax' in cols): gotem = True for j, i in inds: (r, d) = rds[i] if (r >= T.ramin[j] and r <= T.ramax[j] and d >= T.decmin[j] and d <= T.decmax[j]): rcfs[i].append((T.run[j], T.camcol[j], T.field[j], T.ra[j], T.dec[j])) #print '%i fields contain the first query RA,Dec' % len(rcfs[0]) else: print 'you requested fields *containing* the query RA,Dec,' print 'but the fields list file \"%s\" doesn\'t contain RAMIN,RAMAX,DECMIN, and DECMAX columns' % tablefn if not gotem: for j, i in inds: rcfs[i].append( (T.run[j], T.camcol[j], T.field[j], T.ra[j], T.dec[j])) if isscalar(ra) and isscalar(dec): return rcfs[0] return rcfs
def __call__(self, ra, dec, spherematch=True, radius=0, contains=False): T = self.tab # HACK - magic 13x9 arcmin. if radius == 0: radius = sqrt(13.**2 + 9.**2)/2. d2 = arcmin2distsq(radius) if self.sdssxyz is None: self.sdssxyz = radectoxyz(T.ra, T.dec) if not spherematch: rcfs = [] for r,d in broadcast(ra,dec): xyz = radectoxyz(r,d) dist2s = sum((xyz - self.sdssxyz)**2, axis=1) I = flatnonzero(dist2s < d2) rcfs.append(zip(T[I].run, T[I].camcol, T[I].field, T[I].ra, T[I].dec)) else: from astrometry.libkd import spherematch from astrometry.libkd import spherematch_c if self.kd is None: self.kd = spherematch_c.kdtree_build(self.sdssxyz) rds = array([x for x in broadcast(ra,dec)]) xyz = radectoxyz(rds[:,0], rds[:,1]).astype(double) kd2 = spherematch_c.kdtree_build(xyz) notself = False inds,D = spherematch_c.match(self.kd, kd2, np.sqrt(d2), notself) if len(inds) == 0: return [] spherematch_c.kdtree_free(kd2) I = np.argsort(D[:,0]) inds = inds[I] rcfs = [[] for i in range(len(rds))] cols = T.columns() gotem = False if contains: if ('ramin' in cols and 'ramax' in cols and 'decmin' in cols and 'decmax' in cols): gotem = True for j,i in inds: (r,d) = rds[i] if (r >= T.ramin[j] and r <= T.ramax[j] and d >= T.decmin[j] and d <= T.decmax[j]): rcfs[i].append((T.run[j], T.camcol[j], T.field[j], T.ra[j], T.dec[j])) #print '%i fields contain the first query RA,Dec' % len(rcfs[0]) else: print 'you requested fields *containing* the query RA,Dec,' print 'but the fields list file \"%s\" doesn\'t contain RAMIN,RAMAX,DECMIN, and DECMAX columns' % tablefn if not gotem: for j,i in inds: rcfs[i].append((T.run[j], T.camcol[j], T.field[j], T.ra[j], T.dec[j])) if isscalar(ra) and isscalar(dec): return rcfs[0] return rcfs
def tree_build_radec(ra=None, dec=None, xyz=None): if ra is not None: (N,) = ra.shape xyz = np.zeros((N,3)).astype(float) xyz[:,2] = np.sin(np.deg2rad(dec)) cosd = np.cos(np.deg2rad(dec)) xyz[:,0] = cosd * np.cos(np.deg2rad(ra)) xyz[:,1] = cosd * np.sin(np.deg2rad(ra)) kd = spherematch_c.kdtree_build(xyz) return kd
def tree_build(X): ''' X: Numpy array of shape (N,D) Returns: kdtree identifier. ''' return spherematch_c.kdtree_build(X)