try: import defusedxml.cElementTree as ET except ImportError: try: import defusedxml.ElementTree as ET except ImportError: import xml.etree.ElementTree as ET import datetime import numpy as np class Block: """ Represents a "Block" in a tbwk file.""" Measurement = 151 head = None type = None parts = None content = None offset = None def __init__(self, head, type, size, content, offset): self.head = head self.type = type self.size = size self.content = content self.offset = offset if type == 62: self.parse_xml() elif type == 63: self.parse_xml() elif type == 150: self.parse_150() elif type == 151: self.parse_subblock() elif type == 152: self.parse_152() elif type == 920: self.parse_subblock() elif type == 921: self.parse_921() elif type == 922: self.parsed_content = None elif type == 930: self.parse_subblock() elif type == 931: self.parse_931() elif type == 932: self.parse_uv() elif type == 990: self.parse_xml() elif type == 991: self.parse_991() elif type < 10000: raise Exception(f"No clue what block {type} is.") def __repr__(self): # alt_types = (int.from_bytes(self.head[:1], "little"), int.from_bytes(self.head[1:2], "little")) return f"" def describe(self): if self.type == 63: return "Main XML" elif self.type == 991: return self.parsed_content[0] elif self.type == 151: return self.parsed_content[0].parsed_content[1] def parse_xml(self): xml = ET.fromstring(self.content[12:]) self.parsed_content = xml def parse_subblock(self): content = self.content[12:] self.parsed_content = unpack(content) def parse_uv(self): content = self.content[59:] offset = 0 length1, label_long = unpack_string(content) length2, label_short = unpack_string(content[offset + length1 + 1:]) offset += 1 + length1 + 1 + length2 offset += 8 # 8 empty bytes offset += 21 # 4 indicating dimension again, followed by int8, then by 2x float64. # First of these floats was 1 for 0 for y-axis and 1 for x-axis # Second was in both cases 0. Needs more "figuring out". number_of_values = int.from_bytes(content[offset:offset + 4], "little") values = content[offset + 4:] values = np.frombuffer(values, dtype="