I'm writing a little pyephem program where the user passes in the name of a planet or moon, and then the program does some calculations about it. I couldn't find how to look up a planet or moon by name, like you can with stars (ephem.star('Arcturus')), so my program currently has a lookup table for planet and moon names. Can pyephem do this? If not, would it be worth adding?
An interesting question! There does exist an internal method that the underlying _libastro
uses to tell ephem
itself what objects are supported:
import ephem
from pprint import pprint
pprint(ephem._libastro.builtin_planets())
Which prints:
[(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')]
You only need the last of these three items, so you could build a list of names like:
>>> pprint([name for _0, _1, name in ephem._libastro.builtin_planets()])
which returns:
['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']
You could then grab any of these objects, given its name
, with a simple getattr(ephem, name)
call.