-
Notifications
You must be signed in to change notification settings - Fork 8
/
run-realtime.py
145 lines (110 loc) · 3.51 KB
/
run-realtime.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
"""
This is a example script to use the MP model on raw data stream. It connects
to a Mode-S Beast like raw stream.
Usage:
python run-realtime.py --server SERVER_ADDR_OR_IP --port PORT_NUMBER
"""
import os
import matplotlib.pyplot as plt
import time
import threading
import pyModeS as pms
import argparse
from lib import client
import mp_vis
from stream import Stream
parser = argparse.ArgumentParser()
parser.add_argument('--server', help='server address or IP', required=True)
parser.add_argument('--port', help='Raw beast port', required=True)
args = parser.parse_args()
server = args.server
port = int(args.port)
ADSB_TS = []
ADSB_MSGS = []
EHS_TS = []
EHS_MSGS = []
TNOW = 0
TFLAG = 0
STREAM = Stream(lat0=51.99, lon0=4.37)
STREAM.mp.N_AC_PTCS = 250
STREAM.mp.AGING_SIGMA = 180
DATA_LOCK = threading.Lock()
root = os.path.dirname(os.path.realpath(__file__))
class PwmBeastClient(client.BaseClient):
def __init__(self, host, port):
super(PwmBeastClient, self).__init__(host, port)
def handle_messages(self, messages):
global ADSB_TS
global ADSB_MSGS
global EHS_TS
global EHS_MSGS
global TNOW
global TFLAG
for msg, ts in messages:
if len(msg) != 28: # wrong data length
continue
df = pms.df(msg)
if df == 17:
ADSB_TS.append(ts)
ADSB_MSGS.append(msg)
if df == 20 or df == 21:
EHS_TS.append(ts)
EHS_MSGS.append(msg)
def gen_plot():
global ADSB_TS
global ADSB_MSGS
global EHS_TS
global EHS_MSGS
global TNOW
global TFLAG
while True:
if TFLAG >= 15:
print("updating plot...")
DATA_LOCK.acquire()
data = STREAM.mp.construct()
DATA_LOCK.release()
tstr = time.strftime("%Y-%m-%d %H:%M:%S", time.gmtime(TNOW))
plt.figure(figsize=(12, 9))
mp_vis.plot_all_level_wind(STREAM.mp, data=data, return_plot=True, landscape_view=True, barbs=False)
plt.suptitle('Current wind field (UTC %s)' % tstr)
plt.savefig(root+'/data/screenshots/nowfield_wind.png')
plt.figure(figsize=(12, 9))
mp_vis.plot_all_level_temp(STREAM.mp, data=data, return_plot=True, landscape_view=True)
plt.suptitle('Current temperature field (UTC %s)' % tstr)
plt.savefig(root+'/data/screenshots/nowfield_temp.png')
TFLAG = 0
time.sleep(0.2)
def update_mp():
global ADSB_TS
global ADSB_MSGS
global EHS_TS
global EHS_MSGS
global TNOW
global TFLAG
while True:
if len(EHS_TS) == 0:
time.sleep(0.1)
continue
t = EHS_TS[-1] # last timestamp
if t - TNOW > 1:
TNOW = int(t)
TFLAG += 1
STREAM.process_raw(ADSB_TS, ADSB_MSGS, EHS_TS, EHS_MSGS)
STREAM.compute_current_weather()
DATA_LOCK.acquire()
STREAM.update_mp_model()
DATA_LOCK.release()
ADSB_TS = []
ADSB_MSGS = []
EHS_TS = []
EHS_MSGS = []
print("time: %d | n_ptc: %d" % (TNOW, len(STREAM.mp.PTC_X)))
else:
time.sleep(0.1)
thread_gen_plot = threading.Thread(target=gen_plot)
client = PwmBeastClient(host=server, port=port)
thread_client = threading.Thread(target=client.run)
thread_mp = threading.Thread(target=update_mp)
thread_client.start()
thread_mp.start()
thread_gen_plot.start()