def match_radec(ra1, dec1, ra2, dec2, radius_in_deg, notself=False, nearest=False, indexlist=False): ''' (m1,m2,d12) = match_radec(ra1,dec1, ra2,dec2, radius_in_deg) Cross-matches numpy arrays of RA,Dec points. Behaves like spherematch.pro of IDL ra1,dec1 (and 2): RA,Dec in degrees of points to match. Must be scalars or numpy arrays. radius_in_deg: search radius in degrees. notself: if True, avoids returning 'identity' matches; ASSUMES that ra1,dec1 == ra2,dec2. nearest: if True, returns only the nearest match in (ra2,dec2) for each point in (ra1,dec1). indexlist: returns a list of length len(ra1), containing None or a list of ints of matched points in ra2,dec2. Returns this list. Returns: m1: indices into the "ra1,dec1" arrays of matching points. Numpy array of ints. m2: same, but for "ra2,dec2". d12: distance, in degrees, between the matching points. ''' # Convert to coordinates on the unit sphere xyz1 = radectoxyz(ra1, dec1) #if all(ra1 == ra2) and all(dec1 == dec2): if ra1 is ra2 and dec1 is dec2: xyz2 = xyz1 else: xyz2 = radectoxyz(ra2, dec2) r = deg2dist(radius_in_deg) if nearest: (inds,dists2) = _nearest_func(xyz2, xyz1, r, notself=notself) I = np.flatnonzero(inds >= 0) J = inds[I] d = distsq2deg(dists2[I]) else: X = match(xyz1, xyz2, r, notself=notself, indexlist=indexlist) if indexlist: return X (inds,dists) = X dist_in_deg = dist2deg(dists) I,J = inds[:,0], inds[:,1] d = dist_in_deg[:,0] return (I, J, d)
def trees_match(kd1, kd2, radius, nearest=False, notself=False, permuted=True, count=False): ''' Runs rangesearch or nearest-neighbour matching on given kdtrees. 'radius' is Euclidean distance. If 'nearest'=True, returns the nearest neighbour of each point in "kd1"; ie, "I" will NOT contain duplicates, but "J" may. If 'count'=True, also counts the number of objects within range as well as returning the nearest neighbor of each point in "kd1"; the return value becomes I,J,d,counts , counts a numpy array of ints. Returns (I, J, d), where I are indices into kd1 J are indices into kd2 d are distances-squared [counts is number of sources in range] >>> import numpy as np >>> X = np.array([[1, 2, 3, 6]]).T.astype(float) >>> Y = np.array([[1, 4, 4]]).T.astype(float) >>> kd1 = tree_build(X) >>> kd2 = tree_build(Y) >>> I,J,d = trees_match(kd1, kd2, 1.1, nearest=True) >>> print I [0 1 2] >>> print J [0 0 2] >>> print d [ 0. 60. 60.] >>> I,J,d,count = trees_match(kd1, kd2, 1.1, nearest=True, count=True) >>> print I [0 1 2] >>> print J [0 0 2] >>> print d [ 0. 60. 60.] >>> print count [1 1 2] ''' rtn = None if nearest: rtn = spherematch_c.nearest2(kd2, kd1, radius, notself, count) # J,I,d,[count] rtn = (rtn[1], rtn[0], distsq2deg(rtn[2]),) + rtn[3:] else: (inds,dists) = spherematch_c.match(kd1, kd2, radius, notself, permuted) d = dist2deg(dists[:,0]) I,J = inds[:,0], inds[:,1] rtn = (I,J,d) return rtn
def match_radec(ra1, dec1, ra2, dec2, radius_in_deg, notself=False, nearest=False): ''' (m1,m2,d12) = match_radec(ra1,dec1, ra2,dec2, radius_in_deg) Cross-matches numpy arrays of RA,Dec points. Behaves like spherematch.pro of IDL ra1,dec1 (and 2): RA,Dec in degrees of points to match. Must be scalars or numpy arrays. radius_in_deg: search radius in degrees. notself: if True, avoids returning 'identity' matches; ASSUMES that ra1,dec1 == ra2,dec2. nearest: if True, returns only the nearest match in (ra2,dec2) for each point in (ra1,dec1). Returns: m1: indices into the "ra1,dec1" arrays of matching points. Numpy array of ints. m2: same, but for "ra2,dec2". d12: distance, in degrees, between the matching points. ''' # Convert to coordinates on the unit sphere xyz1 = radectoxyz(ra1, dec1) #if all(ra1 == ra2) and all(dec1 == dec2): if ra1 is ra2 and dec1 is dec2: xyz2 = xyz1 else: xyz2 = radectoxyz(ra2, dec2) r = deg2dist(radius_in_deg) if nearest: (inds,dists2) = _nearest_func(xyz2, xyz1, r, notself=notself) I = np.flatnonzero(inds >= 0) J = inds[I] d = distsq2deg(dists2[I]) else: (inds,dists) = match(xyz1, xyz2, r, notself) dist_in_deg = dist2deg(dists) I,J = inds[:,0], inds[:,1] d = dist_in_deg[:,0] return (I, J, d)
def trees_match(kd1, kd2, radius, nearest=False, notself=False, permuted=True): ''' Runs rangesearch or nearest-neighbour matching on given kdtrees. 'radius' is Euclidean distance. If nearest=True, returns the nearest neighbour of each point in "kd1"; ie, "I" will NOT contain duplicates, but "J" may. Returns (I, J, d), where I are indices into kd1 J are indices into kd2 d are distances-squared >>> import numpy as np >>> X = np.array([[1, 2, 3, 6]]).T.astype(float) >>> Y = np.array([[1, 4, 4]]).T.astype(float) >>> kd1 = tree_build(X) >>> kd2 = tree_build(Y) >>> I,J,d = trees_match(kd1, kd2, 1.1, nearest=True) >>> print I [0 1 2] >>> print J [0 0 2] >>> print d [ 0. 60. 60.] ''' if nearest: J,I,d = spherematch_c.nearest2(kd2, kd1, radius, notself) d = distsq2deg(d) else: (inds,dists) = spherematch_c.match(kd1, kd2, radius, notself, permuted) d = dist2deg(dists[:,0]) I,J = inds[:,0], inds[:,1] return I,J,d
def match_radec(ra1, dec1, ra2, dec2, radius_in_deg, notself=False, nearest=False, indexlist=False, count=False): ''' (m1,m2,d12) = match_radec(ra1,dec1, ra2,dec2, radius_in_deg) Cross-matches numpy arrays of RA,Dec points. Behaves like spherematch.pro of IDL ra1,dec1 (and 2): RA,Dec in degrees of points to match. Must be scalars or numpy arrays. radius_in_deg: search radius in degrees. notself: if True, avoids returning 'identity' matches; ASSUMES that ra1,dec1 == ra2,dec2. nearest: if True, returns only the nearest match in (ra2,dec2) for each point in (ra1,dec1). indexlist: returns a list of length len(ra1), containing None or a list of ints of matched points in ra2,dec2. Returns this list. Returns: m1: indices into the "ra1,dec1" arrays of matching points. Numpy array of ints. m2: same, but for "ra2,dec2". d12: distance, in degrees, between the matching points. ''' # Convert to coordinates on the unit sphere xyz1 = radectoxyz(ra1, dec1) #if all(ra1 == ra2) and all(dec1 == dec2): if ra1 is ra2 and dec1 is dec2: xyz2 = xyz1 else: xyz2 = radectoxyz(ra2, dec2) r = deg2dist(radius_in_deg) extra = () if nearest: X = _nearest_func(xyz2, xyz1, r, notself=notself, count=count) if not count: (inds,dists2) = X I = np.flatnonzero(inds >= 0) J = inds[I] d = distsq2deg(dists2[I]) else: #print 'X', X #(inds,dists2,counts) = X J,I,d,counts = X extra = (counts,) print 'I', I.shape, I.dtype print 'J', J.shape, J.dtype print 'counts', counts.shape, counts.dtype else: X = match(xyz1, xyz2, r, notself=notself, indexlist=indexlist) if indexlist: return X (inds,dists) = X dist_in_deg = dist2deg(dists) I,J = inds[:,0], inds[:,1] d = dist_in_deg[:,0] return (I, J, d) + extra
def match_radec(ra1, dec1, ra2, dec2, radius_in_deg, notself=False, nearest=False, indexlist=False, count=False): ''' Cross-matches numpy arrays of RA,Dec points. Behaves like spherematch.pro of IDL. Parameters ---------- ra1, dec1, ra2, dec2 : numpy arrays, or scalars. RA,Dec in degrees of points to match. radius_in_deg : float Search radius in degrees. notself : boolean If True, avoids returning 'identity' matches; ASSUMES that ra1,dec1 == ra2,dec2. nearest : boolean If True, returns only the nearest match in *(ra2,dec2)* for each point in *(ra1,dec1)*. indexlist : boolean If True, returns a list of length *len(ra1)*, containing *None* or a list of ints of matched points in *ra2,dec2*. Returns ------- m1 : numpy array of integers Indices into the *ra1,dec1* arrays of matching points. m2 : numpy array of integers Same, but for *ra2,dec2*. d12 : numpy array, float Distance, in degrees, between the matching points. ''' # Convert to coordinates on the unit sphere xyz1 = radectoxyz(ra1, dec1) #if all(ra1 == ra2) and all(dec1 == dec2): if ra1 is ra2 and dec1 is dec2: xyz2 = xyz1 else: xyz2 = radectoxyz(ra2, dec2) r = deg2dist(radius_in_deg) extra = () if nearest: X = _nearest_func(xyz2, xyz1, r, notself=notself, count=count) if not count: (inds,dists2) = X I = np.flatnonzero(inds >= 0) J = inds[I] d = distsq2deg(dists2[I]) else: #print 'X', X #(inds,dists2,counts) = X J,I,d,counts = X extra = (counts,) print('I', I.shape, I.dtype) print('J', J.shape, J.dtype) print('counts', counts.shape, counts.dtype) else: X = match(xyz1, xyz2, r, notself=notself, indexlist=indexlist) if indexlist: return X (inds,dists) = X dist_in_deg = dist2deg(dists) I,J = inds[:,0], inds[:,1] d = dist_in_deg[:,0] return (I, J, d) + extra