-
Notifications
You must be signed in to change notification settings - Fork 82
/
navigation.py
437 lines (347 loc) · 14.4 KB
/
navigation.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
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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# fmt: off
from operator import attrgetter
from typing import (
TYPE_CHECKING, Iterable, Iterator, List, Optional, Union, cast
)
import numpy as np
import pandas as pd
from shapely.geometry import LineString, MultiLineString
if TYPE_CHECKING:
from ..core import Flight # noqa: 401
from ..core.mixins import PointMixin # noqa: 401
from ..core.structure import Airport, Navaid # noqa: 401
from ..data.basic.airports import Airports # noqa: 401
# fmt: on
class NavigationFeatures:
# shape: Optional[LineString]
# query: Callable[["NavigationFeatures", str], Optional["Flight"]]
def closest_point(
self, points: Union[List["PointMixin"], "PointMixin"]
) -> pd.Series:
"""Selects the closest point of the trajectory with respect to
a point or list of points.
The pd.Series returned by the function is enriched with two fields:
distance (in meters) and point (containing the name of the closest
point to the trajectory)
Example usage:
.. code:: python
>>> item = belevingsvlucht.between(
... "2018-05-30 16:00", "2018-05-30 17:00"
... ).closest_point( # type: ignore
... [
... airports["EHLE"], # type: ignore
... airports["EHAM"], # type: ignore
... navaids["NARAK"], # type: ignore
... ]
... )
>>> f"{item.point}, {item.distance:.2f}m"
"Lelystad Airport, 49.11m"
"""
from ..core.distance import closest_point as cp
# The following cast secures the typing
self = cast("Flight", self)
if not isinstance(points, list):
points = [points]
return min(
(cp(self.data, point) for point in points),
key=attrgetter("distance"),
)
def takeoff_airport(self, **kwargs) -> "Airport":
"""Returns the most probable takeoff airport.
.. code:: python
>>> belevingsvlucht.takeoff_airport()
EHAM/AMS: Amsterdam Schiphol
When data is missing near the ground, it may be relevant
to specify a subset of airports as a keyword parameter.
.. code:: python
>>> missing_data = belevingsvlucht.after("2018-05-30 15:30")
>>> missing_data.takeoff_airport()
NL-0015/nan: Universitair Medisch Centrum Utrecht Heliport
>>> large_airports = airports.query("type == 'large_airport'")
>>> missing_data.takeoff_airport(dataset=large_airports)
EHAM/AMS: Amsterdam Schiphol
"""
from ..core.distance import guess_airport
# The following cast secures the typing
self = cast("Flight", self)
data = self.data.sort_values("timestamp")
return guess_airport(data.iloc[0], **kwargs)
def landing_airport(self, **kwargs) -> "Airport":
"""Returns the most probable landing airport.
.. code:: python
>>> belevingsvlucht.landing_airport()
EHAM/AMS: Amsterdam Schiphol
When data is missing near the ground, it may be relevant
to specify a subset of airports as a keyword parameter.
.. code:: python
>>> missing_data = belevingsvlucht.before("2018-05-30 20:00")
>>> missing_data.landing_airport()
NL-0024/nan: Middenmeer Aerodrome
>>> large_airports = airports.query("type == 'large_airport'")
>>> missing_data.landing_airport(dataset=large_airports)
EHAM/AMS: Amsterdam Schiphol
"""
from ..core.distance import guess_airport
# The following cast secures the typing
self = cast("Flight", self)
data = self.data.sort_values("timestamp")
return guess_airport(data.iloc[-1], **kwargs)
def aligned_on_runway(
self, airport: Union[str, "Airport"]
) -> Iterator["Flight"]:
"""Iterates on all segments of trajectory matching a runway of the
given airport.
Example usage:
>>> sum(1 for _ in belevingsvlucht.aligned_on_runway("EHAM"))
2
"""
from ..data import airports
# The following cast secures the typing
self = cast("Flight", self)
_airport = airports[airport] if isinstance(airport, str) else airport
if _airport is None or _airport.runways.shape.is_empty:
return None
if isinstance(_airport.runways.shape, LineString):
candidate_shapes = [
LineString(list(self.xy_time)).intersection(
_airport.runways.shape.buffer(5e-4)
)
]
else:
candidate_shapes = [
LineString(list(self.xy_time)).intersection(
on_runway.buffer(5e-4)
)
for on_runway in _airport.runways.shape
]
for intersection in candidate_shapes:
if intersection.is_empty:
continue
if isinstance(intersection, LineString):
(*_, start), *_, (*_, stop) = intersection.coords
segment = self.between(start, stop, strict=False)
if segment is not None:
yield segment
if isinstance(intersection, MultiLineString):
(*_, start), *_, (*_, stop) = intersection[0].coords
for chunk in intersection:
(*_, start_bak), *_, (*_, stop) = chunk.coords
if stop - start > 40: # crossing runways and back
start = start_bak
segment = self.between(start, stop, strict=False)
if segment is not None:
yield segment
def on_runway(self, airport: Union[str, "Airport"]) -> Optional["Flight"]:
"""Returns the longest segment of trajectory which perfectly matches
a runway at given airport.
.. code:: python
>>> landing = belevingsvlucht.last(minutes=30).on_runway("EHAM")
>>> landing.mean("altitude")
-26.0
>>> takeoff = belevingsvlucht.first(minutes=30).on_runway("EHAM")
>>> takeoff.mean("altitude")
437.27272727272725
"""
return max(
self.aligned_on_runway(airport),
key=attrgetter("duration"),
default=None,
)
def aligned_on_ils(
self, airport: Union[None, str, "Airport"],
) -> Iterator["Flight"]:
"""Iterates on all segments of trajectory aligned with the ILS of the
given airport. The runway number is appended as a new ``ILS`` column.
Example usage:
.. code:: python
>>> aligned = next(belevingsvlucht.aligned_on_ils('EHAM'))
>>> f"ILS {aligned.max('ILS')} until {aligned.stop:%H:%M}"
'ILS 06 until 20:17'
Be aware that all segments are not necessarily yielded in order.
Consider using ``max(..., key=attrgetter('start'))`` if you want the
last landing attempt, or ``sorted(..., key=attrgetter('start'))`` for
an ordered list
.. code:: python
>>> for aligned in belevingsvlucht.aligned_on_ils('EHLE'):
... print(aligned.start)
2018-05-30 16:50:44+00:00
2018-05-30 18:13:02+00:00
2018-05-30 16:00:55+00:00
2018-05-30 17:21:17+00:00
2018-05-30 19:05:22+00:00
2018-05-30 19:42:36+00:00
>>> from operator import attrgetter
>>> last_aligned = max(
... belevingsvlucht.aligned_on_ils("EHLE"),
... key=attrgetter('start')
... )
"""
from ..data import airports
# The following cast secures the typing
self = cast("Flight", self)
if airport is None:
airport = self.landing_airport()
_airport = airports[airport] if isinstance(airport, str) else airport
if _airport is None or _airport.runways.shape.is_empty:
return None
rad = np.pi / 180
for threshold in _airport.runways.list:
tentative = (
self.bearing(threshold)
.distance(threshold)
.assign(
b_diff=lambda df: df.distance
* (np.radians(df.bearing - threshold.bearing).abs())
)
.query(
f"b_diff.abs() < .1 and cos((bearing - track) * {rad}) > 0"
)
)
if tentative is not None:
for chunk in tentative.split("20s"):
if chunk.longer_than("1 minute"):
yield chunk.assign(ILS=threshold.name)
def aligned_on_navpoint(
self,
points: Iterable["Navaid"],
angle_precision: int = 1,
time_precision: str = "2T",
min_time: str = "30s",
min_distance: int = 80,
) -> Iterator["Flight"]:
"""Iterates on segments of trajectories aligned with one of the given
navigational beacons passed in parameter.
The name of the navigational beacon is assigned in a new column
`navaid`.
"""
# The following cast secures the typing
self = cast("Flight", self)
for navpoint in points:
tentative = (
self.distance(navpoint)
.bearing(navpoint)
.assign(
b_diff=lambda df: df.distance
* (np.radians(df.bearing - df.track).abs()),
delta=lambda df: (df.bearing - df.track),
)
.query(f"delta.abs() < {angle_precision} and distance < 500")
)
if tentative is not None:
for chunk in tentative.split(time_precision):
if (
chunk.longer_than(min_time)
and chunk.min("distance") < min_distance
):
yield chunk.assign(navaid=navpoint.name)
def emergency(self) -> Iterator["Flight"]:
"""Iterates on emergency segments of trajectory.
An emergency is defined with a 7700 squawk code.
"""
sq7700 = self.query("squawk == '7700'") # type: ignore
if sq7700 is None:
return
yield from sq7700.split()
def landing_attempts(
self, dataset: Optional["Airports"] = None
) -> Iterator["Flight"]:
"""Iterates on all landing attempts for current flight.
First, candidates airports are identified in the neighbourhood
of the segments of trajectory below 10,000 ft. By default, the
full airport database is considered but it is possible to restrict
it and pass a smaller database with the dataset parameter.
If no runway information is available for the given airport, no
trajectory segment will be provided.
.. warning::
This API is not stable yet. The interface may change in a near
future.
"""
candidate = self.query("altitude < 8000") # type: ignore
if candidate is not None:
for chunk in candidate.split("10T"):
point = chunk.query("altitude == altitude.min()")
if dataset is None:
cd = point.landing_airport()
else:
cd = point.landing_airport(dataset=dataset)
if cd.runways is not None:
yield from chunk.assign(airport=cd.icao).aligned_on_ils(cd)
def diversion(self) -> Optional["Flight"]:
"""Returns the segment of trajectory after a possible decision
of diversion.
The method relies on the `destination` parameter to identify the
intended destination.
"""
from ..data import airports
f_above = self.query("altitude > 15000") # type: ignore
if (
self.destination != self.destination # type: ignore
or airports[self.destination] is None # type: ignore
or f_above is None
):
return None
return (
f_above.distance(airports[self.destination]) # type: ignore
.diff("distance")
.agg_time("10T", distance_diff="mean")
.query("distance_diff > 0")
)
def diversion_ts(self) -> pd.Timestamp:
diversion = self.diversion()
if diversion is None:
return pd.Timestamp("NaT")
return diversion.start
def holes(self) -> int:
"""Returns the number of 'holes' in a trajectory."""
simplified: "Flight" = self.simplify(25) # type: ignore
if simplified.shape is None:
return -1
return len(simplified.shape.buffer(1e-3).interiors)
def holding_pattern(
self,
min_altitude=7000,
turning_threshold=0.5,
low_limit=pd.Timedelta("30 seconds"),
high_limit=pd.Timedelta("10 minutes"),
turning_limit=pd.Timedelta("5 minutes"),
) -> Iterator["Flight"]:
"""Iterates on parallel segments candidates for identifying
a holding pattern.
.. warning::
This API is not stable yet. The interface may change in a near
future.
"""
# avoid parts that are really way too low
alt_above = self.query(f"altitude > {min_altitude}") # type: ignore
if alt_above is None:
return
straight_line = (
alt_above.assign(
turning_rate=lambda x: x.track_unwrapped.diff()
/ x.timestamp.diff().dt.total_seconds()
)
.filter(turning_rate=17)
.query(f"turning_rate.abs() < {turning_threshold}")
)
if straight_line is None:
return
chunk_candidates = list(
(chunk.start, chunk.duration, chunk.mean("track_unwrapped"), chunk)
for chunk in straight_line.split("10s")
if low_limit <= chunk.duration < high_limit
)
next_ = None
for (
(start1, duration1, track1, chunk1),
(start2, _, track2, chunk2),
) in zip(chunk_candidates, chunk_candidates[1:]):
if (
start2 - start1 - duration1 < turning_limit
and abs(abs(track1 - track2) - 180) < 15
):
yield chunk1
next_ = chunk2
else:
if next_ is not None:
yield next_
next_ = None