-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreamp2csv.py
More file actions
279 lines (246 loc) · 11 KB
/
Copy pathreamp2csv.py
File metadata and controls
279 lines (246 loc) · 11 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
"""
*********************************************************************************
*** ***
*** Reamp exporter ***
*** ***
*** (c) DL2SBA Dietmar Krause 2026 ***
*** ***
*********************************************************************************
"""
import argparse
import struct
import csv
import locale
import os
import sys
import logging
from datetime import timezone
from datetime import datetime
from pathlib import Path
PARM_TIME_RELATIVE = "relative"
PARM_TIME_TIMESTAMP = "timestamp"
PARM_TIME_MICROS = "unix"
DOUBLE_BYTES = 8
LOGGING_CHUNK = 10000
def process_reamp_data(
parm_input_path,
parm_output_path,
parm_delimiter,
parm_encoding,
parm_time,
):
"""
*********************************************************************************
*** ***
*** Exporter ***
*** ***
*********************************************************************************
"""
with open(parm_input_path, "rb") as in_file:
#
logging.info(
f"inputfile size.....{os.fstat(in_file.fileno()).st_size:n} bytes",
)
# process header section
binary_content = in_file.read(0x200)
# extract header fields
file_version = struct.unpack("<h", binary_content[0:2])[0]
file_header_size = struct.unpack("<h", binary_content[2:4])[0]
file_channel_count = struct.unpack("B", binary_content[8:9])[0]
file_channel_map = struct.unpack("B", binary_content[9:10])[0]
file_sample_time = (
struct.unpack("<h", binary_content[10:12])[0] / 10
) # in ms ticks
file_type = struct.unpack("B", binary_content[8:9])[0]
file_start_ts = struct.unpack("<Q", binary_content[0x1F8:0x200])[0] / 1000
logging.info("file_version.......%s", file_version)
logging.info("file_header_size...%s", file_header_size)
logging.info("file_channel_count.%d", file_channel_count)
logging.info("file_channel_map...%s", file_channel_map)
logging.info("file_sample_time...%s ms", file_sample_time)
logging.info("file_type......... %d", file_type)
logging.info("file_start_ts......%s s", file_start_ts)
logging.info(
" %s",
datetime.fromtimestamp(file_start_ts, tz=timezone.utc),
)
# check file version
if file_version != 4:
raise ValueError("Fileversion not supported")
# process data records
# create output file
out_file = open(parm_output_path, "w", newline="", encoding=parm_encoding)
writer = csv.writer(out_file, delimiter=parm_delimiter)
# write csv header
writer.writerow(["time", "channel-0", "channel-1", "channel-2"])
# detector for end of file
# increments are in ms
previous_increment = -1
increment = 0
bytes_per_channel = (file_channel_count + 1) * DOUBLE_BYTES
timestamp_from_increment = ""
num_samples = 0
#
# read the data lines
while True:
value_array = []
# read all data of a sample
sample_raw = in_file.read(bytes_per_channel)
# read DOUBLE_BYTES byte double value
double_increment = struct.unpack("<d", sample_raw[0:DOUBLE_BYTES])[0]
# convert to ms
increment = double_increment
# print(increment)
if increment == previous_increment:
break
previous_increment = increment
timestamp_from_increment = file_start_ts + increment
# interpret time column depending on parm_time
if parm_time == PARM_TIME_RELATIVE:
value_array.append(locale.format_string("%f", increment))
elif parm_time == PARM_TIME_TIMESTAMP:
# timestamp of current sample in ms
ziel_datum = datetime.fromtimestamp(
timestamp_from_increment, tz=timezone.utc
)
# force that always microseconds are printed
zeit_string = ziel_datum.isoformat(timespec="microseconds")
value_array.append(zeit_string)
else:
# timestamp of current sample in s
value_array.append(timestamp_from_increment)
# process each channel in a row
for chan_no in range(file_channel_count):
start_idx = chan_no * DOUBLE_BYTES + DOUBLE_BYTES
stop_idx = start_idx + DOUBLE_BYTES
chan_value = struct.unpack("<d", sample_raw[start_idx:stop_idx])[0]
chan_value_locale = locale.format_string("%f", chan_value)
value_array.append(chan_value_locale)
writer.writerow(value_array)
# one more sample written
num_samples += 1
# cyclic status update
if num_samples % LOGGING_CHUNK == 0:
logging.info("samples written....%d", num_samples)
logging.info("samples written....%d", num_samples)
logging.info("fileLastTS.........%s", timestamp_from_increment)
logging.info(
f"outputfile size....{os.fstat(out_file.fileno()).st_size:n} bytes",
)
logging.info(
" %s",
datetime.fromtimestamp(timestamp_from_increment, tz=timezone.utc),
)
logging.info("... bye ...")
def main():
"""
*********************************************************************************
*** ***
*** Main ***
*** ***
*********************************************************************************
"""
# default level is warning, so that the INFO messages are suppressed by default
logging.basicConfig(
level=logging.WARNING, format="%(asctime)s - %(levelname)s - %(message)s"
)
# init Parser
parser = argparse.ArgumentParser(
description="Converts Reamp-Datafile into CSV-Format. "
"In Excel use the following cell format for English version `DD.MM.YYYY hh:mm:ss,000`. "
"For German version `TT.MM.JJJJ hh:mm:ss,000`",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
# mandatory parameter input filename
parser.add_argument("input_file", help="filename of *.reamp datafile")
# optional delimeter
parser.add_argument(
"-d",
"--delimiter",
choices=[",", ";"],
default=";",
help="Value delimiter in CSV-file",
)
# optional output encoding
parser.add_argument(
"-e",
"--encoding",
default="utf-8",
help="Encoding of output file. For details check https://docs.python.org/3/library/codecs.html",
)
# optional locale
parser.add_argument(
"-l",
"--locale",
default="EN_US",
help="Locale used for number formatting",
)
# optional output file name
parser.add_argument(
"-o",
"--output",
help="Name of the output file. If ommitted, 'inputfile>.csv' is used",
)
# optional mode of time column
parser.add_argument(
"-t",
"--time",
choices=[PARM_TIME_MICROS, PARM_TIME_RELATIVE, PARM_TIME_TIMESTAMP],
default=PARM_TIME_RELATIVE,
help=f"Time column in CSV: "
f"{PARM_TIME_MICROS} gives µs since 1900-01-01. "
f"{PARM_TIME_RELATIVE} gives increasing µs since start of measurement. "
f"{PARM_TIME_TIMESTAMP} gives full timestamp in ISO format with µs resolution",
)
# optional verbose
parser.add_argument(
"-v", "--verbose", action="store_true", help="Verbose log output"
)
# parse args
args = parser.parse_args()
input_file_name = Path(args.input_file)
output_file_name = (
args.output if args.output else input_file_name.with_suffix(".csv")
)
locale_used = args.locale
delimiter_used = args.delimiter
encoding_used = args.encoding
time_used = args.time
if args.verbose:
# CRITICAL 50
# ERROR 40
# WARNING 30
# INFO 20
# DEBUG 10
logging.getLogger().setLevel(20)
logging.info("*******************************************************************")
logging.info("*** ***")
logging.info("*** Reamp exporter ***")
logging.info("*** ***")
logging.info("*** Repository https://github.com/dl2sba/Reamp2CSV ***")
logging.info("*** Homepage https://dl2sba.com ***")
logging.info("*** ***")
logging.info("*** (c) DL2SBA Dietmar Krause 2026 ***")
logging.info("*** ***")
logging.info("*******************************************************************")
logging.info("input filename.....%s", input_file_name)
logging.info("output filename....%s", output_file_name)
logging.info("locale used........%s", locale_used)
logging.info("data delimiter.....%s", delimiter_used)
logging.info("output encoding....%s", encoding_used)
logging.info("time column........%s", time_used)
# pylint: disable=broad-exception-caught
try:
locale.setlocale(locale.LC_ALL, locale_used)
process_reamp_data(
input_file_name, output_file_name, delimiter_used, encoding_used, time_used
)
except Exception as e:
logging.error("An unexpected error occured: [%s]", e)
# *********************************************************************************
# *** ***
# *** Launcher ***
# *** ***
# *********************************************************************************
if __name__ == "__main__":
main()