Skip to content

Instantly share code, notes, and snippets.

@marklit
Last active February 25, 2025 10:54
Show Gist options
  • Save marklit/8932457ba8f0df075e0129003c1fb939 to your computer and use it in GitHub Desktop.
Save marklit/8932457ba8f0df075e0129003c1fb939 to your computer and use it in GitHub Desktop.
Planet Labs TLE Feed
$ python3 -m venv ~/.sats
$ source ~/.sats/bin/activate
$ python3 -m pip install astropy

$ wget https://ephemerides.planet-labs.com/planet_mc.tle
from   datetime import datetime

from   astropy import units as u
from   astropy.time import Time
from   astropy.coordinates import ITRS, \
                                  TEME, \
                                  CartesianDifferential, \
                                  CartesianRepresentation
from   sgp4.api import Satrec
from   sgp4.api import SGP4_ERRORS


lines = open('planet_mc.tle').read().strip().splitlines()

with open('planet_labs.csv', 'w') as f:
    while lines:
        name = lines.pop(0)
        line1 = lines.pop(0)
        line2 = lines.pop(0)

        satellite = Satrec.twoline2rv(line1, line2)

        t = Time(datetime.utcnow().isoformat(), format='isot', scale='utc')
        
        error_code, teme_p, teme_v = satellite.sgp4(t.jd1, t.jd2) # in km and km/s
        
        if error_code != 0:
            raise RuntimeError(SGP4_ERRORS[error_code])

        teme_p = CartesianRepresentation(teme_p * u.km)
        teme_v = CartesianDifferential(teme_v * u.km / u.s)
        teme = TEME(teme_p.with_differentials(teme_v), obstime=t)

        itrs_geo = teme.transform_to(ITRS(obstime=t))
        location = itrs_geo.earth_location
        loc = location.geodetic

        f.write('"%s", "POINT (%f %f)"\n' % (name.lstrip('0').lstrip(), 
                                             loc.lon.deg, 
                                             loc.lat.deg))
@marklit
Copy link
Author

marklit commented Feb 20, 2025

qgis-bin_zMSdeKaNBV

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment