-
Notifications
You must be signed in to change notification settings - Fork 0
/
tracker_chains.py
170 lines (106 loc) · 4.29 KB
/
tracker_chains.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
#!/usr/bin/python3
import util.common as common
from scipy.interpolate import interp1d
import bisect
import multiprocessing
def find_closest_value(alist, x):
i = bisect.bisect_left(alist, x)
if i >= len(alist):
i = len(alist) - 1
elif i and alist[i] - x > x - alist[i - 1]:
i = i - 1
return alist[i] # i
class TargetDataChain:
def __init__(self, acad):
self.acad = acad
self.indexes = []
self.tod_begin = None
self.tod_end = None
self.pos_tods = []
self.positions = []
self.f_pos_lin = None
self.f_pos_quad = None
self.f_pos_cub = None
self.usable = False
def add_index(self, index):
self.indexes.append(index)
def finalize(self):
for index in self.indexes:
assert common.cat062_df["aircraft_address"].iloc[index] == self.acad
# same time issue
if len(self.pos_tods) and common.cat062_df["time_of_day"].iloc[index] == self.pos_tods[-1]:
continue
self.pos_tods.append(common.cat062_df["time_of_day"].iloc[index])
self.positions.append([common.cat062_df["latitude"].iloc[index],
common.cat062_df["longitude"].iloc[index]])
if len(self.pos_tods):
self.tod_begin = self.pos_tods[0]
self.tod_end = self.pos_tods[-1]
if len(self.pos_tods) > 3:
self.f_pos_lin = interp1d(self.pos_tods, self.positions, kind='linear', axis=0)
self.f_pos_quad = interp1d(self.pos_tods, self.positions, kind='quadratic', axis=0)
self.f_pos_cub = interp1d(self.pos_tods, self.positions, kind='cubic', axis=0)
self.usable = True
return self
def exists_at(self, tod, max_delta):
if not self.usable or not self.tod_begin or not self.tod_end:
#print("nope2 usable {}".format(self.usable))
return False
if tod < self.tod_begin or tod > self.tod_end:
#print("nope3")
return False
#print("nope4?")
return abs(find_closest_value(self.pos_tods, tod) - tod) < max_delta
def get_position_at(self, tod):
#assert self.exists_at(tod)
#assert self.f_pos_lin is not None
#data_new2 = self.f_pos_lin(tod)
#assert self.f_pos_quad is not None
#data_new3 = self.f_pos_quad(tod)
data_new4 = self.f_pos_cub(tod)
lat = data_new4[0]
lon = data_new4[1]
# print('tod {} lat {} lon {}'.format(tod, lat, lon))
return lat, lon
class TrackerChains:
def __init__(self):
self.chains = {} # type: Dict[int, TargetDataChain]
def load_data(self):
no_acad_cnt = 0
tmp_chains = {}
for index in range(common.cat062_df.shape[0]):
if index % 200000 == 0:
print('creating tracker chains {}'.format(index))
# data bins
acad = common.cat062_df["aircraft_address"].iloc[index]
if acad:
if acad not in tmp_chains:
tmp_chains[acad] = TargetDataChain(acad)
tmp_chains[acad].add_index(index)
else:
no_acad_cnt += 1
print('creating {} radar chains done, {} of {} trs skipped'.format(
len(tmp_chains), no_acad_cnt, common.cat062_df.shape[0]))
print("finalizing chains")
tmp_chain_vals = tmp_chains.values()
pool = multiprocessing.Pool() # initialise your pool
finalized_chains = pool.map(TargetDataChain.finalize, tmp_chain_vals)
pool.close() # shut down the pool
pool.join()
num_usable = 0
for chain in finalized_chains:
self.chains[chain.acad] = chain
if self.chains[chain.acad].usable:
num_usable += 1
print("finalizing {} chains done, usable {}".format(len(self.chains), num_usable))
def has_position_at(self, acad, tod, time_delta):
if acad not in self.chains:
return False
return self.chains[acad].exists_at(tod, time_delta)
def get_position_at(self, acad, tod):
assert acad in self.chains
return self.chains[acad].get_position_at(tod)
def create():
chains = TrackerChains()
chains.load_data()
return chains