diff --git a/igor2/packed.py b/igor2/packed.py index 15c8cdf..d41ffd1 100644 --- a/igor2/packed.py +++ b/igor2/packed.py @@ -43,7 +43,29 @@ # a later record in the packed file. -def load(filename, strict=True, ignore_unknown=True): +def load(filename, strict=True, ignore_unknown=True, initial_byte_order=None): + """Load a packed experiment file. + + Parameters + ---------- + filename : str or file-like object + The path to the file or a file-like object representing the packed + experiment file. + strict : bool, optional + This parameter is ignored. Defaults to True. + ignore_unknown : bool, optional + If True, ignore unknown record types. Defaults to True. + initial_byte_order : str or None, optional + The initial byte order to use for unpacking. Must be one of '>', '=', + '<'. If None, '=' is used. Defaults to None. + + Returns + ------- + records : list of Record + The records in the packed experiment file. + filesystem : dict + The filesystem structure of the packed experiment file. + """ logger.debug('loading a packed experiment file from {}'.format(filename)) records = [] if hasattr(filename, 'read'): @@ -51,7 +73,8 @@ def load(filename, strict=True, ignore_unknown=True): else: f = open(filename, 'rb') byte_order = None - initial_byte_order = '=' + if initial_byte_order is None: + initial_byte_order = '=' try: while True: PackedFileRecordHeader.byte_order = initial_byte_order @@ -80,7 +103,8 @@ def load(filename, strict=True, ignore_unknown=True): data = bytes(f.read(header['numDataBytes'])) if len(data) < header['numDataBytes']: raise ValueError( - ('not enough data for the next record ({} < {})' + ('not enough data for the next record ({} < {}), ' + 'try loading with a different initial byte order' ).format(len(b), header['numDataBytes'])) record_type = _RECORD_TYPE.get( header['recordType'] & PACKEDRECTYPE_MASK, _UnknownRecord) diff --git a/tests/data/packed-byteorder.pxt b/tests/data/packed-byteorder.pxt new file mode 100755 index 0000000..25fc438 Binary files /dev/null and b/tests/data/packed-byteorder.pxt differ diff --git a/tests/test_pxp.py b/tests/test_pxp.py index f779a1c..d1b3bf6 100644 --- a/tests/test_pxp.py +++ b/tests/test_pxp.py @@ -129,3 +129,26 @@ def test_pxp(): 28.72983551, 28.05199242, 29.29024887, 31.3501091, 32.7331543, 32.87995529, 32.28799438, 31.99738503], dtype=np.float32)) + + +def test_pxt(): + data = loadpxp(data_dir / 'packed-byteorder.pxt', initial_byte_order='>') + records = data[0] + assert len(records) == 2 + assert records[0].variables == {'version': 2, + 'variables': { + 'var_header': {'numSysVars': 0, + 'numUserVars': 0, + 'numUserStrs': 0, + 'numDependentVars': 0, + 'numDependentStrs': 0}, + 'sysVars': {}, + 'userVars': {}, + 'userStrs': {}, + 'dependentVars': [], + 'dependentStrs': []}} + assert np.allclose(records[1].wave['wave']["wData"][:30, 0], np.array( + [14603., 13701., 14907., 13795., 14339., 14942., 14984., 14261., + 12647., 14242., 14470., 13913., 14158., 14754., 14462., 14346., + 14219., 13467., 13595., 14331., 13960., 12934., 12897., 13557., + 13105., 12797., 13234., 13053., 13455., 12825.], dtype='>f8'))