Skip to content

Commit

Permalink
General update regarding raise calls that I misunderstood.
Browse files Browse the repository at this point in the history
  • Loading branch information
ratal authored and ratal committed Mar 31, 2015
1 parent 8f4c08d commit a6299aa
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 53 deletions.
19 changes: 19 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
.eric6project/
_eric6project/
.eric5project/
_eric5project/
.eric4project/
_eric4project/
.ropeproject/
_ropeproject/
.directory/
*.pyc
*.pyo
*.orig
*.bak
*.rej
*~
cur/
tmp/
__pycache__/
*.DS_Store
33 changes: 17 additions & 16 deletions mdf3reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,24 @@
--------------
PythonVersion : float
Python version currently running, needed for compatibility of both python 2.6+ and 3.2+
mdf3reader module
--------------------------
"""

from numpy import average, right_shift, bitwise_and, all, diff, max, min, interp
from numpy import asarray, zeros, recarray, array, reshape
from numpy.core.records import fromfile
from math import log, exp
from sys import platform, version_info
from time import strftime, time
from struct import pack, Struct
from io import open # for python 3 and 2 consistency
from io import open # for python 3 and 2 consistency
from mdfinfo3 import info3
PythonVersion=version_info
PythonVersion=PythonVersion[0]
PythonVersion = version_info
PythonVersion = PythonVersion[0]

def processDataBlocks(Q, buf, info, dataGroup, channelList, multiProc ):
def processDataBlocks(Q, buf, info, dataGroup, channelList, multiProc):
"""Put raw data from buf to a dict L and processes nested nBit channels
Parameters
Expand Down Expand Up @@ -66,28 +67,28 @@ def processDataBlocks(Q, buf, info, dataGroup, channelList, multiProc ):
allChannel=True
else:
allChannel=False
## Processes Bits, metadata
# Processes Bits, metadata
for recordID in buf.keys():
for chan in buf[recordID]['record']:
channelName=chan.name
if (allChannel or channelName in channelList) and chan.signalDataType not in (132, 133):
recordName=buf[recordID]['record'].recordToChannelMatching[channelName] # in case record is used for several channels
temp = buf[recordID]['data'].__getattribute__( str(recordName)+'_title') # extract channel vector

if chan.channelType ==1: # master channel
if chan.channelType ==1: # master channel
channelName = 'master' + str( dataGroup )

# Process concatenated bits inside uint8
if not chan.bitCount//8.0==chan.bitCount/8.0: # if channel data do not use complete bytes
mask = int(pow(2, chan.bitCount+1)-1) # masks isBitUnit8
if chan.signalDataType in (0,1, 9, 10, 13, 14): # integers
temp = right_shift(temp, chan.bitOffset)
if chan.signalDataType in (0, 1, 9, 10, 13, 14): # integers
temp = right_shift(temp, chan.bitOffset)
temp = bitwise_and(temp, mask )
L[channelName] = temp
else: # should not happen
print('bit count and offset not applied to correct data type')
L[channelName] = temp
else: #data using full bytes
else: # data using full bytes
L[channelName] = temp

if multiProc:
Expand Down Expand Up @@ -194,7 +195,7 @@ def logConv(data, conv): # 8 Logarithmic
-----------
converted data to physical value
"""
if conv['P4'] == 0 and conv['P1'] != 0 and conv['P2'] != 0:
if conv['P4'] == 0 and conv['P1'] != 0 and conv['P2'] != 0:
return log(((data - conv['P7'] ) * conv['P6'] - conv['P3'] ) / conv['P1'] ) / conv['P2']
elif conv['P1'] == 0 and conv['P4'] != 0 and conv['P5'] != 0:
return log((conv['P3'] / (data - conv['P7'] ) - conv['P6'] ) / conv['P4'] ) / conv['P5']
Expand Down Expand Up @@ -700,8 +701,7 @@ def read3( self, fileName=None, info=None, multiProc=False, channelList=None, co
try:
fid = open(self.fileName, 'rb')
except IOError:
print('Can not find file'+self.fileName)
raise
raise Exception('Can not find file '+self.fileName)

# Look for the biggest group to process first, to reduce processing time when mutiprocessed
dataGroupList = dict.fromkeys(list(range( info['HDBlock']['numberOfDataGroups'])))
Expand Down Expand Up @@ -803,7 +803,8 @@ def getChannelData3(self, channelName):
if channelName in self:
return self.convert3(channelName)
else:
raise('Channel not in dictionary')
raise KeyError('Channel not in dictionary')
return channelName

def convert3(self, channelName):
"""converts specific channel from raw to physical data according to CCBlock information
Expand Down Expand Up @@ -1086,8 +1087,8 @@ def writePointer(f, pointer, value):
elif data.dtype.kind in ['S', 'U']:
dataType = 7
else:
print('Not recognized dtype')
raise
raise Exception('Not recognized dtype')
return data.dtype
if not data.dtype.kind in ['S', 'U']:
dataTypeList += data.dtype.char
else:
Expand Down
12 changes: 6 additions & 6 deletions mdf4reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ def DATABlock(record, parent_block, channelList=None, sortedFlag=True):
try:
from zlib import decompress
except:
raise('zlib module not found or error while uncompressing')
raise ImportError('zlib module not found or error while uncompressing')
if PythonVersion>3:
parent_block['data']=decompress(parent_block['data']) # decompress data
else:
Expand Down Expand Up @@ -403,7 +403,7 @@ def load(self, record, zip=None, nameList=None, sortedFlag=True): # reads sorted
temps['data']=self.fid.read( temps['dz_data_length'] )
temps['data']=DATABlock(record, parent_block=temps, channelList=nameList, sortedFlag=sortedFlag)
else:
raise('unknown data block')
raise Exception('unknown data block')
return temps['data']

