我正在编写一个小 pyephem 程序,其中用户以行星或月亮的名义传递,然后程序对其进行一些计算。我找不到如何按名称查找行星或月亮,就像使用星星 (ephem.star('Arcturus')) 一样,所以我的程序目前有一个行星和月亮名称的查找表。pyephem可以做到这一点吗?如果没有,是否值得添加?
问问题
824 次
3 回答
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)
调用来获取这些对象中的任何一个。
于 2014-02-17T21:57:33.310 回答
0
您可以在此处找到教程。
例如:
>>> 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')
我认为您可以在那里找到更多详细信息。
于 2014-02-16T14:34:53.547 回答
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)
于 2017-01-18T11:22:27.393 回答