Commit 1232585a authored by Vincent Michel's avatar Vincent Michel

Refactor mapping buffer parser

parent b64e8742
Pipeline #1042 passed with stages
in 1 minute and 18 seconds
......@@ -65,31 +65,39 @@ def dword(array, index, bitsize=16):
def parse_mapping_buffer(raw):
spectrums, statistics = {}, {}
# Header advance
header = raw[0:256]
current = 256
# Header parsing
assert header[0] == 0x55aa
assert header[1] == 0xaa55
assert header[2] == 0x100
mapping_mode = header[3]
buffer_index = dword(header, 4)
buffer_id = ['a', 'b'][header[7]]
buffer_id = header[7]
pixel_number = header[8]
starting_pixel = dword(header, 9)
module_serial_number = header[11]
dc1, de1 = header[12:14]
dc2, de2 = header[14:16]
dc3, de3 = header[16:18]
dc4, de4 = header[18:20]
sizes = header[20:24]
# Check
channel_ids = header[12:20:2]
channel_detector_ids = header[13:20:2]
# Checks
assert mapping_mode == 1 # MCA mapping mode
assert starting_pixel == dword(raw, 256+4)
assert buffer_id in (0, 1)
# Unused information, should we do something with it?
buffer_index, module_serial_number, channel_detector_ids
# Iterate over pixels
for _ in range(pixel_number):
# Pixel header advance
pixel_header = raw[current:current+256]
current += 256
# Pixel header parsing
assert pixel_header[0] == 0x33cc
assert pixel_header[1] == 0xcc33
......@@ -98,27 +106,34 @@ def parse_mapping_buffer(raw):
pixel = dword(pixel_header, 4)
total_size = dword(pixel_header, 6)
sizes = pixel_header[8:12]
# Statistics block
stats_block = [
[dword(pixel_header, 32 + 8 * i + 2 * j) for j in range(4)]
for i in range(4)]
# Iterate over channels
for i in range(4):
# Get statistics
stats = [dword(header, 32 + 8 * i + 2 * j) for j in range(4)]
# Set data
lst = statistics.setdefault(pixel, [])
lst.append(tuple(stats))
# Iterate over channels
spectrums[pixel] = {}
statistics[pixel] = {}
remaining = total_size - 256
for size in sizes:
for index, channel_id, size in zip(range(4), channel_ids, sizes):
# Update remaining size
assert remaining >= 0
if remaining == 0:
break
remaining -= size
# Sectrum Advance
spectrum = raw[current:current+size]
current += size
# Set data
lst = spectrums.setdefault(pixel, [])
lst.append(spectrum)
spectrums[pixel][channel_id] = spectrum
statistics[pixel][channel_id] = tuple(stats_block[index])
# Checks
assert remaining == 0
# Return result
# Return results
return spectrums, statistics
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment