Lunar Phase Calculator

Lunar Phase Calculator

2018, Mar 02    

Calculating Lunar phase

based on your exact spot on Earth was something that sounded interesting to me.

This solution had multiple problems to solve.

  • Know where 'you', the Observer() are at on the Earth.
  • Find out current date and time.
  • Find out how many days are in the current month.
  • Determine what percent of illumination for each date.

Check out a demo LunarPhase.Info

Tech

There are the libraries to reference:

  1. calendar
  2. datetime
  3. math
  4. PyEphem

Here is the link to the Jupyter Notebook Lunar Phase Calculator

In [1]:

        import ephem
        import calendar
        from datetime import datetime

        from math import radians as rad, degrees as deg
        
In [2]:
# set month key variables
        months = ('January','February','March','April','May','June', 'July','August','September','October','November','December')
        jan=0
        feb=1
        mar=2
        apr=3
        may=4
        jun=5
        jul=6
        aug=7
        sep=8
        oct=9
        nov=10
        dec=11
        
In [3]:
# set year and month
now = datetime.today()
#year = now.year # for current year
#month = now.month - 1 # for currnet month

year=2018
month=mar

#print(month)
In [4]:
# Find number of days per month of year
days_per_month = {}
dpm = days_per_month
for i in range(0,12):
    k = months[i]
    v = monthrange(year, i+1)
    days_per_month[i] = (k, v[1])
# print(months[month], year)
In [5]:
g = ephem.Observer()
g.name = 'Los Angeles'
g.lat=rad(34.0211)
g.long=rad(-118.3965)

m = ephem.Moon()
i = 0
days=1
try:
    for i in range(0, dpm[month][days]):
            g.date = date(year,month+1, i+1)
            g.date -= ephem.hour
            m.compute(g)
            print(dpm[month][0][:3],i+1, year,"- " "%.1f" % m.phase)
except ValueError:
    if i > dpm[month][days]:
        i - 1
        print('error')
Mar 1 2018 - 98.4
Mar 2 2018 - 100.0
Mar 3 2018 - 98.8
Mar 4 2018 - 95.2
Mar 5 2018 - 89.4
Mar 6 2018 - 82.1
Mar 7 2018 - 73.7
Mar 8 2018 - 64.5
Mar 9 2018 - 55.0
Mar 10 2018 - 45.5
Mar 11 2018 - 36.2
Mar 12 2018 - 27.5
Mar 13 2018 - 19.4
Mar 14 2018 - 12.4
Mar 15 2018 - 6.8
Mar 16 2018 - 2.7
Mar 17 2018 - 0.4
Mar 18 2018 - 0.3
Mar 19 2018 - 2.4
Mar 20 2018 - 6.7
Mar 21 2018 - 13.1
Mar 22 2018 - 21.5
Mar 23 2018 - 31.4
Mar 24 2018 - 42.3
Mar 25 2018 - 53.7
Mar 26 2018 - 64.9
Mar 27 2018 - 75.4
Mar 28 2018 - 84.6
Mar 29 2018 - 91.8
Mar 30 2018 - 96.9
Mar 31 2018 - 99.5