From 8cf75adb250986db5c20345966fe6c58db63cb29 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillaume=20S=C3=A9razin?= Date: Tue, 31 Jan 2017 16:03:07 +0100 Subject: [PATCH] Slight modifications to improve the use of the default_dtype parameter (#34) * Check the default_dtype to add if necessary the default endian * Modify the IOError when the metadata is absent and the default_dtype is needed * Add a test to check if an IOError is correctly raised when there is no metadata and default_dtype is None --- xmitgcm/mds_store.py | 5 ++++- xmitgcm/test/test_mds_store.py | 6 ++++++ xmitgcm/utils.py | 9 ++++++--- 3 files changed, 16 insertions(+), 4 deletions(-) diff --git a/xmitgcm/mds_store.py b/xmitgcm/mds_store.py index 8477772f..0fcc2406 100644 --- a/xmitgcm/mds_store.py +++ b/xmitgcm/mds_store.py @@ -295,7 +295,10 @@ def __init__(self, data_dir, grid_dir=None, if endian not in ['>', '<', '=']: raise ValueError("Invalid byte order (endian=%s)" % endian) self.endian = endian - self.default_dtype = default_dtype + if default_dtype is not None: + self.default_dtype = np.dtype(default_dtype).newbyteorder(endian) + else: + self.default_dtype = default_dtype # storage dicts for variables and attributes self._variables = xr.core.pycompat.OrderedDict() diff --git a/xmitgcm/test/test_mds_store.py b/xmitgcm/test/test_mds_store.py index 6ee522bc..246350d3 100644 --- a/xmitgcm/test/test_mds_store.py +++ b/xmitgcm/test/test_mds_store.py @@ -455,6 +455,12 @@ def test_open_dataset_no_meta(all_mds_datadirs): assert ds['Eta'].dims == dims_2d assert ds['Eta'].values.ndim == len(dims_2d) + with pytest.raises(IOError, message="Expecting IOError when default_dtype " + "is not precised (i.e., None)"): + xmitgcm.open_mdsdataset(dirname, prefix=['T', 'Eta'], iters=it, + geometry=expected['geometry'], + read_grid=False) + # now get rid of the variables used to infer dimensions with hide_file(dirname, 'XC.meta', 'RC.meta'): with pytest.raises(IOError): diff --git a/xmitgcm/utils.py b/xmitgcm/utils.py index 043f5be1..9973e78d 100644 --- a/xmitgcm/utils.py +++ b/xmitgcm/utils.py @@ -87,11 +87,14 @@ def read_mds(fname, iternum=None, use_mmap=True, force_dict=True, endian='>', try: nrecs, shape, name, dtype, fldlist = get_useful_info_from_meta_file(metafile) dtype = dtype.newbyteorder(endian) - except IOError as e: + except IOError: # we can recover from not having a .meta file if dtype and shape have # been specified already - if (shape is None) or (dtype is None): - raise e + if shape is None: + raise IOError("Cannot find the shape associated to %s in the metadata." %fname) + elif dtype is None: + raise IOError("Cannot find the dtype associated to %s in the metadata, " + "please specify the default dtype to avoid this error." %fname) else: nrecs = 1 shape = list(shape)