Python PyEphem – Get right ascension and declination of the moon

In this post we will be creating a Python script that will calculate the right ascension and declination of the moon, to achieve this we will be using the PyEphem library which is able to perform high-precision astronomy computations.

The right ascension and declination of an object in the sky is the same as the longitude and latitude of a location on earth.

To install the PyEphem library run the following command.

pip install ephem

Our example will take the latitude and longitude coordinates of a given observer location. In this case our location will be the Big Ben tower in London, the coordinates for which are.

'51.5007'  #latitude of Big Ben (degrees)
'-0.1246'  #longitude of Big Ben (degrees)

See the snippet of code below where we utilize the PyEphem library to get the right ascension and declination of the moon.

We begin by setting up an observer object for the Big Ben tower, defining its latitude and longitude coordinates. We then create an object for the moon and calculate its right ascension and declination in radians.

The right ascension is then converted in to hours and the declination is converted to degrees.

import ephem

observer = ephem.Observer() = '51.5007'  
observer.lon = '-0.1246'  

moon = ephem.Moon()

ra = moon.ra  
dec = moon.dec 

ra_hours = ra * 12 / ephem.pi
dec_degrees = dec * 180 / ephem.pi

print(f'The Moon - Right ascension {ra_hours:.2f} hours - Declination {dec_degrees:.1f} degrees')

At the time of writing this the following is returned to output.

The Moon - Right ascension 14.23 hours - Declination -13.5 degrees

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply