6
$\begingroup$

I'm looking for a simple API that will allow me to get positions of planets and the moon for a given date, location and time.
Does anyone know an API like that?
Any suggestions or alternatives would be appreciated.

$\endgroup$
  • $\begingroup$ I think it is not really an API, it is more like a database (although it can have an API). Celestia, Google Sky Map, both have this DB. Celestia is opensource, and its database is free. $\endgroup$ – peterh says reinstate Monica Aug 12 at 14:04
  • 3
    $\begingroup$ JPL HORIZONS will do this and is callable from Python docs. It may not meet the definition of "simple" and it may not integrate with your other code however. $\endgroup$ – astrosnapper Aug 12 at 16:15
  • $\begingroup$ There may be an answer at Where can I find/visualize planets/stars/moons/etc positions? or not, I don't really know what does or doesn't count as an API for you in your case, or if you really need a proper API or something else will do. If you can use Python then Skyfield works quite nicely. If you like compiled code, then Spice... $\endgroup$ – uhoh Aug 13 at 0:37
  • $\begingroup$ Also see Python API for JPL Horizons? where an answer mentions astroquery (as does astrosnapper's comment above). $\endgroup$ – uhoh Aug 13 at 0:41
  • $\begingroup$ Thanks guys for your suggestions. I'll definitely check out the python libraries. $\endgroup$ – Deathbat219 Aug 13 at 6:05
3
$\begingroup$

Astropy is one option:

from astropy.time import Time
from astropy.coordinates import solar_system_ephemeris, EarthLocation
from astropy.coordinates import get_body, get_moon
from astropy.coordinates import SkyCoord, EarthLocation, AltAz
from astropy import units as u
import time;

t = Time("2019-08-11 11:00", scale="utc")
loc = EarthLocation(lat=38.2464000*u.deg, lon=274.236400*u.deg, height=0*u.m)

with solar_system_ephemeris.set('jpl'):
  moon = get_body('moon', t, loc)

altazframe = AltAz(obstime=t, location=loc, pressure=0)
moonaz=moon.transform_to(altazframe)

print(moonaz.alt.degree,moonaz.az.degree)

If you want a library you can ship with a product, you'll want something much smaller. I am actually working on a library converted to many different languages to do just that. Only the JavaScript version has a working Alt Az example, but it should be easy to convert to other languages (and will be eventually).

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.