Skip to content

Commit 9c74c0e

Browse files
committed
ulog2csv: handle strings
1 parent e328bd3 commit 9c74c0e

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

pyulog/ulog2csv.py

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66

77
import argparse
88
import os
9+
import re
10+
911
import numpy as np
1012

1113
from .core import ULog
@@ -80,15 +82,29 @@ def convert_ulog2csv(ulog_file_name, messages, output, delimiter, time_s, time_e
8082
base_name = os.path.basename(output_file_prefix)
8183
output_file_prefix = os.path.join(output, base_name)
8284

85+
array_pattern = re.compile(r"(.*)\[(.*?)\]")
86+
8387
for d in data:
84-
fmt = '{0}_{1}_{2}.csv'
85-
output_file_name = fmt.format(output_file_prefix, d.name.replace('/', '_'), d.multi_id)
86-
fmt = 'Writing {0} ({1} data points)'
87-
# print(fmt.format(output_file_name, len(d.data['timestamp'])))
88+
name_without_slash = d.name.replace('/', '_')
89+
output_file_name = f'{output_file_prefix}_{name_without_slash}_{d.multi_id}.csv'
90+
print(f'Writing {output_file_name} ({len(d.data['timestamp'])} data points)')
8891
with open(output_file_name, 'w', encoding='utf-8') as csvfile:
8992

9093
# use same field order as in the log, except for the timestamp
91-
data_keys = [f.field_name for f in d.field_data]
94+
data_keys = []
95+
array_sizes = {}
96+
for f in d.field_data:
97+
if f.field_name.startswith('_padding'):
98+
continue
99+
result = array_pattern.fullmatch(f.field_name)
100+
if result and f.type_str == 'char': # string (array of char's)
101+
field, array_index = result.groups()
102+
array_index = int(array_index)
103+
array_sizes[field] = max(array_index + 1, array_sizes.get(field, 0))
104+
if array_index == 0:
105+
data_keys.append(field)
106+
else:
107+
data_keys.append(f.field_name)
92108
data_keys.remove('timestamp')
93109
data_keys.insert(0, 'timestamp') # we want timestamp at first position
94110

@@ -110,7 +126,16 @@ def convert_ulog2csv(ulog_file_name, messages, output, delimiter, time_s, time_e
110126
last_elem = len(data_keys)-1
111127
for i in range(time_s_i, time_e_i):
112128
for k in range(len(data_keys)):
113-
csvfile.write(str(d.data[data_keys[k]][i]))
129+
if data_keys[k] in array_sizes: # string
130+
s = ''
131+
for index in range(array_sizes[data_keys[k]]):
132+
character = d.data[f'{data_keys[k]}[{index}]'][i]
133+
if character == 0:
134+
break
135+
s += chr(character)
136+
csvfile.write(s)
137+
else:
138+
csvfile.write(str(d.data[data_keys[k]][i]))
114139
if k != last_elem:
115140
csvfile.write(delimiter)
116141
csvfile.write('\n')

0 commit comments

Comments
 (0)