Created
April 30, 2017 07:57
-
-
Save chew-z/78104c8d14f4be11182abd5803c5c4c4 to your computer and use it in GitHub Desktop.
get todays lunation and moon dates for the year, any year
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 python3 | |
''' | |
Using PyEphem for some scientific fun http://rhodesmill.org/pyephem/ | |
This script shows lunation and moon dates for the year. Maybe more | |
in a future. | |
make alias for your shell | |
alias moon="python3 /path/to/moon.py" | |
''' | |
import datetime | |
import ephem | |
from tabulate import tabulate | |
import argparse | |
def get_phase_on_day(year, month, day): | |
''' | |
Returns a floating-point number from 0-1. where 0=new, 0.5=full, 1=new""" | |
Ephem stores its date numbers as floating points, which the following uses | |
to conveniently extract the percent time between one new moon and the next | |
This corresponds (somewhat roughly) to the phase of the moon. | |
Use Year, Month, Day as arguments | |
''' | |
date = ephem.Date(datetime.date(year, month, day)) | |
nnm = ephem.next_new_moon(date) | |
pnm = ephem.previous_new_moon(date) | |
lunation = (date - pnm) / (nnm - pnm) | |
''' | |
Note that there is a ephem.Moon().phase() command, but this returns the | |
percentage of the moon which is illuminated. This is not really what we want. | |
''' | |
return 29.530588853 * lunation | |
def get_moons_in_year(year): | |
''' | |
Returns a list of the full and new moons in a year. The list contains tuples | |
of either the form (DATE,'full') or the form (DATE,'new') | |
''' | |
moons = [] | |
date = ephem.Date(datetime.date(year, 1, 1)) | |
while date.datetime().year == year: | |
date = ephem.next_full_moon(date) | |
date1 = ephem.localtime(date) | |
date = ephem.next_new_moon(date) | |
date2 = ephem.localtime(date) | |
moons.append(['{0:%a, %d %b %H:%M}'.format(date1), | |
'{0:%a, %d %b %H:%M}'.format(date2)]) | |
''' | |
Note that previous_first_quarter_moon() and previous_last_quarter_moon() | |
are also methods | |
''' | |
# moons.sort(key=lambda x: x[0]) | |
return moons | |
def getArgs(argv=None): | |
''' | |
Command line arguments... | |
''' | |
parser = argparse.ArgumentParser(description='Play with astral bodies.', | |
formatter_class=argparse.ArgumentDefaultsHelpFormatter) | |
parser.add_argument('-y', '--year', | |
default='2017', | |
help='Prints moon phase dates in a given year') | |
return parser.parse_args(argv) | |
if __name__ == '__main__': | |
args = getArgs() | |
year = int(args.year) | |
today = datetime.datetime.now() | |
year, month, day = today.year, today.month, today.day | |
date = ephem.Date(datetime.date(year, month, day)) | |
print("Today's lunation: ", '{:02.1f}'.format(get_phase_on_day(year, month, day))) | |
# print(get_phase_on_day(2017, 5, 11)) | |
mp = get_moons_in_year(year) | |
headers = ['Full Moon', 'New Moon'] | |
print(tabulate(mp, headers, tablefmt="simple")) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment