Skip navigation.
Home
Write anything I want to write...

Build Your Own Ephemerides

1. Introductions

This is a rough guide on how to build or implement your own ephemeris program or library routines. There are some existing programs (and some of them are free) to do such a job. However, there are a few advantages if you build your own one.

  • It's your thing. You have complete control of the licensing and the copyright of the program. You can also improve the programs anyhow you like.
  • Good way to learn Spherical Astronomy. Of course, you need to like mathematics and astronomy. If you have no interest in these subjects at all, it's better to use an existing program or libraries routines for your purpose.
  • Extend it for many other purposes. Since you are familiar with your program, you can easily extend it for many purposes such as (a) calculating the phase of the Moon; (b) predicting eclipses; (c) building your birth charts for many different traditions of astrology; (d) creating calendars (such as the Chinese calendar) that are based on astronomical events; (e) many other things.

While these sound interesting, you have to note that building your very own ephemeris is not for the faint of heart! If you are not familiar with spherical astronomy, be ready to learn a lot of concepts and technical terms related to the subject. As for implementation, expect to write

  • a few hundred lines just for very basic set of features and low-precision calculations;
  • a few thousand lines for more features and satisfactory accuracy;
  • a few ten thousands lines for more complete features and high-precision calculations.

There are some easier ways to tackle this problem. For example, you can interpolate the positions of the planets from some existing ephemerides (such as DE405, DE406). Such an approach is definitely feasible and realistic. But here we focus more on performing the calculations "from scratch" which is useful in many cases. E.g., to calculate just the rough rising and setting time of the sun for a few centuries, it's better to stick to a simpler and less space-consuming low-precision calculation rather than to make use of the gigantic ephemerides.

Let's start.

2. Knowledge Required

Before starting your implementation, there are many concepts you need to understand and study. Here's a list of some important ones.

Mathematical Software Library Routines. In particular, you need to dial with a lot of trigonometric and logarithm calculations. Be very familiar with these to avoid making stupid mistakes. For example, a trigonometric function generally expect an angle expressed in radians, so don't use an angle expressed in degrees directly.

Concepts in Spherical Astronomy. You need to know a lot of thing specific to this subject. Knowing some popular concepts in astronomy like the big bang or the dark energy won't help you here, as we are dealing with locations here! Below is a list of some common concepts just to give you an idea what to expect. Note that the list is NOT exhaustive! The higher precision you want to calculate and implement, the more and the deeper of these concepts you need to understand.

  • Time Tracking. TT/TDT (Terrestrial Time); ET (Ephemeris Time); ST (Sidereal Time); UTC (Coordinated Universal Time); UT (Universal Time); Delta-T; Julian day; Julian ephemeris day; Gregorian calendar; etc.
  • Coordinate Systems. Horizontal coordinates (altitude, azimuth); Equatorial coordinates (right ascension, declination, equinox, solstice, hour-angle, transit, meridian); Ecliptic coordinates (longitude, latitude, obliquity); Galactic coordinates; Heliocentric; Geocentric; Topocentric; etc.
  • Reference Frames. ICRF (International Celestial Reference Frame); FK5 (Fundamental Katalog 5); epoch; mean equator and equinox, etc.
  • Orbital Elements. Perihelion; Aphelion; Perigee; Apogee; Eccentricity; Kepler's equation; Mean longitude; Mean anomaly; True anomaly; Equation of the center; etc.
  • Miscellaneous. Astronomical Unit; Radius Vector; Aberration; Precession; Nutation; Parallax; Refraction; Twilight; Conjunction; Opposition; True/Geometric position; Mean position; Apparent position; etc.

Mathematics. To understand some basic calculations, you need only high-school mathematics. Be ready to deal with a lot of trigonometric functions and analytical geometry. But to deal with very high precision calculations, you may need to know more on calculus and astrophysics.

Where can you learn these concepts? Look for some good books or Internet resources related to this subject. I myself have learned a lot from [PDS] and various Internet resources. To learn spherical astronomy seriously, you can start with [WMS].

3. Guides for the Algorithms

3.1. Notes on Accuracy

Decide carefully the level of precision you need based on the purpose of your implementation. If you just want to use it to draw a simple sundial, then low-precision calculations are sufficient. But if you want to launch a rocket to the Moon or to visit your ET friends many light years away, then stick to the best algorithms known to human beings.

