Last active
September 15, 2022 14:46
-
-
Save laanwj/de2cae0a79537de3d7aa0e4a07c94647 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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