Question

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?

Était-ce utile?

La solution

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.

Autres conseils

You can find a tutorial here.

For example:

>>> 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')

I think your are able to find a lot of more details there.

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)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top