forked from flightaware/dump1090
-
Notifications
You must be signed in to change notification settings - Fork 1
/
fuzzy-30003-matcher.py
executable file
·204 lines (172 loc) · 6.28 KB
/
fuzzy-30003-matcher.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
#!/usr/bin/env python3
#
# Regression testing helper: takes a 3.0.5 port-30003 output file
# and a 3.1.0 port-30003 output file and generates a diff, after
# dealing with the known formatting / data differences
import csv
from contextlib import closing
horizon=5
def fuzzy_match_details(l1, l2):
_, _, type1, _, _, addr1, _, _, _, _, _, cs1, alt1, gs1, hdg1, lat1, lon1, vr1, sq1, change1, emerg1, spi1, aog1 = l1
_, _, type2, _, _, addr2, _, _, _, _, _, cs2, alt2, gs2, hdg2, lat2, lon2, vr2, sq2, change2, emerg2, spi2, aog2 = l2
if addr1 != addr2:
return (False, 'adr')
if type1 != type2:
# 3.0.5: reports DF17 surface/airborne with no position as type 7
# 3.1.0: reports DF17 surface/airborne with no position as type 2/3
if type1 != '7':
return (False, 'typ')
if type2 != '2' and type2 != '3':
return (False, 'typ')
if lat1 != '' or lon1 != '':
return (False, 'typ')
if alt1 != alt2:
# 3.0.5: omits altitude in DF17 if no position was decoded
# 3.1.0: includes it
if type1 != '7' or alt1 != '' or alt2 == '':
return (False, 'alt')
if gs1 != gs2:
# 3.0.5: truncates computed GS
# 3.1.0: rounds computed GS
if gs1 == '' or gs2 == '' or abs(int(gs1) - int(gs2)) > 1:
return (False, 'gs ')
if hdg1 != hdg2:
# 3.0.5: truncates computed heading
# 3.1.0: rounds computed heading
if hdg1 == '' or hdg2 == '':
return (False, 'hdg')
delta = abs(int(hdg1) - int(hdg2))
if delta > 180:
delta = 360 - delta
if delta > 1:
return False
if lat1 != lat2:
return (False, 'lat')
if lon1 != lon2:
return (False, 'lon')
if vr1 != vr2:
return (False, 'vr ')
if sq1 != sq2:
# 3.0.5: strips leading zeros
# 3.1.0: preserves leading zeros
if ('0' + sq1) != sq2 and ('00' + sq1) != sq2 and ('000' + sq1) != sq2:
return (False, 'sqk')
# 3.1.0: only reports these when available
if change1 != change2:
if change1 != '0' or change2 != '':
return (False, 'chg')
if emerg1 != emerg2:
if emerg1 != '0' or emerg2 != '':
return (False, 'emg')
if spi1 != spi2:
if spi1 != '0' or spi2 != '':
return (False, 'spi')
if aog1 != aog2:
# 3.1.0: different rules for when AOG is reported
if aog1 != '' and aog2 != '':
return (False, 'aog')
return (True, None)
def fuzzy_match(l1, l2):
return fuzzy_match_details(l1, l2)[0]
def fuzzy_match_reason(l1, l2):
return fuzzy_match_details(l1, l2)[1]
def next_line(reader, queue):
if queue:
return queue.pop()
line = next(reader, None)
if line is None or len(line) == 0:
return None
else:
return [reader.line_num] + line
def unpush_line(queue, line):
queue.insert(0, line)
def csv_diff(path1, path2):
diffs = []
q1 = []
q2 = []
with closing(open(path1, 'r')) as f1, closing(open(path2, 'r')) as f2:
r1 = csv.reader(f1)
r2 = csv.reader(f2)
l1 = next_line(r1, q1)
l2 = next_line(r2, q2)
while (l1 is not None) or (l2 is not None):
if l1 is None:
yield ('+', None, l2)
l2 = next_line(r2, q2)
continue
if l2 is None:
yield ('-', l1, None)
l1 = next_line(r1, q1)
continue
if fuzzy_match(l1, l2):
yield (' ', l1, l2)
l1 = next_line(r1, q1)
l2 = next_line(r2, q2)
continue
#print('mismatch:', l1, l2)
save_1 = []
save_2 = []
found = False
for i in range(horizon):
next_l2 = next_line(r2, q2)
if next_l2 is not None:
if fuzzy_match(l1, next_l2):
# skip l2 and any lines in save_2
# continue with l1 and next_l2
yield('+', None, l2)
for l in save_2:
yield('+', None, l)
l2 = next_l2
q1.extend(reversed(save_1))
found = True
break
else:
save_2.append(next_l2)
next_l1 = next_line(r1, q1)
if next_l1 is not None:
if fuzzy_match(next_l1, l2):
# skip l1 and any lines in save_1
# continue with next_l1 and l2
yield('-', l1, None)
for l in save_1:
yield('-', l, None)
l1 = next_l1
q2.extend(reversed(save_2))
found = True
break
else:
save_1.append(next_l1)
if found:
#print('new l1:', l1)
#print('new l2:', l2)
#print('new q1:')
#for q in q1: print(q)
#print('new q2:')
#for q in q2: print(q)
continue
#print('lookahead: nothing likely')
q1.extend(reversed(save_1))
q2.extend(reversed(save_2))
yield ('*', l1, l2)
l1 = next_line(r1, q1)
l2 = next_line(r1, q2)
def format_line(line):
line_num = line[0]
subrow = line[1:3] + line[5:6] + line[11:]
return str(line_num) + ': ' + ','.join(subrow)
if __name__ == '__main__':
import sys
for action, old, new in csv_diff(sys.argv[1], sys.argv[2]):
if action == ' ':
if False: print (' ' + format_line(new))
elif action == '*':
reason = fuzzy_match_reason(old, new)
print ('< ' + reason + ' ' + format_line(old))
print ('> ' + reason + ' ' + format_line(new))
elif action == '-':
# 3.0.5: emits lines for all-zero messages
# 3.1.0: doesn't
if old[5] != '000000':
print ('- ' + format_line(old))
elif action == '+':
print ('+ ' + format_line(new))