Skip to content

Instantly share code, notes, and snippets.

@laanwj laanwj/moonphase.py
Last active May 21, 2018

Embed
What would you like to do?
#!/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
You canโ€™t perform that action at this time.