-
Notifications
You must be signed in to change notification settings - Fork 1
/
wspr_utils.py
244 lines (200 loc) · 9.91 KB
/
wspr_utils.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
from spot_processing import Station
from collections import defaultdict
import urllib.request
import numpy as np
from scipy.interpolate import splev, splrep
import matplotlib.pyplot as plt
from scipy import interpolate
import datetime, time
from coords_utils import *
import random
import matplotlib.dates as mdates
def strip_data(data):
for i, row in enumerate(data):
for j, el in enumerate(row):
data[i][j] = el.strip()
return data
def running_mean(x, N):
x = np.array(x)
x = np.concatenate((x, x[0:N-1]), axis=0)
cumsum = np.cumsum(np.insert(x, 0, 0))
return (cumsum[N:] - cumsum[:-N]) / float(N)
def get_url(band, spot_number, reporter):
return "http://wsprnet.org/olddb?mode=html&band=%d&limit=%d&findcall=&findreporter=%s&sort=date" % (band, spot_number, reporter)
def download_wspr_data(band, spot_number, reporter):
link = get_url(band, spot_number, reporter)
contents = urllib.request.urlopen(link).read()
return contents
def extract_wspr_data(contents, timestamp_start, timestamp_stop):
d_dict = defaultdict(list)
reporter_loc_dict = {}
for content in contents:
for row in content.splitlines():
row = row.decode()
if "<tr id=\"evenrow\"><td align=left> " in row or "<tr id=\"oddrow\"><td align=left> " in row:
row = row.replace(" ", "")
row_list = row.split(";")[1::2]
#print (row_list)
timestamp = row_list[0]
callsign = row_list[1]
frequency = float(row_list[2])
snr = float(row_list[3])
locator = row_list[5]
power = float(row_list[7])
reporter = row_list[8]
reporter_locator = row_list[9]
distance = float(row_list[10])
if timestamp_stop >= get_unixtime(timestamp) >= timestamp_start:
d_dict[reporter].append((timestamp, callsign, frequency, snr, locator, power, distance))
reporter_loc_dict[reporter] = (reporter_locator)
else:
#d_dict[reporter].append(None)
reporter_loc_dict[reporter] = (reporter_locator)
return reporter_loc_dict, d_dict
def extract_info(d_dict):
callsign_dict = defaultdict(int)
coord_dict = {}
dist_dict = {}
for reporter in d_dict:
dist_dict = {el[1]: el[6] for el in d_dict[reporter]}
for el in d_dict[reporter]:
call = el[1]
locator = el[4]
callsign_dict[call] += 1
coord_dict[call] = loc2coords(locator)
return dist_dict, coord_dict, callsign_dict
def most_spotted(callsign_dict):
callsign_sorted_byspots = sorted(callsign_dict, key=callsign_dict.get, reverse=True)
return callsign_sorted_byspots
def data_by_callsign(callsign_sorted_byspots, d_dict):
data_bycallsign_dict = {}
for call in callsign_sorted_byspots:
data_bycallsign_dict[call] = {}
for reporter in d_dict:
tmp_dict = []
for el in d_dict[reporter]:
if call in el:
tmp_dict.append(el[0:1]+el[2:])
data_bycallsign_dict[call][reporter] = tmp_dict
return data_bycallsign_dict
def data_by_callsign_common(data_bycallsign_dict, reporter_list):
common_ts_bycall = {}
for call in data_bycallsign_dict.keys():
if sum([1 for rep in data_bycallsign_dict[call]]) == 2: # check whether callsign was heard by both reporters
timeseries_list = []
for rep in reporter_list:
ts_tmp = [el[0] for el in data_bycallsign_dict[call][rep]]
#ts_tmp = sorted(ts_tmp, key=lambda k: get_unixtime(k), reverse=False)
timeseries_list.append(ts_tmp)
common_ts = []
for i, ts1 in enumerate(timeseries_list[0]):
if ts1 in timeseries_list[1]:
j = timeseries_list[1].index(ts1)
common_ts.append((i,j))
common_ts_bycall[call] = common_ts
return common_ts_bycall
def get_unixtime(date_string):
date_time_obj = datetime.datetime.strptime(date_string, '%Y-%m-%d %H:%M')
unixtime = time.mktime(date_time_obj.timetuple())
return unixtime
def plot_avg_snr(data_bycallsign_dict, timestamp_start, timestamp_stop, reporter_list):
ax = plt.figure(figsize=(20,10))
for i, reporter in enumerate(reporter_list):
snr_list_ = []
for call in data_bycallsign_dict:
timeseries = data_bycallsign_dict[call][reporter]
timeseries = sorted(timeseries, key=lambda k: timeseries[0], reverse=True)
unixtime_list = [get_unixtime(el[0]) for el in timeseries]
snr_list = [el[2] for el in timeseries]
datetime_list = [datetime.datetime.fromtimestamp(ts) for ts in unixtime_list]
snr_list_.append( (unixtime_list, snr_list) )
avg_snr_dict = defaultdict(list)
for data in snr_list_:
ts_list = data[0]
snr_list__ = data[1]
for ts, snr in zip(ts_list, snr_list__):
avg_snr_dict[ts].append(snr)
x_list = []
y_list = []
for ts in avg_snr_dict:
x_list.append(ts)
y_list.append(np.mean(avg_snr_dict[ts]))
#plt.errorbar(datetime.datetime.fromtimestamp(ts), np.mean(avg_snr_dict[ts]),yerr=np.std(avg_snr_dict[ts]), c=["r", "k"][i])
plt.scatter(datetime.datetime.fromtimestamp(ts), np.mean(avg_snr_dict[ts]), c=["r", "k"][i])
(m, b) = np.polyfit(x_list, y_list, 1)
yp = np.polyval([m, b], x_list)
plt.plot([datetime.datetime.fromtimestamp(x) for x in x_list], yp, c=["r", "k"][i],
label="Linear fit of SNR trend for %s: %f dB/h"%(reporter, m*3600))
#plt.plot([datetime.datetime.fromtimestamp(x) for x in x_list], running_mean(y_list, 10), c=["r", "k"][i],
# label="SNR running AVG for %s"%(reporter), linestyle='dashed')
plt.grid()
plt.title("Time evolution of SNR for two antennas (AVG)")
plt.xlabel("Time")
plt.ylabel("SNR (dB)")
plt.xlim(datetime.datetime.fromtimestamp(timestamp_start), datetime.datetime.fromtimestamp(timestamp_stop))
plt.legend()
def get_snr_bycall(callsign_sorted_byspots, data_bycallsign_dict,
timestamp_start, timestamp_stop, antenna_rotation_time, topn=-1, plot_flag=False):
if plot_flag:
ax = plt.figure(figsize=(30,20))
snr_dict = {}
for call in callsign_sorted_byspots[:topn]:
snr_dict[call] = {}
for i, reporter in enumerate(data_bycallsign_dict[call]):
timeseries = data_bycallsign_dict[call][reporter]
timeseries = sorted(timeseries, key=lambda k: timeseries[0], reverse=True)
unixtime_list = [get_unixtime(el[0]) for el in timeseries]
snr_list = [el[2] for el in timeseries]
datetime_list = [datetime.datetime.fromtimestamp(ts) for ts in unixtime_list]
snr_dict[call][reporter] = (unixtime_list, snr_list)
if plot_flag:
plt.plot(datetime_list, snr_list, "-o", label=call+" "+reporter, alpha=0.6, c=["r", "k"][i])
#text(unixtime_list[0], snr_list[0], "%s"%call)
#legend()
if plot_flag:
#plt.axvline(timestamp_start, c="r")
#plt.axvline(antenna_rotation_time, c="b")
plt.grid()
plt.title("Time evolution of SNR for two antennas")
plt.xlabel("Time")
plt.ylabel("SNR (dB)")
plt.xlim(datetime.datetime.fromtimestamp(timestamp_start), datetime.datetime.fromtimestamp(timestamp_stop))
return snr_dict
def get_deltasnr_bycall(callsign_sorted_byspots, data_bycallsign_dict, dist_dict,
timestamp_start, timestamp_stop, antenna_rotation_time, reporter_list, common_ts_bycall, rx_offset, topn=-1, plot_flag=False, country=False):
if plot_flag:
ax = plt.figure(figsize=(26,20))
deltasnr_bycall_dict = {}
for call in callsign_sorted_byspots[:topn]:
timeseries = common_ts_bycall[call]
if len(timeseries) == 0:
continue
unixtime_list = [get_unixtime(data_bycallsign_dict[call][reporter_list[0]][el[0]][0]) for el in timeseries]
snr_list_0 = [data_bycallsign_dict[call][reporter_list[0]][el[0]][2] for el in timeseries]
snr_list_1 = [data_bycallsign_dict[call][reporter_list[1]][el[1]][2] for el in timeseries]
unixtime_list, snr_list_0, snr_list_1 = (list(t) for t in zip(*sorted(zip(unixtime_list, snr_list_0, snr_list_1), key=lambda k: k[0], reverse=False)))
datetime_list = [datetime.datetime.fromtimestamp(ts) for ts in unixtime_list]
deltasnr_list = [snr_1-snr_0-rx_offset for snr_0,snr_1 in zip(snr_list_0, snr_list_1)]
deltasnr_bycall_dict[call] = (unixtime_list, deltasnr_list)
if plot_flag:
cdist = dist_dict[call]
plt.plot(datetime_list, deltasnr_list, "-o", label=call+" "+reporter_list[0], c=cdist, cmap=plt.cm.plasma)
if not country:
plt.text(datetime_list[0], deltasnr_list[0]+0.5*random.random(), "%s"%call)
if len(unixtime_list)>1:
plt.text(datetime_list[-1], deltasnr_list[-1]+0.5*random.random(), "%s"%call)
else:
plt.text(datetime_list[0], deltasnr_list[0]+0.5*random.random(), Station(call).country)
if plot_flag:
#plt.axvline(timestamp_start, c="r")
#plt.axvline(antenna_rotation_time, c="b")
plt.grid()
plt.title("Time evolution of difference in SNR between two antennas")
plt.xlabel("Time")
plt.ylabel("Delta SNR (dB)")
plt.xlim(datetime.datetime.fromtimestamp(timestamp_start), datetime.datetime.fromtimestamp(timestamp_stop))
plt.ylim(-25, 25)
#plt.gcf().fmt_xdata = mdates.DateFormatter('%H-%M')
#plt.gcf().autofmt_xdate()
#legend()
return deltasnr_bycall_dict