Created
December 9, 2011 22:15
-
-
Save sbp/1453553 to your computer and use it in GitHub Desktop.
Phase of the moon
This file contains hidden or 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/env python | |
# http://en.wikipedia.org/wiki/Lunar_phase#Calculating_phase | |
import math, decimal, datetime | |
dec = decimal.Decimal | |
def position(now=None): | |
if now is None: | |
now = datetime.datetime.now() | |
diff = now - datetime.datetime(2011, 11, 25, 6, 9, 35) | |
days = dec(diff.days) + (dec(diff.seconds) / dec(86400)) | |
lunations = days % dec("29.530588853") | |
return lunations | |
def phase(pos): | |
sixteenth = dec("29.530588853") / dec(16) | |
if pos < sixteenth: | |
return "New Moon" | |
elif pos < sixteenth * 3: | |
return "Waxing Crescent" | |
elif pos < sixteenth * 5: | |
return "First Quarter" | |
elif pos < sixteenth * 7: | |
return "Waxing Gibbous" | |
elif pos < sixteenth * 9: | |
return "Full Moon" | |
elif pos < sixteenth * 11: | |
return "Waning Gibbous" | |
elif pos < sixteenth * 13: | |
return "Last Quarter" | |
elif pos < sixteenth * 15: | |
return "Waning Crescent" | |
else: return "New Moon" | |
def main(): | |
pos = position() | |
phasename = phase(pos) | |
roundedpos = round(float(pos / dec("29.530588853")), 3) | |
print "%s (%s)" % (phasename, roundedpos) | |
if __name__=="__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment