-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessed_data.py
355 lines (302 loc) · 10.7 KB
/
processed_data.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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
import os
import numpy as np
import json
import pickle
import struct
from datetime import datetime
from collections import namedtuple
from pyproj import CRS, Transformer
compress = False
events = ["ac2021"] # ["acws2020", "prada2021", "ac2021"]
stats = {
"heading": "headingIntep",
"heel": "heelInterp",
"pitch": "pitchInterp",
"speed": "speedInterp",
"tws": "twsInterp",
"twd": "twdInterp",
"port foil": "leftFoilPosition",
"stbd foil": "rightFoilPosition",
"both foils": "both foils",
"vmg": "vmg",
"cvmg": "cvmg",
"twa": "twa",
"twa_abs": "twa_abs",
"vmg/tws": "tws/vmg",
}
Packets = namedtuple(
"packet_ids",
"boat penalty course_info course_boundary wind windpoint buoy roundingtime audio stats",
)
Course_info = namedtuple(
"course_Info",
"raceId startTime numLegs courseAngle raceStatus boattype liveDelaySecs",
)
packets_id = Packets(179, 182, 177, 185, 178, 190, 181, 180, 186, 183)
with open("raw/yt_videos.json", "rb") as f:
yt_videos = json.load(f)
with open("raw/yt_video_offsets.json", "rb") as f:
yt_offsets = json.load(f)
def read_videos(date):
selected = dict()
for yt_title, v in yt_videos.items():
yt_id = v["contentDetails"]["videoId"]
yt_date = v["contentDetails"]["videoPublishedAt"]
yt_date = yt_date.replace("Z", "+00:00")
yt_date = datetime.fromisoformat(yt_date)
if yt_date.date() == date.date():
if yt_id not in yt_offsets:
continue
if "Port Entry" in yt_title:
selected["PRT"] = v["contentDetails"]
selected["PRT"]["offset"] = yt_offsets[yt_id]
elif "Starboard Entry" in yt_title:
selected["STBD"] = v["contentDetails"]
selected["STBD"]["offset"] = yt_offsets[yt_id]
elif (
"Full Race" in yt_title
or "The 36th America’s Cup Presented by PRADA | 🔴 LIVE Day" in yt_title
or "🔴 LIVE Day" in yt_title
or "🔴LIVE Day" in yt_title
and not "Eye" in yt_title
):
selected["TV"] = v["contentDetails"]
selected["TV"]["offset"] = yt_offsets[yt_id]
return selected
def read_packets(path):
with open(path, "rb") as f:
b = bytearray(f.read()).split(b"\x10\x03\x10")
data = {i: [] for i in packets_id}
packets = []
for i, p in enumerate(b):
packets.append(p)
if p[0] == packets_id.course_info:
data[p[0]].append(read_course_info(p))
# elif p[0] == packets_id.course_boundary:
# data[p[0]].append(read_course_boundary(p))
return data, packets
def get_course_info(path):
for file in os.listdir(f"raw/{path}"):
if file.endswith(".bin"):
path_bin = f"raw/{path}/{file}"
data, packets = read_packets(path_bin)
d = data[packets_id.course_info][-1]
return dict(zip(d._fields, d))
def read_course_info(p):
p_id = p[0]
n = p[1]
p = p[2:]
return Course_info(*struct.unpack("!HIBHBBB", p))
def read_course_boundary(p):
data = struct.unpack(f"!BBHIB{p[8]*2}I", p)
# 16 repeats increasing the n of bytes
points = [(i - 2147483648) / 1e7 for i in data[5:]]
points = zip(points[0::2], points[1::2])
# data = Course_boundary(*data[2:5], tuple(points))
return data
def read_events(events):
events_data = dict().fromkeys(events)
for event in events:
print(f"Reading event {event}")
if event in os.listdir("raw"):
events_data[event] = read_races(event)
return events_data
def read_races(event):
races = dict()
for i in os.listdir(f"raw/{event}"):
print(f"Reading race {i}")
if "RacesList" in i:
continue
path = f"{event}/{i}"
if "stats.json" not in os.listdir(f"raw/{path}"):
raise FileNotFoundError(f"{path}/stats.json not found")
races[str(i)] = read_race(i, event)
return races
def read_race(i, event):
path = f"{event}/{i}"
with open(f"raw/{path}/stats.json", "rb") as f:
race = json.load(f)
boats = read_boats(race, f"raw/{path}")
race["course_info"] = get_course_info(path)
date = datetime.fromtimestamp(race["course_info"]["startTime"])
race["course_info"]["startTime"] = date.hour * 3600 + date.minute * 60 + date.second
race["yt_videos"] = read_videos(date)
race_no = int(i)
def get_dt(race_no, this_date, dt=0):
if race_no == 1:
return 0
elif os.path.exists(f"raw/{event}/{race_no-1}"):
prev_race = get_course_info(f"{event}/{race_no-1}")
prev_date = datetime.fromtimestamp(prev_race["startTime"])
if prev_date.date() == this_date.date():
dt += this_date.timestamp() - prev_date.timestamp()
dt += get_dt(race_no - 1, prev_date)
return dt
else:
return 0
else:
return 0
dt = get_dt(race_no, date)
print(f"race {i}, dt {dt}")
for k in race["yt_videos"]:
race["yt_videos"][k]["offset"] += dt
if compress:
b1 = interpolate_boat(boats[0], race)
b2 = interpolate_boat(boats[1], race)
save_stats(race, path)
save_boats((b1, b2), path)
return race
def read_boats(race, path):
print("Reading boats")
if not "boat1.json" in os.listdir(path) and not "boat2.json" in os.listdir(path):
raise FileNotFoundError
with open(f"{path}/boat1.json", "rb") as f:
boat1 = json.load(f)
with open(f"{path}/boat2.json", "rb") as f:
boat2 = json.load(f)
return boat1, boat2
def interpolate_boat(boat_data, race):
boat = dict()
x = stat("heading", boat_data, race)
x = x["x"]
x = np.linspace(x[0], x[-1], int(x[-1] - x[0]) * 2)
for s in stats:
data = stat(s, boat_data, race)
y = np.interp(x, data["x"], data["y"])
boat[s] = y
# ----coordinates----
# UTM zone 60S
crs_utm = CRS.from_epsg(27260)
# Web mercator
crs_wm = CRS.from_epsg(3857)
tr = Transformer.from_crs(crs_utm, crs_wm)
lon_y_raw = [i[0] for i in boat_data["coordIntep"]["xCerp"]["valHistory"]]
lat_y_raw = [i[0] for i in boat_data["coordIntep"]["yCerp"]["valHistory"]]
boat_data["lon"] = []
boat_data["lat"] = []
boat_data["lonx"] = [i[1] for i in boat_data["coordIntep"]["xCerp"]["valHistory"]]
boat_data["latx"] = [i[1] for i in boat_data["coordIntep"]["yCerp"]["valHistory"]]
for i, (lon, lat) in enumerate(zip(lon_y_raw, lat_y_raw)):
lon, lat = tr.transform(lon, lat)
boat_data["lon"].append(lon)
boat_data["lat"].append(lat)
boat["lon"] = np.interp(x, boat_data["lonx"], boat_data["lon"])
boat["lat"] = np.interp(x, boat_data["latx"], boat_data["lat"])
# ----end of coord interp-----
race_start = boat_data["legInterp"]["valHistory"][1][1] * 1000
boat["legs"] = np.array([i[1] for i in boat_data["legInterp"]["valHistory"]])
boat["legs"] = np.array(boat["legs"] * 1000, dtype="timedelta64[ms]")
boat["x"] = np.array(x * 1000, dtype="timedelta64[ms]")
color, name = get_boat_info(boat_data, race)
boat["color"] = color
boat["name"] = name
return boat
def save_boats(boats, path):
path = "ac36data/" + path
# if path not in os.listdir():
try:
os.mkdir(path)
except FileExistsError:
pass
with open(f"{path}/boats.bin", "wb") as f:
for b in boats:
pickle.dump(b, f)
def save_stats(race, path):
path = "ac36data/" + path
try:
os.mkdir(path)
except FileExistsError:
pass
with open(f"{path}/stats.bin", "wb") as f:
pickle.dump(race, f)
# --------Returning individual properties--------
def get_twa(boat, race):
twd = stat("twd", boat, race)
cog = stat("heading", boat, race)
x = cog["x"]
x = np.linspace(x[0], x[-1], int(x[-1] - x[0]))
twd = np.interp(x, twd["x"], twd["y"])
cog = np.interp(x, cog["x"], cog["y"])
y = twd - cog
y[y < 0] += 360
y[y > 180] -= 360
return dict(x=x, y=y)
def get_twa_abs(boat, race):
d = get_twa(boat, race)
x = np.abs(d["x"])
y = np.abs(d["y"])
return dict(x=x, y=y)
def get_vmg(boat, race):
twa = get_twa_abs(boat, race)
sog = stat("speed", boat, race)
x = twa["x"]
x = np.linspace(x[0], x[-1], len(x))
sog = np.interp(x, sog["x"], sog["y"])
twa = twa["y"]
y = np.cos(np.deg2rad(twa)) * sog
legs = boat["legInterp"]["valHistory"]
for i, leg in enumerate(legs[1:], 1):
if not leg[0] % 2 and i != len(legs) - 1:
mask = np.logical_and(x < legs[i + 1][1], x > legs[i][1])
y[mask] *= -1
return dict(x=x, y=y)
def get_cvmg(boat, race):
# VMG to course axis
course_axis = race["course_info"]["courseAngle"]
heading = stat("heading", boat, race)
heading_to_course = course_axis - heading["y"]
sog = stat("speed", boat, race)
y = np.cos(np.deg2rad(heading_to_course)) * sog["y"]
x = sog["x"]
legs = boat["legInterp"]["valHistory"]
for i, leg in enumerate(legs[1:], 1):
if not leg[0] % 2 and i != len(legs) - 1:
mask = np.logical_and(x < legs[i + 1][1], x > legs[i][1])
y[mask] *= -1
return dict(x=x, y=y)
def get_both_foils(boat, race):
stbd = stat("stbd foil", boat, race)
port = stat("port foil", boat, race)
x = np.concatenate((stbd["x"], port["x"]))
y = np.concatenate((stbd["y"], port["y"]))
return dict(x=x, y=y)
def get_vmgtws(boat, race):
vmg = get_vmg(boat, race)
tws = stat("tws", boat, race)
tws = np.interp(vmg["x"], tws["x"], tws["y"])
return dict(x=vmg["x"], y=vmg["y"] / tws)
def get_datetime(dic, boat, race):
race_start = boat["legInterp"]["valHistory"][1][1]
x = dic["x"]
x = np.array(x - race_start, dtype="timedelta64[s]")
return dict(x=x, y=dic["y"])
funcs = {
"vmg": get_vmg,
"cvmg": get_cvmg,
"twa": get_twa,
"twa_abs": get_twa_abs,
"both foils": get_both_foils,
"vmg/tws": get_vmgtws,
}
def stat(key, boat, race):
if key in funcs:
return funcs[key](boat, race)
unit = 1
if key == "tws":
unit = 1.94384
s = boat[stats[key]]["valHistory"]
s = np.array(s)
x = s[:, 1]
y = s[:, 0] * unit
return dict(x=x, y=y)
def get_boat_info(b, race):
if str(b["teamId"]) == race["LegStats"][0]["Boat"][0]["TeamID"]:
c = race["LegStats"][0]["Boat"][0]["TeamColour"]
n = race["LegStats"][0]["Boat"][0]["Country"]
else:
c = race["LegStats"][0]["Boat"][1]["TeamColour"]
n = race["LegStats"][0]["Boat"][1]["Country"]
return c, n
if __name__ == "__main__":
compress = True
read_events(events)