forked from AutonomyLab/bebop_hacking
-
Notifications
You must be signed in to change notification settings - Fork 0
/
blackbox_to_csv.py
98 lines (78 loc) · 2.62 KB
/
blackbox_to_csv.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
#!/usr/bin/python
"""
Converts a Parrot Bebop blackbox recording into a .csv file.
@author: Matthew Lloyd (github@matthewlloyd.net)
"""
import optparse
import os
import struct
class BlackboxFileReader(object):
def __init__(self, filename):
self.filename = filename
self._read_file()
def _read_file(self):
# Read the file.
contents = open(self.filename, 'rb').read()
# Extract the JSON header.
data_index = contents.index('-- Data\x0a')
header_lines = contents[:data_index].split('\x0a')
packets = contents[data_index + len('-- Data\x0a'):]
print data_index + len('-- Data\x0a')
# Parse the header.
self.packet_length = 0
self.column_indices = {}
for line in header_lines:
line = line.strip()
# Extract the column information.
if line.startswith('nentries:'):
self.nentries = int(line.split(':')[1])
continue
if line.startswith('datasize:'):
self.datasize = int(line.split(':')[1])
continue
if line.startswith('titles:'):
self.titles = map(lambda s: s.strip(), line.split(':')[1].split(','))
continue
assert self.datasize == 8
assert len(self.titles) == self.nentries
self.packet_length = self.datasize * self.nentries
assert self.packet_length > 0
self.struct_format = '<' + 'd' * self.nentries
for i, title in enumerate(self.titles):
self.column_indices[title] = i
# Unpack the packets
self.packets = []
i = 0
while len(packets) - i >= self.packet_length:
packet = packets[i:i + self.packet_length]
packet_fields = struct.unpack(self.struct_format, packet)
self.packets.append(packet_fields)
i += self.packet_length
# TODO: warn if there is data left over
def make_csv(reader):
f = reader.filename
if os.path.exists(f + '.csv'):
return
print 'Creating CSV file for %s' % (f,)
c = open(f + '.csv', 'w')
c.write(','.join(c for c in reader.titles) + '\n')
for packet in reader.packets:
c.write(','.join(str(c) for c in packet) + '\n')
c.close()
def main():
parser = optparse.OptionParser()
parser.add_option("-d", "--dir", default=None, dest="dir", help="directory to search for *.pud files")
for flag in ('csv', ):
parser.add_option("--%s" % flag, action="store_true", dest=flag, default=True)
parser.add_option("--no-%s" % flag, action="store_false", dest=flag)
(options, args) = parser.parse_args()
files = list(args)
if options.dir is not None:
files = files + list(os.path.join(options.dir, f) for f in os.listdir(options.dir) if f.endswith('.pud'))
for filename in files:
print "Processing %s..." % (filename,)
reader = BlackboxFileReader(filename)
if options.csv:
make_csv(reader)
if __name__ == '__main__':
main()