Python code to track the location of the Moon and Planets
Posted On March 1, 2023
PyEphem library can be used to track the location of the Moon and planets. Here’s an example of Python code that uses the PyEphem library:
import ephem
# Create instances of the Moon and some planets
moon = ephem.Moon()
mercury = ephem.Mercury()
venus = ephem.Venus()
mars = ephem.Mars()
jupiter = ephem.Jupiter()
saturn = ephem.Saturn()
uranus = ephem.Uranus()
neptune = ephem.Neptune()
# Define an observer
observer = ephem.Observer()
observer.lat = '37.7749'
observer.lon = '-122.4194'
observer.elevation = 0
# Get the current positions of the objects
moon.compute(observer)
mercury.compute(observer)
venus.compute(observer)
mars.compute(observer)
jupiter.compute(observer)
saturn.compute(observer)
uranus.compute(observer)
neptune.compute(observer)
# Print the positions
print("Moon:")
print("Azimuth: ", moon.az)
print("Altitude: ", moon.alt)
print("\nMercury:")
print("Azimuth: ", mercury.az)
print("Altitude: ", mercury.alt)
print("\nVenus:")
print("Azimuth: ", venus.az)
print("Altitude: ", venus.alt)
print("\nMars:")
print("Azimuth: ", mars.az)
print("Altitude: ", mars.alt)
print("\nJupiter:")
print("Azimuth: ", jupiter.az)
print("Altitude: ", jupiter.alt)
print("\nSaturn:")
print("Azimuth: ", saturn.az)
print("Altitude: ", saturn.alt)
print("\nUranus:")
print("Azimuth: ", uranus.az)
print("Altitude: ", uranus.alt)
print("\nNeptune:")
print("Azimuth: ", neptune.az)
print("Altitude: ", neptune.alt)
This code creates instances of the Moon and some planets, defines an observer, and calculates the current positions of the objects in terms of azimuth and altitude. Note that the observer’s location is set to the latitude and longitude of San Francisco, but can be changed to any desired location.
Here is an example of what the output of the code might look like:
Moon:
Azimuth: 174:36:07.7
Altitude: 43:24:02.6
Mercury:
Azimuth: 195:55:10.1
Altitude: -21:12:02.6
Venus:
Azimuth: 199:51:08.3
Altitude: -23:21:08.3
Mars:
Azimuth: 177:24:59.2
Altitude: 11:36:38.7
Jupiter:
Azimuth: 93:15:24.4
Altitude: 28:29:47.9
Saturn:
Azimuth: 50:34:47.0
Altitude: 20:14:33.3
Uranus:
Azimuth: 269:02:26.9
Altitude: 3:03:59.2
Neptune:
Azimuth: 273:51:06.6
Altitude: -1:45:31.7
Keep in mind that the output will vary depending on the observer’s location and the current date/time.