From 8df86762588d9e4dc61cafe40170dd01bd3da950 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Mon, 16 Jul 2012 22:01:57 -0400 Subject: [PATCH] Add support for zero-length waves. --- igor/binarywave.py | 32 +++++++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/igor/binarywave.py b/igor/binarywave.py index ae60f14..ebb5fcb 100644 --- a/igor/binarywave.py +++ b/igor/binarywave.py @@ -34,6 +34,9 @@ import types import numpy +_buffer = buffer # save builtin buffer for clobbered situations + + class Field (object): """Represent a Structure field. @@ -172,8 +175,24 @@ class Structure (struct.Struct): return self._unflatten_args(struct.Struct.unpack(self, string)) def unpack_from(self, buffer, offset=0): - return self._unflatten_args( - struct.Struct.unpack_from(self, buffer, offset)) + try: + args = struct.Struct.unpack_from(self, buffer, offset) + except struct.error as e: + if not self.name in ('WaveHeader2', 'WaveHeader5'): + raise + # HACK! For WaveHeader5, when npnts is 0, wData is + # optional. If we couldn't unpack the structure, fill in + # wData with zeros and try again, asserting that npnts is + # zero. + if len(buffer) - offset < self.size: + # missing wData? Pad with zeros + buffer += _buffer('\x00'*(self.size + offset - len(buffer))) + args = struct.Struct.unpack_from(self, buffer, offset) + unpacked = self._unflatten_args(args) + data = dict(zip([f.name for f in self.fields], + unpacked)) + assert data['npnts'] == 0, data['npnts'] + return self._unflatten_args(args) def unpack_dict(self, string): return dict(zip([f.name for f in self.fields], @@ -513,12 +532,15 @@ def loadibw(filename, strict=True): assert waveDataSize == wave_info['npnts'] * t.itemsize, ( '{}, {}, {}, {}'.format( waveDataSize, wave_info['npnts'], t.itemsize, t)) - tail_data = array.array('f', b[-tail:]) - data_b = buffer(buffer(tail_data) + f.read(waveDataSize-tail)) if version == 5: - shape = [n for n in wave_info['nDim'] if n > 0] + shape = [n for n in wave_info['nDim'] if n > 0] or (0,) else: shape = (wave_info['npnts'],) + if wave_info['npnts'] == 0: + data_b = buffer('') + else: + tail_data = array.array('f', b[-tail:]) + data_b = buffer(buffer(tail_data) + f.read(waveDataSize-tail)) data = numpy.ndarray( shape=shape, dtype=t.newbyteorder(byteOrder), -- 2.26.2