-
Notifications
You must be signed in to change notification settings - Fork 73
/
Copy pathmoonphase.py
executable file
·61 lines (56 loc) · 1.95 KB
/
moonphase.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/usr/bin/env python3
#
# moonphase.py - by Don Cross - 2019-07-26
#
# Example Python program for Astronomy Engine:
# https://github.com/cosinekitty/astronomy
#
# This program calculates the Moon's ecliptic phase and illumination percentage
# for a given date and time, or for the computer's current date and time if none is given.
# It also finds the dates and times of the subsequent 10 quarter phase changes.
#
# To execute, run the command:
# python3 moonphase.py [date]
#
import sys
import astronomy
from typing import List
def QuarterName(quarter: int) -> str:
return [
'New Moon',
'First Quarter',
'Full Moon',
'Third Quarter'
][quarter]
def main(args: List[str]) -> int:
if len(args) == 1:
time = astronomy.Time.Now()
elif len(args) == 2:
time = astronomy.Time.Parse(args[1])
else:
print('USAGE: {} [yyyy-mm-ddThh:mm:ssZ]'.format(args[0]))
return 1
# Calculate the Moon's ecliptic phase angle,
# which ranges from 0 to 360 degrees.
# 0 degrees = new moon,
# 90 degrees = first quarter,
# 180 degrees = full moon,
# 270 degrees = third quarter.
phase = astronomy.MoonPhase(time)
print("{} : Moon's ecliptic phase angle = {:0.3f} degrees.".format(time, phase))
# Calculate the fraction of the Moon's disc
# that appears illuminated, as seen from the Earth.
illum = astronomy.Illumination(astronomy.Body.Moon, time)
print("{} : Moon's illuminated fraction = {:0.2f}%.".format(time, 100.0 * illum.phase_fraction))
print()
# Predict when the next 10 lunar quarter phases will happen.
print('The next 10 lunar quarters are:')
for i in range(10):
if i == 0:
mq = astronomy.SearchMoonQuarter(time)
else:
mq = astronomy.NextMoonQuarter(mq)
print('{} : {}'.format(mq.time, QuarterName(mq.quarter)))
return 0
if __name__ == '__main__':
sys.exit(main(sys.argv))