Files
astronomy/demo/python/moonphase.py
Don Cross 5744c9ebe9 Moon phase demos also calculate illuminated fraction.
The phrase "Moon phase" is ambiguous, because sometimes
it means relative ecliptic longitude, other times it means
illuminated fraction. The "moonphase" demos were only
calculating the relative ecliptic longitude, which was
confusing. Now they calculate both.
2022-02-20 15:27:46 -05:00

61 lines
1.9 KiB
Python
Executable File

#!/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
def QuarterName(quarter):
return [
'New Moon',
'First Quarter',
'Full Moon',
'Third Quarter'
][quarter]
def main(args):
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))