Guest User

Untitled

a guest
May 26th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #!/usr/bin/python3
  2. import datetime
  3. import bisect
  4. import ephem
  5.  
  6. DAY = 1.0/29.33
  7. MOONPHASE = [
  8. (0.0/4.0 + DAY, '🌑', 'New moon'),
  9. (1.0/4.0 - DAY, '🌒', 'Waxing crescent moon'),
  10. (1.0/4.0 + DAY, '🌓', 'First quarter moon'),
  11. (2.0/4.0 - DAY, '🌔', 'Waxing gibbous moon'),
  12. (2.0/4.0 + DAY, '🌕', 'Full moon'),
  13. (3.0/4.0 - DAY, '🌖', 'Waning gibbous moon'),
  14. (3.0/4.0 + DAY, '🌗', 'Last quarter moon'),
  15. (4.0/4.0 - DAY, '🌘', 'Waning crescent moon'),
  16. (4.0/4.0, '🌑', 'New moon'),
  17. ]
  18. RANGES = [x[0] for x in MOONPHASE]
  19.  
  20. def get_phase_on_day(ddata):
  21. """Returns a floating-point number from 0-1. where 0=new, 0.5=full, 1=new"""
  22. date = ephem.Date(ddata)
  23.  
  24. # The following extract the percent time between one new moon and the next
  25. # This corresponds (somewhat roughly) to the phase of the moon.
  26. # Note that there is a ephem.Moon().phase(), but this returns the
  27. # percentage of the moon which is illuminated. This is not really what we
  28. # want.
  29. nnm = ephem.next_new_moon (date)
  30. pnm = ephem.previous_new_moon(date)
  31. lunation = (date-pnm)/(nnm-pnm)
  32. return lunation
  33.  
  34. lunation = get_phase_on_day(datetime.date.today())
  35. phase = bisect.bisect_right(RANGES, lunation)
  36. print('%.0f%% %s %s' % (lunation * 100.0, MOONPHASE[phase][1], MOONPHASE[phase][2]))
Add Comment
Please, Sign In to add comment