-
Notifications
You must be signed in to change notification settings - Fork 1
/
compute_events.py
230 lines (179 loc) · 5.95 KB
/
compute_events.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
import datetime
import csv
from skyfield import api as skyfieldapi, eclipselib, almanac # noqa
import requests
ts = skyfieldapi.load.timescale()
# grab the ephemeris file that covers the year range
eph = skyfieldapi.load('de440.bsp')
def get_equinox_sols(start_date, end_date):
'''
Get datetimes of solstices and equinoxes between start_date and end_date # noqa
Ref:
https://rhodesmill.org/skyfield/almanac.html#the-seasons
'''
eq_sol = []
t, y = almanac.find_discrete(
ts.utc(
start_date.year,
start_date.month,
start_date.day,
start_date.hour,
start_date.minute
),
ts.utc(
end_date.year,
end_date.month,
end_date.day,
end_date.hour,
end_date.minute
),
almanac.seasons(eph)
)
for yi, ti in zip(y, t):
dt = ti.utc_datetime().isoformat(timespec='seconds')
data_out = {
'datetime': dt,
'event_type': almanac.SEASON_EVENTS[yi],
'all_day': True
}
eq_sol.append(data_out)
return eq_sol
def get_full_moon(start_date, end_date):
'''
Get datetimes of full moons between start_date and end_date # noqa
Ref:
https://rhodesmill.org/skyfield/almanac.html#phases-of-the-moon
'''
full_moons = []
t, y = almanac.find_discrete(
ts.utc(
start_date.year,
start_date.month,
start_date.day,
start_date.hour,
start_date.minute
),
ts.utc(
end_date.year,
end_date.month,
end_date.day,
end_date.hour,
end_date.minute
),
almanac.moon_phases(eph)
)
for yi, ti in zip(y, t):
phase = almanac.MOON_PHASES[yi]
if phase.casefold() == 'full moon':
dt = ti.utc_datetime().isoformat(timespec='seconds')
data_out = {
'datetime': dt,
'event_type': almanac.MOON_PHASES[yi],
'all_day': True
}
full_moons.append(data_out)
return full_moons
def get_lunar_eclipses(start_date, end_date):
'''
Get datetimes of lunar eclipses between start_date and end_date # noqa
Ref:
https://rhodesmill.org/skyfield/almanac.html#lunar-eclipses
'''
lunar_eclipses = []
t0 = ts.utc(
start_date.year,
start_date.month,
start_date.day,
start_date.hour,
start_date.minute
)
t1 = ts.utc(
end_date.year,
end_date.month,
end_date.day,
end_date.hour,
end_date.minute
)
t, y, details = eclipselib.lunar_eclipses(t0, t1, eph)
for ti, yi in zip(t, y):
dt = ti.utc_datetime().isoformat(timespec='seconds')
data_out = {
'datetime': dt,
'event_type': eclipselib.LUNAR_ECLIPSES[yi] + ' lunar eclipse'
}
lunar_eclipses.append(data_out)
return lunar_eclipses
def get_eclipses_solar(start_date, end_date):
'''
Find the datetimes of each predicted solar eclipse between two dates
Ref:
https://eclipse.gsfc.nasa.gov/5MCSE/5MCSEcatalog.txt
'''
solar_eclipses = []
r = requests.get('https://eclipse.gsfc.nasa.gov/5MCSE/5MCSEcatalog.txt')
lines = r.text.splitlines()
# just want data from 1550-2649
data = lines[8436:11047]
# https://eclipse.gsfc.nasa.gov/SEcat5/catkey.html
eclipse_type_map = {
'P': 'Partial solar eclipse',
'A': 'Annular solar eclipse',
'T': 'Total solar eclipse',
'H': 'Hybrid or annular/total solar eclipse'
}
fixed_width_map = {
'date': slice(13, 24),
'time': slice(26, 34),
'eclipse_type': slice(56, 57),
'duration': slice(-6, -1)
}
for line in data:
date = line[fixed_width_map['date']]
time = line[fixed_width_map['time']]
date_and_time = f'{date} {time}'
date_parsed = datetime.datetime.strptime(date_and_time, '%Y %b %d %H:%M:%S') # noqa
date_parsed = date_parsed.replace(tzinfo=datetime.timezone.utc)
eclipse_type = eclipse_type_map[line[fixed_width_map['eclipse_type']]]
duration = line[fixed_width_map['duration']].strip()
seconds, minutes = None, None
if duration and duration != '-':
seconds, minutes = duration.split('m')
dt = date_parsed.isoformat(timespec='seconds')
data_out = {
'datetime': dt,
'event_type': eclipse_type,
'duration_minutes': minutes,
'duration_seconds': seconds
}
solar_eclipses.append(data_out)
return solar_eclipses
if __name__ == '__main__':
min_date, max_date = (
datetime.datetime(1550, 1, 5, 0, 0, 0),
datetime.datetime(2649, 12, 31, 23, 59, 59)
)
data_map = {
'solstices-and-equinoxes': get_equinox_sols(min_date, max_date),
'lunar-eclipses': get_lunar_eclipses(min_date, max_date),
'solar-eclipses': get_eclipses_solar(min_date, max_date),
'full-moons': get_full_moon(min_date, max_date)
}
data_combined = []
headers_all = set()
for key in data_map:
data = data_map[key]
data_combined.extend(data)
filepath = f'data/{key}.csv'
print(f'Writing data for {filepath} ...')
headers = data[0].keys()
headers_all.update(headers)
with open(filepath, 'w') as outfile:
writer = csv.DictWriter(outfile, fieldnames=headers)
writer.writeheader()
writer.writerows(data)
combo_filepath = 'data/celestial-almanac.csv'
print(f'Writing {combo_filepath} ...')
with open(combo_filepath, 'w') as outfile:
writer = csv.DictWriter(outfile, fieldnames=headers_all)
writer.writeheader()
writer.writerows(data_combined)