This is a perfect case where the scipy.spatial.cKDTree()
class can be used to query all the points at once:
from scipy.spatial import cKDTree
k = cKDTree(data[:, 6:8]) # creating the KDtree using the Xpos and Ypos
xyCenters = np.array([[200.6, 310.9],
[300, 300],
[400, 400]])
print(k.query(xyCenters))
# (array([ 1.59740195, 1.56033234, 0.56352196]),
# array([ 2662, 22789, 5932]))
where [ 2662, 22789, 5932]
are the indices corresponding to the three closest points given in xyCenters
. You can use these indices to get your ra
and dec
values very efficiently using np.take()
:
dists, indices = k.query(xyCenters)
myra = np.take(ra, indices)
mydec = np.take(dec, indices)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…