diff options
| author | Daniel Thompson <daniel@redfelineninja.org.uk> | 2021-08-07 20:13:40 (GMT) |
|---|---|---|
| committer | Daniel Thompson <daniel@redfelineninja.org.uk> | 2021-08-07 20:13:40 (GMT) |
| commit | a28a2cd7f462a217ab5598853835e806c83967f7 (patch) | |
| tree | 87c9d8e1da3bd45ce9e0a41b7e5c3269325f8840 /tools | |
| parent | 15434a38411ce49e3250e966ffd2472efe65f5df (diff) | |
tools: hrs2csv: Add a simple parser for hrs.data files
Signed-off-by: Daniel Thompson <daniel@redfelineninja.org.uk>
Diffstat (limited to 'tools')
| -rwxr-xr-x | tools/hrs2csv.py | 38 |
1 files changed, 38 insertions, 0 deletions
diff --git a/tools/hrs2csv.py b/tools/hrs2csv.py new file mode 100755 index 0000000..6c978e0 --- /dev/null +++ b/tools/hrs2csv.py @@ -0,0 +1,38 @@ +#!/usr/bin/env python3 + +# SPDX-License-Identifier: LGPL-3.0-or-later +# Copyright (C) 2021 Daniel Thompson + +import sys + +def parse_record(view): + '''Consume a single set of samples and format as a line for a CSV file.''' + # Verify synchronization + assert(view[0] == 0xffff) + + # Extract the timestamp and format it in ISO 8601 format + (YY, MM, DD, hh, mm, ss) = view[1:7] + print(f'"{YY:04}{MM:02}{DD:02}T{hh:02}{mm:02}{ss:02}"', end='') + + # Consume data until we reach the synchronization token + offset = 8 + while offset < len(view) and view[offset] != 0xffff: + print(f',{view[offset]}', end='') + offset += 1 + + # Close the current record and return + print('') + return offset + +# Open and read the file named in our first argument +with open(sys.argv[1], 'rb') as f: + rawdata = f.read() + +# Re-interpret the raw data as an array of 16-bit values +view = memoryview(rawdata) +data = view.cast('H') + +# Process the data one record at a time +offset = 0 +while offset < len(data): + offset += parse_record(data[offset:]) |
