Skip to content

Commit

Permalink
further fixed bug with signed integers in bitarray data parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
ratal committed Jun 17, 2015
1 parent 73d3593 commit 4fec906
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 15 deletions.
2 changes: 1 addition & 1 deletion mdfconverter/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@
# along with this program. If not, see http://www.gnu.org/licenses.
#
# ----------------------------------------------------------------------
__version__ = "0.1.8"
__version__ = "0.1.9"
4 changes: 2 additions & 2 deletions mdfreader.e4p
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE Project SYSTEM "Project-5.1.dtd">
<!-- eric project file for project mdfreader -->
<!-- Saved: 2015-05-21, 22:22:18 -->
<!-- Saved: 2015-06-15, 23:19:28 -->
<!-- Copyright (C) 2015 Aymeric Rateau, aymeric.rateau@gmail.com -->
<Project version="5.1">
<Language>en</Language>
Expand All @@ -23,12 +23,12 @@
<Source>mdfconverter/mdfconverter.py</Source>
<Source>mdfreader/mdfinfo4.py</Source>
<Source>mdfreader/mdfinfo3.py</Source>
<Source>mdfreader/tests/test.py</Source>
<Source>mdfreader/mdf3reader.py</Source>
<Source>mdfreader/mdf4reader.py</Source>
<Source>README</Source>
<Source>LICENSE</Source>
<Source>setup.py</Source>
<Source>mdfreader/tests/tests.py</Source>
</Sources>
<Forms/>
<Translations/>
Expand Down
2 changes: 1 addition & 1 deletion mdfreader/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
32 changes: 22 additions & 10 deletions mdfreader/mdf4reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand All @@ -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)]
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down

0 comments on commit 4fec906

Please sign in to comment.