def readRecord(self, recordID, buf, channelList=None):
Expand Down Expand Up @@ -965,8 +965,7 @@ def read4( self, fileName=None, info = None, multiProc = False, channelList=None
try:
fid = open( self.fileName, 'rb' )
except IOError:
print('Can not find file'+self.fileName)
raise
raise Exception('Can not find file '+self.fileName)

if self.multiProc:
# prepare multiprocessing of dataGroups
Expand Down Expand Up @@ -1109,7 +1108,8 @@ def getChannelData4(self, channelName):
if channelName in self:
return convertChannelData4(self[channelName], channelName, self.convert_tables)[channelName]
else:
raise('Channel not in dictionary')
raise KeyError('Channel not in dictionary')
return channelName

def convertChannel4(self, channelName):
"""converts specific channel from raw to physical data according to CCBlock information
Expand Down Expand Up @@ -1497,7 +1497,7 @@ def valueToValueTableWOInterpConv(vect, cc_val):
try:
from scipy import interpolate
except:
raise('Please install scipy to convert channel')
raise ImportError('Please install scipy to convert channel')
f = interpolate.interp1d( intVal, physVal , kind='nearest', bounds_error=False) # nearest
return f(vect) # fill with Nan out of bounds while should be bounds
else:
Expand Down
3 changes: 1 addition & 2 deletions mdfinfo3.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ def __init__(self, fileName=None, fid=None, filterChannelNames = False):
try:
fid = open( self.fileName, 'rb' )
except IOError:
print('Can not find file'+self.fileName)
raise
raise IOError('Can not find file '+self.fileName)
self.readinfo3( fid )
elif fileName == None and fid!=None:
self.readinfo3(fid)
Expand Down
3 changes: 1 addition & 2 deletions mdfinfo4.py
Original file line number Diff line number Diff line change
Expand Up @@ -761,8 +761,7 @@ def __init__(self, fileName=None, fid=None):
try:
fid = open( self.fileName, 'rb' )
except IOError:
print('Can not find file '+self.fileName)
raise
raise IOError('Can not find file '+self.fileName)
self.readinfo( fid )
# Close the file
fid.close()
Expand Down
30 changes: 10 additions & 20 deletions mdfreader.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,7 @@ def readinfo( self, fileName = None, filterChannelNames=False ):
try:
fid = open( self.fileName, 'rb' )
except IOError:
print('Can not find file'+self.fileName)
raise
raise Exception('Can not find file '+self.fileName)
# read Identifier block
fid.seek(28)
VersionNumber = unpack( '<H', fid.read( 2 ) )
Expand Down Expand Up @@ -170,8 +169,7 @@ def listChannels( self, fileName = None ):
try:
fid = open( self.fileName, 'rb' )
except IOError:
print('Can not find file'+self.fileName)
raise
raise Exception('Can not find file '+self.fileName)
# read Identifier block
fid.seek(28)
VersionNumber=unpack( '<H', fid.read( 2 ) )
Expand Down Expand Up @@ -449,8 +447,7 @@ def plot( self, channels ):
try:
import matplotlib.pyplot as plt
except:
print('matplotlib not found' )
raise
raise ImportError('matplotlib not found' )
if type(channels) is str:
channels={channels}
for channelName in channels:
Expand Down Expand Up @@ -636,8 +633,7 @@ def exportToNetCDF( self, filename = None, sampling = None ):
try:
from scipy.io import netcdf
except:
print( 'scipy.io module not found' )
raise
raise ImportError( 'scipy.io module not found' )
def cleanName( name ):
allowedStr=' ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-+_.@'
buf=''
Expand Down Expand Up @@ -733,8 +729,7 @@ def exportToHDF5( self, filename = None, sampling = None ):
import h5py
import os
except:
print( 'h5py not found' )
raise
raise ImportError( 'h5py not found' )
def setAttribute(obj, name, value):
if value is not None and len(value)>0:
try:
Expand Down Expand Up @@ -804,8 +799,7 @@ def exportToMatlab( self, filename = None ):
try:
from scipy.io import savemat
except:
print( 'scipy module not found' )
raise
raise ImportError( 'scipy module not found' )
if filename == None:
filename = splitext(self.fileName)[0]
filename = filename + '.mat'
Expand Down Expand Up @@ -846,8 +840,7 @@ def exportToExcel( self , filename = None ):
else:
import xlwt3 as xlwt
except:
print( 'xlwt module missing' )
raise
raise ImportError( 'xlwt module missing' )
if filename == None:
filename = filename = splitext(self.fileName)[0]
filename = filename + '.xls'
Expand Down Expand Up @@ -918,8 +911,7 @@ def exportToXlsx(self, filename=None):
try:
import openpyxl
except:
print('Module openpyxl missing')
raise
raise ImportError('Module openpyxl missing')
if filename == None:
filename = splitext(self.fileName)[0]
filename = filename + '.xlsx'
Expand Down Expand Up @@ -1027,8 +1019,7 @@ def mergeMdf(self, mdfClass):
self.convertAllChannel() # make sure all channels are converted
unionedList=list(mdfClass.keys()) and list(self.keys())
if not len(list(self.masterChannelList.keys()))==1:
print( 'Data not resampled')
raise
raise Exception( 'Data not resampled')
initialTimeSize=len(self.getChannelData('master'))
for channel in unionedList:
data = self.getChannelData(channel)
Expand Down Expand Up @@ -1067,8 +1058,7 @@ def convertToPandas(self, sampling=None):
try:
import pandas as pd
except:
print('Module pandas missing')
raise
raise ImportError('Module pandas missing')
if sampling is not None:
self.resample(sampling)
datetimeInfo=datetime64(self.date.replace(':','-')+'T'+self.time)
Expand Down
9 changes: 3 additions & 6 deletions mdfreaderui.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,7 @@ def on_Convert_clicked(self):
if not len(self.resampleValue.text())==0:
resampleFlag=True
else:
print('Empty field for resampling')
raise
raise Exception('Empty field for resampling')
else:
resampleFlag=False
args=[(str(self.FileList.takeItem(0).text()),channelList,resampleFlag,resampleValue,convertFlag,convertSelection) for i in range(self.FileList.count())]
Expand All @@ -112,8 +111,7 @@ def on_Convert_clicked(self):
# import first file
fileNameList=[]
if len(self.resampleValue.text())==0:
print('Wrong value for re-sampling')
raise
raise Exception('Wrong value for re-sampling')
convertFlag=False
convertSelection=self.convertSelection
resampleValue=float(self.resampleValue.text())
Expand Down Expand Up @@ -326,8 +324,7 @@ def processMDFstar(args):
return processMDF(*args)
except :
print('Error, following file might be corrupted : '+args[0]) # Shows fileName and parameters to help finding corrupted files
print('Please re-try by removing this file from the list and restart mdfconverter to kill processes and clean memory')
raise # produce error and stops all processes
raise Exception('Please re-try by removing this file from the list and restart mdfconverter to kill processes and clean memory')

def convertChannelList(channelList):
if PythonVersion<3:
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.0.5',
version='0.0.6',

description='A Measured Data Format file parser',
long_description=long_description,
Expand Down

0 comments on commit a6299aa

Please sign in to comment.