-
Notifications
You must be signed in to change notification settings - Fork 0
/
comet.py
293 lines (231 loc) · 9.77 KB
/
comet.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
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Look up a comet and compute its position, using Skyfield.
# This is a brand new function in Skyfield!
# https://rhodesmill.org/skyfield/kepler-orbits.html
from skyfield.api import Loader, Topos
from skyfield.data import mpc
from skyfield.constants import GM_SUN_Pitjeva_2005_km3_s2 as GM_SUN
from skyfield import almanac
import dateutil.parser
from datetime import timedelta
import argparse
import sys, os
load = Loader('./skyfield')
with load.open(mpc.COMET_URL) as f:
comets = mpc.load_comets_dataframe(f)
# Index by designation for fast lookup.
comets = comets.set_index('designation', drop=False)
ts = load.timescale(builtin=True)
eph = load('de421.bsp')
sun = eph['sun']
earth = eph['earth']
WHICH_TWILIGHT = "Nautical twilight"
def comet_by_name(namepat):
# Exact search: row = comets.loc[cometname]
# How to search for something containing a string in pandas:
rows = comets[comets['designation'].str.contains(namepat)]
# Found it!
if len(rows) == 1:
return rows.iloc[0]
# No match
if len(rows) == 0:
return None
# Multiple matches, so print them but return nothing.
# pandas iterrows() returns two things but they're both the same object,
# not an index and an object like you might expect. So ignore r.
print("Matches from", len(comets), 'comets loaded:')
for r, row in rows.iterrows():
print(row['designation'])
return None
def print_event_table(observer, cometvec, alm, t0, t1):
"""For debugging, Print a chromological table of all rise/set events.
Harder to read but useful for debugging and guaranteed to
be in chronological order.
"""
t, y = almanac.find_discrete(t0, t1, alm)
for ti, yi in zip(t, y):
alt, az, distance = \
observer.at(ti).observe(cometvec).apparent().altaz()
t = ti.utc_datetime().astimezone()
d = t.strftime("%Y-%m-%d")
print("Rise" if yi else " Set", d, t.strftime("%H:%M %Z"),
"%3d°%2d'" % az.dms()[:2])
def print_rises_sets(observer, cometvec, alm, t0, t1):
"""Print rises and sets in separate columns, easier to read."""
t, y = almanac.find_discrete(t0, t1, alm)
fmt = "%-10s %-10s %-8s %-10s %-8s %-14s"
print(fmt % ("Date", "Rise", "Azimuth", "Set", "Azimuth",
"Distance"))
def reset_strings():
nonlocal risetime, riseaz, settime, setaz, datestr
risetime = ''
riseaz = ''
settime = ''
setaz = ''
datestr = ''
reset_strings()
for ti, yi in zip(t, y):
alt, az, distance = \
observer.at(ti).observe(cometvec).apparent().altaz()
t = ti.utc_datetime().astimezone()
newdatestr = t.strftime("%Y-%m-%d")
if not datestr:
datestr = newdatestr
elif newdatestr != datestr and (risetime or settime):
print(fmt % (datestr, risetime, riseaz, settime, setaz,
str(distance)))
reset_strings()
datestr = newdatestr
if yi:
risetime = t.strftime("%H:%M %Z")
riseaz = "%3d°%2d'" % az.dms()[:2]
else:
settime = t.strftime("%H:%M %Z")
setaz = "%3d°%2d'" % az.dms()[:2]
# Print a line for the final date
if risetime or settime:
print(fmt % (datestr, risetime, riseaz, settime, setaz,
str(distance)))
def find_twilights(adate, alm_twilights):
"""Find twilight times at dawn and dusk on the given day.
Return a skyvec Time.
"""
# Convert to an aware datetime in the local timezone
t0 = adate.utc_datetime().astimezone()
# Start at midnight
t0 = t0.replace(hour=0, minute=0, second=0, microsecond=0)
t1 = t0 + timedelta(days=1)
times, events = almanac.find_discrete(ts.utc(t0), ts.utc(t1),
alm_twilights)
twilights = []
# Using Astronomical twilight makes sense, but maybe the
# user wants to be a little more ambitious in searching
for i, e in enumerate(events):
if almanac.TWILIGHTS[e] == WHICH_TWILIGHT:
twilights.append(times[i])
return twilights
def svt2str(svt):
"""SkyView Time to string in local timezone"""
return svt.utc_datetime().astimezone().strftime("%Y-%m-%d %H:%M %Z")
def print_alt_table(obstime, cometvec, obsvec, alm_twilights):
"""Print a table of the comet's alt and az for one day
during darkness and astronomical twilight,
and only when the comet is up.
"""
dawn, dusk = [ t.utc_datetime().astimezone() for t in find_twilights(obstime, alm_twilights) ]
t0 = obstime.utc_datetime().astimezone()
# Start at midnight
t0 = t0.replace(hour=0, minute=0, second=0, microsecond=0)
curday = t0.day
INCR = timedelta(minutes=15)
night = True
fmt= "%22s %10s %10s"
print(fmt % ("Time", "Altitude", "Azimuth"))
while t0.day == curday:
# print(" Start loop:", t0.strftime("%Y-%m-%d %H:%M %Z"))
if night and t0 > dawn and t0 < dusk:
print(WHICH_TWILIGHT, "dawn")
night = False
elif not night and t0 >= dusk:
print(WHICH_TWILIGHT, "dusk")
night = True
if night:
t0t = ts.utc(t0)
alt, az, distance = \
obsvec.at(t0t).observe(cometvec).apparent().altaz()
t0s = t0.strftime("%Y-%m-%d %H:%M %Z")
if alt.degrees > 0:
print(fmt % (t0s, "%3d°%2d'" % alt.dms()[:2],
"%3d°%2d'" % az.dms()[:2]))
# else:
# print(t0s, "too low")
# else:
# print(t0.strftime("%Y-%m-%d %H:%M %Z"), "too bright")
t0 += INCR
def calc_comet(comet_df, obstime, earthcoords, numdays=0, alt_table=False):
# Generating a position.
cometvec = sun + mpc.comet_orbit(comet_df, ts, GM_SUN)
cometpos = earth.at(obstime).observe(cometvec)
ra, dec, distance = cometpos.radec()
print("RA", ra, " DEC", dec, " Distance", distance)
if earthcoords:
if len(earthcoords) > 2:
elev = earthcoords[2]
else:
elev = 0
obstopos = Topos(latitude_degrees=earthcoords[0],
longitude_degrees=earthcoords[1],
elevation_m=elev)
print("\nObserver at",
obstopos.latitude, "N ", obstopos.longitude, "E ",
"Elevation", obstopos.elevation.m, "m")
obsvec = earth + obstopos
alt, az, distance = \
obsvec.at(obstime).observe(cometvec).apparent().altaz()
print("Altitude", alt, " Azumuth", az, distance)
alm_twilights = almanac.dark_twilight_day(eph, obstopos)
dawn, dusk = find_twilights(obstime, alm_twilights)
print(WHICH_TWILIGHT, ": Dawn", svt2str(dawn), "Dusk", svt2str(dusk))
if numdays:
print("\nRises and sets over", numdays, "days:")
datetime1 = obstime.utc_datetime() - timedelta(hours=2)
t0 = ts.utc(datetime1)
t1 = ts.utc(datetime1 + timedelta(days=numdays))
alm = almanac.risings_and_settings(eph, cometvec, obstopos)
t, y = almanac.find_discrete(t0, t1, alm)
print_rises_sets(obsvec, cometvec, alm, t0, t1)
if alt_table:
print()
oneday = timedelta(days=1)
while True:
print_alt_table(obstime, cometvec, obsvec, alm_twilights)
numdays -= 1
if numdays <= 0:
break
# Add a day: there doesn't seem to be a way to do this
# while staying within skyview's Time object.
obstime = ts.utc(obstime.utc_datetime() + oneday)
def Usage():
print(f"Usage: {os.path.basename(sys.argv[0])} comet-name [date]")
print(" comet-name may be partial, e.g. '2020 F3'.")
print(" date can be any format that dateutil.parser can handle.")
sys.exit(1)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description="Look up a comet and compute its position.")
parser.add_argument('-t', action="store", dest="time",
help='Time (any format dateutil.parser handles)')
parser.add_argument('-c', action="store", dest="coords",
nargs=2, type=float,
help="Observer's Latitude and longitude (degrees)")
parser.add_argument('-e', action="store", dest="elev", type=float,
help="Observer's elevation (meters)")
parser.add_argument('-d', action="store", dest="numdays",
type=int, default=0,
help="Number of days to show risings/settings")
parser.add_argument('-a', "--alttable", dest="alttable", default=False,
action="store_true",
help="Print a table of altitudes "
"when the comet is visible")
parser.add_argument("cometname",
help="Name (full or partial) of a comet")
args = parser.parse_args(sys.argv[1:])
# print(args)
if args.time:
pdate = dateutil.parser.parse(args.time)
if not pdate.tzinfo:
# interpret it locally by default
pdate = pdate.astimezone()
# skyfield Time seems to handle aware datetimes okay,
# so even though this says utc it will handle the given tzinfo.
t = ts.utc(pdate)
else:
t = ts.now()
if args.coords and args.elev:
args.coords.append(args.elev)
comet_df = comet_by_name(args.cometname)
if comet_df is not None:
print(comet_df['designation'], " ",
t.utc_datetime().astimezone().strftime("%Y-%m-%d %H:%M %Z"))
calc_comet(comet_df, t, args.coords, args.numdays, args.alttable)