-
Notifications
You must be signed in to change notification settings - Fork 82
/
decode.py
1177 lines (1007 loc) · 35.9 KB
/
decode.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
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# fmt: off
import logging
import os
import signal
import socket
import threading
from collections import UserDict
from datetime import datetime, timedelta, timezone
from operator import itemgetter
from pathlib import Path
from typing import (
Any, Callable, Dict, Iterable, Iterator,
List, Optional, TextIO, Tuple, Union
)
import pandas as pd
import pyModeS as pms
from tqdm.autonotebook import tqdm
from ...core import Flight, Traffic
from ...data.basic.airports import Airport
# fmt: on
def next_msg(chunk_it: Iterator[bytes]) -> Iterator[bytes]:
data = b""
for chunk in chunk_it:
data += chunk
while len(data) >= 23:
it = data.find(0x1A)
if it < 0:
break
data = data[it:]
if len(data) < 23:
break
if data[1] == 0x33:
yield data[:23]
data = data[23:]
continue
elif data[1] == 0x32:
data = data[16:]
continue
elif data[1] == 0x31:
data = data[11:]
continue
elif data[1] == 0x34:
data = data[23:]
continue
else:
data = data[1:]
def decode_time_default(
msg: str, time_0: Optional[datetime] = None
) -> datetime:
return datetime.now(timezone.utc)
def decode_time_radarcape(
msg: str, time_0: Optional[datetime] = None
) -> datetime:
now = datetime.now(timezone.utc)
if time_0 is not None:
now = time_0
timestamp = int(msg[4:16], 16)
nanos = timestamp & 0x00003FFFFFFF
secs = timestamp >> 30
now = now.replace(hour=0, minute=0, second=0, microsecond=0)
now += timedelta(seconds=secs, microseconds=nanos / 1000)
return now
def decode_time_dump1090(
msg: str, time_0: Optional[datetime] = None
) -> datetime:
now = datetime.now(timezone.utc)
if time_0 is not None:
now = time_0
else:
now = now.replace(hour=0, minute=0, second=0, microsecond=0)
timestamp = int(msg[4:16], 16)
# dump1090/net_io.c => time (in 12Mhz ticks)
now += timedelta(seconds=timestamp / 12e6)
return now
decode_time: Dict[str, Callable[[str, Optional[datetime]], datetime]] = {
"radarcape": decode_time_radarcape,
"dump1090": decode_time_dump1090,
"default": decode_time_default,
}
class StoppableThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the to_be_stopped() condition."""
def __init__(self, *args, **kwargs) -> None:
super().__init__(*args, **kwargs)
self.daemon = True # is it redundant?
self._stop_event = threading.Event()
def stop(self) -> None:
self._stop_event.set()
def to_be_stopped(self) -> bool:
return self._stop_event.is_set()
class Aircraft(object):
def __init__(self, icao24: str, lat0: float, lon0: float) -> None:
self.icao24 = icao24
self._callsign: Optional[str] = None
self._flight: Optional[Flight] = None
self.cumul: List[Dict] = []
self.t0: Optional[datetime] = None
self.t1: Optional[datetime] = None
self.tpos: Optional[datetime] = None
self.m0: Optional[str] = None
self.m1: Optional[str] = None
self.lat: Optional[float] = None
self.lon: Optional[float] = None
self.alt: Optional[float] = None
self.trk: Optional[float] = None
self.spd: Optional[float] = None
self.lat0: float = lat0
self.lon0: float = lon0
self.version: Optional[int] = None
self.nic_a: Optional[int] = None
self.nic_bc: Optional[int] = None
self.nic_s: Optional[int] = None
self.lock = threading.Lock()
@property
def flight(self) -> Optional[Flight]:
with self.lock: # access then clear not thread-safe, hence the lock
df = pd.DataFrame.from_records(self.cumul)
self.cumul.clear()
if self._flight is not None:
df = pd.concat([self._flight.data, df], sort=False)
if self.version is not None:
# remove columns added by nuc_p, nuc_r
if "HPL" in df.columns:
df = df.drop(columns=["HPL", "RCu", "RCv"])
if "HVE" in df.columns:
df = df.drop(columns=["HVE", "VVE"])
if len(df) == 0:
return None
self._flight = Flight(
df.assign(
callsign=df.callsign.replace("", None)
.fillna(method="ffill")
.fillna(method="bfill")
)
)
return self._flight
@property
def callsign(self):
return self._callsign
@callsign.setter
def callsign(self, args):
t, msg = args
callsign = pms.adsb.callsign(msg).strip("_")
if callsign == "":
return
self._callsign = callsign
with self.lock:
self.cumul.append(
dict(timestamp=t, icao24=self.icao24, callsign=self._callsign)
)
@property
def speed(self):
pass
@speed.setter
def speed(self, args):
t, msg = args
vdata = pms.adsb.velocity(msg)
if vdata is None:
return
spd, trk, roc, tag = vdata
if tag != "GS":
# does it ever happen...
return
if (spd is None) or (trk is None):
return
self.spd = spd
self.trk = trk
delta = pms.adsb.altitude_diff(msg)
with self.lock:
self.cumul.append(
dict(
timestamp=t,
icao24=self.icao24,
groundspeed=spd,
track=trk,
vertical_rate=roc,
)
)
if delta is not None and self.alt is not None:
self.cumul[-1]["geoaltitude"] = self.alt + delta
@property
def position(self):
pass
@position.setter
def position(self, args):
t, msg = args
oe = pms.adsb.oe_flag(msg)
setattr(self, "m" + str(oe), msg)
setattr(self, "t" + str(oe), t)
if (
self.t0 is not None
and self.t1 is not None
and abs((self.t0 - self.t1).total_seconds()) < 10
):
latlon = pms.adsb.position(
self.m0, self.m1, self.t0, self.t1, self.lat0, self.lon0
)
else:
latlon = None
if latlon is not None:
self.tpos = t
self.lat, self.lon = latlon
self.alt = pms.adsb.altitude(msg)
with self.lock:
self.cumul.append(
dict(
timestamp=t,
icao24=self.icao24,
latitude=self.lat,
longitude=self.lon,
altitude=self.alt,
onground=False,
)
)
@property
def surface(self):
pass
@surface.setter
def surface(self, args):
t, msg = args
self.lat, self.lon = pms.adsb.surface_position_with_ref(
msg, self.lat0, self.lon0
)
with self.lock:
self.cumul.append(
dict(
timestamp=t,
icao24=self.icao24,
latitude=self.lat,
longitude=self.lon,
onground=True,
)
)
@property
def altcode(self):
pass
@altcode.setter
def altcode(self, args):
t, msg = args
self.alt = pms.common.altcode(msg)
with self.lock:
self.cumul.append(
dict(timestamp=t, icao24=self.icao24, altitude=self.alt)
)
@property
def idcode(self):
pass
@idcode.setter
def idcode(self, args):
t, msg = args
with self.lock:
self.cumul.append(
dict(
timestamp=t,
icao24=self.icao24,
squawk=pms.common.idcode(msg),
)
)
@property
def bds20(self):
pass
@bds20.setter
def bds20(self, args):
t, msg = args
callsign = pms.commb.cs20(msg).strip("_")
if callsign == "":
return
self._callsign = callsign
with self.lock:
# in case altitude was already included from altcode (DF 4 or 20)
# or squawk from idcode (DF5 or 21)
last_entry = self.cumul[-1] if len(self.cumul) > 0 else None
if last_entry is not None and last_entry["timestamp"] == t:
self.cumul[-1] = dict(**last_entry, callsign=self._callsign)
else:
self.cumul.append(
dict(
timestamp=t, icao24=self.icao24, callsign=self._callsign
)
)
@property
def bds40(self):
pass
@bds40.setter
def bds40(self, args):
t, msg = args
with self.lock:
# in case altitude was already included from altcode (DF 4 or 20)
# or squawk from idcode (DF5 or 21)
last_entry = self.cumul[-1] if len(self.cumul) > 0 else None
if last_entry is not None and last_entry["timestamp"] == t:
self.cumul[-1] = {
**last_entry,
**dict(
# FMS selected altitude (ft)
selected_fms=pms.commb.selalt40fms(msg),
# MCP/FCU selected altitude (ft)
selected_mcp=pms.commb.selalt40mcp(msg),
# Barometric pressure (mb)
barometric_setting=pms.commb.p40baro(msg),
),
}
else:
self.cumul.append(
dict(
timestamp=t,
icao24=self.icao24,
# FMS selected altitude (ft)
selected_fms=pms.commb.selalt40fms(msg),
# MCP/FCU selected altitude (ft)
selected_mcp=pms.commb.selalt40mcp(msg),
# Barometric pressure (mb)
barometric_setting=pms.commb.p40baro(msg),
)
)
@property
def bds44(self):
pass
@bds44.setter
def bds44(self, args):
t, msg = args
wind = pms.commb.wind44(msg)
wind = wind if wind is not None else (None, None)
with self.lock:
# in case altitude was already included from altcode (DF 4 or 20)
# or squawk from idcode (DF 5 or 21)
last_entry = self.cumul[-1] if len(self.cumul) > 0 else None
if last_entry is not None and last_entry["timestamp"] == t:
self.cumul[-1] = {
**last_entry,
**dict(
# Humidity (%)
humidity=pms.commb.hum44(msg),
# Average static pressure (hPa)
pressure=pms.commb.p44(msg),
# Static air temperature (C)
temperature=pms.commb.temp44(msg),
turbulence=pms.commb.turb44(msg),
# Wind speed (kt) and direction (true) (deg)
windspeed=wind[0],
winddirection=wind[1],
),
}
else:
self.cumul.append(
dict(
timestamp=t,
icao24=self.icao24,
# Humidity (%)
humidity=pms.commb.hum44(msg),
# Average static pressure (hPa)
pressure=pms.commb.p44(msg),
# Static air temperature (C)
temperature=pms.commb.temp44(msg),
turbulence=pms.commb.turb44(msg),
# Wind speed (kt) and direction (true) (deg)
windspeed=wind[0],
winddirection=wind[1],
)
)
@property
def bds45(self):
pass
@bds45.setter
def bds45(self, args):
t, msg = args
with self.lock:
# in case altitude was already included from altcode (DF 4 or 20)
# or squawk from idcode (DF 5 or 21)
last_entry = self.cumul[-1] if len(self.cumul) > 0 else None
if last_entry is not None and last_entry["timestamp"] == t:
self.cumul[-1] = {
**last_entry,
**dict(
# Turbulence level (0-3)
turbulence=pms.commb.turb45(msg),
# Wind shear level (0-3)
wind_shear=pms.commb.ws45(msg),
# Microburst level (0-3)
microburst=pms.commb.mb45(msg),
# Icing level (0-3)
icing=pms.commb.ic45(msg),
# Wake vortex level (0-3)
wake_vortex=pms.commb.wv45(msg),
# Static air temperature (C)
temperature=pms.commb.temp45(msg),
# Average static pressure (hPa)
pressure=pms.commb.p45(msg),
# Radio height (ft)
radio_height=pms.commb.rh45(msg),
),
}
else:
self.cumul.append(
dict(
timestamp=t,
icao24=self.icao24,
# Turbulence level (0-3)
turbulence=pms.commb.turb45(msg),
# Wind shear level (0-3)
wind_shear=pms.commb.ws45(msg),
# Microburst level (0-3)
microburst=pms.commb.mb45(msg),
# Icing level (0-3)
icing=pms.commb.ic45(msg),
# Wake vortex level (0-3)
wake_vortex=pms.commb.wv45(msg),
# Static air temperature (C)
temperature=pms.commb.temp45(msg),
# Average static pressure (hPa)
pressure=pms.commb.p45(msg),
# Radio height (ft)
radio_height=pms.commb.rh45(msg),
)
)
@property
def bds50(self):
pass
@bds50.setter
def bds50(self, args):
t, msg = args
with self.lock:
# in case altitude was already included from altcode (DF 4 or 20)
# or squawk from idcode (DF5 or 21)
last_entry = self.cumul[-1] if len(self.cumul) > 0 else None
if last_entry is not None and last_entry["timestamp"] == t:
self.cumul[-1] = {
**last_entry,
**dict(
# Ground speed (kt)
groundspeed=pms.commb.gs50(msg),
# Roll angle (deg)
roll=pms.commb.roll50(msg),
# True airspeed (kt)
TAS=pms.commb.tas50(msg),
# True track angle (deg)
track=pms.commb.trk50(msg),
# Track angle rate (deg/sec)
track_rate=pms.commb.rtrk50(msg),
),
}
else:
self.cumul.append(
dict(
timestamp=t,
icao24=self.icao24,
# Ground speed (kt)
groundspeed=pms.commb.gs50(msg),
# Roll angle (deg)
roll=pms.commb.roll50(msg),
# True airspeed (kt)
TAS=pms.commb.tas50(msg),
# True track angle (deg)
track=pms.commb.trk50(msg),
# Track angle rate (deg/sec)
track_rate=pms.commb.rtrk50(msg),
)
)
@property
def bds60(self):
pass
@bds60.setter
def bds60(self, args):
t, msg = args
with self.lock:
# in case altitude was already included from altcode (DF 4 or 20)
# or squawk from idcode (DF5 or 21)
last_entry = self.cumul[-1] if len(self.cumul) > 0 else None
if last_entry is not None and last_entry["timestamp"] == t:
self.cumul[-1] = {
**last_entry,
**dict(
# Indicated airspeed (kt)
IAS=pms.commb.ias60(msg),
# Magnetic heading (deg)
heading=pms.commb.hdg60(msg),
# Mach number (-)
Mach=pms.commb.mach60(msg),
# Barometric altitude rate (ft/min)
vertical_rate_barometric=pms.commb.vr60baro(msg),
# Inertial vertical speed (ft/min)
vertical_rate_inertial=pms.commb.vr60ins(msg),
),
}
else:
self.cumul.append(
dict(
timestamp=t,
icao24=self.icao24,
# Indicated airspeed (kt)
IAS=pms.commb.ias60(msg),
# Magnetic heading (deg)
heading=pms.commb.hdg60(msg),
# Mach number (-)
Mach=pms.commb.mach60(msg),
# Barometric altitude rate (ft/min)
vertical_rate_barometric=pms.commb.vr60baro(msg),
# Inertial vertical speed (ft/min)
vertical_rate_inertial=pms.commb.vr60ins(msg),
)
)
@property
def nuc_p(self):
pass
@nuc_p.setter
def nuc_p(self, args):
t, msg = args
with self.lock:
hpl, rcu, rcv = pms.adsb.nuc_p(msg)
current = dict(
# Horizontal Protection Limit
HPL=hpl,
# 95% Containment Radius on horizontal position error
RCu=rcu,
# 95% Containment Radius on vertical position error
RCv=rcv,
)
last_entry = self.cumul[-1] if len(self.cumul) > 0 else None
if last_entry is not None and last_entry["timestamp"] == t:
self.cumul[-1] = {**last_entry, **current}
else:
self.cumul.append(
dict(timestamp=t, icao24=self.icao24, **current)
)
@property
def nic_v1(self):
pass
@nic_v1.setter
def nic_v1(self, args):
t, msg = args
if self.nic_s is None:
return
with self.lock:
hcr, vpl = pms.adsb.nic_v1(msg, self.nic_s)
last_entry = self.cumul[-1] if len(self.cumul) > 0 else None
current = dict(
# Horizontal Containment Radius
HCR=hcr,
# Vertical Protection Limit
VPL=vpl,
)
if last_entry is not None and last_entry["timestamp"] == t:
self.cumul[-1] = {**last_entry, **current}
else:
self.cumul.append(
dict(timestamp=t, icao24=self.icao24, **current)
)
@property
def nic_v2(self):
pass
@nic_v2.setter
def nic_v2(self, args):
t, msg = args
if self.nic_a is None or self.nic_bc is None:
return
with self.lock:
hcr = pms.adsb.nic_v2(msg, self.nic_a, self.nic_bc)
last_entry = self.cumul[-1] if len(self.cumul) > 0 else None
current = dict(
# Horizontal Containment Radius
HCR=hcr
)
if last_entry is not None and last_entry["timestamp"] == t:
self.cumul[-1] = {**last_entry, **current}
else:
self.cumul.append(
dict(timestamp=t, icao24=self.icao24, **current)
)
@property
def nuc_r(self):
pass
@nuc_r.setter
def nuc_r(self, args):
t, msg = args
with self.lock:
hve, vve = pms.adsb.nuc_v(msg)
last_entry = self.cumul[-1] if len(self.cumul) > 0 else None
current = dict(
# Horizontal Velocity Error
HVE=hve,
# Vertical Velocity Error
VVE=vve,
)
if last_entry is not None and last_entry["timestamp"] == t:
self.cumul[-1] = {**last_entry, **current}
else:
self.cumul.append(
dict(timestamp=t, icao24=self.icao24, **current)
)
@property
def nac_v(self):
pass
@nac_v.setter
def nac_v(self, args):
t, msg = args
with self.lock:
hfm, vfm = pms.adsb.nac_v(msg)
last_entry = self.cumul[-1] if len(self.cumul) > 0 else None
current = dict(
# Horizontal Figure of Merit for rate (GNSS)
HFM=hfm,
# Vertical Figure of Merit for rate (GNSS)
VFM=vfm,
)
if last_entry is not None and last_entry["timestamp"] == t:
self.cumul[-1] = {**last_entry, **current}
else:
self.cumul.append(
dict(timestamp=t, icao24=self.icao24, **current)
)
@property
def nac_p(self):
pass
@nac_p.setter
def nac_p(self, args):
t, msg = args
with self.lock:
epu, vepu = pms.adsb.nac_p(msg)
last_entry = self.cumul[-1] if len(self.cumul) > 0 else None
current = dict(
# Estimated Position Uncertainty
EPU=epu,
# Vertical Estimated Position Uncertainty
VEPU=vepu,
)
if last_entry is not None and last_entry["timestamp"] == t:
self.cumul[-1] = {**last_entry, **current}
else:
self.cumul.append(
dict(timestamp=t, icao24=self.icao24, **current)
)
@property
def sil(self):
pass
@sil.setter
def sil(self, args):
t, msg = args
with self.lock:
phcr, pvpl, base = pms.adsb.sil(msg, self.version)
last_entry = self.cumul[-1] if len(self.cumul) > 0 else None
current = dict(
version=self.version,
# Probability exceeding Horizontal Containment Radius
pHCR=phcr,
# Probability exceeding Vertical Protection Limit
pVPL=pvpl,
sil_base=base,
)
if last_entry is not None and last_entry["timestamp"] == t:
self.cumul[-1] = {**last_entry, **current}
else:
self.cumul.append(
dict(timestamp=t, icao24=self.icao24, **current)
)
class AircraftDict(UserDict):
lat0: float
lon0: float
def __missing__(self, key):
self[key] = value = Aircraft(key, self.lat0, self.lon0)
return value
def set_latlon(self, lat0, lon0):
self.lat0 = lat0
self.lon0 = lon0
for ac in self.values():
ac.lat0 = lat0
ac.lon0 = lon0
class Decoder:
thread: Optional[StoppableThread]
def __init__(
self, reference: Union[None, str, Airport, Tuple[float, float]] = None
) -> None:
if isinstance(reference, str):
from ...data import airports
reference = airports[reference]
if reference is None:
logging.warning(
"No valid reference position provided. Fallback to (0, 0)"
)
lat0, lon0 = 0.0, 0.0
elif isinstance(reference, Airport):
lat0, lon0 = reference.latlon
else:
lat0, lon0 = reference
self.acs: AircraftDict = AircraftDict()
self.acs.set_latlon(lat0, lon0)
self.thread = None
@classmethod
def from_file(
cls,
filename: Union[str, Path],
reference: Union[str, Airport, Tuple[float, float]],
uncertainty: bool = False,
) -> "Decoder":
if isinstance(filename, str):
filename = Path(filename)
with filename.open("r") as fh:
all_lines = fh.readlines()
decoder = cls(reference)
decoder.process_msgs(
list(
(
datetime.fromtimestamp(
float(line.strip().split(",")[0]), timezone.utc
),
line.strip().split(",")[1][18:],
)
for line in all_lines
),
uncertainty=uncertainty,
)
return decoder
@classmethod
def from_binary(
cls,
filename: Union[str, Path],
reference: Union[str, Airport, Tuple[float, float]],
*,
uncertainty: bool = False,
time_fmt: str = "dump1090",
time_0: Optional[datetime] = None,
redefine_mag: int = 10,
fh: Optional[TextIO] = None,
):
decoder = cls(reference)
redefine_freq = 2 ** redefine_mag - 1
decode_time_here = decode_time.get(time_fmt, decode_time_default)
def next_in_binary(filename: Union[str, Path]) -> Iterator[bytes]:
with Path(filename).open("rb") as fh:
while True:
get = fh.read()
if len(get) == 0:
return
yield get
for i, bin_msg in tqdm(enumerate(next_msg(next_in_binary(filename)))):
if len(bin_msg) < 23:
continue
msg = "".join(["{:02x}".format(t) for t in bin_msg])
now = decode_time_here(msg, time_0)
if fh is not None:
fh.write("{},{}\n".format(now.timestamp(), msg))
if i & redefine_freq == redefine_freq:
decoder.redefine_reference(now)
decoder.process(now, msg[18:], uncertainty=uncertainty)
return decoder
@classmethod
def from_rtlsdr(
cls,
reference: Union[str, Airport, Tuple[float, float]],
file_pattern: str = "~/ADSB_EHS_RAW_%Y%m%d_dump1090.csv",
uncertainty: bool = False,
) -> "Decoder": # coverage: ignore
from .rtlsdr import MyRtlReader
decoder = cls(reference)
# dump file
now = datetime.now(timezone.utc)
filename = now.strftime(file_pattern)
today = os.path.expanduser(filename)
fh = open(today, "a", 1)
rtlsdr = MyRtlReader(decoder, fh, uncertainty=uncertainty)
decoder.thread = StoppableThread(target=rtlsdr.run)
signal.signal(signal.SIGINT, rtlsdr.stop)
decoder.thread.start()
return decoder
@classmethod
def from_socket(
cls,
socket: socket.socket,
reference: Union[str, Airport, Tuple[float, float]],
*,
uncertainty: bool,
time_fmt: str = "default",
time_0: Optional[datetime] = None,
redefine_mag: int = 7,
fh: Optional[TextIO] = None,
) -> "Decoder": # coverage: ignore
decoder = cls(reference)
redefine_freq = 2 ** redefine_mag - 1
decode_time_here = decode_time.get(time_fmt, decode_time_default)
def next_in_socket() -> Iterator[bytes]:
while True:
if decoder.thread is None or decoder.thread.to_be_stopped():
socket.close()
return
yield socket.recv(2048)
def decode():
for i, bin_msg in enumerate(next_msg(next_in_socket())):
msg = "".join(["{:02x}".format(t) for t in bin_msg])
# Timestamp decoding
now = decode_time_here(msg, time_0)
if fh is not None:
fh.write("{},{}\n".format(now.timestamp(), msg))
if len(bin_msg) < 23:
continue
if (
time_fmt != "radarcape"
and i & redefine_freq == redefine_freq
):
decoder.redefine_reference(now)
decoder.process(now, msg[18:], uncertainty=uncertainty)
decoder.thread = StoppableThread(target=decode)
decoder.thread.start()
return decoder
def stop(self):
if self.thread is not None and self.thread.is_alive():
self.thread.stop()
self.thread.join()
def __del__(self):
self.stop()
@classmethod
def from_dump1090(
cls,
reference: Union[str, Airport, Tuple[float, float]],
file_pattern: str = "~/ADSB_EHS_RAW_%Y%m%d_dump1090.csv",
uncertainty: bool = False,
) -> "Decoder": # coverage: ignore
now = datetime.now(timezone.utc)
filename = now.strftime(file_pattern)
today = os.path.expanduser(filename)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 30005))
fh = open(today, "a", 1)
return cls.from_socket(
s,
reference,
uncertainty=uncertainty,
time_fmt="dump1090",
time_0=now,
fh=fh,
)
@classmethod
def from_address(
cls,
host: str,
port: int,
reference: Union[str, Airport, Tuple[float, float]],
file_pattern: str = "~/ADSB_EHS_RAW_%Y%m%d_tcp.csv",
uncertainty: bool = False,
) -> "Decoder": # coverage: ignore
now = datetime.now(timezone.utc)
filename = now.strftime(file_pattern)
today = os.path.expanduser(filename)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, port))
fh = open(today, "a", 1)
return cls.from_socket(
s, reference, uncertainty=uncertainty, time_fmt="radarcape", fh=fh
)
def redefine_reference(self, time: datetime) -> None:
pos = list(
(ac.lat, ac.lon)
for ac in self.acs.values()
if ac.alt is not None
and ac.alt < 5000
and ac.tpos is not None
and (time - ac.tpos).total_seconds() < 20 * 60
)
n = len(pos)
if n > 0:
self.acs.set_latlon(
sum(a[0] for a in pos) / n, sum(a[1] for a in pos) / n
)
def process_msgs(
self, msgs: Iterable[Tuple[datetime, str]], uncertainty: bool = False
) -> None:
for i, (time, msg) in tqdm(enumerate(msgs), total=sum(1 for _ in msgs)):