我有两个星系目录,并希望将目录1中的每个星系与目录2中最接近的星系配对。我已经编写了一个脚本,只为一个输入目录执行此操作(效率不高但它适用于我需要),发布在下面。
当我更新了我为两个目录编写的代码时,目录2不会在列表中进一步迭代而不是j = 3.由于某种原因,它会卡在那里。
一个目录输入的代码:
def nnpairs(x):
"""
find nearest neighbour of each galaxy and pairs them up
"""
nearest_neighbours = []
shortest_distances = []
displacements = []
m_diffs = []
galaxies1 = []
galaxies2 = []
for i in range (len(x)):
nearest_neighbour = []
shortest_distance = []
displacement = []
m_diff = []
galaxy1 = []
galaxy2 = []
for j in range(len(x)):
if j == 0 and i!=j:
shortest_distance = separation(x[i], x[j])
if (separation(x[i], x[j])) < (shortest_distance) and i!=j:
shortest_distance = separation(x[i], x[j])
nearest_neighbour = CATAID[i], CATAID[j]
displacement = displacement_along_loa(x[i],x[j])
dRA1, dRA2, dDEC1, dDEC2 = displacement
galaxy1 = (CATAID[i], shortest_distance, dRA1, dDEC1)
galaxy2 = (CATAID[j], shortest_distance, dRA2, dDEC2)
m_diff = mass_diff(x[i],x[j])
nearest_neighbours.append(nearest_neighbour)
shortest_distances.append(shortest_distance)
displacements.append(displacement)
m_diffs.append(m_diff)
galaxies1.append(galaxy1)
galaxies2.append(galaxy2)
data = list(zip(nearest_neighbours))
return data
两个目录的代码:
def nnpairs_2(catalog1, catalog2):
"""
find nearest neighbour of each galaxy and pairs them up
"""
nearest_neighbours = []
shortest_distances = []
displacements = []
m_diffs = []
for i in range (len(catalog1)):
nearest_neighbour = []
shortest_distance = []
displacement = []
m_diff = []
j = 0
while j < len(catalog2):
if j == 0 and i!=0:
shortest_distance = separation(catalog1[i], catalog2[j])
if (separation(catalog1[i], catalog2[j])) < (shortest_distance) and i!=j:
shortest_distance = separation(catalog1[i], catalog2[j])
nearest_neighbour = (i,j)
displacement = displacement_along_loa(catalog1[i],catalog2[j])
m_diff = mass_diff(catalog1[i],catalog2[j])
j = j+1
nearest_neighbours.append(nearest_neighbour)
shortest_distances.append(shortest_distance)
displacements.append(displacement)
m_diffs.append(m_diff)
data = list(zip(nearest_neighbours))
return data
这个想法是代码找到每个星系之间的分离并为最小的分离创建最近的邻居对,但对于第二个代码,它只匹配目录1中的所有星系与目录2中的第三个条目。
我知道这里有很多信息,但我们非常感谢任何帮助,提前谢谢!