forked from USC-NSL/ripe-atlas
-
Notifications
You must be signed in to change notification settings - Fork 0
/
probe_filter.py
executable file
·83 lines (67 loc) · 2.52 KB
/
probe_filter.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
#!/usr/bin/python
import sys
import fetch_active
class Filter(object):
def __init__(self, probe_list):
self.probe_list = probe_list
def within(self, lat, lon, max_dist):
import hmvp
points = [] # [(probe['latitude'], probe['longitude']) for probe in self.probe_list]
good_probes = [] #terrible name. fix me please
for probe in self.probe_list:
try:
point = (probe['latitude'], probe['longitude'])
points.append(point)
good_probes.append(probe)
except:
continue
if len(points) != len(good_probes):
raise Exception('Number of points is not same as number of probes')
filtered = hmvp.distance.within((lat,lon), points, good_probes, max_dist)
#filtered is a list of (distance, probe) tuples so just extract the probes
return zip(*filtered)[1]
def separated_by(self, distance):
import hmvp
asn_probes = {}
#separate probes by asn
for probe in self.probe_list:
try:
try:
asn = probe['asn_v4']
except:
continue
asn_probes[asn].append(probe)
except KeyError:
asn_probes[asn] = [probe]
"""
For each set of probes in each ASN,
"""
filtered_probes = []
for asn, probe_list in asn_probes.items():
points = [(p['latitude'], p['longitude']) for p in probe_list]
filtered = hmvp.distance.dist_filter(points, probe_list, distance)
filtered_probes.extend(filtered)
return filtered_probes
if __name__ == '__main__':
if len(sys.argv) != 3:
sys.stderr.write('Usage: probe-file max-dist\n')
sys.exit(1)
input_file = sys.argv[1]
max_dist = float(sys.argv[2])
probe_list = fetch_active.load(input_file)
asn_dict = {} #organize by asn
for probe in probe_list:
try:
asn = probe['asn_v4']
if asn != None:
try:
asn_dict[asn].append(probe)
except:
asn_dict[asn] = [probe]
except:
pass
for probe_sub_list in asn_dict.values():
probe_filter = Filter(probe_sub_list)
filtered_probes = probe_filter.separated_by(max_dist)
lines = fetch_active.json2tab(filtered_probes)
print('\n'.join(lines))