-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
403 lines (339 loc) · 14 KB
/
main.py
File metadata and controls
403 lines (339 loc) · 14 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
"""
main.py
-------
Demonstrates solar eclipse calculations, including:
- Eclipse search (optional / disabled)
- Besselian elements computation
- Maximum eclipse time and gamma value
- Geographic coordinates of maximum eclipse
- Eclipse start and end times
- Plotting the eclipse path
Note that t for evaluating Besselian Elements is in TT hours
"""
from typing import List # noqa: F401
from skyfield.api import GREGORIAN_START, load
import pedatetime
import psebessel
import psegam
import psecentralcoords
import psestartendtime
import pselocalcirumstances
def decimal_hours(hours: int, minutes: int, seconds: int) -> float:
"""
Convert hours, minutes, and seconds to decimal hours.
Example:
0h 1m 30s -> 0.025 hours
"""
return hours + minutes / 60.0 + seconds / 3600.0
def round2(n: float) -> int:
"""
Standard rounding
Example:
3.4 -> 3
"""
if n >= 0:
return int(n + 0.5)
else:
return int(n - 0.5)
def main() -> None:
"""
Main driver function demonstrating eclipse computations
and visualization.
"""
# ------------------------------------------------------------------
# Eclipse Search Example (disabled)
# ------------------------------------------------------------------
# Uncomment to perform an actual eclipse search.
#
# print("Solar Eclipse Search:")
# import psefinder
# dt_start = pedatetime.datetime(2024, 1, 1, 0, 0, 0)
# dt_end = pedatetime.datetime(2025, 1, 1, 0, 0, 0)
#
# step = pedatetime.timedelta(0, 2, 0, 0)
#
# psefinder.sefinder(dt_start, dt_end, step, False, False) # UT1
# psefinder.sefinder(dt_start, dt_end, step, False, True) # sep angle + UT1
# psefinder.sefinder(dt_start, dt_end, step, True, False) # TT
# psefinder.sefinder(dt_start, dt_end, step, True, True) # sep angle + TT
print()
# ------------------------------------------------------------------
# Besselian Elements Example
# ------------------------------------------------------------------
ts = load.timescale()
ts.julian_calendar_cutoff = GREGORIAN_START
# UT1 datetime of maximum eclipse (can change to TT or any timescale)
dt_max_ut1: pedatetime.datetime = pedatetime.datetime(2024, 4, 8, 18, 17, 20)
t_max = ts.ut1(
dt_max_ut1.year,
dt_max_ut1.month,
dt_max_ut1.day,
dt_max_ut1.hour,
dt_max_ut1.minute,
dt_max_ut1.second,
)
# Delta-T in seconds (TT - UT1)
delta_t_sec: int = round2(float(t_max.delta_t))
dt_max_tt: pedatetime.datetime = dt_max_ut1 + pedatetime.timedelta(
0, 0, 0, delta_t_sec
)
print(f"Data for {dt_max_ut1.isoformat()} UT1 ({dt_max_tt.isoformat()} TT) Eclipse")
# ------------------------------------------------------------------
# Round to nearest hour for polynomial interpolation
# ------------------------------------------------------------------
dt_max_rounded: pedatetime.datetime = dt_max_tt.copy()
if dt_max_tt.minute >= 30:
dt_max_rounded.sub_minutes(dt_max_tt.minute)
dt_max_rounded.sub_seconds(dt_max_tt.second)
dt_max_rounded.add_hour()
else:
dt_max_rounded.sub_minutes(dt_max_tt.minute)
dt_max_rounded.sub_seconds(dt_max_tt.second)
# ------------------------------------------------------------------
# Compute Besselian elements at plus/minus 2 hours
# ------------------------------------------------------------------
TM2 = psebessel.besselian_find(dt_max_rounded - pedatetime.timedelta(0, 2, 0, 0))
TM1 = psebessel.besselian_find(dt_max_rounded - pedatetime.timedelta(0, 1, 0, 0))
T0 = psebessel.besselian_find(dt_max_rounded)
TP1 = psebessel.besselian_find(dt_max_rounded + pedatetime.timedelta(0, 1, 0, 0))
TP2 = psebessel.besselian_find(dt_max_rounded + pedatetime.timedelta(0, 2, 0, 0))
# ------------------------------------------------------------------
# Polynomial coefficients (Besselian elements)
# For L1 and L2, use a second-degree polynomial. However for improved accuracy, use a third-degree polynomial
# ------------------------------------------------------------------
X_poly = psebessel.find_3rd_degree_polynomial(TM2[0], TM1[0], T0[0], TP1[0], TP2[0])
Y_poly = psebessel.find_3rd_degree_polynomial(TM2[1], TM1[1], T0[1], TP1[1], TP2[1])
D_poly = psebessel.find_3rd_degree_polynomial(TM2[2], TM1[2], T0[2], TP1[2], TP2[2])
L1_poly = psebessel.find_3rd_degree_polynomial(
TM2[3], TM1[3], T0[3], TP1[3], TP2[3]
)
L2_poly = psebessel.find_3rd_degree_polynomial(
TM2[4], TM1[4], T0[4], TP1[4], TP2[4]
)
# Micro is linear
Micro_poly = psebessel.find_1st_degree_polynomial(T0[5], TP1[5])
tan_f1: float = T0[6]
tan_f2: float = T0[7]
# ------------------------------------------------------------------
# Display coefficients
# ------------------------------------------------------------------
print(f"{'n':<3} {'X':>12} {'Y':>14} {'D':>14} {'L1':>14} {'L2':>14} {'Micro':>14}")
for n, values in enumerate(
zip(X_poly, Y_poly, D_poly, L1_poly, L2_poly, Micro_poly)
):
print(
f"{n} "
f"{values[0]:14.10f} {values[1]:14.10f} {values[2]:14.10f} "
f"{values[3]:14.10f} {values[4]:14.10f} {values[5]:14.10f}"
)
print(f"tan(f1) = {tan_f1:14.10f} tan(f2) = {tan_f2:14.10f}")
# ------------------------------------------------------------------
# T0 Eclipse Time
# ------------------------------------------------------------------
base_dt_hour: pedatetime.datetime = dt_max_rounded.copy()
print(f"T0: {base_dt_hour.isoformat()} TT")
# ------------------------------------------------------------------
# Maximum Eclipse Time
# ------------------------------------------------------------------
decimal_time_tt: float = (dt_max_tt - dt_max_rounded.copy()).total_seconds / 3600.0
print(f"Maximum Eclipse: {dt_max_tt.isoformat()} TT")
print(f"Delta T: {delta_t_sec}s")
# ------------------------------------------------------------------
# Gamma at Maximum Eclipse
# ------------------------------------------------------------------
gamma_val: float = psegam.gamma(X_poly, Y_poly, decimal_time_tt) # ty:ignore[invalid-argument-type]
print(f"Gamma: {gamma_val}")
# ------------------------------------------------------------------
# Maximum Eclipse Geographic Location
# ------------------------------------------------------------------
lat_max_umb, lon_max_umb = psecentralcoords.coords(
X_poly, # ty:ignore[invalid-argument-type]
Y_poly, # ty:ignore[invalid-argument-type]
D_poly, # ty:ignore[invalid-argument-type]
Micro_poly, # ty:ignore[invalid-argument-type]
delta_t_sec,
decimal_time_tt,
)
if lat_max_umb is None and lon_max_umb is None:
import psepartialmaxcoords
lat_max, lon_max = psepartialmaxcoords.coords(
X_poly, # ty:ignore[invalid-argument-type]
Y_poly, # ty:ignore[invalid-argument-type]
D_poly, # ty:ignore[invalid-argument-type]
Micro_poly, # ty:ignore[invalid-argument-type]
L1_poly, # ty:ignore[invalid-argument-type]
L2_poly, # ty:ignore[invalid-argument-type]
tan_f1,
tan_f2,
base_dt_hour.hour,
delta_t_sec,
)
else:
lat_max, lon_max = psecentralcoords.coords(
X_poly, # ty:ignore[invalid-argument-type]
Y_poly, # ty:ignore[invalid-argument-type]
D_poly, # ty:ignore[invalid-argument-type]
Micro_poly, # ty:ignore[invalid-argument-type]
delta_t_sec,
decimal_time_tt,
)
if lat_max is not None and lon_max is not None:
print(f"Maximum Eclipse Location: {lat_max}, {lon_max}")
# ------------------------------------------------------------------
# Eclipse Start and End Times
# ------------------------------------------------------------------
# Penumbral contacts
t_start_pen, t_end_pen = psestartendtime.startendtime(X_poly, Y_poly, L1_poly) # ty:ignore[invalid-argument-type]
tt_start_pen: pedatetime.datetime = base_dt_hour + pedatetime.timedelta(
0, 0, 0, int(round2(t_start_pen * 3600))
)
tt_end_pen: pedatetime.datetime = base_dt_hour + pedatetime.timedelta(
0, 0, 0, int(round2(t_end_pen * 3600))
)
print(f"Eclipse Start (Penumbra): {tt_start_pen.isoformat()} TT")
print(f"Eclipse End (Penumbra): {tt_end_pen.isoformat()} TT")
# Umbral contacts (only if central eclipse exists)
if lat_max_umb is not None and lon_max_umb is not None:
t_start_umb, t_end_umb = psestartendtime.startendtime(X_poly, Y_poly, L2_poly) # ty:ignore[invalid-argument-type]
tt_start_umb: pedatetime.datetime = base_dt_hour + pedatetime.timedelta(
0, 0, 0, int(round2(t_start_umb * 3600))
)
tt_end_umb: pedatetime.datetime = base_dt_hour + pedatetime.timedelta(
0, 0, 0, int(round2(t_end_umb * 3600))
)
print(f"Eclipse Start (Central Umbra): {tt_start_umb.isoformat()} TT")
print(f"Eclipse End (Central Umbra): {tt_end_umb.isoformat()} TT")
# ------------------------------------------------------------------
# Local Circumstances
# ------------------------------------------------------------------
obs_lat: float = 25.155
obs_lon: float = -104.174
obs_height_m: float = 100
print(f"Circumstances at {obs_lat}, {obs_lon}, Height {obs_height_m}m")
print("If no output that mean it is out of the eclipse region")
c1, c2, ge, c3, c4, mag, _ = pselocalcirumstances.get_local_circumstances(
X_poly,
Y_poly,
D_poly,
Micro_poly,
L1_poly,
L2_poly,
tan_f1,
tan_f2,
base_dt_hour.hour,
obs_lat,
obs_lon,
obs_height_m,
delta_t_sec,
)
if c1 is not None:
C1: pedatetime.datetime = base_dt_hour + pedatetime.timedelta(
0, 0, 0, int(round2(c1 * 3600))
)
print(f"C1: {C1.isoformat()} TT")
if c2 is not None:
C2: pedatetime.datetime = base_dt_hour + pedatetime.timedelta(
0, 0, 0, int(round2(c2 * 3600))
)
print(f"C2: {C2.isoformat()} TT")
if ge is not None:
GE: pedatetime.datetime = base_dt_hour + pedatetime.timedelta(
0, 0, 0, int(round2(ge * 3600))
)
print(f"Greatest Eclipse: {GE.isoformat()} TT")
salt, saz, malt, maz = pselocalcirumstances.sun_moon_pos(
X_poly,
Y_poly,
D_poly,
Micro_poly,
L1_poly,
L2_poly,
tan_f1,
tan_f2,
ge,
obs_lat,
obs_lon,
obs_height_m,
delta_t_sec,
)
print(f"Sun & Moon AltAz at GE (location) {salt},{saz} & {malt},{maz}")
if c3 is not None:
C3: pedatetime.datetime = base_dt_hour + pedatetime.timedelta(
0, 0, 0, int(round2(c3 * 3600))
)
print(f"C3: {C3.isoformat()} TT")
if c4 is not None:
C4: pedatetime.datetime = base_dt_hour + pedatetime.timedelta(
0, 0, 0, int(round2(c4 * 3600))
)
print(f"C4: {C4.isoformat()} TT")
if mag is not None:
print(f"Magnitude: {mag}")
# ------------------------------------------------------------------
# Compute Eclipse Path for Plotting
# ------------------------------------------------------------------
import setesting
setesting.test(
X_poly,
Y_poly,
D_poly,
Micro_poly,
L1_poly,
L2_poly,
tan_f1,
tan_f2,
decimal_time_tt,
delta_t_sec,
)
# ------------------------------------------------------------------
# Compute Eclipse Path for Plotting
# ------------------------------------------------------------------
if lat_max is not None and lon_max is not None:
step_seconds: int = 60
step_hours: float = decimal_hours(0, 0, step_seconds)
path_lats: List[float] = []
path_lons: List[float] = []
current_time: float = t_start_pen
while current_time < t_end_pen:
current_time += step_hours
lat, lon = psecentralcoords.coords(
X_poly, # ty:ignore[invalid-argument-type]
Y_poly, # ty:ignore[invalid-argument-type]
D_poly, # ty:ignore[invalid-argument-type]
Micro_poly, # ty:ignore[invalid-argument-type]
delta_t_sec,
current_time,
)
if lat is not None and lon is not None:
path_lats.append(lat)
path_lons.append(lon)
import matplotlib.pyplot as plt
import cartopy.crs as ccrs
import cartopy.feature as cfeature
proj = ccrs.Orthographic(central_longitude=lon_max, central_latitude=lat_max)
fig = plt.figure(figsize=(8, 8))
ax = plt.axes(projection=proj)
ax.add_feature(cfeature.OCEAN, facecolor="blue") # ty:ignore[unresolved-attribute]
ax.add_feature(cfeature.LAND, facecolor="green") # ty:ignore[unresolved-attribute]
ax.add_feature(cfeature.LAKES, facecolor="lightblue") # ty:ignore[unresolved-attribute]
ax.add_feature(cfeature.BORDERS, edgecolor="black", linewidth=1) # ty:ignore[unresolved-attribute]
ax.coastlines(color="black", linewidth=0.8) # ty:ignore[unresolved-attribute]
ax.plot(
path_lons,
path_lats,
color="black",
linewidth=2,
transform=ccrs.PlateCarree(),
)
ax.plot(
lon_max,
lat_max,
marker="o",
color="white",
markersize=6,
transform=ccrs.PlateCarree(),
)
ax.set_global() # ty:ignore[unresolved-attribute]
fig.savefig("central_path.png")
if __name__ == "__main__":
main()