-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathldparser.py
480 lines (386 loc) · 16.1 KB
/
ldparser.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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
""" Parser for MoTec ld files
Code created through reverse engineering the data format.
"""
import datetime
import struct
import numpy as np
class ldData(object):
"""Container for parsed data of an ld file.
Allows reading and writing.
"""
def __init__(self, head, channs):
self.head = head
self.channs = channs
def __getitem__(self, item):
if not isinstance(item, int):
col = [n for n, x in enumerate(self.channs) if x.name == item]
if len(col) != 1:
raise Exception("Could get column", item, col)
item = col[0]
return self.channs[item]
def __iter__(self):
return iter([x.name for x in self.channs])
@classmethod
def frompd(cls, df):
# type: (pd.DataFrame) -> ldData
"""Create and ldData object from a pandas DataFrame.
Example:
import pandas as pd
import numpy as np
from ldparser import ldData
# create test dataframe
df = pd.DataFrame(np.random.randn(6,4),columns=list('ABCD'))
print(df)
# create an lddata object from the dataframe
l = ldData.frompd(df)
# write an .ld file
l.write('/tmp/test.ld')
# just to check, read back the file
l = ldData.fromfile('/tmp/test.ld')
# create pandas dataframe
df = pd.DataFrame(data={c: l[c].data for c in l})
print(df)
"""
# for now, fix datatype and frequency
freq, dtype = 10, np.float32
# pointer to meta data of first channel
meta_ptr = struct.calcsize(ldHead.fmt) + struct.calcsize(ldEvent.fmt)
# list of columns to read - only accept numeric data
cols = [c for c in df.columns if np.issubdtype(df[c].dtype, np.number)]
# pointer to data of first channel
chanheadsize = struct.calcsize(ldChan.fmt)
data_ptr = meta_ptr + len(cols) * chanheadsize
event_ptr = struct.calcsize(ldHead.fmt)
event = ldEvent("testevent", "0", "testcomment", 0, None)
# create a mocked header
head = ldHead(meta_ptr, data_ptr, event_ptr, event,
"testdriver", "testvehicleid", "testvenue",
datetime.datetime.now(),
"just a test")
# create the channels, meta data and associated data
channs, prev, next = [], 0, meta_ptr + chanheadsize
for n, col in enumerate(cols):
# create mocked channel header
chan = ldChan(None,
meta_ptr, prev, next if n < len(cols)-1 else 0,
data_ptr, len(df[col]),
dtype, freq, 0, 1, 1, 0,
col, col, "m")
# link data to the channel
chan._data = df[col].to_numpy(dtype)
# calculate pointers to the previous/next channel meta data
prev = meta_ptr
meta_ptr = next
next += chanheadsize
# increment data pointer for next channel
data_ptr += chan._data.nbytes
channs.append(chan)
return cls(head, channs)
@classmethod
def fromfile(cls, f):
# type: (str) -> ldData
"""Parse data of an ld file
"""
return cls(*read_ldfile(f))
def write(self, f):
# type: (str) -> ()
"""Write an ld file containing the current header information and channel data
"""
# convert the data using scale/shift etc before writing the data
conv_data = lambda c: ((c.data / c.mul) - c.shift) * c.scale / pow(10., -c.dec)
with open(f, 'wb') as f_:
self.head.write(f_, len(self.channs))
f_.seek(self.channs[0].meta_ptr)
list(map(lambda c: c[1].write(f_, c[0]), enumerate(self.channs)))
list(map(lambda c: f_.write(conv_data(c).astype(c.dtype)), self.channs))
class ldEvent(object):
fmt = '<64s64s1024sH'
def __init__(self, name, session, comment, venue_ptr, venue):
self.name, self.session, self.comment, self.venue_ptr, self.venue = \
name, session, comment, venue_ptr, venue
@classmethod
def fromfile(cls, f):
# type: (file) -> ldEvent
"""Parses and stores the event information in an ld file
"""
name, session, comment, venue_ptr = struct.unpack(
ldEvent.fmt, f.read(struct.calcsize(ldEvent.fmt)))
name, session, comment = map(decode_string, [name, session, comment])
venue = None
if venue_ptr > 0:
f.seek(venue_ptr)
venue = ldVenue.fromfile(f)
return cls(name, session, comment, venue_ptr, venue)
def write(self, f):
f.write(struct.pack(ldEvent.fmt,
self.name.encode(),
self.session.encode(),
self.comment.encode(),
self.venue_ptr))
if self.venue_ptr > 0:
f.seek(self.venue_ptr)
self.venue.write(f)
def __str__(self):
return "%s; venue: %s"%(self.name, self.venue)
class ldVenue(object):
fmt = '<64s1034xH'
def __init__(self, name, vehicle_ptr, vehicle):
self.name, self.vehicle_ptr, self.vehicle = name, vehicle_ptr, vehicle
@classmethod
def fromfile(cls, f):
# type: (file) -> ldVenue
"""Parses and stores the venue information in an ld file
"""
name, vehicle_ptr = struct.unpack(ldVenue.fmt, f.read(struct.calcsize(ldVenue.fmt)))
vehicle = None
if vehicle_ptr > 0:
f.seek(vehicle_ptr)
vehicle = ldVehicle.fromfile(f)
return cls(decode_string(name), vehicle_ptr, vehicle)
def write(self, f):
f.write(struct.pack(ldVenue.fmt, self.name.encode(), self.vehicle_ptr))
if self.vehicle_ptr > 0:
f.seek(self.vehicle_ptr)
self.vehicle.write(f)
def __str__(self):
return "%s; vehicle: %s"%(self.name, self.vehicle)
class ldVehicle(object):
fmt = '<64s128xI32s32s'
def __init__(self, id, weight, type, comment):
self.id, self.weight, self.type, self.comment = id, weight, type, comment
@classmethod
def fromfile(cls, f):
# type: (file) -> ldVehicle
"""Parses and stores the vehicle information in an ld file
"""
id, weight, type, comment = struct.unpack(ldVehicle.fmt, f.read(struct.calcsize(ldVehicle.fmt)))
id, type, comment = map(decode_string, [id, type, comment])
return cls(id, weight, type, comment)
def write(self, f):
f.write(struct.pack(ldVehicle.fmt, self.id.encode(), self.weight, self.type.encode(), self.comment.encode()))
def __str__(self):
return "%s (type: %s, weight: %i, %s)"%(self.id, self.type, self.weight, self.comment)
class ldHead(object):
fmt = '<' + (
"I4x" # ldmarker
"II" # chann_meta_ptr chann_data_ptr
"20x" # ??
"I" # event_ptr
"24x" # ??
"HHH" # unknown static (?) numbers
"I" # device serial
"8s" # device type
"H" # device version
"H" # unknown static (?) number
"I" # num_channs
"4x" # ??
"16s" # date
"16x" # ??
"16s" # time
"16x" # ??
"64s" # driver
"64s" # vehicleid
"64x" # ??
"64s" # venue
"64x" # ??
"1024x" # ??
"I" # enable "pro logging" (some magic number?)
"66x" # ??
"64s" # short comment
"126x" # ??
)
def __init__(self, meta_ptr, data_ptr, event_ptr, event, driver, vehicleid, venue, datetime, short_comment):
self.meta_ptr, self.data_ptr, self.event_ptr, self.event, self.driver, self.vehicleid, \
self.venue, self.datetime, self.short_comment = meta_ptr, data_ptr, event_ptr, event, \
driver, vehicleid, venue, datetime, short_comment
@classmethod
def fromfile(cls, f):
# type: (file) -> ldHead
"""Parses and stores the header information of an ld file
"""
(_, meta_ptr, data_ptr, event_ptr,
_, _, _,
_, _, _, _, n,
date, time,
driver, vehicleid, venue,
_, short_comment) = struct.unpack(ldHead.fmt, f.read(struct.calcsize(ldHead.fmt)))
date, time, driver, vehicleid, venue, short_comment = \
map(decode_string, [date, time, driver, vehicleid, venue, short_comment])
try:
# first, try to decode datatime with seconds
_datetime = datetime.datetime.strptime(
'%s %s'%(date, time), '%d/%m/%Y %H:%M:%S')
except ValueError:
_datetime = datetime.datetime.strptime(
'%s %s'%(date, time), '%d/%m/%Y %H:%M')
event = None
if event_ptr > 0:
f.seek(event_ptr)
event = ldEvent.fromfile(f)
return cls(meta_ptr, data_ptr, event_ptr, event, driver, vehicleid, venue, _datetime, short_comment)
def write(self, f, n):
f.write(struct.pack(ldHead.fmt,
0x40,
self.meta_ptr, self.data_ptr, self.event_ptr,
1, 0x4240, 0xf,
0x1f44, "ADL".encode(), 420, 0xadb0, n,
self.datetime.date().strftime("%d/%m/%Y").encode(),
self.datetime.time().strftime("%H:%M:%S").encode(),
self.driver.encode(), self.vehicleid.encode(), self.venue.encode(),
0xc81a4, self.short_comment.encode()
))
if self.event_ptr > 0:
f.seek(self.event_ptr)
self.event.write(f)
def __str__(self):
return 'driver: %s\n' \
'vehicleid: %s\n' \
'venue: %s\n' \
'event: %s\n' \
'session: %s\n' \
'short_comment: %s'%(
self.driver, self.vehicleid, self.venue, self.event.name, self.event.session, self.short_comment)
class ldChan(object):
"""Channel (meta) data
Parses and stores the channel meta data of a channel in a ld file.
Needs the pointer to the channel meta block in the ld file.
The actual data is read on demand using the 'data' property.
"""
fmt = '<' + (
"IIII" # prev_addr next_addr data_ptr n_data
"H" # some counter?
"HHH" # datatype datatype rec_freq
"hhhh" # shift mul scale dec_places
"32s" # name
"8s" # short name
"12s" # unit
"40x" # ? (40 bytes for ACC, 32 bytes for acti)
)
def __init__(self, _f, meta_ptr, prev_meta_ptr, next_meta_ptr, data_ptr, data_len,
dtype, freq, shift, mul, scale, dec,
name, short_name, unit):
self._f = _f
self.meta_ptr = meta_ptr
self._data = None
(self.prev_meta_ptr, self.next_meta_ptr, self.data_ptr, self.data_len,
self.dtype, self.freq,
self.shift, self.mul, self.scale, self.dec,
self.name, self.short_name, self.unit) = prev_meta_ptr, next_meta_ptr, data_ptr, data_len,\
dtype, freq,\
shift, mul, scale, dec,\
name, short_name, unit
@classmethod
def fromfile(cls, _f, meta_ptr):
# type: (str, int) -> ldChan
"""Parses and stores the header information of an ld channel in a ld file
"""
with open(_f, 'rb') as f:
f.seek(meta_ptr)
(prev_meta_ptr, next_meta_ptr, data_ptr, data_len, _,
dtype_a, dtype, freq, shift, mul, scale, dec,
name, short_name, unit) = struct.unpack(ldChan.fmt, f.read(struct.calcsize(ldChan.fmt)))
name, short_name, unit = map(decode_string, [name, short_name, unit])
def safe_get(lst, idx):
if idx < 0 or idx >= len(lst):
return None
return lst[idx]
if dtype_a in [0x07]:
dtype = safe_get([None, np.float16, None, np.float32], dtype - 1)
elif dtype_a in [0, 0x03, 0x05]:
dtype = safe_get([None, np.int16, None, np.int32], dtype - 1)
else:
dtype = None
return cls(_f, meta_ptr, prev_meta_ptr, next_meta_ptr, data_ptr, data_len,
dtype, freq, shift, mul, scale, dec,name, short_name, unit)
def write(self, f, n):
if self.dtype == np.float16 or self.dtype == np.float32:
dtype_a = 0x07
dtype = {np.float16: 2, np.float32: 4}[self.dtype]
else:
dtype_a = 0x05 if self.dtype == np.int32 else 0x03
dtype = {np.int16: 2, np.int32: 4}[self.dtype]
f.write(struct.pack(ldChan.fmt,
self.prev_meta_ptr, self.next_meta_ptr, self.data_ptr, self.data_len,
0x2ee1+n, dtype_a, dtype, self.freq, self.shift, self.mul, self.scale, self.dec,
self.name.encode(), self.short_name.encode(), self.unit.encode()))
@property
def data(self):
# type: () -> np.array
""" Read the data words of the channel
"""
if self.dtype is None:
raise ValueError(f'Channel {self.name} has unknown data type')
if self._data is None:
# jump to data and read
with open(self._f, 'rb') as f:
f.seek(self.data_ptr)
try:
self._data = np.fromfile(f,
count=self.data_len, dtype=self.dtype)
self._data = (self._data/self.scale * pow(10., -self.dec) + self.shift) * self.mul
if len(self._data) != self.data_len:
raise ValueError("Not all data read!")
except ValueError as v:
print(v, self.name, self.freq,
hex(self.data_ptr), hex(self.data_len),
hex(len(self._data)),hex(f.tell()))
# raise v
return self._data
def __str__(self):
return 'chan %s (%s) [%s], %i Hz'%(
self.name,
self.short_name, self.unit,
self.freq)
def decode_string(bytes):
# type: (bytes) -> str
"""decode the bytes and remove trailing zeros
"""
try:
return bytes.decode('ascii').strip().rstrip('\0').strip()
except Exception as e:
print("Could not decode string: %s - %s"%(e, bytes))
return ""
# raise e
def read_channels(f_, meta_ptr):
# type: (str, int) -> list
""" Read channel data inside ld file
Cycles through the channels inside an ld file,
starting with the one where meta_ptr points to.
Returns a list of ldchan objects.
"""
chans = []
while meta_ptr:
chan_ = ldChan.fromfile(f_, meta_ptr)
chans.append(chan_)
meta_ptr = chan_.next_meta_ptr
return chans
def read_ldfile(f_):
# type: (str) -> (ldHead, list)
""" Read an ld file, return header and list of channels
"""
head_ = ldHead.fromfile(open(f_,'rb'))
chans = read_channels(f_, head_.meta_ptr)
return head_, chans
if __name__ == '__main__':
""" Small test of the parser.
Decodes all ld files in the directory. For each file, creates
a plot for data with the same sample frequency.
"""
import sys, os, glob
from itertools import groupby
import pandas as pd
import matplotlib.pyplot as plt
if len(sys.argv)!=2:
print("Usage: ldparser.py /some/path/")
exit(1)
for f in glob.glob('%s/*.ld'%sys.argv[1]):
print(os.path.basename(f))
l = ldData.fromfile(f)
print(l.head)
print(list(map(str, l)))
print()
# create plots for all channels with the same frequency
for f, g in groupby(l.channs, lambda x:x.freq):
df = pd.DataFrame({i.name.lower(): i.data for i in g})
df.plot()
plt.show()