From 4fec906114e918567a6fac3f2d8c5525c6f229ab Mon Sep 17 00:00:00 2001 From: Aymeric Rateau Date: Wed, 17 Jun 2015 21:39:13 +0100 Subject: [PATCH] further fixed bug with signed integers in bitarray data parsing --- mdfconverter/__init__.py | 2 +- mdfreader.e4p | 4 ++-- mdfreader/__init__.py | 2 +- mdfreader/mdf4reader.py | 32 ++++++++++++++++++++++---------- setup.py | 2 +- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/mdfconverter/__init__.py b/mdfconverter/__init__.py index 04d793c..e2c6b5e 100644 --- a/mdfconverter/__init__.py +++ b/mdfconverter/__init__.py @@ -12,4 +12,4 @@ # along with this program. If not, see http://www.gnu.org/licenses. # # ---------------------------------------------------------------------- -__version__ = "0.1.8" +__version__ = "0.1.9" diff --git a/mdfreader.e4p b/mdfreader.e4p index 5e28343..17d0355 100644 --- a/mdfreader.e4p +++ b/mdfreader.e4p @@ -1,7 +1,7 @@ - + en @@ -23,12 +23,12 @@ mdfconverter/mdfconverter.py mdfreader/mdfinfo4.py mdfreader/mdfinfo3.py - mdfreader/tests/test.py mdfreader/mdf3reader.py mdfreader/mdf4reader.py README LICENSE setup.py + mdfreader/tests/tests.py diff --git a/mdfreader/__init__.py b/mdfreader/__init__.py index 5081b13..f8ffe48 100644 --- a/mdfreader/__init__.py +++ b/mdfreader/__init__.py @@ -16,7 +16,7 @@ __author__ = 'Aymeric Rateau (aymeric.rateau@gmail.com)' __copyright__ = 'Copyright (c) 2015 Aymeric Rateau' __license__ = 'GPLV3' -__version__ = "0.1.8" +__version__ = "0.1.9" #if it's run as a script or imported within python, this happens if __name__ == 'mdfreader': diff --git a/mdfreader/mdf4reader.py b/mdfreader/mdf4reader.py index 71cb2bd..73478a7 100644 --- a/mdfreader/mdf4reader.py +++ b/mdfreader/mdf4reader.py @@ -990,6 +990,21 @@ def readBitarray(self, bita, channelList=None): if channel.name in channelList: format.append(channel.RecordFormat) buf = recarray(self.numberOfRecords, format) + def signedInt(temp, extension): + """ extend bits of signed data managing two's complement + """ + extension.setall(False) + extensionInv = bitarray(extension, endian='little') + extensionInv.setall(True) + for i in range(self.numberOfRecords): # extend data of bytes to match numpy requirement + signBit = temp[i][-1] + if not signBit: # positive value, extend with 0 + temp[i].extend(extension) + else: # negative value, extend with 1 + signBit = temp[i].pop(-1) + temp[i].extend(extensionInv) + temp[i].append(signBit) + return temp # read data for chan in range(len(self)): if self[chan].name in channelList: @@ -1006,16 +1021,13 @@ def readBitarray(self, bita, channelList=None): for i in range(self.numberOfRecords): # extend data of bytes to match numpy requirement temp[i].extend(byte) else: # signed integer (two's complement), keep sign bit and extend with bytes - byteInv = bitarray(len(byte), endian='little') - byteInv.setall(True) - for i in range(self.numberOfRecords): # extend data of bytes to match numpy requirement - signBit = temp[i][-1] - if not signBit: # positive value, extend with 0 - temp[i].extend(byte) - else: # negative value, extend with 1 - signBit = temp[i].pop(-1) - temp[i].extend(byteInv) - temp[i].append(signBit) + temp = signedInt(temp, byte) + nTrailBits = self[chan].nBytes*8 - self[chan].bitCount + if self[chan].signalDataType in (2, 3) and \ + nbytes == self[chan].nBytes and \ + nTrailBits > 0: # Ctype byte length but signed integer + trailBits = bitarray(nTrailBits, endian='little') + temp = signedInt(temp, trailBits) if 's' not in self[chan].Format: temp = [self[chan].CFormat.unpack(temp[i].tobytes())[0] \ for i in range(self.numberOfRecords)] diff --git a/setup.py b/setup.py index c0f68f7..5674337 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ # Versions should comply with PEP440. For a discussion on single-sourcing # the version across setup.py and the project code, see # https://packaging.python.org/en/latest/development.html#single-sourcing-the-version - version='0.1.8', + version='0.1.9', description='A Measured Data Format file parser', long_description=long_description,