Created
July 1, 2015 07:12
-
-
Save miklb/ed145757971096565723 to your computer and use it in GitHub Desktop.
Calculate Lunar Phase
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/env python | |
""" | |
moonphase.py - Calculate Lunar Phase | |
Author: Sean B. Palmer, inamidst.com | |
Cf. http://en.wikipedia.org/wiki/Lunar_phase#Lunar_phase_calculation | |
""" | |
import math, decimal, datetime | |
dec = decimal.Decimal | |
def position(now=None): | |
if now is None: | |
now = datetime.datetime.now() | |
diff = now - datetime.datetime(2001, 1, 1) | |
days = dec(diff.days) + (dec(diff.seconds) / dec(86400)) | |
lunations = dec("0.20439731") + (days * dec("0.03386319269")) | |
return lunations % dec(1) | |
def phase(pos): | |
index = (pos * dec(8)) + dec("0.5") | |
index = math.floor(index) | |
return { | |
0: "New Moon", | |
1: "Waxing Crescent", | |
2: "First Quarter", | |
3: "Waxing Gibbous", | |
4: "Full Moon", | |
5: "Waning Gibbous", | |
6: "Last Quarter", | |
7: "Waning Crescent" | |
}[int(index) & 7] | |
def main(): | |
pos = position() | |
phasename = phase(pos) | |
roundedpos = round(float(pos), 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
@sbp howdy! I did leave credit in the script and I honestly don't remember where I came across it or why I originally shared it. I posted this gist 9 years ago! Thanks for pointing folks to the original source and offering support.