-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdlogcsv.py
More file actions
executable file
·90 lines (70 loc) · 3.08 KB
/
Copy pathdlogcsv.py
File metadata and controls
executable file
·90 lines (70 loc) · 3.08 KB
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
#!/usr/bin/env python
import argparse
import csv
import decimal
import sys
from dlog import Dlog
def format_header_info(header: Dlog.Body.Header) -> list:
"""Extract the information from the header in a presentable form.
Args:
header: a Dlog header object
Returns:
list: header info as formatted strings
"""
voltage_scale_strings = {1000: "mV", 1: "V"}
voltage_scale = 1/header.voltage_scale
if header.voltage_scale in voltage_scale_strings:
voltage_scale = voltage_scale_strings[header.voltage_scale]
sample_rate = decimal.Decimal(header.sample_rate)
delay = decimal.Decimal(header.delay)
info = [f'log format: {header.dlog_format.name}',
f'stop reason: {header.stop_reason.name}',
f'number of samples: {header.num_samples}',
f'voltage units: {voltage_scale}',
f'sample rate: {sample_rate.normalize().to_eng_string()} Sa/s',
f'delay: {delay.normalize().to_eng_string()} s',
f'number of channels: {header.num_channels}',
f'channel map: {header.channel_map[:header.num_channels]}']
return info
def write_csv(data, log_header=None, column_header=None, file=sys.stdout):
"""Write the given data to a CSV file.
Args:
data: iterable of samples (in turn, an iterable of channel data points)
log_header: optional list of log header fields
log_header: optional list of log header fields
file: file-like object to write the CSV to
"""
csv_writer = csv.writer(file)
if log_header is not None:
csv_writer.writerow(log_header)
if column_header is not None:
csv_writer.writerow(column_header)
csv_writer.writerows(data)
def main():
parser = argparse.ArgumentParser()
parser.add_argument('inputfile', help='input log file')
parser.add_argument('--quiet', '-q', action='store_true',
help='omit log file information (on stderr)')
parser.add_argument('--no-csv-log-header', action='store_true',
help='omit log header for CSV output')
parser.add_argument('--no-csv-column-header', action='store_true',
help='omit column header for CSV output')
args = parser.parse_args()
dlog = Dlog.from_file(args.inputfile)
header = dlog.body.header
if not args.quiet:
print('\n'.join(['Header Information'] + format_header_info(header)),
file=sys.stderr)
timestamped_data = ([i/header.sample_rate, *s.channel]
for i, s in enumerate(dlog.body.data.samples))
log_header = column_header = None
if not args.no_csv_log_header:
# extract the header fields (all public attributes)
log_header = [f'{f}={v}' for f, v in vars(header).items() if not
f.startswith('_')]
if not args.no_csv_column_header:
column_header = ['Time'] + ['Channel ' + str(c) for c in
header.channel_map[:header.num_channels]]
write_csv(timestamped_data, log_header, column_header)
if __name__ == "__main__":
main()