From 1effe91a0f18fe1c948fc22a732e1de8e4961a63 Mon Sep 17 00:00:00 2001 From: "HauptMachine\\vonGostev" Date: Sat, 1 May 2021 22:22:36 +0300 Subject: [PATCH] Add path to error handling --- setup.py | 8 ++++---- tekwfm2/tekwfm.py | 38 ++++++++++++++++++++------------------ 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/setup.py b/setup.py index 3fb4f6e..f382886 100644 --- a/setup.py +++ b/setup.py @@ -12,7 +12,7 @@ setuptools.setup( name="tekwfm2", - version="0.1.0", + version="0.1.1", author="Pavel Gostev", author_email="gostev.pavel@physics.msu.ru", description="Parser for Tektronix WFM v.2 binary files", @@ -27,7 +27,7 @@ ], python_requires='>=3.6', include_package_data=True, - install_requires = [ - 'numpy' - ], + install_requires=[ + 'numpy' + ], ) diff --git a/tekwfm2/tekwfm.py b/tekwfm2/tekwfm.py index 4f88dc9..1273eac 100644 --- a/tekwfm2/tekwfm.py +++ b/tekwfm2/tekwfm.py @@ -20,7 +20,6 @@ """ - import struct import numpy as np @@ -30,28 +29,30 @@ class WfmReadError(Exception): pass -def read_wfm(target): - """return sample data from target WFM file""" - with open(target, 'rb') as f: +def read_wfm(path): + """return sample data from path WFM file""" + with open(path, 'rb') as f: hbytes = f.read(838) - meta = decode_header(hbytes) + meta = decode_header(path, hbytes) # file signature checks if meta['byte_order'] != 0x0f0f: - raise WfmReadError('big-endian not supported in this version') + raise WfmReadError( + path, 'big-endian not supported in this version') if meta['version'] != b':WFM#002': - raise WfmReadError('only version 2 wfms supported in this version') + raise WfmReadError( + path, 'only version 2 wfms supported in this version') if meta['imp_dim_count'] != 1: - raise WfmReadError('imp dim count not 1') + raise WfmReadError(path, 'imp dim count not 1') if meta['exp_dim_count'] != 1: - raise WfmReadError('exp dim count not 1') + raise WfmReadError(path, 'exp dim count not 1') if meta['record_type'] != 2: - raise WfmReadError('not WFMDATA_VECTOR') + raise WfmReadError(path, 'not WFMDATA_VECTOR') # if meta['exp_dim_1_type'] != 0: - # raise WfmReadError('not EXPLICIT_SAMPLE') + # raise WfmReadError(path, 'not EXPLICIT_SAMPLE') if meta['time_base_1'] != 0: - raise WfmReadError('not BASE_TIME') + raise WfmReadError(path, 'not BASE_TIME') if meta['fastframe']: - raise WfmReadError('Fast Frames are not supported') + raise WfmReadError(path, 'Fast Frames are not supported') # read curve block bin_wave = np.memmap(filename=f, dtype=meta['dformat'], @@ -64,11 +65,11 @@ def read_wfm(target): return scaled_array, meta -def decode_header(header_bytes): +def decode_header(path, header_bytes): """returns a dict of wfm metadata""" wfm_info = {} if len(header_bytes) != 838: - raise WfmReadError('wfm header bytes not 838') + raise WfmReadError(path, 'wfm header bytes not 838') wfm_info['byte_order'] = struct.unpack_from('H', header_bytes, offset=0)[0] wfm_info['version'] = struct.unpack_from('8s', header_bytes, offset=2)[0] wfm_info['imp_dim_count'] = struct.unpack_from( @@ -119,7 +120,8 @@ def decode_header(header_bytes): elif code == 4 and bps == 4: dformat = 'single' else: - raise WfmReadError('data type code or bytes-per-sample not understood') + raise WfmReadError( + path, 'data type code or bytes-per-sample not understood') wfm_info['dformat'] = dformat wfm_info['dlen'] = dsize // bps return wfm_info @@ -143,10 +145,10 @@ def __init__(self, path): self.y, meta = read_wfm(path) except Exception as E: raise WfmReadError(path, E) - + for k, v in meta.items(): setattr(self, k, v) - + samples = len(self.y) self.tstop = samples * self.tscale + self.tstart self.x = np.linspace(self.tstart, self.tstop, samples, endpoint=False)