6
6
7
7
import argparse
8
8
import os
9
+ import re
10
+
9
11
import numpy as np
10
12
11
13
from .core import ULog
@@ -80,15 +82,29 @@ def convert_ulog2csv(ulog_file_name, messages, output, delimiter, time_s, time_e
80
82
base_name = os .path .basename (output_file_prefix )
81
83
output_file_prefix = os .path .join (output , base_name )
82
84
85
+ array_pattern = re .compile (r"(.*)\[(.*?)\]" )
86
+
83
87
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)' )
88
91
with open (output_file_name , 'w' , encoding = 'utf-8' ) as csvfile :
89
92
90
93
# 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 )
92
108
data_keys .remove ('timestamp' )
93
109
data_keys .insert (0 , 'timestamp' ) # we want timestamp at first position
94
110
@@ -110,7 +126,16 @@ def convert_ulog2csv(ulog_file_name, messages, output, delimiter, time_s, time_e
110
126
last_elem = len (data_keys )- 1
111
127
for i in range (time_s_i , time_e_i ):
112
128
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 ]))
114
139
if k != last_elem :
115
140
csvfile .write (delimiter )
116
141
csvfile .write ('\n ' )
0 commit comments