5: Float64l, 7: Int64ul, # timestamp? 8: VBString, 9: SomeKindOfEnumMaybe, 11: Const(b'\x00' * 2), # null? 35: Const(b'\x00' * 6), # EOF }) ) # Specialization for loading float data faster TaggedFloat64 = ExprAdapter( Struct(Const('tag'/Int16ul, 5), 'value'/Float64l), encoder=lambda obj, ctx: Container(tag=obj.tag, value=obj.value), decoder=lambda obj, ctx: obj.value) DataList = Struct( 'size'/Int64ul, OnDemand(Array(lambda ctx: ctx.size, TaggedFloat64)), Const('\xc0\xff\xee\x01') # XXX: probably useful ) # XXX: hacks bad_strings = ('\xc0\xff\xee\x01\x00\x00', '\x01#Eg\x00\x00') Property = Struct( 'peek'/Peek(String(6)), Embedded(IfThenElse( this.peek in bad_strings, Padding(6), Struct('label'/VBString, 'TaggedData'/TaggedData))) ) Properties = GreedyRange(Property) LabeledDataList = Struct( 'label'/VBString, Padding(18),
def is_ParameterList(block): return block.BlockType.param != 0 ParameterList = RepeatUntil(obj_.Name == 'END', Parameter) FloatData = Array(this.BlockLength, Float32l) StringData = String(this.BlockLength * 4) DirectoryEntry = Struct( 'BlockType' / BlockType, 'BlockLength' / Int32ul, 'DataPtr' / Int32ul, 'Block' / Pointer( this.DataPtr, FunctionSwitch( [(is_ParameterList, ParameterList), (lambda ctx: ctx.BlockType.extend != 0, OnDemand(StringData)), (lambda ctx: ctx.BlockType.data not in (0, 13), OnDemand(FloatData))]))) # The entire file. OpusFile = Struct( Const(b'\n\n\xfe\xfe'), # 0x0a0afefe magic 'Version' / Float64l, 'FirstDirPtr' / Int32ul, 'MaxDirSize' / Int32ul, 'CurrDirSize' / Int32ul, 'Directory' / Pointer(this.FirstDirPtr, Array(this.MaxDirSize, DirectoryEntry))) def iter_blocks(opus_data):
'exp'/Byte, 'index'/Int16sl, 'time'/Float32l, 'next'/Float32l, 'nois'/Float32l, 'npts'/Int32sl, 'scan'/Int32sl, 'wlevel'/Float32l, Padding(4), 'float_y'/Computed(this.exp == 128), 'num_pts'/Computed( lambda ctx: ctx.npts if ctx.npts > 0 else ctx._.Header.npts), 'exponent'/Computed( lambda ctx: ctx.exp if 0 < ctx.exp < 128 else ctx._.Header.exp), 'raw_x'/If(this._.Header.TFlags.use_subfile_xs, OnDemand(Array(this.num_pts, Int32sl))), 'raw_y'/IfThenElse(this.float_y, OnDemand(Array(this.num_pts, Float32l)), OnDemand(Array(this.num_pts, Int32sl))) ) LogData = Struct( 'log_start'/Tell, 'sizd'/Int32sl, 'sizm'/Int32sl, 'text_offset'/Int32sl, 'bins'/Int32sl, 'dsks'/Int32sl, Padding(44), 'content'/Pointer(this.log_start + this.text_offset, OnDemand(String(this.sizd)))
# burnhdr3 contains filenames that should be backed up burnhdr3 = Struct( "burnhdr3", Magic(b"SUNP BURN HDR 3"), Padding(1), ULInt32("num_filenames"), Padding(12), MetaArray( lambda ctx: ctx.num_filenames, String("filename", 0x70, padchar=b"\x00") ), # or is this 0 terminated?! cf bogus IN here: b'A:\\RO_RES\\COLD.BIN\x00IN', ) code = OnDemand(Bytes("code", lambda ctx: ctx.len_code)) sunp_file = Struct( "sunp_file", Magic(b"SUNP BURN FILE"), Padding(2, strict=True), # guessing... ULInt32("file_size"), ULInt32("off_burnhdr1"), ULInt32("off_burnhdr2"), ULInt32("off_code"), ULInt32("unk_zero"), ULInt32("off_burnhdr3"), Value("len_code", lambda ctx: ctx.off_burnhdr3 - ctx.off_code), #Padding(472), Pointer(lambda ctx: ctx.off_burnhdr1, burnhdr1), Pointer(lambda ctx: ctx.off_burnhdr2, burnhdr2),
'K': HeaderVersionK, 'L': Computed(_wrong_version_error), 'M': HeaderVersionM, }, default=Computed(_wrong_version_error)))) Subfile = Struct( 'flags' / Byte, 'exp' / Byte, 'index' / Int16sl, 'time' / Float32l, 'next' / Float32l, 'nois' / Float32l, 'npts' / Int32sl, 'scan' / Int32sl, 'wlevel' / Float32l, Padding(4), 'float_y' / Computed(this.exp == 128), 'num_pts' / Computed(lambda ctx: ctx.npts if ctx.npts > 0 else ctx._.Header.npts), 'exponent' / Computed(lambda ctx: ctx.exp if 0 < ctx.exp < 128 else ctx._.Header.exp), 'raw_x' / If(this._.Header.TFlags.use_subfile_xs, OnDemand(Array(this.num_pts, Int32sl))), 'raw_y' / IfThenElse(this.float_y, OnDemand(Array(this.num_pts, Float32l)), OnDemand(Array(this.num_pts, Int32sl)))) LogData = Struct( 'log_start' / Tell, 'sizd' / Int32sl, 'sizm' / Int32sl, 'text_offset' / Int32sl, 'bins' / Int32sl, 'dsks' / Int32sl, Padding(44), 'content' / Pointer(this.log_start + this.text_offset, OnDemand(String(this.sizd)))) # The entire file. SPCFile = Struct( 'Header' / Header, 'xvals' / If(this.Header.TFlags.has_xs, OnDemand(Array(this.Header.npts, Float32l))), 'Subfile' / Array(this.Header.nsub, Subfile), 'LogData' / If(this.Header.log_offset != 0, Pointer(this.Header.log_offset, LogData)))
"maxmem" / Int16ul, "relocation_stackseg" / Int16ul, "exe_stackptr" / Int16ul, "checksum" / Int16ul, "exe_ip" / Int16ul, "relocation_codeseg" / Int16ul, "table_offset" / Int16ul, "overlay" / Int16ul, Padding(8), "oem_id" / Int16ul, "oem_info" / Int16ul, Padding(20), "coff_header_pointer" / Int32ul, "_assembly_start" / Tell, "code" / OnDemand(HexDump(Bytes(this.coff_header_pointer - this._assembly_start))), ) symbol_table = "symbol_table" / Struct( "name" / String(8, padchar=b"\x00"), "value" / Int32ul, "section_number" / Enum( ExprAdapter( Int16sl, encoder=lambda obj, ctx: obj + 1, decoder=lambda obj, ctx: obj - 1, ), UNDEFINED=-1, ABSOLUTE=-2, DEBUG=-3, _default_=Pass, ), "complex_type" / Enum(
''' Siemens/Bruker Diffrac-AT Raw Format * https://github.com/wojdyr/xylib/blob/master/xylib/brucker_raw.cpp ''' from __future__ import absolute_import import numpy as np from construct import (Padding, Struct, Switch, String, Array, Const, OnDemand, Embedded, Computed, Int32ul, Int16ul, Float64l, Float32l, this, Check) from .construct_utils import FixedSizeCString Block_v2 = Struct('header_len' / Int16ul, 'num_steps' / Int16ul, Padding(4), 'time_per_step' / Float32l, 'x_step' / Float64l, 'x_start' / Float64l, Padding(26), 'temperature' / Int16ul, Padding(this.header_len - 48), 'y' / OnDemand(Array(this.num_steps, Float32l))) RAW_v2 = Struct('num_steps' / Int32ul, Padding(162), 'date_time_measure' / FixedSizeCString(20), 'anode_material' / FixedSizeCString(2), 'lambda1' / Float32l, 'lambda2' / Float32l, 'intensity_ratio' / Float32l, Padding(8), 'sample_runtime' / Float32l, Padding(42), 'blocks' / Array(this.num_steps, Block_v2)) Block_v101 = Struct( 'header_len' / Int32ul, Check(this.header_len == 304), 'num_steps' / Int32ul, 'start_theta' / Float64l, 'start_2theta' / Float64l, Padding(76), 'high_voltage' / Float32l, 'amplifier_gain' / Float32l, 'discriminator_1_lower_level' / Float32l, Padding(64), 'step_size' / Float64l, Padding(8), 'time_per_step' / Float32l, Padding(12), 'rpm' / Float32l,