$ 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))