From: W. Trevor King Date: Tue, 17 Jul 2012 11:49:06 +0000 (-0400) Subject: API cleanup for binarywave (loadibw->load, move checksum to util, ...). X-Git-Tag: v0.2~39 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=4e9f269977ac7c8a690d1a03b521715876329811;p=igor.git API cleanup for binarywave (loadibw->load, move checksum to util, ...). Also moved byte_order to the util module and renamed a few functions internal to the binarywave module so they start with an underscore. The loadibw -> load (and saveibw -> save) change is because from igor.binarywave import load gives you enough of an idea about what you're importing. If you want to keep it explicit in your client module, use from igor.binarywave import load as loadibw which we do in the test.py module. --- diff --git a/igor/binarywave.py b/igor/binarywave.py index a79c6be..70af310 100644 --- a/igor/binarywave.py +++ b/igor/binarywave.py @@ -35,6 +35,8 @@ import numpy as _numpy from .struct import Structure as _Structure from .struct import Field as _Field from .util import assert_null as _assert_null +from .util import byte_order as _byte_order +from .util import checksum as _checksum # Numpy doesn't support complex integers by default, see @@ -226,22 +228,14 @@ WaveHeader5 = _Structure( # Begin functions from ReadWave.c -def need_to_reorder_bytes(version): +def _need_to_reorder_bytes(version): # If the low order byte of the version field of the BinHeader # structure is zero then the file is from a platform that uses # different byte-ordering and therefore all data will need to be # reordered. return version & 0xFF == 0 -def byte_order(needToReorderBytes): - little_endian = _sys.byteorder == 'little' - if needToReorderBytes: - little_endian = not little_endian - if little_endian: - return '<' # little-endian - return '>' # big-endian - -def version_structs(version, byte_order): +def _version_structs(version, byte_order): if version == 1: bin = BinHeader1 wave = WaveHeader2 @@ -265,20 +259,8 @@ def version_structs(version, byte_order): wave.set_byte_order(byte_order) return (bin, wave, checkSumSize) -def checksum(buffer, byte_order, oldcksum, numbytes): - x = _numpy.ndarray( - (numbytes/2,), # 2 bytes to a short -- ignore trailing odd byte - dtype=_numpy.dtype(byte_order+'h'), - buffer=buffer) - oldcksum += x.sum() - if oldcksum > 2**31: # fake the C implementation's int rollover - oldcksum %= 2**32 - if oldcksum > 2**31: - oldcksum -= 2**31 - return oldcksum & 0xffff - # Translated from ReadWave() -def loadibw(filename, strict=True): +def load(filename, strict=True): if hasattr(filename, 'read'): f = filename # filename is actually a stream object else: @@ -287,16 +269,17 @@ def loadibw(filename, strict=True): BinHeaderCommon.set_byte_order('=') b = buffer(f.read(BinHeaderCommon.size)) version = BinHeaderCommon.unpack_dict_from(b)['version'] - needToReorderBytes = need_to_reorder_bytes(version) - byteOrder = byte_order(needToReorderBytes) + needToReorderBytes = _need_to_reorder_bytes(version) + byteOrder = _byte_order(needToReorderBytes) if needToReorderBytes: BinHeaderCommon.set_byte_order(byteOrder) version = BinHeaderCommon.unpack_dict_from(b)['version'] - bin_struct,wave_struct,checkSumSize = version_structs(version, byteOrder) + bin_struct,wave_struct,checkSumSize = _version_structs( + version, byteOrder) b = buffer(b + f.read(bin_struct.size + wave_struct.size - BinHeaderCommon.size)) - c = checksum(b, byteOrder, 0, checkSumSize) + c = _checksum(b, byteOrder, 0, checkSumSize) if c != 0: raise ValueError( ('This does not appear to be a valid Igor binary wave file. ' @@ -431,5 +414,5 @@ def loadibw(filename, strict=True): return data, bin_info, wave_info -def saveibw(filename): +def save(filename): raise NotImplementedError diff --git a/igor/util.py b/igor/util.py index 7b2c34f..55c015c 100644 --- a/igor/util.py +++ b/igor/util.py @@ -4,6 +4,8 @@ import sys as _sys +import numpy as _numpy + def hex_bytes(buffer, spaces=None): r"""Pretty-printing for binary buffers. @@ -51,3 +53,25 @@ def assert_null(buffer, strict=True): else: _sys.stderr.write( 'warning: post-data padding not zero: {}\n'.format(hex_string)) + +# From ReadWave.c +def byte_order(needToReorderBytes): + little_endian = _sys.byteorder == 'little' + if needToReorderBytes: + little_endian = not little_endian + if little_endian: + return '<' # little-endian + return '>' # big-endian + +# From ReadWave.c +def checksum(buffer, byte_order, oldcksum, numbytes): + x = _numpy.ndarray( + (numbytes/2,), # 2 bytes to a short -- ignore trailing odd byte + dtype=_numpy.dtype(byte_order+'h'), + buffer=buffer) + oldcksum += x.sum() + if oldcksum > 2**31: # fake the C implementation's int rollover + oldcksum %= 2**32 + if oldcksum > 2**31: + oldcksum -= 2**31 + return oldcksum & 0xffff diff --git a/test/test.py b/test/test.py index efe7019..f52df5d 100644 --- a/test/test.py +++ b/test/test.py @@ -699,7 +699,7 @@ import os.path from pprint import pformat import sys -from igor.binarywave import loadibw +from igor.binarywave import load as loadibw from igor.packed import load as loadpxp