forked from Yaroslavus/manticore_3.0
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanticore_decoding.py
581 lines (533 loc) · 27.2 KB
/
manticore_decoding.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 20 23:45:35 2020
@author: yaroslav
"""
import manticore_tools as tools
import time # TODO why do we need It?
MAX_EVENT_NUMBER = 0
MIN_EVENT_NUMBER = 999999999999
#TOTAL_DICT_OF_DAYS = dict() # TODO dict() is more clearly than the {}
# =============================================================================
#
# =============================================================================
def to_process(start_time):
"""Manages the conveyor of processing. Put all the files on it in order.
Takes .files_list.txt and file by file (line by line) put them to the
process_one_file function like children in "Another Brick In The Wall".
In addition provides all needed interface. Exactly from here
comes the BASH outstream through all binary files cleaning."""
print("Start to_process!\n")
TOTAL_DICT_OF_DAYS = create_empty_dict_of_days(start_time)
with open('.files_list.txt', 'r') as file_of_files:
number_of_files_to_process = len(file_of_files.readlines())
# TODO лучше сформировать files_list в with выше и не открывать тот, что ниже + не files_list, а files_to_process_list
with open(".files_list.txt", "r") as files_list:
#print("\nStart to process...\n")
counter = 0
for file_to_process in files_list:
file_to_process = tools.check_and_cut_the_tail(file_to_process)
file_day = file_to_process[:-18]
tail = file_to_process[-3:]
min_number, max_number = to_process_single_file(file_to_process)
#print(min_number, max_number)
if min_number != "empty" and max_number != "empty":
#print(min_number, TOTAL_DICT_OF_DAYS[file_day][tail][0], TOTAL_DICT_OF_DAYS[file_day][tail][1])
#print(type(min_number), type(TOTAL_DICT_OF_DAYS[file_day][tail][0]), type(TOTAL_DICT_OF_DAYS[file_day][tail][1]))
if min_number < TOTAL_DICT_OF_DAYS[file_day][tail][0]:
TOTAL_DICT_OF_DAYS[file_day][tail][0] = min_number
if max_number > TOTAL_DICT_OF_DAYS[file_day][tail][1]:
TOTAL_DICT_OF_DAYS[file_day][tail][1] = max_number
print("\nPreparing binary files:\n")
counter += 1
tools.syprogressbar(
counter,
number_of_files_to_process,
u'\u24B7',
"preparing binary files",
start_time)
#print("\n{} is processing now.".format(file_to_process)) TODO remove me!
with open(".total_dict_of_days.txt", "w+") as dict_file:
for day, tail_list in TOTAL_DICT_OF_DAYS.items():
dict_file.write(str(day) + "\n")
dict_file.write(str(len(tail_list)) + "\n")
for tail, list_of_max_min in tail_list.items():
dict_file.write("{}\t{}\t{}\n".format(tail, list_of_max_min[0], list_of_max_min[1]))
print("End to_process!\n")
#==============================================================================
#
# =============================================================================
def create_empty_dict_of_days(start_time):
print("Start create_empty_dict_of_days...")
global MIN_EVENT_NUMBER
global MAX_EVENT_NUMBER
TOTAL_DICT_OF_DAYS = dict()
#dict_of_max_min = {} ATTENTION def IT here ERROR!!!!
#print("Event numbers range in parallel bsms are finding out...")
with open('.files_list.txt', 'r') as files:
files_list = files.readlines() # TODO not files_list, but files_full_path_list
days_list = sorted(list(set_of_days(files_list)))
print('\n\n\n')
i=0
for day in days_list:
tails_list = sorted(list(set_of_tails(files_list, day)))
print("[{}] tails_list={}".format(i, tails_list))
j=0
dict_of_max_min = dict() # TODO IT should be here
for tail in tails_list:
print("[{}:{}] tail={}".format(i, j, tail))
dict_of_max_min[tail] = [MIN_EVENT_NUMBER, MAX_EVENT_NUMBER]
TOTAL_DICT_OF_DAYS[day] = dict_of_max_min
j += 1
i += 1
print("TOTAL_DICT_OF_DAYS=", TOTAL_DICT_OF_DAYS)
print('\n\n\n')
print("End create_empty_dict_of_days...")
return TOTAL_DICT_OF_DAYS
# =============================================================================
#
# =============================================================================
def set_of_days(files_list):
"""Creates days_set which contain full pathes of all the days present
in .files_list. If you preprocessed one day, there will be only it."""
days_set = set()
for file in files_list:
file_directory = tools.check_and_cut_the_tail(file)[:-18] # TODO It was changed!
days_set.add(file_directory)
#print("Set of days have been created.")
#print(sorted(list(days_set))) TODO remove me!
return days_set
# =============================================================================
#
# =============================================================================
def set_of_tails(files_list, day):
"""Creates tails_set for every day. Works for every day from days_set.
Tails_set contains the tails of all files preprocessed in the
directory of this day. For example, if you preprocessed only the
files xxx.001, set will contain only one item - "001". If you
preprocessed all the day, the set will contain all the tails from
('001', '002', '003', ..., the last one) of this day."""
# global TOTAL_DICT_OF_DAYS # TODO why The PO does need It?
tails_set = set()
for file in files_list:
if file[:-19] == day:
file = tools.check_and_cut_the_tail(file)
tails_set.add(file[-3:]) #TODO It was changed from 60: to -3:
#print("Set of tails have been created.")
#print(sorted(list(tails_set))) #TODO remove me!
return tails_set
def to_process_single_file(file_to_process):
"""Conducts one input file through all sequence of needed operations.
Firstly checks is needed pedestal files exist. If no:
- calculates pedestals,
- calculates pedestals sigmas,
- make ignore file, which contains information is channel make noise
(concluded from the sigmas).
If needed pedestal files exists, skips it and calculates amplitudes
for all events in this files and lightweight header file. By the way,
header file is not further used anywhere. It is rather a tribute to
tradition. What the data processing without header files?!
The details of functions see in their own docstrings."""
print("Start to process singl file: ", file_to_process)
with open(".mess.txt", "a") as mess_file:
# if (tools.is_exist(tools.make_PED_file_temp(file_to_process) + ".spd") or
# tools.is_exist(tools.make_PED_file_temp(file_to_process) + ".ssg") or
# tools.is_exist(tools.make_PED_file_temp(file_to_process) + ".sig")) is False:
# .spd - static pedestals
print("Static pedestals files creating...")
#TODO более наглядно создать стороку ped_file_temp = make_PED_file_temp(file_to_process), а затем ее передовать дальше
# это улучшит читаемость
ped_file_temp = tools.make_PED_file_temp(file_to_process)
# TODO запись в .mess_file.txt можно вынести в отдельную функцию и добавить ее в tools
# prototype: fill_mess(temp, format) с форматом по умочанию 0, для записи в него например files_list.txt
mess_file.write("Made temporary file: {}.spd\n".format(
tools.make_PED_file_temp(file_to_process)))
print("Made temporary file: {}.spd".format(
tools.make_PED_file_temp(file_to_process)))
# .ssg - sigmas of static pedestals
print("Calculating static pedestals sigmas...")
mess_file.write("Made temporary file: {}.ssg\n".format(
tools.make_PED_file_temp(file_to_process)))
print("Made temporary file: {}.ssg".format(
tools.make_PED_file_temp(file_to_process)))
# .sig ignore status from static pedestals sigma
# (PED_sigma < PED_average + 3*sigma_sigma)
print("Compiling file with channels to be ignored...")
mess_file.write("Made temporary file: {}.sig\n".format(
tools.make_PED_file_temp(file_to_process)))
print("Made temporary file: {}.sig".format(
tools.make_PED_file_temp(file_to_process)))
make_static_pedestals(file_to_process)
# else: print("Static pedestal file exists.")
# if (tools.is_exist(tools.make_BSM_file_temp(file_to_process) + ".dpd") or
# tools.is_exist(tools.make_BSM_file_temp(file_to_process) + ".dsg") or
# tools.is_exist(tools.make_BSM_file_temp(file_to_process) + ".dig")) is False:
# .dpd - dynamic pedestals
print("Dynamic pedestals files creating...")
mess_file.write("Made temporary file: {}.dpd\n".format(
tools.make_BSM_file_temp(file_to_process)))
print("Made temporary file: {}.dpd".format(
tools.make_BSM_file_temp(file_to_process)))
# .dsg - sigmas of dynamic pedestals
print("Calculating dynamic pedestals sigmas...")
mess_file.write("Made temporary file: {}.dsg\n".format(
tools.make_BSM_file_temp(file_to_process)))
print("Made temporary file: {}.dsg".format(
tools.make_BSM_file_temp(file_to_process)))
# .dig ignore status from dynamic pedestals sigma
# (PED_sigma < PED_average + 3*sigma_sigma)
print("Compiling file with channels to be ignored...")
mess_file.write("Made temporary file: {}.dig\n".format(
tools.make_BSM_file_temp(file_to_process)))
print("Made temporary file: {}.dig".format(
tools.make_BSM_file_temp(file_to_process)))
make_dynamic_pedestals(file_to_process)
# else: print("Dynamic pedestal file exists.")
# if (tools.is_exist(tools.make_BSM_file_temp(file_to_process) + ".amp") or
# tools.is_exist(tools.make_BSM_file_temp(file_to_process) + ".hdr")) is False:
# .hdr - file with only events number #1, event number #2,
# time of event and maroc number
print("Creating of the header...")
mess_file.write("Made temporary file: {}.hdr\n".format(
tools.make_BSM_file_temp(file_to_process)))
print("Made temporary file: {}.hdr".format(
tools.make_BSM_file_temp(file_to_process)))
# .amp - raw amplitudes
print("The raw amplitudes file creating...")
mess_file.write("Made temporary file: {}.amp\n".format(
tools.make_BSM_file_temp(file_to_process)))
print("Made temporary file: {}.amp".format(
tools.make_BSM_file_temp(file_to_process)))
min_number, max_number = make_clean_amplitudes_and_headers_1(file_to_process)
# else:
# print("Raw amplitude file is exist.")
# min_number, max_number = "empty", "empty"
# if (tools.is_exist(tools.make_BSM_file_temp(file_to_process) + ".asp")) is False:
# .asp - amplitudes minus static pedestals
print("The static amplitudes file creating...")
mess_file.write("Made temporary file: {}.asp\n".format(
tools.make_BSM_file_temp(file_to_process)))
print("Made temporary file: {}.asp".format(
tools.make_BSM_file_temp(file_to_process)))
make_static_amplitudes(file_to_process)
# else: print("STatic amplitude file is exist.")
# if (tools.is_exist(tools.make_BSM_file_temp(file_to_process) + ".adp")) is False:
# .adp - amplitudes minus dynamic pedestals
print("The raw amplitudes file creating...")
mess_file.write("Made temporary file: {}.adp\n".format(
tools.make_BSM_file_temp(file_to_process)))
print("Made temporary file: {}.adp".format(
tools.make_BSM_file_temp(file_to_process)))
make_dynamic_amplitudes(file_to_process)
# else: print("Dynamic amplitude file is exist.")
print("End to process single file: ", file_to_process)
return min_number, max_number
# =============================================================================
#
# =============================================================================
def make_static_pedestals (file_to_process):
"""This function is the attempt to rebuild the original function
'make_quick_pedestal' in more accurate way."""
counter = 0
number_of_codes = 64
PED = []
PED_av = [0]*number_of_codes
PED_sum = [0]*number_of_codes
PED_sigma = [0]*number_of_codes
sigma_sigma = 0
ignore_status = [0]*64
chunk_size = 156
codes_beginning_byte = 24
codes_ending_byte = 152
with open(file_to_process[:-18] + "PED/" + file_to_process[-12:-4] + ".ped", "rb") as ped_fin:
chunk = ped_fin.read(chunk_size)
while chunk:
ped_array = list(tools.unpacked_from_bytes("<64h", chunk[codes_beginning_byte:codes_ending_byte]))
for i in range(number_of_codes):
ped_array[i] /= 4
PED.append(ped_array)
for i in range(number_of_codes):
PED_av[i] += ped_array[i]
counter += 1
chunk = ped_fin.read(chunk_size)
for i in range(number_of_codes):
PED_av[i] /= counter
for line in PED:
for i in range(len(line)):
line[i] = tools.square_root((line[i] - PED_av[i])*(line[i] - PED_av[i]))
for line in PED:
for i in range(len(line)):
PED_sum[i] += line[i]
for i in range(number_of_codes):
PED_sigma[i] = PED_sum[i]/counter
sigma_av = sum(PED_sigma)/len(PED_sigma)
for item in PED_sigma:
sigma_sigma += (sigma_av - item)**2
sigma_sigma = tools.square_root(sigma_sigma/len(PED_sigma))
for i in range(len(PED_av)):
if not (-1*sigma_av - 3*sigma_sigma < PED_sigma[i] < sigma_av + 3*sigma_sigma):
ignore_status[i] += (i%2 +1)
with open(tools.make_PED_file_temp(file_to_process) + ".sig", "wb") as ignore_status_fout:
ignore_pack = tools.packed_bytes('<64B', ignore_status)
ignore_status_fout.write(ignore_pack)
with open(tools.make_PED_file_temp(file_to_process) + ".spd", "wb") as ped_fout:
peds_average = tools.packed_bytes('<64f', PED_av)
ped_fout.write(peds_average)
with open(tools.make_PED_file_temp(file_to_process) + ".ssg", "wb") as sigma_fout:
peds_sigma = tools.packed_bytes('<64f', PED_sigma)
sigma_fout.write(peds_sigma)
# =============================================================================
#
# =============================================================================
def make_dynamic_pedestals(file_to_process):
counter = [0]*64
number_of_codes = 64
PED = []
PED_av = [0]*number_of_codes
PED_sum = [0]*number_of_codes
PED_sigma = [0]*number_of_codes
sigma_sigma = 0
ignore_status = [0]*64
chunk_size = 156
codes_beginning_byte = 24
codes_ending_byte = 152
with open(file_to_process, "rb") as codes_fin:
chunk = codes_fin.read(chunk_size)
while chunk:
codes_array = list(tools.unpacked_from_bytes("<64h", chunk[codes_beginning_byte:codes_ending_byte]))
for i in range(0, len(codes_array), 2):
trigger_indicator = int(bin(codes_array[i])[-1])
if trigger_indicator == 1:
codes_array[i] = 0
codes_array[i+1] = 0
else:
counter[i] += 1
counter[i+1] += 1
for i in range(number_of_codes):
if codes_array[i] != 0:
codes_array[i] /= 4
PED.append(codes_array)
for i in range(number_of_codes):
PED_av[i] += codes_array[i]
chunk = codes_fin.read(chunk_size)
for i in range(number_of_codes):
if counter[i] != 0:
PED_av[i] /= counter[i]
for line in PED:
for i in range(len(line)):
line[i] = tools.square_root((line[i] - PED_av[i])*(line[i] - PED_av[i]))
for line in PED:
for i in range(len(line)):
PED_sum[i] += line[i]
for i in range(number_of_codes):
PED_sigma[i] = PED_sum[i]/counter[i]
sigma_av = sum(PED_sigma)/len(PED_sigma)
for item in PED_sigma:
sigma_sigma += (sigma_av - item)**2
sigma_sigma = tools.square_root(sigma_sigma/len(PED_sigma))
for i in range(len(PED_av)):
if not (-1*sigma_av - 3*sigma_sigma < PED_sigma[i] < sigma_av + 3*sigma_sigma):
ignore_status[i] += (i%2 +1)
with open(tools.make_BSM_file_temp(file_to_process) + ".dig", "wb") as ignore_status_fout:
ignore_pack = tools.packed_bytes('<64B', ignore_status)
ignore_status_fout.write(ignore_pack)
with open(tools.make_BSM_file_temp(file_to_process) + ".dpd", "wb") as ped_fout:
peds_average = tools.packed_bytes('<64f', PED_av)
ped_fout.write(peds_average)
with open(tools.make_BSM_file_temp(file_to_process) + ".dsg", "wb") as sigma_fout:
peds_sigma = tools.packed_bytes('<64f', PED_sigma)
sigma_fout.write(peds_sigma)
# =============================================================================
#
# =============================================================================
def make_clean_amplitudes_and_headers_1 (file_to_process):
"""This function is the attempt to rebuild the original function
'make_clean_amplitudes_and_headers' in more accurate way."""
chunk_size = 156
codes_beginning_byte = 24
codes_ending_byte = 152
number_1_beginning_byte = 4
number_1_ending_byte = 8
maroc_number_byte = 20
min_number = 999999999999
max_number = 0
with open(file_to_process, "rb") as codes_fin:
with open(tools.make_BSM_file_temp(file_to_process) + ".amp", "wb") as clean_file:
with open(tools.make_BSM_file_temp(file_to_process) + ".hdr", "wb") as header_file:
chunk_counter = 0
chunk = codes_fin.read(chunk_size)
while chunk:
# try:
clean_amplitudes = [0]*64
event_number = tools.unpacked_from_bytes('I', chunk[number_1_beginning_byte:number_1_ending_byte])[0]
if event_number > max_number:
max_number = event_number
if event_number < min_number:
min_number = event_number
codes_array = tools.unpacked_from_bytes(
'<64h', chunk[codes_beginning_byte:codes_ending_byte])
for i in range(0, len(clean_amplitudes), 2):
if codes_array[i] <= 1800:
clean_amplitudes[i] = codes_array[i]/4
clean_amplitudes[i+1] = 0
else:
clean_amplitudes[i] = codes_array[i + 1]/4
clean_amplitudes[i+1] = 1
# clean_amplitudes[i+1] = int(bin(codes_array[i])[-1])
clean_amplitudes_pack = tools.packed_bytes(
'fB'*32,
clean_amplitudes)
# chunk_size in wfp_file will be 282 bytes
clean_file.write(chunk[:codes_beginning_byte])
clean_file.write(clean_amplitudes_pack)
clean_file.write(chunk[codes_ending_byte:])
# chunk_size in header will be 17 bytes
header_file.write(
chunk[number_1_beginning_byte:maroc_number_byte +1])
# except Exception:
# print("{} Chunk number {} in file {} is seems to be corrupted!\n".format(
# "RAW CHUNK CORRUPTION ERROR!",
# chunk_counter,
# file_to_process))
chunk_counter += 1
chunk = codes_fin.read(chunk_size)
return min_number, max_number
# =============================================================================
#
# =============================================================================
def make_static_amplitudes(file_to_process):
chunk_size = 156
codes_beginning_byte = 24
codes_ending_byte = -4
# number_1_beginning_byte = 4
# number_1_ending_byte = 8
# maroc_number_byte = 20
with open(tools.make_PED_file_temp(file_to_process) + ".spd", "rb") as ped_fin:
peds = ped_fin.read()
try:
peds_array = tools.unpacked_from_bytes('<64f', peds)
except Exception:
print("{} File {} is seems to be corrupted!\n".format(
"QPD-file CORRUPTION ERROR!",
tools.make_PED_file_temp(file_to_process) + ".spd"))
with open(tools.make_PED_file_temp(file_to_process) + ".sig", "rb") as ig_fin:
ig_bytes = ig_fin.read()
try:
ig_array = tools.unpacked_from_bytes('<64B', ig_bytes)
except Exception:
print("{} File {} is seems to be corrupted!\n".format(
"IG-file CORRUPTION ERROR!",
tools.make_PED_file_temp(file_to_process) + ".sig"))
with open(file_to_process, "rb") as codes_fin:
with open(tools.make_BSM_file_temp(file_to_process) + ".asp", "wb") as cleaned_file:
print("create .asp file: ", (tools.make_BSM_file_temp(file_to_process) + ".asp"))
chunk_counter = 0
chunk = codes_fin.read(chunk_size)
while chunk:
# try:
cleaned_amplitudes = [0]*96
codes_array = tools.unpacked_from_bytes(
'<64h', chunk[codes_beginning_byte:codes_ending_byte])
for i in range(0, len(cleaned_amplitudes), 3):
if codes_array[2*(i//3)] <= 1800:
cleaned_amplitudes[i] =\
codes_array[2*(i//3)]/4 - peds_array[2*(i//3)]
cleaned_amplitudes[i+1] = 0
else:
cleaned_amplitudes[i] =\
codes_array[2*(i//3) + 1]/4 - peds_array[2*(i//3) + 1]
cleaned_amplitudes[i+1] = 1
# cleaned_amplitudes[i+1] = int(bin(codes_array[2*(i//3)])[-1])
# ig_status = 0 if BOTH channels IS ignored
# 1 if LOW IS NOT ignored, HIGH IS ignored
# 2 if HIGH IS NOT ignored, LOW IS ignored
# 3 if BOTH IS NOT ignored
cleaned_amplitudes[i+2] =\
ig_array[2*(i//3)] + ig_array[2*(i//3) + 1]
cleaned_amplitudes_pack = tools.packed_bytes(
'fBB'*32,
cleaned_amplitudes)
# chunk_size in wfp_file will be 282 bytes
cleaned_file.write(chunk[:codes_beginning_byte])
cleaned_file.write(cleaned_amplitudes_pack)
cleaned_file.write(chunk[codes_ending_byte:])
# chunk_size in header will be 17 bytes
# except Exception:
# print("{} Chunk number {} in file {} is seems to be corrupted!\n".format(
# "RAW CHUNK CORRUPTION ERROR!",
# chunk_counter,
# file_to_process))
chunk_counter += 1
chunk = codes_fin.read(chunk_size)
# return min_number, max_number
# =============================================================================
#
# =============================================================================
def make_dynamic_amplitudes(file_to_process):
chunk_size = 156
codes_beginning_byte = 24
codes_ending_byte = 152
# number_1_beginning_byte = 4
# number_1_ending_byte = 8
# maroc_number_byte = 20
with open(tools.make_BSM_file_temp(file_to_process) + ".dpd", "rb") as ped_fin:
peds = ped_fin.read()
try:
peds_array = tools.unpacked_from_bytes('<64f', peds)
except Exception:
print("{} File {} is seems to be corrupted!\n".format(
"QPD-file CORRUPTION ERROR!",
tools.make_BSM_file_temp(file_to_process) + ".dpd"))
with open(tools.make_BSM_file_temp(file_to_process) + ".dig", "rb") as ig_fin:
ig_bytes = ig_fin.read()
try:
ig_array = tools.unpacked_from_bytes('<64B', ig_bytes)
except Exception:
print("{} File {} is seems to be corrupted!\n".format(
"IG-file CORRUPTION ERROR!",
tools.make_BSM_file_temp(file_to_process) + ".dig"))
with open(file_to_process, "rb") as codes_fin:
with open(tools.make_BSM_file_temp(file_to_process) + ".adp", "wb") as cleaned_file:
chunk_counter = 0
chunk = codes_fin.read(chunk_size)
while chunk:
# try:
cleaned_amplitudes = [0]*96
codes_array = tools.unpacked_from_bytes(
'<64h', chunk[codes_beginning_byte:codes_ending_byte])
for i in range(0, len(cleaned_amplitudes), 3):
if codes_array[2*(i//3)] <= 1800:
cleaned_amplitudes[i] =\
codes_array[2*(i//3)]/4 - peds_array[2*(i//3)]
cleaned_amplitudes[i+1] = 0
else:
cleaned_amplitudes[i] =\
codes_array[2*(i//3) + 1]/4 - peds_array[2*(i//3) + 1]
cleaned_amplitudes[i+1] = 1
# cleaned_amplitudes[i+1] =\
# int(bin(codes_array[2*(i//3)])[-1])
# ig_status = 0 if BOTH channels IS ignored
# 1 if LOW IS NOT ignored, HIGH IS ignored
# 2 if HIGH IS NOT ignored, LOW IS ignored
# 3 if BOTH IS NOT ignored
cleaned_amplitudes[i+2] =\
ig_array[2*(i//3)] + ig_array[2*(i//3) + 1]
cleaned_amplitudes_pack = tools.packed_bytes(
'fBB'*32,
cleaned_amplitudes)
# chunk_size in wfp_file will be 282 bytes
cleaned_file.write(chunk[:codes_beginning_byte])
cleaned_file.write(cleaned_amplitudes_pack)
cleaned_file.write(chunk[codes_ending_byte:])
# chunk_size in header will be 17 bytes
# except Exception:
# print("{} Chunk number {} in file {} is seems to be corrupted!\n".format(
# "RAW CHUNK CORRUPTION ERROR!",
# chunk_counter,
# file_to_process))
chunk_counter += 1
chunk = codes_fin.read(chunk_size)
# =============================================================================
#
# =============================================================================