PyEphem
PyEphem is a Python library for computing positions of the planets and stars. It provides precise astronomical calculations, including positions of celestial bodies, times of sunrise/sunset, moon phases, and more, based on standard algorithms. The current version is 4.2.1, with releases typically occurring a few times a year for minor updates and bug fixes, and major versions every 1-2 years.
Warnings
- gotcha PyEphem uses radians for all internal angle calculations and returns values in radians by default. For human-readable output or input, explicitly convert using `ephem.degrees()`.
- gotcha PyEphem operates internally on UTC (Coordinated Universal Time). Providing naive `datetime` objects that are meant to be local time will be misinterpreted as UTC, leading to incorrect calculations.
- gotcha After changing an observer's date or creating a new observer, you MUST call the `.compute(observer)` method on celestial body objects to update their positions for the new time/location. Failing to do so will result in calculations based on the body's default epoch or previous observer's time.
- gotcha Latitude and longitude strings can be provided directly to `observer.lat` and `observer.lon` (e.g., `'42.35'`), but ensure you understand the sign conventions: positive for North/East, negative for South/West. If providing floats, they are assumed to be in radians unless explicitly converted.
Install
-
pip install ephem
Imports
- ephem
import ephem
- Observer
import ephem observer = ephem.Observer()
Quickstart
import ephem
import datetime
# Create an observer at a specific location and time (UTC)
boston = ephem.Observer()
boston.lat = '42.35' # North latitude
boston.lon = '-71.05' # West longitude
boston.elevation = 0 # Meters above sea level
boston.date = datetime.datetime.utcnow() # Set date to current UTC time
# Create a planet object (Mars)
mars = ephem.Mars()
# Compute its position for the observer's location and time
mars.compute(boston)
# Print celestial coordinates, converting radians to degrees for readability
print(f"Mars Right Ascension (RA): {ephem.degrees(mars.ra)}")
print(f"Mars Declination (Dec): {ephem.degrees(mars.dec)}")
print(f"Mars Azimuth: {ephem.degrees(mars.az)}")
print(f"Mars Altitude: {ephem.degrees(mars.alt)}")
print(f"Mars Distance: {mars.range:.2f} AU")