public
anonymous / pyephem_example.py
Created

  • Download Gist
pyephem_example.py
Python
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 62 63 64
from datetime import datetime
 
import ephem
from pytz import timezone, utc
 
def is_visible(body):
alt = body.alt
if (0 <= alt <= 180):
return alt
return None
# to see a list of cities, use ephem.cities._city_data.keys()
try:
here = ephem.city('Boulder')
except:
here = ephem.Observer()
here.name = 'Boulder'
#here.lat, here.lon, here.elevation = 40, -105.17, 1700
here.lat, here.lon, here.elevation = '40:1.28.30', '-105:15.35.09', 1700
here.pressure = 0
tz = timezone('US/Mountain')
 
utcer = lambda d: utc.localize(d.datetime())
localtimer = lambda d: tz.normalize(d.astimezone(tz))
 
now = utc.localize(datetime.utcnow())
here.date = now
 
sun = ephem.Sun(here)
moon = ephem.Moon(here)
venus = ephem.Venus(here)
mars = ephem.Mars(here)
jupiter = ephem.Jupiter(here)
saturn = ephem.Saturn(here)
 
###
events = []
def append(*args):
events.append(args)
 
# rising examples
append("Sunrise", here.next_rising(sun))
append("Moonrise", here.next_rising(moon))
append("Venus rise", here.next_rising(venus))
append("Mars rise", here.next_rising(mars))
append("Jupiter rise", here.next_rising(jupiter))
append("Saturn rise", here.next_rising(saturn))
 
# setting examples
here.horizon='4' # add a custom value for Boulder
append("Sunset", here.next_setting(sun))
#append("Moonset", here.next_setting(moon))
 
# moon examples
append("Full moon", ephem.next_full_moon(here.date))
 
# solstice examples
append("Solstice", ephem.next_solstice(here.date))
append("Equinox", ephem.next_equinox(here.date))
 
#
events.sort(key = lambda et: et[-1])
for e, nt in events[:4]:
t = utcer(nt)
print e, localtimer(t), t-now

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.