You may wonder, why don't we just go straight to the 'exact'/'perfect'-precision implementation? With such an implementation, we can then extend it anyhow we like. It's true, but there are some pragmatic issues. First, the state-of-the-art theories generally involve a lot of tedious calculations. E.g., ELP2000-82B is one of the best theories for calculating the position of the Moon. To calculate the position of the Moon in any given instant, you need to perform over 30 thousand trigonometric calculations. So... Second, storing such information takes space. You may not want to include such data especially for a portable device. Third, implementing the best solutions involves much more efforts in implementation and testing.

We will briefly mention the kind of applications you can do for calculations with different levels of precision. An interesting trade-off on the precision is to implement some calculations with different levels of precisions. Use only the one you need for your applications. E.g., to predict solar eclipse, you can first estimate the date and time a solar eclipse may occur by using low-precision (but faster) calculations. Then, use high-precision (but slower) algorithms to calculate the exact times related to the eclipse.

Last but not least, you need to know the maximum error introduced by each algorithm. With this, you can tell when the algorithm fails. For example, if an algorithm tells you that a new Moon starts from 11.59pm tonight, but its precision is low and may incur up to 10 minutes of maximum error in time, then you can't tell whether a new Moon starts today or tomorrow. Such a case may not affect you if you want to observe other stars on a new Moon night, but it can be extremely important if you want to calculate an astronomical lunar calendar. To resolve such a case, you need to resort to high-precision calculations.

3.2. Low-Precision Algorithms

To find out the rough locations of the planets at night, to design a normal sundial, to include a cute moon phase indicator in your PDA, to design a compass based on the sun and moon positions, or to know the rough rising and setting time of the sun and the moon, low-precision calculations are sufficient.

There are a few good places to learn such calculations. The book [PDS] covers a lot of useful concepts and low-precision algorithms. The following websites contain good resources too: Approximate astronomical positions, How to compute planetary positions.

With this level of precession, you normally don't need to distinguish between the TT (Terrestrial Time) and UTC (Coordinated Universal Time), among the various reference frames, and you need only simple linear interpolation.

3.3. High-Precision Algorithms

To calculate an accurate birth chart for the modern western astrology or chinese astrology, to calculate lunar and solar eclipses with satisfactory results, or to calculate a calendar that is based on astronomical events, you may need high-precision algorithms. In particular, knowing the maximum possible error that can incur could be critical for some applications.

A good book that includes such algorithms is [JM]. The book introduces various important concepts. The calculations of the planet positions are based on an bridged version of the VSOP87 theory, which is one of the best planetary theories we have. While the calculations of the moon position is based on an abridged version of the famous ELP 2000/82 lunar solution.

You need to distinguish among the time systems here. Don't get confused between TT and UTC. You need to implement the routines to convert between them. A proper interpolation may be necessary to obtain good conversion results. You also need to distinguish among the true, mean, and apparent positions. Be careful to deal with nutation, precession, aberration, parallax, refraction, etc.; and convert among the different reference frames properly.

3.4. "Platinum" Algorithms

If the calculations above don't suit your purpose because you want to design a bullet-proof astrological birth chart, to calculate astronomical calendars, to predict eclipses to an accuracy in the order of milliseconds, or to navigate your spacecraft to another galaxy, then you need to study more and implement very high-precision algorithms.

You can find a lot of great resources from the wonderful Internet. Some useful web sites are listed here.

There are more things you need to take care of. A cubic spline interpolation for Delta-T (= TT - UT1) would be great. You may also want to learn more about astrodynamics and numerical integration.

4. References

[JM] Jean Meeus. Astronomical Algorithms. 2nd Edition. December 1998. ISBN 0-943-39661-1.
[PDS] Peter Duffett-Smith. Practical Astronomy with Your Calculator. Third Edition. March 1989. ISBN 0-521-33699-7.
[WMS] W. M. Smart. Textbook on Spherical Astronomy. 6th Edition (Revised by G.M. Green). 1977. ISBN 0-521-21516-1/0-521-29180-1.
Also take a look at the links I've bookmarked under the Astronomy category on this site.