Track earth satellite TLE orbits using up-to-date 2020 version of SGP4
Project description
This Python package computes the position and velocity of an earth-orbiting satellite, given the satellite’s TLE orbital elements from a source like CelesTrak. It implements the most recent version of SGP4, and is regularly run against the SGP4 test suite to make sure that its satellite position predictions agree to within 0.1 mm with the predictions of the standard distribution of the algorithm. This error is far less than the 1–3 km/day by which satellites themselves deviate from the ideal orbits described in TLE files.
If your platform supports it, this package compiles the verbatim source code from the official C++ version of SGP4. You can call the routine directly, or through an array API that loops over arrays of satellites and arrays of times with machine code instead of Python.
Otherwise, a slower but reliable Python implementation of SGP4 is used instead.
Note that the SGP4 propagator returns raw x,y,z Cartesian coordinates in a “True Equator Mean Equinox” (TEME) reference frame that’s centered on the Earth but does not rotate with it — an “Earth centered inertial” (ECI) reference frame. The SGP4 propagator itself does not implement the math to convert these positions into more official ECI frames like J2000 or the ICRF; nor to convert positions into any Earth-centered Earth-fixed (ECEF) frames like the ITRS; nor to convert them to latitudes and longitudes through an Earth ellipsoid like WGS84.
For conversions into other coordinate frames, look for a comprehensive astronomy library that is built atop this one, like the Skyfield library:
https://rhodesmill.org/skyfield/earth-satellites.html
Usage
This library uses the same function names as the official C++ code, to help users who may already be familiar with SGP4 in other languages. Here is how to compute the x,y,z position and velocity for the International Space Station at 12:50:19 on 29 June 2000:
>>> from sgp4.api import Satrec >>> >>> s = '1 25544U 98067A 19343.69339541 .00001764 00000-0 38792-4 0 9991' >>> t = '2 25544 51.6439 211.2001 0007417 17.6667 85.6398 15.50103472202482' >>> satellite = Satrec.twoline2rv(s, t) >>> >>> jd, fr = 2458827, 0.362605 >>> e, r, v = satellite.sgp4(jd, fr) >>> e 0 >>> print(r) # True Equator Mean Equinox position (km) (-6102.44..., -986.33..., -2820.31...) >>> print(v) # True Equator Mean Equinox velocity (km/s) (-1.45..., -5.52..., 5.10...)
As input, you can provide either:
A simple floating-point Julian Date for jd and the value 0.0 for fr, if you are happy with the precision of a 64-bit floating point number. Note that modern Julian Dates are greater than 2,450,000 which means that nearly half of the precision of a 64-bit float will be consumed by the whole part that specifies the day. The remaining digits will provide a precision for the fraction of around 20.1 µs. This should be no problem for the accuracy of your result — satellite positions usually off by a few kilometers anyway, far less than a satellite moves in 20.1 µs — but if you run a solver that dives down into the microseconds while searching for a rising or setting time, the solver might be bothered by the 20.1 µs plateau between each jump in the satellite’s position.
Or, you can provide a coarse date jd plus a very precise fraction fr that supplies the rest of the value. The Julian Date for which the satellite position is computed is the sum of the two values. One common practice is to provide the whole number as jd and the fraction as fr; another is to have jd carry the fraction 0.5 since UTC midnight occurs halfway through each Julian Date. Either way, splitting the value allows a solver to run all the way down into the nanoseconds and still see SGP4 respond smoothly to tiny date adjustments with tiny changes in the resulting satellite position.
Here is how to intrepret the results:
e will be a non-zero error code if the satellite position could not be computed for the given date. You can from sgp4.api import SGP4_ERRORS to access a dictionary mapping error codes to error messages explaining what each code means.
r measures the satellite position in kilometers from the center of the earth in the idiosyncratic True Equator Mean Equinox coordinate frame used by SGP4.
v velocity is the rate at which the position is changing, expressed in kilometers per second.
If your application does not natively handle Julian dates, you can compute jd and fr from calendar dates using jday().
>>> from sgp4.api import jday >>> jd, fr = jday(2019, 12, 9, 12, 0, 0) >>> jd 2458826.5 >>> fr 0.5
OMM
The industry is making adjustments because the fixed-width TLE format will soon run out of satellite numbers.
Some TLE files now use a new “Alpha-5” convention that expands the range of satellite numbers by using an initial letter; for example, “E8493” means satellite 148493. This library now supports the Alpha-5 convention and should return the correct integer in Python.
Some authorities are now distributing satellite elements in an “OMM” Orbit Mean Elements Message format that replaces the TLE format. You can learn about OMM in Dr. T.S. Kelso’s “A New Way to Obtain GP Data” at the CelesTrak site.
You can already try out experimental support for OMM:
>>> from sgp4 import omm
Reading OMM data takes two steps, because OMM supports several different text formats. First, parse the input text to recover the field names and values that it stores; second, build a Python satellite object from those field values. For example, to load OMM from XML:
>>> with open('sample_omm.xml') as f: ... fields = next(omm.parse_xml(f)) >>> sat = Satrec() >>> omm.initialize(sat, fields)
Or, to load OMM from CSV:
>>> with open('sample_omm.csv') as f: ... fields = next(omm.parse_csv(f)) >>> sat = Satrec() >>> omm.initialize(sat, fields)
Either way, the satellite object should wind up properly initialized and ready to start producing positions.
If you are interested in saving satellite parameters using the new OMM format, then read the section on “Export” below.
Epoch
Over a given satellite’s lifetime, dozens or hundreds of different TLE records will be produced as its orbit evolves. Each TLE record specifies the “epoch date” for which it is most accurate. Typically a TLE is only useful for a couple of weeks to either side of its epoch date, beyond which its predictions become unreliable.
Satellite objects natively provide their epoch as a two-digit year and then a fractional number of days into the year:
>>> satellite.epochyr 19 >>> satellite.epochdays 343.69339541
Because Sputnik was launched in 1957, satellite element sets will never refer to an earlier year, so years 57 through 99 mean 1957–1999 while 0 through 56 mean 2000–2056. The TLE format will presumably be obsolete in 2057 and have to be upgraded to 4-digit years.
To turn the number of days and its fraction into a calendar date and time, use the days2mdhms() function.
>>> from sgp4.api import days2mdhms >>> month, day, hour, minute, second = days2mdhms(19, 343.69339541) >>> month 12 >>> day 9 >>> hour 16 >>> minute 38 >>> second 29.363424
The SGP4 library also translates those two numbers into a Julian date and fractional Julian date, since Julian dates are more commonly used in astronomy.
>>> satellite.jdsatepoch 2458826.5 >>> satellite.jdsatepochF 0.69339541
Finally, a convenience function is available in the library if you need the epoch date and time as Python datetime.
>>> from sgp4.conveniences import sat_epoch_datetime >>> sat_epoch_datetime(satellite) datetime.datetime(2019, 12, 9, 16, 38, 29, 363423, tzinfo=UTC)
Array Acceleration
To avoid the expense of Python loops when you have many dates, you can pass them as arrays to another method that understands NumPy:
>>> import numpy as np >>> np.set_printoptions(precision=2)
>>> jd = np.array((2458826, 2458826, 2458826, 2458826)) >>> fr = np.array((0.0001, 0.0002, 0.0003, 0.0004))
>>> e, r, v = satellite.sgp4_array(jd, fr)
>>> print(e) [0 0 0 0] >>> print(r) [[-3431.31 2620.15 -5252.97] [-3478.86 2575.14 -5243.87] [-3526.09 2529.89 -5234.28] [-3572.98 2484.41 -5224.19]] >>> print(v) [[-5.52 -5.19 1.02] [-5.49 -5.22 1.08] [-5.45 -5.25 1.14] [-5.41 -5.28 1.2 ]]
To avoid the expense of Python loops when you have many satellites and dates, build a SatrecArray from several individual satellites. Its sgp4() method will expect both jd and fr to be NumPy arrays, so if you only have one date, be sure to provide NumPy arrays of length one. Here is a sample computation for 2 satellites and 4 dates:
>>> s = '1 20580U 90037B 19342.88042116 .00000361 00000-0 11007-4 0 9996' >>> t = '2 20580 28.4682 146.6676 0002639 185.9222 322.7238 15.09309432427086' >>> satellite2 = Satrec.twoline2rv(s, t)
>>> from sgp4.api import SatrecArray >>> a = SatrecArray([satellite, satellite2]) >>> e, r, v = a.sgp4(jd, fr)
>>> np.set_printoptions(precision=2) >>> print(e) [[0 0 0 0] [0 0 0 0]] >>> print(r) [[[-3431.31 2620.15 -5252.97] [-3478.86 2575.14 -5243.87] [-3526.09 2529.89 -5234.28] [-3572.98 2484.41 -5224.19]] <BLANKLINE> [[ 5781.85 2564. -2798.22] [ 5749.36 2618.59 -2814.63] [ 5716.35 2672.94 -2830.78] [ 5682.83 2727.05 -2846.68]]] >>> print(v) [[[-5.52 -5.19 1.02] [-5.49 -5.22 1.08] [-5.45 -5.25 1.14] [-5.41 -5.28 1.2 ]] <BLANKLINE> [[-3.73 6.33 -1.91] [-3.79 6.3 -1.88] [-3.85 6.28 -1.85] [-3.91 6.25 -1.83]]]
Attributes
The attributes of a Satrec object carry the data loaded from the TLE entry. Most of this class’s hundred-plus attributes are intermediate values of interest only to the propagation algorithm itself. Here are the attributes set by sgp4.io.twoline2rv() in which users are likely to be interested:
- satnum
Unique satellite number given in the TLE file.
- epochyr
Full four-digit year of this element set’s epoch moment.
- epochdays
Fractional days into the year of the epoch moment.
- jdsatepoch
Julian date of the epoch (computed from epochyr and epochdays).
- ndot
First time derivative of the mean motion (ignored by SGP4).
- nddot
Second time derivative of the mean motion (ignored by SGP4).
- bstar
Ballistic drag coefficient B* in inverse earth radii.
- inclo
Inclination in radians.
- nodeo
Right ascension of ascending node in radians.
- ecco
Eccentricity.
- argpo
Argument of perigee in radians.
- mo
Mean anomaly in radians.
- no_kozai
Mean motion in radians per minute.
The parameters are also listed briefly, along with their units, in the “Providing your own elements” section below.
Export
If you have a Satrec you want to share with friends or persist to a file, there’s an export routine that will turn it back into a TLE:
>>> from sgp4 import exporter >>> line1, line2 = exporter.export_tle(satellite) >>> line1 '1 25544U 98067A 19343.69339541 .00001764 00000-0 38792-4 0 9991' >>> line2 '2 25544 51.6439 211.2001 0007417 17.6667 85.6398 15.50103472202482'
And another that produces the fields defined by the new OMM format (see the “OMM” section above):
>>> from pprint import pprint >>> fields = exporter.export_omm(satellite, 'ISS (ZARYA)') >>> pprint(fields) {'ARG_OF_PERICENTER': 17.6667, 'BSTAR': 3.8792e-05, 'CENTER_NAME': 'EARTH', 'CLASSIFICATION_TYPE': 'U', 'ECCENTRICITY': 0.0007417, 'ELEMENT_SET_NO': 999, 'EPHEMERIS_TYPE': 0, 'EPOCH': '2019-12-09T16:38:29.363423', 'INCLINATION': 51.6439, 'MEAN_ANOMALY': 85.6398, 'MEAN_ELEMENT_THEORY': 'SGP4', 'MEAN_MOTION': 15.501034720000002, 'MEAN_MOTION_DDOT': 0.0, 'MEAN_MOTION_DOT': 1.764e-05, 'NORAD_CAT_ID': 25544, 'OBJECT_ID': '1998-067A', 'OBJECT_NAME': 'ISS (ZARYA)', 'RA_OF_ASC_NODE': 211.2001, 'REF_FRAME': 'TEME', 'REV_AT_EPOCH': 20248, 'TIME_SYSTEM': 'UTC'}
Gravity
The SGP4 algorithm operates atop a set of constants specifying how strong the Earth’s gravity is. The most recent official paper on SGP4 (see below) specifies that “We use WGS-72 as the default value”, so this Python module uses the same default. But in case you want to use either the old legacy version of the WGS-72 constants, or else the non-standard but more modern WGS-84 constants, the twoline2rv() constructor takes an optional argument:
>>> from sgp4.api import WGS72OLD, WGS72, WGS84 >>> satellite3 = Satrec.twoline2rv(s, t, WGS84)
You will in general get less accurate results if you choose WGS-84. Even though it reflects more recent and accurate measures of the Earth, satellite TLEs across the industry are most likely generated with WGS-72 as their basis. The positions you generate will better agree with the real positions of each satellite if you use the same underlying gravity constants as were used to generate the TLE.
Providing your own elements
If instead of parsing a TLE you want to provide your own orbital elements, you can call the sgp4init() method of any existing satellite object to reset it to those new elements.
>>> sat = Satrec() >>> sat.sgp4init( ... WGS72, # gravity model ... 'i', # 'a' = old AFSPC mode, 'i' = improved mode ... 5, # satnum: Satellite number ... 18441.785, # epoch: days since 1949 December 31 00:00 UT ... 2.8098e-05, # bstar: drag coefficient (/earth radii) ... 6.969196665e-13, # ndot (NOT USED): ballistic coefficient (revs/day) ... 0.0, # nddot (NOT USED): mean motion 2nd derivative (revs/day^3) ... 0.1859667, # ecco: eccentricity ... 5.7904160274885, # argpo: argument of perigee (radians) ... 0.5980929187319, # inclo: inclination (radians) ... 0.3373093125574, # mo: mean anomaly (radians) ... 0.0472294454407, # no_kozai: mean motion (radians/minute) ... 6.0863854713832, # nodeo: right ascension of ascending node (radians) ... )
The two parameters marked “NOT USED” above, ndot and nddot, do get saved to the satellite object, and do get written out if you write the parameters to a TLE or OMM file. But they are ignored by SGP4 when doing propagation, so you can leave them 0.0 without any effect on the resulting satellite positions.
To compute the “epoch” value, you can simply take a normal Julian date and subtract 2433281.5 days.
In addition to setting the attributes natively set by the underlying sgp4init() routine, this library also goes ahead and sets the date fields epochyr, epochdays, jdsatepoch, and jdsatepochF.
The character provided as the second argument can be 'a' to run the computations so that they are compatible with the old Air Force Space Command edition of the library, or 'i' to run the new and improved version of the SGP4 algorithm.
You can also directly access a satellite’s orbital parameters by asking for the attributes sat.epoch, sat.bstar, and so forth, using the names given in the comments above.
Validation against the official algorithm
This implementation passes all of the automated tests in the August 2010 release of the reference implementation of SGP4 by Vallado et al., who originally published their revision of SGP4 in 2006:
Vallado, David A., Paul Crawford, Richard Hujsak, and T.S. Kelso, “Revisiting Spacetrack Report #3,” presented at the AIAA/AAS Astrodynamics Specialist Conference, Keystone, CO, 2006 August 21–24.
If you would like to review the paper, it is available online. You can always download the latest version of their code for comparison against this Python module (or other implementations) at AIAA-2006-6753.zip.
For developers
Developers can check out this full project from GitHub:
https://github.com/brandon-rhodes/python-sgp4
To run its unit tests, install Python 2, Python 3, and the tox testing tool. The tests runing in Python 2 will exercise the fallback pure-Python version of the routines, while Python 3 exercises the fast new C++ accelerated code:
cd python-sgp4 tox
Legacy API
Before this library pivoted to wrapping Vallado’s official C++ code and was operating in pure Python only, it had a slightly quirkier API, which is still supported for compatibility with older clients. You can learn about it by reading the documentation from version 1.4 or earlier:
Changelog
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for sgp4-2.16-cp39-cp39-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d2f7453e8b25f5f9607574dc65bcb2833e1df094a8ae6d67ed57f4dae4b0008f |
|
MD5 | 2307ba08c6d8bddd4d881b1cfb6bc4bb |
|
BLAKE2b-256 | 9cba3af10d4454da291c5fa5b4a1f53f03a507645d26bcd567a3e3154a73958a |
Hashes for sgp4-2.16-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d58e2b89fd1e0aa2fd9043380aa61a0849220668c69e3b62b59dd74175890611 |
|
MD5 | 2eb06d34b947ac9f66ca1427d669badc |
|
BLAKE2b-256 | 9d5817e06fe5739297c47a65507e83c1c2aefb99a5fd54c508192d13ba265669 |
Hashes for sgp4-2.16-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ea3e509692aa1fbbb0fd58e8b885ebfd773d71e2ae879f913410ea0efc2e0230 |
|
MD5 | 001159cf0e05f046750318b1a07295fe |
|
BLAKE2b-256 | a2afbb07ed3a8b2bd690351afc7f8201f20de779bda7118ac405919fb34db3e2 |
Hashes for sgp4-2.16-cp38-cp38-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25104dc69c99e5a435e61a62d1711ad0e3d6797a4837697b77281cba5a580dca |
|
MD5 | 1a45b1f16bc643bd810540031492deda |
|
BLAKE2b-256 | 02a73c9c865d6b60f71d11cee91d9b722124129756d7305b3ae4a8063c0a5a08 |
Hashes for sgp4-2.16-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 51ab3c9875c4fa762ff9b9dd2ba934a859f4985ba4ad63805d3760d9c0a19a65 |
|
MD5 | 9dc249a661b76958c6346141b6b7e29d |
|
BLAKE2b-256 | df52f57d7d3e3b3a2c64643d9e75d4a705694a1f038594905a0f77b118082866 |
Hashes for sgp4-2.16-cp38-cp38-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a54f907df0b3501383b10718c1892c06bd95e3797577e9d22928a8d94db1c884 |
|
MD5 | acf9fc8eb15a1efc67696715c59e748c |
|
BLAKE2b-256 | 04f2c232f77d485a45be1b525faea0b443904f35e5aaec6a4473ce97e3078440 |
Hashes for sgp4-2.16-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9581abeb775d0d03b552b0ca1474d8d18025c3f34992a6c8b2987b7fdab46e01 |
|
MD5 | bb101de8c4a5cc01b1ecb119dd0a1e03 |
|
BLAKE2b-256 | b2330c2b8ceddf8183830c4c658e046bef93a6b77ec8fde9e1dc1ed4fcc1b535 |
Hashes for sgp4-2.16-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | d068705a0abcbdf02b80204258fb3f57b4574deb164611b7d729fbae1a593162 |
|
MD5 | bb9e3550535296a0341aecc3b1b443a3 |
|
BLAKE2b-256 | 855b977befc261540e41e862437a3ef9942a90d5a716335c77ed554e09ab5c47 |
Hashes for sgp4-2.16-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4b60c98ec67638e9b67a4ce1ebaac0995cf39303e66bd7db29dde12f43b485d0 |
|
MD5 | 1c78824c82f62217d67c615bd6d95d3b |
|
BLAKE2b-256 | b5c0e3d342c548808f1b5ae64d2d2ea8d832298a0f7a2fadbb259b5b08f969be |
Hashes for sgp4-2.16-cp37-cp37m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0f71175d63bea8c7773a76d6a0d247f10d94328076b1e1d145f2ace56b7ad42c |
|
MD5 | 44e20526c3386a6c0afe3987647679a6 |
|
BLAKE2b-256 | 4fca2016fd3b29ae9d1b512e9265489ac9e9f46533f08a2e64b26861f294450e |
Hashes for sgp4-2.16-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97e385486a6e1b546a0386d727372cfad3b4f9680c059a93344e56b94bfa2a93 |
|
MD5 | f7f777a5c4a5e7f11c992997c35dc3be |
|
BLAKE2b-256 | df8fb70932cdc95012da2a15b475e4f3bf8a735398ae2949263d3222b88d50cf |
Hashes for sgp4-2.16-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dae907a6c8769eb6c85aea5ceff87e91d60ea4d603f3d844186b0de5c8306d26 |
|
MD5 | d70c53a4d0dfc5ce22f7ab297007781e |
|
BLAKE2b-256 | 765dc972f5388652676a69c17b439b88f063fdbf5c8495720d17b3a4a48d5f91 |
Hashes for sgp4-2.16-cp37-cp37m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cf1156b8531fb803ea13fae225e11c7282e4bf88674104d65cba8386e509c7c6 |
|
MD5 | 4941788f077d2358c1d82cd31097236a |
|
BLAKE2b-256 | b4a4b49e0c424079dcd0fcf29e7b115d8c64185791398a16f7312c918abe3460 |
Hashes for sgp4-2.16-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e841d9031a4fddb5867dc61428c37b081e1391de914f338a56df67f7e4837655 |
|
MD5 | c2d930bfdebb57e62c4494195981ab3f |
|
BLAKE2b-256 | 67895df26662a1e94c9dca1835b350ca688e6a5d45185a340e325015b5087ca2 |
Hashes for sgp4-2.16-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34977fadb0b1cf0409d67a85aa8051e71bc12e9b812cb946575413ae4962a1aa |
|
MD5 | 402e43bed9a8c5048bea919ab98e818d |
|
BLAKE2b-256 | 6448645d415f085d52e66813787931cb0539aa851f6816c6eaf157cbead9ecf2 |
Hashes for sgp4-2.16-cp36-cp36m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ef095517176bad1aa93fd748d60f6f3d80ad87c60ab2e745c0fd438be717c875 |
|
MD5 | a6515215211adf36d5e8acb5adcb656b |
|
BLAKE2b-256 | 9d7a3e1d9d3b108e43351a2b2e405666f4a45e5a84eb6a74bc1dfaa720389ffa |
Hashes for sgp4-2.16-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cc30f07f317960af4c2e355196d7d2d400c73c5fc8ad944650625117d3ddee03 |
|
MD5 | f8d515a36f4a674e61e93b26a2747cca |
|
BLAKE2b-256 | fe7fe725d37cdf2c9fea3dcc28cbce168ab87a0a94ee8b8fe73cb489bcef4705 |
Hashes for sgp4-2.16-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 851208ebc04caaef8db8767bc1a2164cb394f983023b269e22ab0ffcc6cb3e3e |
|
MD5 | 0b19f001b23b9929d62efdd3ef5a0ea1 |
|
BLAKE2b-256 | 85c3cb07ee2ce62dfaece35a5456e7606dbad03fc3bd0874916608464c839fa3 |
Hashes for sgp4-2.16-cp36-cp36m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b9178bed00a332d22bc6dcb8294031457bc06808aa41279f4660c4528e465a34 |
|
MD5 | 703fea7adf0186a532917673593792f9 |
|
BLAKE2b-256 | 3ca2be797b7f2ba0348c05ac3c79d9f8da74a5349d1c6cc200a130e0fffe8201 |