Track earth satellite TLE orbits using up-to-date 2010 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 that’s necessary to convert these positions into more official ECI frames like J2000 or the ICRF. Nor does it provide conversion into any Earth-centered Earth-fixed (ECEF) frames whose coordinates are fixed with respect to the Earth’s surface, like the ITRF that defines latitude and longitude.
For conversions into other coordinate frames, look for a comprehensive astronomy library that is built atop this one, like the Skyfield library:
http://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) (-6102.44..., -986.33..., -2820.31...) >>> print(v) (-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 that is within a few weeks or months of the satellite’s epoch, and 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
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]]]
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.
Look at the class’s documentation for details.
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.exporter import export_tle >>> line1, line2 = 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'
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: ballistic coefficient (revs/day) ... 0.0, # nddot: second derivative of mean motion (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) ... )
To compute the “epoch” value, simply take a normal Julian date and subtract 2433281.5 days.
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 backup 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.9-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bff5b972be3188ce2c314ef084f63e022867006d6a13c88b22ceddd6ee387a57 |
|
MD5 | 7fe6ecba0336f76a921e0be0d0f7a59e |
|
BLAKE2b-256 | 8e7a4ec63a98999bfb791024163e8674e312b7a28a1cb7919ffda7880b3bf711 |
Hashes for sgp4-2.9-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5bc452c7b72c951a18cf583c30d38e8102c07835c02f49b762ff0ab734b74377 |
|
MD5 | 4a0ca4770670f53582b11bab0f193720 |
|
BLAKE2b-256 | e3b685a6bbfe744443c087afe635693bf311b631774f9471ae7c257b98257e38 |
Hashes for sgp4-2.9-cp38-cp38-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fca9a239160cf976f5777fbc65de26caf3a00418a7ee9191b4bb74d58b209791 |
|
MD5 | 2903cb95c7944d2f06615c9482e804d1 |
|
BLAKE2b-256 | bf8adde1092ab2d147211d8c943401633ffe32d38af7dcbff8ce150640b19d4c |
Hashes for sgp4-2.9-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f01f4eca8332bb749c7da393dac53dbd58df22cf41a837f5bb4bd8fe16499a55 |
|
MD5 | 434424cef712bda2bc31de1a97f622f6 |
|
BLAKE2b-256 | 84c00246c57cf262308483bffe19a701f0ddb2011373af729fe413d86ce6ddff |
Hashes for sgp4-2.9-cp38-cp38-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 07ade1cfb2b9c547a86a024ad759cb59268d8c8ddc45d5b1ad5ed22ee9158dbb |
|
MD5 | 9ab6ad80bb35020b0dc6cfab11e64425 |
|
BLAKE2b-256 | ecd347e1b157affd20be4053a0d3efd204b241d86cc252060352bf520c1e4d84 |
Hashes for sgp4-2.9-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 90c8643fd5bb5d62d87ba1b0979337ffb6ead063d808a7bb04f66e409f5fb882 |
|
MD5 | 59bebe10bdfad6e0eeae2b079242ba71 |
|
BLAKE2b-256 | fb9ab74fb58c177c840008bf0a9313dc0cef7f4335c7484f72b83dffaef7dd63 |
Hashes for sgp4-2.9-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4abba644885d08da9e0317d1e846823372de96259eb63c3d0ad54cb5a4888596 |
|
MD5 | 5a53bfaf3951e6f3765e20002ba9a4b4 |
|
BLAKE2b-256 | 37f4688b5e815052c9f8d753fd83f78dbb3d7c446d025c2a8169705f3c7caa78 |
Hashes for sgp4-2.9-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 37e3ed6af47adb6488ed71ed40db244f9a48e9f82d2629b32a3d98b4f2ba5976 |
|
MD5 | 825ce7fef0b38a258b5249cccd194181 |
|
BLAKE2b-256 | c64d5dcb0219b43919226e421050a353eb1bbc14b8f503728b492c99185070e7 |
Hashes for sgp4-2.9-cp37-cp37m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a8bcdd6a8a96918713bcf0dd01e52b57b408264a47a90bba2f8672cd239f6f55 |
|
MD5 | 9e630143b2db50db55e6cf251be71a83 |
|
BLAKE2b-256 | 5595369fed8d6adfea49349521ab8984b5b3312dd0c45ad5eee4c19fe70ad72b |
Hashes for sgp4-2.9-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c7586aeb9f652401d7642ca64087cdcc791acf1bce8338998eb67bff322e8da5 |
|
MD5 | b5c80d0250b0241447840cde1ee241b1 |
|
BLAKE2b-256 | c3548e52ae11dec936c18b10c98910adab636c274abd145f121de07cfe0ff98a |
Hashes for sgp4-2.9-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ae57ee4bf53adc88552d7057c934bc334efd1854ed1a3027476fd4bc2d4223d1 |
|
MD5 | bd875ba1d6e84d89266457b4f484e415 |
|
BLAKE2b-256 | f5c2f5b93f1886a82ff6af016524a107b3ef986da7e2982d5d7442c59dab5777 |
Hashes for sgp4-2.9-cp37-cp37m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e9b830de8422db4cb8e48d852bbd881938afca2ca349d5e963d5a83b9915435a |
|
MD5 | e73fc8ad70e3fecd30021ebf804e40e6 |
|
BLAKE2b-256 | a6e6d2390592a9a6c11dae407000f65c135716f9caa571aab1c3ab12697a7660 |
Hashes for sgp4-2.9-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | df3c5c5fd5604c2dcc8c470765e7ddeb71e5d31733e91c952fe6491ef64d3348 |
|
MD5 | 1cbd0f6ce6a61c76ce7c2956c82ff41e |
|
BLAKE2b-256 | 8bdac7fe3d20b0886cfa35df783bfea17a7782c977f9fb24a74c16ee5c3ac5df |
Hashes for sgp4-2.9-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0309595a9f924183480f8a59b54bc43cbdaf8531f740fbc9186b840f78859cc0 |
|
MD5 | b99b4264fd6f888912920c3069ccd85b |
|
BLAKE2b-256 | 4ba2e0d3b1f695b77c4639ea7e7e7a6cfc1967da19c31ee2acd1f4679392a0a6 |
Hashes for sgp4-2.9-cp36-cp36m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a0dd92219e6c9cd36ee74ed80fcd4d2fe2487aacdca1f2a17a9411f95d353311 |
|
MD5 | 320286bb6e91bb170615183566398d45 |
|
BLAKE2b-256 | 71a8a2fd2f6a5b242c0a98e81793d06820c2ec4bb2323d465d009bf015386f14 |
Hashes for sgp4-2.9-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b578fb31c753095fb60741149fe42159d232be927331acbbe85704becee3474 |
|
MD5 | 8f89f007f03beaa362ed86b82c0efdc7 |
|
BLAKE2b-256 | 51531e1b91e1dff5531b9e41e5324454f6177a38651d23ba006109b03aad692a |
Hashes for sgp4-2.9-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1c1c4e15d6ee97315b82905fe0904faa962ceec6329caf95cd5044343d3565a3 |
|
MD5 | 93c3273b82fd104c6dcb684979fb036c |
|
BLAKE2b-256 | b31a190ee9372c2613019b21f67fc00c48069069920dd8a76fb3640f6ac73906 |
Hashes for sgp4-2.9-cp36-cp36m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | dba037f029cf184a2d2c275afc8ccb455a75a182cc6af89d4181917aabd77813 |
|
MD5 | 9881021ad6ec524cd9ecba3abe816124 |
|
BLAKE2b-256 | e25ae563b190d629d135c90417c40d4b7713c2cd96cee75cabba32f9e6f4e092 |
Hashes for sgp4-2.9-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25a5954c137f7a674ca19b4e94f93934c01198896af4554b0390f8a1c7cba49b |
|
MD5 | 81ceee06987eb9b9ed9ea35ddf95d144 |
|
BLAKE2b-256 | 0ce73ac68d9e63641c5e6af8379bde80d98b45d84c31082d37a9ec6cae1b0cfc |
Hashes for sgp4-2.9-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2aa678bba55e579f26228de23b70bb5749b933e5460a49113327c6ca8add1635 |
|
MD5 | a652dd80d44cbcf13782dc5d1281979e |
|
BLAKE2b-256 | 095cd23bd27845923c97fd32b7464b6afc60a65243487b267398dcbd865e733e |
Hashes for sgp4-2.9-cp35-cp35m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | add22e59a812699428157c542f5b3d9e368ecc6dfb871b81a1f7c5032e703f16 |
|
MD5 | aab039070d5e7de55bd63f6e18bab80e |
|
BLAKE2b-256 | 28e23af9e7e70e6042a14db3e39f394f0b93784c09f62c78733bf63bcd8ec8d5 |
Hashes for sgp4-2.9-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | c1b03c0cbd02820b232a0112767e0fc5f6889ae4c884d4a4f078e77afdfcb758 |
|
MD5 | 411cf70be3708fc82d47e575f3ada126 |
|
BLAKE2b-256 | 35dc34837efbda366d706055eeb8206e6a7785408ecc931f4641ecc8c9777257 |
Hashes for sgp4-2.9-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e15c545aa6b42dd4a7b6e7df8cd40833e7b58e010015f72f9a8119a6bea10541 |
|
MD5 | 47ec61664adbc3cf697cbf8583f414a3 |
|
BLAKE2b-256 | 561c230706bd1dec4e834356824ee52a9bf494a7fe8c3f1bbfc42d0c02c59758 |
Hashes for sgp4-2.9-cp35-cp35m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 966133ce7d2c477ad90b5813ec59ab65a8e64bc56a4c203d7a880eef22ee27d7 |
|
MD5 | 7eb9da88e90c17c8ee2e9521176b848e |
|
BLAKE2b-256 | d13d79916e2f2694ad8d27704785cd68f2864a9cf5c4fa53b60890ded3669c20 |