Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable specifying byte order in packed #14

Merged
merged 3 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 27 additions & 3 deletions igor2/packed.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,38 @@
# 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'):
f = filename # filename is actually a stream object
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
Expand Down Expand Up @@ -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)
Expand Down
Binary file added tests/data/packed-byteorder.pxt
Binary file not shown.
23 changes: 23 additions & 0 deletions tests/test_pxp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Loading