forked from mfg92/hantek1008py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsvexport.py
executable file
·607 lines (506 loc) · 31.5 KB
/
csvexport.py
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
from hantek1008 import Hantek1008, CorrectionDataType, ZeroOffsetShiftCompensationFunctionType
from typing import Union, Optional, List, Dict, Any, IO, TextIO
import logging as log
import argparse
import time
import datetime
import os
import lzma
import sys
import numpy
import math
import win32pipe, win32file
from usb.core import USBError
from threading import Timer
from time import sleep
from utils.csvwriter import ThreadedCsvWriter, CsvWriter
from enum import Enum
assert sys.version_info >= (3, 6)
# SamplingMode = enum.Enum("SamplingMode", ["BURST", "ROLL"])
class PipeServer():
def __init__(self, pipeName):
self.pipe = win32pipe.CreateNamedPipe(
r'\\.\pipe\\' + pipeName,
win32pipe.PIPE_ACCESS_OUTBOUND,
win32pipe.PIPE_TYPE_MESSAGE | win32pipe.PIPE_READMODE_MESSAGE | win32pipe.PIPE_WAIT,
1, 65536, 65536,
0,
None)
# Carefull, this blocks until a connection is established
def connect(self):
win32pipe.ConnectNamedPipe(self.pipe, None)
# Message without tailing '\n'
def write(self, message):
win32file.WriteFile(self.pipe, message.encode() + b'\n')
def close(self):
win32file.CloseHandle(self.pipe)
class ArgparseEnum(Enum):
def __str__(self) -> str:
assert isinstance(self.value, str)
return self.value
class RawVoltMode(ArgparseEnum):
VOLT = "volt"
RAW = "raw"
VOLT_AND_RAW = "volt+raw"
class SamplingMode(ArgparseEnum):
BURST = "burst"
ROLL = "roll"
class TimestampStyle(ArgparseEnum):
OWN_ROW = "own_row"
FIRST_COLUMN = "first_column"
def main(csv_file_path: str,
selected_channels: Optional[List[int]]=None,
vertical_scale_factor: Optional[List[float]]=[1.0],
calibrate_output_file_path: Optional[str]=None,
calibrate_channels_at_once: Optional[int]=None,
calibration_file_path: Optional[str]=None,
zero_offset_shift_compensation_channel: Optional[int]=None,
zero_offset_shift_compensation_function_file_path: Optional[str]=None,
zero_offset_shift_compensation_function_time_offset_sec: int=0,
raw_or_volt: RawVoltMode=RawVoltMode.VOLT,
samlping_mode: SamplingMode=SamplingMode.ROLL,
sampling_rate: float=440,
ns_per_div: int=500_000,
timestamp_style: TimestampStyle=TimestampStyle.OWN_ROW,
do_sampling_rate_measure: bool=True) -> None:
if selected_channels is None or len(selected_channels) == 0:
selected_channels = list(range(1, 9))
assert len(set(selected_channels)) == len(selected_channels)
assert all(1 <= c <= 8 for c in selected_channels)
selected_channels = [i-1 for i in selected_channels]
assert zero_offset_shift_compensation_channel is None or zero_offset_shift_compensation_function_file_path is None
assert zero_offset_shift_compensation_channel is None or 1 <= zero_offset_shift_compensation_channel <= 8
if zero_offset_shift_compensation_channel is not None:
zero_offset_shift_compensation_channel -= 1
assert zero_offset_shift_compensation_function_time_offset_sec >= 0
assert vertical_scale_factor is None or isinstance(vertical_scale_factor, List)
if vertical_scale_factor is None:
vertical_scale_factor = [1.0] * 8
elif len(vertical_scale_factor) == 1:
vertical_scale_factor = [1.0 if i not in selected_channels
else vertical_scale_factor[0]
for i in range(8)]
else:
assert len(vertical_scale_factor) == len(selected_channels)
# the vscale value of a channel is the value in vertical_scale_factor
# on the same index as the channel in selected channel
# or 1.0 if the channel is not in selected_channels
vertical_scale_factor = [1.0 if i not in selected_channels
else vertical_scale_factor[selected_channels.index(i)]
for i in range(8)]
correction_data: CorrectionDataType = [{} for _ in range(8)] # list of dicts of dicts
# use case: correction_data[channel_id][vscale][units] = correction_factor
zero_offset_shift_compensation_function = None
if zero_offset_shift_compensation_function_file_path:
globals_dict: Dict[str, Any] = {}
with check_and_open_file(zero_offset_shift_compensation_function_file_path) as f:
exec(f.read(), globals_dict)
zero_offset_shift_compensation_function = globals_dict["calc_zos"]
assert callable(zero_offset_shift_compensation_function)
if calibration_file_path:
with check_and_open_file(calibration_file_path) as f:
import json
calibration_data = json.load(f)
log.info(f"Using calibration data from file '{calibration_file_path}' to correct measured values")
for channel_id, channel_cdata in sorted(calibration_data.items()):
channel_id = int(channel_id)
if len(channel_cdata) == 0:
continue
log.info(f"Channel {channel_id+1}:")
for test in channel_cdata:
vscale = test["vscale"]
test_voltage = test["test_voltage"]
units = test["measured_value"] - test["zero_offset"]
correction_factor = test_voltage / (units * 0.01 * vscale)
if test_voltage == 0:
continue
assert 0.5 < correction_factor < 2.0, "Correction factor seems to be false"
#log.info(f" {test} -> {correction_factor}")
log.info(f"{test_voltage:>6}V -> {correction_factor:0.5f}")
if vscale not in correction_data[channel_id]:
correction_data[channel_id][vscale] = {}
correction_data[channel_id][vscale][units] = correction_factor
# log.info("\n".join(str(x) for x in correction_data))
channels_without_cd = [i + 1 for i, x in enumerate(correction_data) if len(x) == 0]
if len(channels_without_cd) > 0:
log.warning(f"There is no calibration data for channel(s): {channels_without_cd}")
device = connect(ns_per_div, vertical_scale_factor, selected_channels, correction_data, zero_offset_shift_compensation_channel,
zero_offset_shift_compensation_function, zero_offset_shift_compensation_function_time_offset_sec)
if calibrate_output_file_path:
assert calibrate_channels_at_once is not None
calibration_routine(device, calibrate_output_file_path, calibrate_channels_at_once)
device.close()
sys.exit()
measured_sampling_rate = None
if do_sampling_rate_measure:
measurment_duration = 10
log.info(f"Measure sample rate of device (takes about {measurment_duration} sec) ...")
measured_sampling_rate = measure_sampling_rate(device, sampling_rate, measurment_duration)
log.info(f"-> {measured_sampling_rate:.4f} Hz")
# TODO Remove
# sys.exit()
csv_file_path_zero = csv_file_path
# data collection is in loop because in case of an error it restarts the collection
for i in range(1, 100):
try:
sample(device, raw_or_volt, selected_channels, samlping_mode, sampling_rate, vertical_scale_factor,
csv_file_path, timestamp_style, measured_sampling_rate)
# no error? -> finished by user interaction
break
except USBError as usb_error:
# usb error bug occurred? try to close the device or reset it, sleep a sec and restart
log.error(str(usb_error))
try:
device.close()
except:
try:
sleep(0.5)
device._dev.reset()
except:
pass
sleep(1.0)
device = connect(ns_per_div, vertical_scale_factor, selected_channels, correction_data,
zero_offset_shift_compensation_channel,
zero_offset_shift_compensation_function,
zero_offset_shift_compensation_function_time_offset_sec)
if csv_file_path_zero != '-':
csv_file_path = f"{csv_file_path_zero}.{i:02d}"
log.info("Exporting data finished")
device.close()
def connect(ns_per_div: int,
vertical_scale_factor: Union[float, List[float]],
selected_channels: List[int],
correction_data: Optional[CorrectionDataType] = None,
zero_offset_shift_compensation_channel: Optional[int] = None,
zero_offset_shift_compensation_function: Optional[ZeroOffsetShiftCompensationFunctionType] = None,
zero_offset_shift_compensation_function_time_offset_sec: int = 0) -> Hantek1008:
device = Hantek1008(ns_per_div=ns_per_div,
vertical_scale_factor=vertical_scale_factor,
active_channels=selected_channels,
correction_data=correction_data,
zero_offset_shift_compensation_channel=zero_offset_shift_compensation_channel,
zero_offset_shift_compensation_function=zero_offset_shift_compensation_function,
zero_offset_shift_compensation_function_time_offset_sec
=zero_offset_shift_compensation_function_time_offset_sec)
try:
log.info("Connecting...")
try:
device.connect()
except RuntimeError as e:
log.error(str(e))
sys.exit(1)
log.info("Connection established")
log.info("Initialising...")
try:
device.init()
except RuntimeError as e:
log.error(str(e))
sys.exit(1)
log.info("Initialisation completed")
except KeyboardInterrupt:
device.close()
sys.exit(0)
return device
def sample(device: Hantek1008,
raw_or_volt: RawVoltMode,
selected_channels: List[int],
sampling_mode: SamplingMode,
sampling_rate: float,
vertical_scale_factor: List[float],
csv_file_path: str,
timestamp_style: TimestampStyle,
measured_sampling_rate: Optional[float] = None
) -> None:
def check_sleep(amount):
start = datetime.datetime.now()
time.sleep(amount)
end = datetime.datetime.now()
delta = end - start
return delta.seconds + delta.microseconds / 1000000.
from past.builtins import xrange
error = sum(abs(check_sleep(0.050) - 0.050) for i in xrange(10)) / 10
log.info(f"Processing data of channel{'' if len(selected_channels) == 1 else 's'}:"
f" {' '.join([str(i+1) for i in selected_channels])}")
computed_actual_sampling_rate = Hantek1008.actual_sampling_rate_factor(len(selected_channels)) * sampling_rate
if len(selected_channels) != Hantek1008.channel_count():
log.warning(f"When not using all 8 channels, the actual sampling rate ({computed_actual_sampling_rate:.2f}) is "
f"higher than the given sampling_rate ({sampling_rate})! "
f"Best is to use the --measuresamplingrate flag.")
if raw_or_volt == RawVoltMode.VOLT_AND_RAW: # add the coresponding raw values to the selected channel list
selected_channels += [sc + Hantek1008.channel_count() for sc in selected_channels]
try:
print('connecting to pipe')
pipe = PipeServer("CS")
pipe.connect()
print('connected')
print(error)
# TODO: make this configurable
milli_volt_int_representation = False
def splitByChunks(lst, chunk_count):
chunk_size = len(lst) // chunk_count
return [lst[i:i + chunk_size] for i in range(0, len(lst), chunk_size)]
def write_per_channel_data(per_channel_data: Dict[int, Union[List[int], List[float]]],
time_of_first_value: Optional[float],
time_of_last_value: float,
) \
-> None:
# sort all channels the same way as in selected_channels
data = ''
for key, value in per_channel_data.items():
chunks = splitByChunks(value, 50)
data += 'DATA:' + str(key+1) + ':'
for chunk in chunks:
data += str(numpy.average(chunk)) + '='
data += ';'
pipe.write(data)
if sampling_mode == SamplingMode.ROLL:
last_timestamp = datetime.datetime.now().timestamp()
for per_channel_data in device.request_samples_roll_mode(mode=str(raw_or_volt), sampling_rate=sampling_rate):
now_timestamp = datetime.datetime.now().timestamp()
write_per_channel_data(per_channel_data, last_timestamp, now_timestamp)
last_timestamp = now_timestamp
else: # burst mode
# TODO currently not supported
# TODO missing features:
# * timestamp_style
assert timestamp_style == TimestampStyle.OWN_ROW
while True:
startTime = time.time()
print('write:', datetime.datetime.now().timestamp(), sep='')
per_channel_data = device.request_samples_burst_mode()
now_timestamp = datetime.datetime.now().timestamp()
write_per_channel_data(per_channel_data, None, now_timestamp)
offset = time.time() - startTime
if(offset < 0.5):
sleep(0.5 - (time.time() - startTime) - 0.01)
except KeyboardInterrupt:
log.info("Sample collection was stopped by user")
pass
#
# if csv_writer:
# csv_writer.close()
def measure_sampling_rate(device: Hantek1008, used_sampling_rate: float, measurment_duration: float) -> float:
required_samples = max(4, int(math.ceil(measurment_duration * used_sampling_rate)))
counter = -1
start_time: float = 0
for per_channel_data in device.request_samples_roll_mode(sampling_rate=used_sampling_rate):
if counter == -1: # skip first samples to ignore the duration of initialisation
start_time = time.perf_counter()
counter = 0
counter += len(per_channel_data[0])
if counter >= required_samples:
break
duration = time.perf_counter() - start_time
return counter/duration
def calibration_routine(device: Hantek1008, calibrate_file_path: str, channels_at_once: int) -> None:
assert channels_at_once in [1, 2, 4, 8]
print("This interactive routine will generate a calibration that can later be used "
"to get more precise results. It works by connecting different well known "
"voltages one after another to a channel. Once all calibration voltages are "
"measured, the same is done for every other channel.")
import json
required_calibration_samples_nun = 512
calibration_data: Dict[int, List[Dict[str, Any]]] = {} # dictionary of lists
device.pause()
test_voltages = None
while test_voltages is None:
try:
in_str = input("Calibration voltages (x, y, z, ...): ")
test_voltages = [float(v) for v in in_str.split(',')]
if len(test_voltages) < 1:
print("Input must contain at least one voltage")
except ValueError:
print("Input must be comma separated floats")
print(f"Calibration voltages are: {' '.join([ f'{v}V' for v in test_voltages])}")
for channel_id in range(8):
calibration_data[channel_id] = []
for channel_id in range(0, 8, channels_at_once):
for test_voltage in test_voltages:
cmd = input(f"Do {test_voltage}V measurement on channel {channel_id+1}"
f"{(' to ' + str(channel_id+channels_at_once)) if channels_at_once>1 else ''} (Enter),"
f" skip voltage (s), skip channel (ss) or quit (q): ")
if cmd == 'q':
return
elif cmd == 'ss':
break
elif cmd == 's':
continue
device.cancel_pause()
print(f"Measure {required_calibration_samples_nun} values for {test_voltage}V...")
data = []
for _, row in zip(
range(required_calibration_samples_nun),
device.request_samples_roll_mode_single_row(mode="raw")):
data.append(row)
pass
device.pause()
channel_data = list(zip(*data))
for calibrated_channel_id in range(channel_id, channel_id+channels_at_once):
cd = channel_data[calibrated_channel_id]
avg = sum(cd) / len(cd)
calibration_data[calibrated_channel_id].append({
"test_voltage": test_voltage,
"measured_value": round(avg, 2),
"vscale": device.get_vscales()[calibrated_channel_id],
"zero_offset": round(device.get_zero_offset(channel_id=calibrated_channel_id), 2)
})
with open(calibrate_file_path, 'w') as calibration_file:
calibration_file.write(json.dumps(calibration_data))
def check_and_open_file(file_path: str) -> TextIO:
if not os.path.exists(file_path):
log.error(f"There is no file '{file_path}'.")
sys.exit(1)
if os.path.isdir(file_path):
log.error(f"'{file_path}' is a directory.")
sys.exit(1)
return open(file_path)
if __name__ == "__main__":
description = f"""\
Collect data from device 'Hantek 1008'. Usage examples:
* Save data sampled with 22 Hz in file 'my_data.csv':
{sys.argv[0]} my_data.csv --channels 1 2 --samplingrate 22
* Create and fill calibration file 'my_cal.json':
{sys.argv[0]} --calibrate my_cal.cd.json 1
"""
def channel_type(value: str) -> int:
ivalue = int(value)
if 1 <= ivalue <= 8:
return ivalue
raise argparse.ArgumentTypeError(f"There is no channel {value}")
str_to_log_level = {log.getLevelName(ll).lower(): ll for ll in [log.DEBUG, log.INFO, log.WARN]}
parser = argparse.ArgumentParser(formatter_class=argparse.RawDescriptionHelpFormatter,
description=description)
command_group = parser.add_mutually_exclusive_group(required=True)
command_group.add_argument(metavar='csv_path', dest='csv_path', nargs='?',
type=str, default=None,
help='Экспортирует измеренные данные в данный файл в формате CSV.'
" Если имя файла заканчивается на'.xz', содержимое сжимается с помощью lzma/xz."
" Это уменьшает размер файла до ~ 1/12 по сравнению с несжатым форматом."
" Эти файлы можно распаковать с помощью'xz-dk <имя файла>'.")
command_group.add_argument('--calibrate', metavar=('calibrationfile_path', 'channels_at_once'), nargs=2,
type=str, default=None,
help=' Если установлено, откалибруйте устройство, измерив заданное напряжение, и запишите'
' значения калибровки в заданный файл.'
' Несколько каналов (1, 2, 4 или все 8) могут быть откалиброваны одновременно'
' при подаче того же напряжения. Игнорирует все остальные аргументы.')
parser.add_argument('-s', '--channels', metavar='channel', nargs='+',
type=channel_type, default=list(range(1, 9)),
help="Выбирает интересующие каналы.")
parser.add_argument('-l', '--loglevel', dest='log_level', nargs='?',
type=str, default="info", choices=str_to_log_level.keys(),
help='Определяет уровень отладки.')
parser.add_argument('-v', '--vscale', metavar='scale', nargs="+",
type=float, default=[1.0], choices=Hantek1008.valid_vscale_factors(),
help='Устанавливает предварительную шкалу в аппаратном обеспечении, которая должна быть 1, 0,125 или 0,02. Если одно значение равно '
'учитывая, что все выбранные каналы будут использовать этот vscale, в противном случае должно быть одно значение '
'для каждого выбранного канала.')
parser.add_argument('-c', '--calibrationfile', dest="calibration_file_path", metavar='calibrationfile_path',
type=str, default=None,
help="Используйте содержимое данного файла калибровки для корректировки измеренных образцов.")
parser.add_argument('-r', '--raw', dest="raw_or_volt",
type=str, default=RawVoltMode.VOLT, const=RawVoltMode.RAW, nargs='?', choices=list(RawVoltMode),
help="Указывает, следует ли преобразовывать значения выборки, возвращенные с устройства "
"к вольтам (используя данные калибровки, если указано) или нет. Если не задано, значение по умолчанию "
"значение равно 'вольт'. Если флаг установлен без параметра, используется 'raw'.")
parser.add_argument('-z', '--zoscompensation', dest="zos_compensation", metavar='x',
type=str, default=None, nargs='*',
help=
""" Компенсирует сдвиг смещения нуля, возникающий в более длительных временных масштабах.
Есть два возможных способа компенсировать это:
(A) Вычисление смещения неиспользуемого канала: Требуется по крайней мере один неиспользуемый канал, убедитесь, что
чтобы к данному каналу не подавалось внешнее напряжение.
(B) Вычисление сдвига с помощью заданной функции. Такая функция вычисляет
поправочный коэффициент на основе времени, прошедшего с момента запуска.
По умолчанию компенсация не выплачивается. Если используется без аргумента, метод A используется на канале 8.
Если задан целочисленный аргумент, на этом канале используется метод A. В противном случае используется метод B,
который ожидает путь к файлу python с функцией
(calc_zos(ch: int, vscale: float, dtime: float)->float) в нем
и в качестве второго аргумента смещение по времени (как долго устройство уже работает в сек).
""")
parser.add_argument('-b', '--samplingmode', dest='sampling_mode',
type=SamplingMode, default=SamplingMode.ROLL, choices=list(SamplingMode),
help="TODO")
parser.add_argument('-f', '--samplingrate', dest='sampling_rate',
type=float, default=440, choices=Hantek1008.valid_roll_mode_sampling_rates(),
help='Задает частоту дискретизации (в Гц), которую устройство должно использовать в режиме рулона (по умолчанию:440). '
'Если используются не все каналы, фактическая частота дискретизации выше. Факторами являются: '
f'{[Hantek1008.actual_sampling_rate_factor(ch) for ch in range(1, 9)]}. '
'Например, если используются только два канала, фактическая частота дискретизации на 3,03 выше '
'чем заданное значение. Свободный канал, используемый для компенсации zos, уменьшит '
'фактическая выборка осуществляется таким же образом, как если бы канал обычно использовался.')
parser.add_argument('-n', '--nsperdiv', dest='ns_per_div',
type=float, default=500_000, choices=Hantek1008.valid_burst_mode_ns_per_divs(),
help='Устанавливает горизонтальное разрешение (в наносекундах на деление), которое устройство должно использовать в '
'режим серийной съемки (по умолчанию:500_000). Один div содержит около 25 образцов.'
'Если используются не все каналы, фактическое разрешение увеличивается на неизвестный коэффициент.')
parser.add_argument('-m', '--measuresamplingrate', dest='do_sampling_rate_measure', action="store_const",
default=False, const=True,
help='Измеряет точную частоту дискретизации, которую устройство достигает с помощью встроенного '
'таймера. Увеличивает продолжительность запуска на ~10 сек.')
parser.add_argument('-t', '--timestampstyle', dest="timestamp_style",
type=TimestampStyle, default=TimestampStyle.OWN_ROW, nargs='?', choices=list(TimestampStyle),
help="Задает стиль меток времени, включенных в выходные данные CSV. Там"
" есть два варианта: когда используется стиль 'own_row', каждый раз, когда устройство отправляет кучу"
" из измеренных образцов они записываются в выходные данные CSV, за которыми следует одна строка с"
" отметкой времени."
" Используйте опцию 'first_column', чтобы первый столбец каждой строки имел интерполированную"
" отметку времени. Значение по умолчанию - 'own_row'.")
args = parser.parse_args()
args.log_level = str_to_log_level[args.log_level]
def arg_assert(ok: bool, fail_message: str) -> None:
if not ok:
parser.error(fail_message)
if args.calibrate is not None:
calibrate_channels_at_once = args.calibrate[1]
arg_assert(calibrate_channels_at_once.isdigit() and int(calibrate_channels_at_once) in [1, 2, 4, 8],
"The second argument must be 1, 2, 4 or 8.")
arg_assert(len(args.vscale) == 1 or len(args.vscale) == len(args.channels),
"There must be one vscale factor or as many as selected channels")
arg_assert(len(set(args.channels)) == len(args.channels),
"Selected channels list is not a set (multiple occurrences of the same channel id")
# arg_assert(args.calibration_file_path is None or not args.raw_or_volt.contains("volt"),
# "--calibrationfile can not be used together with the '--raw volt' flag")
# arg_assert(args.zos_compensation is None or not args.raw_or_volt.contains("volt"),
# "--zoscompensation can not be used together with the '--raw volt' flag")
if args.zos_compensation is not None:
arg_assert(len(args.zos_compensation) <= 2, "'--zoscompensation' only awaits 0, 1 or 2 parameters")
if len(args.zos_compensation) == 0:
# defaults to channel 8
args.zos_compensation = [8]
if len(args.zos_compensation) == 1: # if compensation via unused channel is used
args.zos_compensation[0] = channel_type(args.zos_compensation[0])
arg_assert(len(args.channels) < 8,
"Zero-offset-shift-compensation is only possible if there is at least one unused channel")
arg_assert(args.zos_compensation[0] not in args.channels,
f"The channel {args.zos_compensation[0]} is used for Zero-offset-shift-compensation,"
f" but it is also a selected channel")
if len(args.zos_compensation) == 2: # if compensation via function is used
arg_assert(args.zos_compensation[1].isdigit(), "The second argument must be an int")
args.zos_compensation[1] = int(args.zos_compensation[1])
arg_assert(not (args.do_sampling_rate_measure and args.sampling_mode == SamplingMode.BURST),
"Measuring the sample rate only works in roll mode")
log.basicConfig(level=args.log_level, format='%(levelname)-7s: %(message)s')
main(selected_channels=args.channels,
vertical_scale_factor=args.vscale,
csv_file_path=args.csv_path,
calibrate_output_file_path=args.calibrate[0] if args.calibrate else None,
calibrate_channels_at_once=int(args.calibrate[1]) if args.calibrate else None,
calibration_file_path=args.calibration_file_path,
raw_or_volt=args.raw_or_volt,
zero_offset_shift_compensation_channel=
args.zos_compensation[0]
if args.zos_compensation is not None and len(args.zos_compensation) == 1
else None,
zero_offset_shift_compensation_function_file_path=
args.zos_compensation[0]
if args.zos_compensation is not None and len(args.zos_compensation) == 2
else None,
zero_offset_shift_compensation_function_time_offset_sec=
args.zos_compensation[1]
if args.zos_compensation is not None and len(args.zos_compensation) == 2
else 0,
samlping_mode=args.sampling_mode,
sampling_rate=args.sampling_rate,
ns_per_div=args.ns_per_div,
timestamp_style=args.timestamp_style,
do_sampling_rate_measure=args.do_sampling_rate_measure)