我正在尝试编写一个非常简单的程序,它以公里为单位打印到任何行星的当前距离。我正在使用天域。 这是我的火星代码:
from skyfield.api import earth, mars, now
ra, dec, distance = earth(now()).observe(mars).radec()
print(distance)
这将以天文单位打印出距离。 为了转换为公里,我尝试乘以 149597871:
from skyfield.api import earth, mars, now
ra, dec, distance = earth(now()).observe(mars).radec()
print(distance*149597871)
但这会返回一个错误:
TypeError: unsupported operand type(s) for *: 'Distance' and 'int'
我能做什么?
最佳答案
distance
是一个 Distance
对象,所以如果您想要以公里为单位的距离,您可以简单地使用 distance.km
。如果您想自己进行转换,可以使用 distance.AU * 149597871
。 (如果不完全匹配,那是因为Skyfield使用149597870.700 km/au进行换算)。
可以在源码中看到这个类的实现here .
关于python 将 au 转换为 km,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28867022/