## Using PyEphem to get the ground coordinates of a satellite

The PyEphem package is pretty amazing. The tutorial will show you many things.

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`

and then:

1. 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";```
2. 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.

3. 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? Mark Galassi is a research scientist in Los Alamos National Laboratory, working on astrophysics and nuclear non-proliferation. This is his "Programming for Research" blog.
This entry was posted in mapping, python, try this and tagged . Bookmark the permalink.

### 5 Responses to Using PyEphem to get the ground coordinates of a satellite

1. Mitch says:

Hi Mark,

I was wondering if you know if the lat long information is the intersection of the nadir vector from the sat. with the ellipsoid, geoid or is it terrain corrected?

2. Dan says:

great post – I get a value of 155:15:46:5 what is this? radians? or an error?
Thanks

3. pymat says:

This is a pretty helpful post, thank you for sharing. I’d like to obtain something similar but without the use of inputting “tle”, instead used a satellite’s cartesian coordinates, XYZ, and obtain the sat_lat, sat_long, and range. In other words, a satellite’s orbital information and then obtain then the ground longitude/latitude under that satellite for a sequence of epochs. How can I circumvent the above “sat = ephem.readtle(name,line1,line2)”? This is utlimately what I’d like to do, without the sv.tle as input (I have instead cartesian coords).

4. ig says:

Awesome article.

5. Harry says:

Hi there, do you know what the underlying mathematical/physical model that is being used to calculate the position in the compute() function in PyEphem, is it SGP4? Thanks