Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/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]))
Add Comment
Please, Sign In to add comment