summaryrefslogtreecommitdiff
path: root/moonphase.py
diff options
context:
space:
mode:
authorluxagraf <sng@luxagraf.net>2023-06-15 15:58:59 -0500
committerluxagraf <sng@luxagraf.net>2023-06-15 15:58:59 -0500
commitab987e10f154f5536bb8fd936ae0966e909fa969 (patch)
tree9de5076f38b71ececb1bc94f8d9d19170898d603 /moonphase.py
added all my scriptssynced/master
Diffstat (limited to 'moonphase.py')
-rwxr-xr-xmoonphase.py50
1 files changed, 50 insertions, 0 deletions
diff --git a/moonphase.py b/moonphase.py
new file mode 100755
index 0000000..1bee795
--- /dev/null
+++ b/moonphase.py
@@ -0,0 +1,50 @@
+#!/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
+import decimal
+import 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)
+ num_days = float(roundedpos * 29.6)
+ print("%s (%s) %s days" % (phasename, roundedpos, num_days))
+
+if __name__=="__main__":
+ main()