Skip to content

Instantly share code, notes, and snippets.

@laanwj
Last active September 15, 2022 14:46
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save laanwj/de2cae0a79537de3d7aa0e4a07c94647 to your computer and use it in GitHub Desktop.
Save laanwj/de2cae0a79537de3d7aa0e4a07c94647 to your computer and use it in GitHub Desktop.
#!/usr/bin/python3
import datetime
import bisect
import ephem
DAY = 1.0/29.33
MOONPHASE = [
(0.0/4.0 + DAY, 'πŸŒ‘', 'New moon'),
(1.0/4.0 - DAY, 'πŸŒ’', 'Waxing crescent moon'),
(1.0/4.0 + DAY, 'πŸŒ“', 'First quarter moon'),
(2.0/4.0 - DAY, 'πŸŒ”', 'Waxing gibbous moon'),
(2.0/4.0 + DAY, 'πŸŒ•', 'Full moon'),
(3.0/4.0 - DAY, 'πŸŒ–', 'Waning gibbous moon'),
(3.0/4.0 + DAY, 'πŸŒ—', 'Last quarter moon'),
(4.0/4.0 - DAY, '🌘', 'Waning crescent moon'),
(4.0/4.0, 'πŸŒ‘', 'New moon'),
]
RANGES = [x[0] for x in MOONPHASE]
def get_phase_on_day(ddata):
"""Returns a floating-point number from 0-1. where 0=new, 0.5=full, 1=new"""
date = ephem.Date(ddata)
# The following extract the percent time between one new moon and the next
# This corresponds (somewhat roughly) to the phase of the moon.
# Note that there is a ephem.Moon().phase(), but this returns the
# percentage of the moon which is illuminated. This is not really what we
# want.
nnm = ephem.next_new_moon (date)
pnm = ephem.previous_new_moon(date)
lunation = (date-pnm)/(nnm-pnm)
return lunation
lunation = get_phase_on_day(datetime.date.today())
phase = bisect.bisect_right(RANGES, lunation)
print('%.0f%% %s %s' % (lunation * 100.0, MOONPHASE[phase][1], MOONPHASE[phase][2]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment