SGP4 Satellite Orbit Propagator
The `sgp4` Python library provides a highly accurate implementation of the SGP4 and SDP4 orbital propagation algorithms, as described in the 2020 revision of the Spacetrack Report #3. It allows users to track Earth satellites given their Two-Line Element (TLE) data, predicting their position and velocity at specific times. The library is actively maintained, with version 2.25 being the latest, and typically sees several releases per year addressing minor bugs and precision improvements.
Warnings
- gotcha `sgp4` expects naive UTC `datetime` objects or Julian dates. Passing timezone-aware `datetime` objects or naive `datetime` objects in a non-UTC timezone can lead to incorrect calculations or errors.
- gotcha The position and velocity outputs from `satellite.sgp4()` are always in kilometers (km) and kilometers per second (km/s) respectively. Users expecting meters or other units must perform explicit conversions.
- gotcha The `Satrec.twoline2_parse` method requires TLE strings to adhere strictly to the NORAD two-line element set format (69 characters per line). Malformed TLEs (e.g., incorrect length, invalid characters) will result in `ValueError` or `IndexError`.
- breaking In `sgp4` version 1.10, the direct return type of `Satrec.jday()` changed from a single Julian date float to a tuple of two floats (Julian date integer part, Julian date fractional part). While the `sgp4()` propagation method handles this internally, direct access to `Satrec.jday` from code written for versions prior to 1.10 will break if not updated.
Install
-
pip install sgp4
Imports
- Satrec
from sgp4.api import Satrec
- WGS72
from sgp4.api import WGS72
- jday_from_datetime
from sgp4.api import jday_from_datetime
Quickstart
from datetime import datetime, timezone
from sgp4.api import Satrec
# Example TLE for NOAA 15 (valid for a specific epoch)
# Use actual current TLE data for real-world applications
tle_line1 = '1 25338U 98030A 23098.50000000 .00000000 00000-0 57606-2 0 9994'
tle_line2 = '2 25338 98.7402 65.3400 0001000 90.0000 270.0000 14.28000000000000'
# Parse the TLE data into a Satrec object
satellite = Satrec.twoline2_parse(tle_line1, tle_line2)
# Define the time for propagation (example: April 8, 2023, 12:00:00 UTC)
# SGP4 expects naive UTC datetimes or Julian dates.
prop_time = datetime(2023, 4, 8, 12, 0, 0, tzinfo=timezone.utc).replace(tzinfo=None)
# Propagate the satellite to the specified time
# The sgp4 method returns (error_code, position_km, velocity_km_s)
e, r, v = satellite.sgp4(
prop_time.year, prop_time.month, prop_time.day,
prop_time.hour, prop_time.minute, prop_time.second + prop_time.microsecond / 1_000_000
)
if e == 0:
print(f"Satellite ID: {satellite.satnum}")
print(f"Propagation time: {prop_time} UTC")
print(f"Position (km, ECI): {r}")
print(f"Velocity (km/s, ECI): {v}")
else:
print(f"SGP4 error code: {e}. Check TLE validity or propagation time range.")