-
Notifications
You must be signed in to change notification settings - Fork 10
/
sun.py
58 lines (41 loc) · 1.43 KB
/
sun.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
"""
A script which provides functions to timelapse.py for determining if it is
currently light out (and if it is worth taking photos right now)
"""
from datetime import datetime, timedelta
import ephem
btown = ephem.Observer()
btown.pressure = 0
# standard horizon found in documentation
btown.horizon = '-0:34'
# bloomington lat and lon
# you will need to change this to match your location
btown.lat, btown.lon = '39.170637', '-86.556237'
# NOTE: all datetimes are UTC
def previous_setting(dt) :
btown.date = dt.strftime("%Y/%m/%d %H:%M")
setting = btown.previous_setting(ephem.Sun())
return datetime.strptime(str(setting), '%Y/%m/%d %H:%M:%S')
def next_setting(dt) :
btown.date = dt.strftime("%Y/%m/%d %H:%M")
setting = btown.next_setting(ephem.Sun())
return datetime.strptime(str(setting), '%Y/%m/%d %H:%M:%S')
def next_rising(dt) :
btown.date = dt.strftime("%Y/%m/%d %H:%M")
setting = btown.next_rising(ephem.Sun())
return datetime.strptime(str(setting), '%Y/%m/%d %H:%M:%S')
def is_light(dt) :
set = previous_setting(dt)
rise = next_rising(dt)
return rise - set > timedelta(hours = 24)
def is_dark(dt) :
return not is_light(dt)
if __name__ == '__main__' :
"""
basic testing function: will say if it is light out every hour from now
untill tomorrow this time
"""
print is_light(datetime.utcnow())
for i in range(24) :
dt = datetime.utcnow() + timedelta(hours = i)
print dt, is_light(dt)