-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathpyephem.py
executable file
·138 lines (99 loc) · 3.48 KB
/
pyephem.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python3
import ephem
from datetime import datetime
from datetime import timedelta
from datetime import timezone
import math
import json
import logging
logging.basicConfig(level=logging.INFO)
logger = logging
LATITUDE = 33
LONGITUDE = -84
PRESSURE = 1010 # 0 disables refraction
utcnow = datetime.now(tz=timezone.utc) # ephem expects UTC dates
#utcnow = datetime.now(tz=timezone.utc) + timedelta(days=10)
obs = ephem.Observer()
obs.lat = math.radians(LATITUDE)
obs.lon = math.radians(LONGITUDE)
obs.pressure = PRESSURE
obs.date = utcnow
logger.info('Latitude: %s', obs.lat.znorm)
sun = ephem.Sun()
sun.compute(obs)
# Sun
sun_alt_deg = math.degrees(sun.alt)
sun_ha_rad = obs.sidereal_time() - sun.ra
sun_ha_deg = math.degrees(sun_ha_rad)
sun.compute(obs)
sun_rise_date = obs.next_rising(sun)
sun_set_date = obs.next_setting(sun)
sun_next_transit = obs.next_transit(sun)
obs.date = sun_rise_date
sun_rise_ha = math.degrees(obs.sidereal_time() - sun.ra)
logger.info('Sun alt: %0.7f rad', sun.alt)
logger.info('Sun alt: %0.1f', sun_alt_deg)
logger.info('Sun HA: %0.1f', sun_ha_deg)
logger.info('Sun rise: %s', ephem.localtime(sun_rise_date))
logger.info('Sun set: %s', ephem.localtime(sun_set_date))
logger.info('Sun Transit: %s', ephem.localtime(sun_next_transit))
logger.info('Sun rise HA: %0.1f', sun_rise_ha)
obs.horizon = math.radians(-18)
sun.compute(obs)
sun_dawn_date = obs.next_rising(sun)
sun_twilight_date = obs.next_setting(sun)
logger.info('Sun dawn: %s', ephem.localtime(sun_dawn_date))
logger.info('Sun twilight: %s', ephem.localtime(sun_twilight_date))
# Moon
obs.date = datetime.now(tz=timezone.utc) # ephem expects UTC dates
obs.horizon = math.radians(0)
moon = ephem.Moon()
moon.compute(obs)
moon_alt_deg = math.degrees(moon.alt)
logger.info('Moon alt: %0.1f', moon_alt_deg)
logger.info('Moon phase: %0.2f%%', moon.moon_phase * 100)
obs.date = utcnow
sun.compute(obs)
moon.compute(obs)
#quarter
quarter_names = (
'Waxing Crescent',
'Waxing Gibbous',
'Waning Gibbous',
'Waning Crescent'
)
sun_lon = ephem.Ecliptic(sun).lon
moon_lon = ephem.Ecliptic(moon).lon
sm_angle = (moon_lon - sun_lon) % math.tau
#logger.info('Sun/Moon angle: %0.8f', sm_angle)
moon_quarter = int(sm_angle * 4.0 // math.tau)
#logger.info('Quarter: %0.8f', sm_angle * 4.0 / math.tau)
logger.info('Phase percent: %0.1f', (sm_angle / math.tau) * 100)
logger.info('Quarter: %d, %s', moon_quarter + 1, quarter_names[moon_quarter])
try:
if math.degrees(sun.alt) < 0:
logger.info('Sun below horizon')
sun_civilDawn_date = obs.next_rising(sun, use_center=True).datetime()
else:
logger.info('Sun already above horizon')
sun_civilDawn_date = obs.previous_rising(sun, use_center=True).datetime()
except ephem.NeverUpError:
# northern hemisphere
sun_civilDawn_date = utcnow + timedelta(years=10)
except ephem.AlwaysUpError:
# southern hemisphere
sun_civilDawn_date = utcnow - timedelta(days=1)
try:
sun_civilTwilight_date = obs.next_setting(sun, use_center=True).datetime()
except ephem.AlwaysUpError:
# northern hemisphere
sun_civilTwilight_date = utcnow - timedelta(days=1)
except ephem.NeverUpError:
# southern hemisphere
sun_civilTwilight_date = utcnow + timedelta(years=10)
data = {
'sunrise' : sun_civilDawn_date.replace(tzinfo=timezone.utc).isoformat(),
'sunset' : sun_civilTwilight_date.replace(tzinfo=timezone.utc).isoformat(),
'streamDaytime' : False,
}
print(json.dumps(data, indent=4))