I had a simple application for it: I needed to take a satellite’s orbital information and get the ground longitude/latitude under that satellite at a given time.
A satellite’s orbital information is usually given by what are called “two line elements” (TLEs) (see the example below), but some calculation is necessary to go from a TLE to the position over earth at a given time. PyEphem does it with great simplicity.
You can install PyEphem with Python’s pip packaging system:
pip install pyephem
- start with the satellite TLE, which you can obtain in a variety of ways. In real life we might get this from a program which grabs it from the web (for example here), but to hard-code a simple case (international space station) we can simply paste this code into a Python interpreter:
import ephem import datetime ## [...] name = "ISS (ZARYA)"; line1 = "1 25544U 98067A 12304.22916904 .00016548 00000-0 28330-3 0 5509"; line2 = "2 25544 51.6482 170.5822 0016684 224.8813 236.0409 15.51231918798998";
- create a PyEphem Body object from it:
tle_rec = ephem.readtle(name, line1, line2) tle_rec.compute()
Note that the ephem.readtle() routine creates a PyEphem Body object from that TLE, and the compute() method recalculates all the parameters in the Ephem body for the current moment in time (datetime.datetime.now()).
You can use any moment in time and calculate the values at that time with something like tle_rec.compute(datetime.datetime(2012, 1, 8, 11, 23, 42)) for January 8, 2012, 11:23:42am.
- Obtain the longitude and latitude from the tle_rec object:
print tle_rec.sublong, tle_rec.sublat
These sublong and sublat values are expressed as PyEphem Angle objects; when printed they are human readable longitude/latitude strings: -44:41:57.2 47:52:58.9. When accessed as real numbers they are in radians, so keep that in mind when adapting them to use with something like Python’s excellent Basemap package, which uses degrees instead of radians.
So, two lines of code to go from TLE to ground longitude/latitude; pretty good, eh?