Skip to main content
 首页 » 编程设计

python之如何索引天文表中的可观察坐标

2025年04月02日17kenshinobiy

我昨天问了这个问题 ( How to sort through a list of observable coordinates? ) 关于对坐标列表进行排序以删除低于阈值的某些值,我从 @MSeifert 那里得到了很好的答案,但我有一个表格,其中这些坐标值与其他坐标值相匹配目标的属性(例如视星等和 Alt/Az 坐标),所以我现在要求的是一种在 astropy.table 而不是 astropy 中执行此屏蔽技术的方法。 coordinates.SkyCoord 来 self 之前问题的列表。

让我概述一下问题:

我使用这条线获得我的初始 RA 和 Dec 坐标:

radecs = astropy.coordinates.SkyCoord(ra=phi*u.rad, dec=(0.5*np.pi - theta)*u.rad) 

然后我将其转换为Table:

x=Table([radecs.ra,radecs.dec,altaz.alt,altaz.az,app_mag], names=('RA',  
'Dec','Altitude', 'Azimuth','Apparent Magnitude')) 

忽略幅度列,它看起来像这样:

我想删除与偏角小于 -10 度相对应的行,类似于我在第一个问题中要求这样做的方式,但也从表中删除整行数据,而不仅仅是'radecs' 值。

我找到了 this在线问题向我暗示从表中删除行的功能不存在(至少在 2013 年 6 月),但我认为应该有一个相当简单的解决方案来解决我的问题。

请您参考如下方法:

很抱歉让你问另一个问题。我认为您可能已将坐标保存在一列中而不是不同的列中。

如果它们在不同的列中,它的工作方式与其他答案完全相同(但您必须稍微更改索引),因为您需要索引列而不是属性:

x=Table(...) 
x1 = x[x['Dec'] > -10*u.degree] # Remove everything below -10degree declination 
x2 = x[x['Apparent Magnitude'] < 20] # Remove every star with a magnitude above 20 

因此 x['Apparent Magnitude'] 为您提供列 'Apparent Magnitude' 等等。

此外:您还可以指定更复杂的条件:

magnitude_below_20 = x['Apparent Magnitude'] < 20 
dec_above_m10 = x['Dec'] > -10*u.degree 
# Get every row that satisfies both conditions: 
x_from_comb_condition = x[magnitude_below_20 & dec_above_m10] 
# or using the numpy-ufunc (gives the same result): 
x_from_comb_condition = x[np.logical_and(magnitude_below_20, dec_above_m10)] 

或者如果一个条件适用(| 或 np.logical_or)或者如果条件不适用(~ 或 np .logical_not)


例如:

from astropy.coordinates import SkyCoord 
from astropy.table import Table 
import astropy.units as u 
import numpy as np 
phi = np.linspace(0,2*np.pi,20) 
theta = np.linspace(0, np.pi, 20) 
radecs = SkyCoord(ra=phi*u.rad, dec=(0.5*np.pi - theta)*u.rad) 
app_mag = np.random.uniform(15,25, 20) 
x=Table([radecs.ra,radecs.dec,app_mag], names=('RA', 'Dec','Apparent Magnitude')) 
x[x['Dec'] > -10*u.degree] 

给我:

RA              Dec             Apparent Magnitude 
deg             deg  
float64         float64         float64 
0.0             90.0            20.1080708665 
18.9473684211   80.5263157895   22.7223534546 
37.8947368421   71.0526315789   19.4416167208 
56.8421052632   61.5789473684   20.7207435685 
75.7894736842   52.1052631579   19.9318711443 
94.7368421053   42.6315789474   23.8544483535 
113.684210526   33.1578947368   15.8981196334 
132.631578947   23.6842105263   24.2866475431 
151.578947368   14.2105263158   15.9503148326 
170.526315789   4.73684210526   16.5505303858 
189.473684211   -4.73684210526  24.194771397 

而完整的表是:

RA              Dec             Apparent Magnitude 
deg             deg  
float64         float64         float64 
0.0             90.0            20.1080708665 
18.9473684211   80.5263157895   22.7223534546 
37.8947368421   71.0526315789   19.4416167208 
56.8421052632   61.5789473684   20.7207435685 
75.7894736842   52.1052631579   19.9318711443 
94.7368421053   42.6315789474   23.8544483535 
113.684210526   33.1578947368   15.8981196334 
132.631578947   23.6842105263   24.2866475431 
151.578947368   14.2105263158   15.9503148326 
170.526315789   4.73684210526   16.5505303858 
189.473684211   -4.73684210526  24.194771397 
208.421052632   -14.2105263158  15.4721094564 
227.368421053   -23.6842105263  20.6082525987 
246.315789474   -33.1578947368  21.9730819638 
265.263157895   -42.6315789474  17.3627571053 
284.210526316   -52.1052631579  22.7065806097 
303.157894737   -61.5789473684  23.7244993197 
322.105263158   -71.0526315789  19.7676029836 
341.052631579   -80.5263157895  19.2663871267 
0.0             -90.0           19.5025214878 

所以这只保留满足条件的行并“忽略”所有其他行。


可以手动删除行(如果您知道行号):

x.remove_row(0) # Removes the first row 
x.remove_row(-1) # Removes the last row 

但通常情况下,如果您想因条件而丢弃行,那么索引就是您想要的。 remove_row 更适合手动删除一/两行。也许是因为它们是误报或其他原因...... 他们就地工作。所以永远不要做 x = x.remove_row(-1) 因为 x 将是 None。