Skip to content

Instantly share code, notes, and snippets.

@greg-randall
Created February 25, 2024 01:00
Show Gist options
  • Save greg-randall/6c7aebe3e9ac25debee098e92bea324e to your computer and use it in GitHub Desktop.
Save greg-randall/6c7aebe3e9ac25debee098e92bea324e to your computer and use it in GitHub Desktop.
Human Readable Moon Phase from Latitude & Longitude
from datetime import datetime
from skyfield.api import load
from skyfield.framelib import ecliptic_frame
from tzfpy import get_tz
import pytz
def human_moon(lat, lon):
ts = load.timescale()
# Get the current time in the specified timezone
current_time = datetime.now(pytz.timezone(get_tz(lon,lat)))
# Convert the current time to UTC and get the corresponding Skyfield time
t = ts.utc(current_time.year, current_time.month, current_time.day, current_time.hour, current_time.minute, current_time.second)
eph = load('de421.bsp')
sun, moon, earth = eph['sun'], eph['moon'], eph['earth']
e = earth.at(t)
s = e.observe(sun).apparent()
m = e.observe(moon).apparent()
_, slon, _ = s.frame_latlon(ecliptic_frame)
_, mlon, _ = m.frame_latlon(ecliptic_frame)
phase = (mlon.degrees - slon.degrees) % 360.0
phase_name = ['New Moon','Waxing Crescent','First Quarter','Waxing Gibbous','Full Moon','Waning Gibbous','Last Quarter','Waning Crescent']
phase_index = int((phase + 22.5) / 45) & 7
return phase_name[phase_index]
print(human_moon(30,-84))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment