Skip to content

Commit

Permalink
Slight modifications to improve the use of the default_dtype parameter (
Browse files Browse the repository at this point in the history
#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
  • Loading branch information
serazing authored and rabernat committed Jan 31, 2017
1 parent d6f73cd commit 8cf75ad
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 4 deletions.
5 changes: 4 additions & 1 deletion xmitgcm/mds_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 6 additions & 0 deletions xmitgcm/test/test_mds_store.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
9 changes: 6 additions & 3 deletions xmitgcm/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 8cf75ad

Please sign in to comment.