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 this package produces raw Earth-centered cartesian coordinates. It does not implement all the steps necessary to convert satellite positions into geographic coordinates. For that, 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
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 (kg/m2er) ... 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.
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.8-cp38-cp38-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | a547f937164820ccfa61a38b6de4d05c3ee793c78480a0a2bbaec46967d69285 |
|
MD5 | beb03e9c6f23c967aad56d4c084b0704 |
|
BLAKE2b-256 | f58c24c898a09ccdcbabdd50ce28331e11f98a268094a479c8c9911b20dc4456 |
Hashes for sgp4-2.8-cp38-cp38-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7ee649fdd16bdd6850d53afebfaa905aee2d103536ee27375c23053f32921994 |
|
MD5 | 67ea82f787ffe781a1b241f6da70ce14 |
|
BLAKE2b-256 | 8f628ac4956d700c240e2e7083b3d2feb3623f65ebe51b936a6f88009f2d9a8c |
Hashes for sgp4-2.8-cp38-cp38-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 673bd4267e3ff468cc608ab844493ec2488042a2190f768541734259f056f91a |
|
MD5 | b2fe6a8a0c2cfd412bba7024b1f971ba |
|
BLAKE2b-256 | fc7b40c6bb9a5c271d5ab0b6b1108516c2102147611303be0c323063a0fe8476 |
Hashes for sgp4-2.8-cp38-cp38-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f7c2c5b5740575a08f53d4ab9838a13d1dc21bcfa1c43eb6174d9a6bbdfe72b7 |
|
MD5 | 55e98012929b158e800fd32daf351cf2 |
|
BLAKE2b-256 | 82a94d26942c5d9aad263193e5b9c34c3c2fa3272cd02ff608aa931645e79f61 |
Hashes for sgp4-2.8-cp38-cp38-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 99e18e134cfa1f70edd4ac7672d19029c12a1625b89962d3499e78e1408f1715 |
|
MD5 | 7e31e796ec4e107611e699c26c11db5a |
|
BLAKE2b-256 | 9174c85e4a196296f9aa23a9947b681290a47b6f8964d3d99cf2b81e16a6ab3a |
Hashes for sgp4-2.8-cp38-cp38-macosx_10_9_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 212ae140aabd34a85a2aabf8b9eecf70fc288bb494325886f14094252a54f252 |
|
MD5 | 9c30770f28000869d3b76bba6cdd4310 |
|
BLAKE2b-256 | 519cc7697092895516637128d398ff4652feb3795de4781b02efad9ba19fe975 |
Hashes for sgp4-2.8-cp37-cp37m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | fc0dd1d85a4e096ebec7c999f93e5f8dc006c1169180c53db03070034adadbbb |
|
MD5 | 0b463aa3858c73ac70f01e590419b991 |
|
BLAKE2b-256 | 4dd02bde665b66aa08cc6caa59e8f24a14e4051b59ae392716a11de492d873b3 |
Hashes for sgp4-2.8-cp37-cp37m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | cafa9f1b20e4854db20e5a733b023c86177e07c86a0ccec4c3fc3319b080caa5 |
|
MD5 | 98cc0a6f138ba98e497d99c84c636729 |
|
BLAKE2b-256 | 54029b92d22a83114ba394c47c1212decd40c077fa24ab5cfe05f01a786aa273 |
Hashes for sgp4-2.8-cp37-cp37m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 247307b230fe80f1e9cd3cdaa8499929ab6eff448e3cf0b0275c99531838a999 |
|
MD5 | 58f79721e194f4168847e436d6d77135 |
|
BLAKE2b-256 | eb10c5525642148a41da77467aba94f650aed54b5cee87c92080d4c24f15ef0e |
Hashes for sgp4-2.8-cp37-cp37m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e6116acc4f01e7052c10254cdd343b237f395326e1d1e209af63e74c2f22ac75 |
|
MD5 | b1b23ac440e52058d5685bbea60dc7c1 |
|
BLAKE2b-256 | fd7e6c3b68fbbfc3c117c35be958ec2faab4eb5b8d9f4d8e801a8bf2daed0a17 |
Hashes for sgp4-2.8-cp37-cp37m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ad0996e65e5d3d3c34ab5007da0ce823bd9b0700cb4c7b2c1140e6a891f12f0 |
|
MD5 | cd52d3ead1de952e0c1ccea2891c33c1 |
|
BLAKE2b-256 | 14a63b4b4cda35a79bdfe981cb2ff244ab17ea23e9eff4d93ad3f3b3cd36c34a |
Hashes for sgp4-2.8-cp37-cp37m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 299b428ec72cc57b9efab2943020aec9b1264173554881b9a9bd2a37464fdc60 |
|
MD5 | 0a5f36ad3858742fb25992426d4763d0 |
|
BLAKE2b-256 | 5bd7a84034a0d84f851728edfe599bc91898b551957603c3d40f1186aa776a61 |
Hashes for sgp4-2.8-cp36-cp36m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 1fbba43950a430a09a555d026ed2d617deb220c489a0c44e2a7328f741a75d14 |
|
MD5 | 600577764bb7fb6926e9871956f50c0b |
|
BLAKE2b-256 | 7ba9e269a95f06e279707a70249564c0d9015bbd13716bb88384c222627d1ffa |
Hashes for sgp4-2.8-cp36-cp36m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9bb2f4684ecfeb7eca2469025527da8f56c39ef16928543ef35a22a688fab4a1 |
|
MD5 | a0f477bb4f9876dec13eea6d36cfdde3 |
|
BLAKE2b-256 | d36ed786a7fb48286a1550f1e9061a0c1382439819c63fc9b0922e1bc73839ec |
Hashes for sgp4-2.8-cp36-cp36m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2179f4d5dab1bbcaab6cb9babc29b56093941530fccb1fcd58f23169efbd42bf |
|
MD5 | 094414b2f637f74b23c087588691f86a |
|
BLAKE2b-256 | fa75c84b4fbd3315838357e4f357af396065b7ead8dbe8eb74be18f5fc7c3e9e |
Hashes for sgp4-2.8-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e2928a3861110b1aa44d587011ef3991d7c779e3ab3146d79d7f9bbb6bc93594 |
|
MD5 | 9e00d8d2e9e37843ce8513c416e3b2c7 |
|
BLAKE2b-256 | 63454446f48731a5d6de3a1ed6a82953f8d2b33736dde27fe0b5c65b285ebbbb |
Hashes for sgp4-2.8-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 04522e659d4ab75223fda5d400972895d88b67d09404d17f97c75d95af94e082 |
|
MD5 | f826bc29684ed09964fa86aa42d697a1 |
|
BLAKE2b-256 | 48f64d534c25b8b5cb909134935d51965c6f37021953c8f812c6f326866e111e |
Hashes for sgp4-2.8-cp36-cp36m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8c737fea659ce8e49c9684573bb7b9fb25d53fc096b636d7ec7af0de67984bae |
|
MD5 | 1174a50a65abd409b53c1053e3ebc2f5 |
|
BLAKE2b-256 | 96cc2b7a019ebee06d197611462393ab652d566094a2bfa60f6b6c359f3a6ca7 |
Hashes for sgp4-2.8-cp35-cp35m-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5d914e36f54f95710f4aea4a9450641921f25fa3734c46493e8253c6e09c3860 |
|
MD5 | 0e479217ff988f4a19a4f5eff873baad |
|
BLAKE2b-256 | 1637b3eeeadb4ae831bc12e8f59e72264422bada18501ca2ea6f1831f987b7f4 |
Hashes for sgp4-2.8-cp35-cp35m-manylinux2010_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 49ae8c34525de6fa6d9b7bb1d32447c5b861c4e3a3f890628caba2a3fafd5c75 |
|
MD5 | 4187d838826d0b13566177172b40a5e4 |
|
BLAKE2b-256 | e5407b583a82fd5684ba10a6ef632d524580cc4b01c0d9b417747e12bfcdc814 |
Hashes for sgp4-2.8-cp35-cp35m-manylinux2010_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9e52fa1bed747e3d3e84f68b016669abc8ea16045c9bcd4d588a2ebf7336daa0 |
|
MD5 | 6d6bdd9f361e5fdc3617dfa0f75271f4 |
|
BLAKE2b-256 | 76fb4bd50e891a60b0ee4f2217c5d033aaa04372d6e28547893f55cfdb79871a |
Hashes for sgp4-2.8-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 79bacea627701f7f2a3fc7b79456c65ebdb8796b8fabb7122d5c0732ad680c1f |
|
MD5 | 63b4b22c8907d5a059d727f26d3c6ae9 |
|
BLAKE2b-256 | 87f9370fe0c7aab2b297c24a17f1ba6c5bcee18449d9d684f35e21f23132c582 |
Hashes for sgp4-2.8-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce88980abf32031b878281864735b8b8624cfc738641d441683876e6f82be74e |
|
MD5 | ed3abeb48294610298fadb5ffbd886d2 |
|
BLAKE2b-256 | 9eabc3cb70ff4bcb9e8a94a0af5ad64af2633325eaaf6b542c5204f725be2598 |
Hashes for sgp4-2.8-cp35-cp35m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b11e80338bc896d1a3ccb11bdc294d4cc4f64e932e3787c485caac4ca97f60fb |
|
MD5 | a75d668cf1b75f201c02e7cc63c0714a |
|
BLAKE2b-256 | 5ca7bc75b531285724b7e7c7103227c5914f3922aa9f8dbf048d4a310d6fb9ad |