在pyephem中查找行星或月亮的名字?

时间:2014-02-16 14:18:11

标签: python pyephem

我正在编写一个小小的pyephem程序,用户传递行星或月亮的名称,然后程序会对其进行一些计算。我找不到如何通过名字查找行星或月亮,就像你可以用星星(ephem.star('Arcturus')),所以我的程序目前有一个行星和月亮名称的查找表。 pyephem可以这样做吗?如果没有,是否值得添加?

3 个答案:

答案 0 :(得分:2)

一个有趣的问题!确实存在一个内部方法,基础_libastro用于告诉ephem自己支持哪些对象:

import ephem
from pprint import pprint
pprint(ephem._libastro.builtin_planets())

打印哪些:

[(0, 'Planet', 'Mercury'),
 (1, 'Planet', 'Venus'),
 (2, 'Planet', 'Mars'),
 (3, 'Planet', 'Jupiter'),
 (4, 'Planet', 'Saturn'),
 (5, 'Planet', 'Uranus'),
 (6, 'Planet', 'Neptune'),
 (7, 'Planet', 'Pluto'),
 (8, 'Planet', 'Sun'),
 (9, 'Planet', 'Moon'),
 (10, 'PlanetMoon', 'Phobos'),
 (11, 'PlanetMoon', 'Deimos'),
 (12, 'PlanetMoon', 'Io'),
 (13, 'PlanetMoon', 'Europa'),
 (14, 'PlanetMoon', 'Ganymede'),
 (15, 'PlanetMoon', 'Callisto'),
 (16, 'PlanetMoon', 'Mimas'),
 (17, 'PlanetMoon', 'Enceladus'),
 (18, 'PlanetMoon', 'Tethys'),
 (19, 'PlanetMoon', 'Dione'),
 (20, 'PlanetMoon', 'Rhea'),
 (21, 'PlanetMoon', 'Titan'),
 (22, 'PlanetMoon', 'Hyperion'),
 (23, 'PlanetMoon', 'Iapetus'),
 (24, 'PlanetMoon', 'Ariel'),
 (25, 'PlanetMoon', 'Umbriel'),
 (26, 'PlanetMoon', 'Titania'),
 (27, 'PlanetMoon', 'Oberon'),
 (28, 'PlanetMoon', 'Miranda')]

您只需要这三个项目中的最后一个,因此您可以构建一个名称列表,如:

>>> pprint([name for _0, _1, name in ephem._libastro.builtin_planets()])

返回:

['Mercury',
 'Venus',
 'Mars',
 'Jupiter',
 'Saturn',
 'Uranus',
 'Neptune',
 'Pluto',
 'Sun',
 'Moon',
 'Phobos',
 'Deimos',
 'Io',
 'Europa',
 'Ganymede',
 'Callisto',
 'Mimas',
 'Enceladus',
 'Tethys',
 'Dione',
 'Rhea',
 'Titan',
 'Hyperion',
 'Iapetus',
 'Ariel',
 'Umbriel',
 'Titania',
 'Oberon',
 'Miranda']

然后,您可以使用name调用getattr(ephem, name)来抓取任何这些对象。

答案 1 :(得分:0)

您可以找到教程here

例如:

>>> import ephem
>>> u = ephem.Uranus()
>>> u.compute('1781/3/13')
>>> print u.ra, u.dec, u.mag
5:35:45.28 23:32:54.1 5.6
>>> print ephem.constellation(u)
('Tau', 'Taurus')

我认为你可以在那里找到更多细节。

答案 2 :(得分:0)

import ephem
from ephem import *

## Planet name plus () and return
## just to show what the name must be

buscar = 'Jupiter()' + '\n'
aqui = city('Bogota')
aqui.date = now() - 5/24     ## Substract the time zone hours from UTC


if buscar[-3:-1] == '()':    ## Delete unwanted chars
    astro = buscar[:-3]
    cuerpo = getattr(ephem, astro)() ## YOUR ANSWER

## Body test
cuerpo.compute(aqui)
print(aqui.name, aqui.date)
print(cuerpo.name, cuerpo.az, cuerpo.alt)