Skip to content

Commit

Permalink
Merge pull request #14 from kmnhan/byteorder
Browse files Browse the repository at this point in the history
Enable specifying byte order in packed
  • Loading branch information
paulmueller authored Apr 3, 2024
2 parents 589bcb2 + 025fb0f commit 88fb35f
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 3 deletions.
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'))

0 comments on commit 88fb35f

Please sign in to comment.