kepler.py

View source on GitHub

Kepler is a program which simulates what an Earth-based telescope would see when observing the transit of an exoplanet in front of another solar system’s star.

It was designed to generate short, educational animations to help explain the concept to students. I wrote it as an optional, extra-credit assignment for my Introduction to Astronomy class - the assignment was to create an educational graphic for future classes.

Here’s a what Jupiter orbiting the Sun in Mercury’s orbit would look like:

The program actually generates four separate, synchronized animations:

  1. Upper-left: The (exaggerated) color of what each pixel would see
  2. Upper-right: The orbit, sun, and planet (to scale)
  3. Center-right: The non-pixelated image of the planet crossing the star
  4. Bottom: The lightcurve of the observation, with simulated noise.

The program is able to handle all the normal parameters for keplerian orbits. Here’s Jupiter on a completely unrealistic, inclined, comet-like path in front of the Sun:

You can also use Kepler to simulate lunar eclipses since it is essentially the same phenomenon (a big dark thing going in front of the big circular light thing):

I wrote the program in python and used basically only numpy and matplotlib; no libraries were used for orbital or geometry calculations. There’s a ton of 2-d and 3-d trig involved, and it got pretty nasty pretty quickly. It also includes a hand-built Newton’s Method minimum finder since I got sick of trying to analytically solve for the transit point.

I’m not going to write up the mess that is the geometry involved here, but if you want a taste the code is at https://github.com/TimEwing/kepler. There is only one commit, no documentation, and only a few comments but other than the math its mostly just configuring matplotlib. The whole project was less than 48 hours start-to-finish. The highlight is probably the solution for the circle-circle-square intersection; that chunk of code took the majority of the project’s time.

Updated: