From 8f1aa12c3d40cf106ce444bf7bff306dad4096ea Mon Sep 17 00:00:00 2001 From: juliowerner Date: Sun, 22 Jul 2018 16:50:37 -0300 Subject: [PATCH 1/4] Fixed get_data for REAL 8 elements,, previous version allowed just REAL 4x --- nefis/dataset.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/nefis/dataset.py b/nefis/dataset.py index 30221a5..2d25c45 100644 --- a/nefis/dataset.py +++ b/nefis/dataset.py @@ -19,11 +19,16 @@ MAXDIMS = 5 MAXGROUPS = 100 MAXELEMENTS = 1000 -DTYPES = { +DTYPES32 = { 'REAL': np.float32, 'INTEGER': np.int32, 'CHARACTE': bytes } +DTYPES64 = { + 'REAL': np.float64, + 'INTEGER': np.int64, + 'CHARACTE': bytes +} dump_tmpl = """ @@ -375,7 +380,12 @@ def get_data(self, group, element, t=0): length ) # lookup data type - dtype = DTYPES[elm_type.strip()] + if elm_single_byte == 4: + dtype = DTYPES32[elm_type.strip()] + elif elm_single_byte == 8: + dtype = DTYPES64[elm_type.strip()] + else: + raise ValueError("Element type is not 32/64 bits") if dtype is bytes: # return bytes return buffer_res.rstrip() From 2d88457b94bffc50f5eb6a03cd44508c4fc29ca2 Mon Sep 17 00:00:00 2001 From: juliowerner Date: Tue, 24 Jul 2018 22:18:59 -0300 Subject: [PATCH 2/4] Now fixed without created other issues with characters ;) --- nefis/dataset.py | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/nefis/dataset.py b/nefis/dataset.py index 2d25c45..36ad841 100644 --- a/nefis/dataset.py +++ b/nefis/dataset.py @@ -19,17 +19,6 @@ MAXDIMS = 5 MAXGROUPS = 100 MAXELEMENTS = 1000 -DTYPES32 = { - 'REAL': np.float32, - 'INTEGER': np.int32, - 'CHARACTE': bytes -} -DTYPES64 = { - 'REAL': np.float64, - 'INTEGER': np.int64, - 'CHARACTE': bytes -} - dump_tmpl = """ nefis ${ds.def_file} { @@ -351,7 +340,6 @@ def get_data(self, group, element, t=0): elm_count, elm_dimensions) = result logger.info('got info: %s', result) - usr_index = np.zeros((5, 3), dtype=np.int32) # first timestep: 0 -> 1 based usr_index[0, 0] = t + 1 @@ -380,18 +368,28 @@ def get_data(self, group, element, t=0): length ) # lookup data type - if elm_single_byte == 4: - dtype = DTYPES32[elm_type.strip()] - elif elm_single_byte == 8: - dtype = DTYPES64[elm_type.strip()] + datatype = elm_type.strip().upper() + if datatype == 'CHARACTE': + dtype = bytes + elif datatype == 'REAL': + if elm_single_byte == 4: + dtype = np.float32 + elif elm_single_byte == 8: + dtype = np.float64 + elif datatype == 'INTEGER': + if elm_single_byte == 4: + dtype = np.int32 + elif elm_single_byte == 8: + dtype = np.int64 else: - raise ValueError("Element type is not 32/64 bits") + raise ValueError() if dtype is bytes: # return bytes return buffer_res.rstrip() # convert to typed array data = np.fromstring(buffer_res, dtype=dtype) # return shaped array + print(data) return data.reshape(elm_dimensions[::-1]) def dump_json(self): From 1a66fc93dc60cf81b5335bf0eecbe0133985e99d Mon Sep 17 00:00:00 2001 From: juliowerner Date: Tue, 24 Jul 2018 22:37:48 -0300 Subject: [PATCH 3/4] Added value error message and code were formatted through autopep8. --- nefis/dataset.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/nefis/dataset.py b/nefis/dataset.py index 36ad841..be43a72 100644 --- a/nefis/dataset.py +++ b/nefis/dataset.py @@ -42,6 +42,7 @@ class NefisException(Exception): """A nefis exception""" + def __init__(self, message, status): super(Exception, self).__init__(message) self.status = status @@ -81,7 +82,6 @@ def flat(self): ) - def wrap_error(func): """wrap a nefis function to raise an error""" @functools.wraps(func) @@ -114,6 +114,7 @@ def default(self, obj): class Nefis(object): """Nefis file""" + def __init__(self, def_file, ac_type=b'r', coding=b' '): """dat file is expected to be named .dat instead of .def""" @@ -369,20 +370,20 @@ def get_data(self, group, element, t=0): ) # lookup data type datatype = elm_type.strip().upper() - if datatype == 'CHARACTE': + if datatype == 'CHARACTE': dtype = bytes elif datatype == 'REAL': if elm_single_byte == 4: dtype = np.float32 elif elm_single_byte == 8: dtype = np.float64 - elif datatype == 'INTEGER': + elif datatype == 'INTEGER': if elm_single_byte == 4: dtype = np.int32 elif elm_single_byte == 8: dtype = np.int64 else: - raise ValueError() + raise ValueError('Invalid Datatype: {} {}'.format(elm_type.strio(), elm_single_byte)) if dtype is bytes: # return bytes return buffer_res.rstrip() From 4203b92dbb1879f806a8822b0b09822be7bcdc64 Mon Sep 17 00:00:00 2001 From: juliowerner Date: Tue, 24 Jul 2018 22:48:42 -0300 Subject: [PATCH 4/4] removed forgotten print --- nefis/dataset.py | 1 - 1 file changed, 1 deletion(-) diff --git a/nefis/dataset.py b/nefis/dataset.py index be43a72..70e6675 100644 --- a/nefis/dataset.py +++ b/nefis/dataset.py @@ -390,7 +390,6 @@ def get_data(self, group, element, t=0): # convert to typed array data = np.fromstring(buffer_res, dtype=dtype) # return shaped array - print(data) return data.reshape(elm_dimensions[::-1]) def dump_json(self):