Skip to content

Commit

Permalink
attempt to fix #497 (#1382)
Browse files Browse the repository at this point in the history
* fixes #497 by catching scipy netcdf error and adding additional suggestions before rethrowing

* adding test

* flake8 passes
  • Loading branch information
gidden authored and shoyer committed May 25, 2017
1 parent 5007961 commit f517be7
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
20 changes: 18 additions & 2 deletions xarray/backends/scipy_.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def _decode_attrs(d):


class ScipyArrayWrapper(NdimSizeLenMixin, DunderArrayMixin):

def __init__(self, variable_name, datastore):
self.datastore = datastore
self.variable_name = variable_name
Expand Down Expand Up @@ -77,8 +78,22 @@ def _open_scipy_netcdf(filename, mode, mmap, version):
# it's a NetCDF3 bytestring
filename = BytesIO(filename)

return scipy.io.netcdf_file(filename, mode=mode, mmap=mmap,
version=version)
try:
return scipy.io.netcdf_file(filename, mode=mode, mmap=mmap,
version=version)
except TypeError as e: # netcdf3 message is obscure in this case
errmsg = e.args[0]
if 'is not a valid NetCDF 3 file' in errmsg:
msg = """
If this is a NetCDF4 file, you may need to install the
netcdf4 library, e.g.,
$ pip install netcdf4
"""
errmsg += msg
raise TypeError(errmsg)
else:
raise


class ScipyDataStore(WritableCFDataStore, DataStorePickleMixin):
Expand All @@ -89,6 +104,7 @@ class ScipyDataStore(WritableCFDataStore, DataStorePickleMixin):
It only supports the NetCDF3 file-format.
"""

def __init__(self, filename_or_obj, mode='r', format=None, group=None,
writer=None, mmap=None, autoclose=False):
import scipy
Expand Down
9 changes: 9 additions & 0 deletions xarray/tests/test_backends.py
Original file line number Diff line number Diff line change
Expand Up @@ -897,6 +897,15 @@ def test_netcdf3_endianness(self):
for var in expected.values():
self.assertTrue(var.dtype.isnative)

@requires_netCDF4
def test_nc4_scipy(self):
with create_tmp_file() as tmp_file:
with nc4.Dataset(tmp_file, 'w', format='NETCDF4') as rootgrp:
rootgrp.createGroup('foo')

with self.assertRaisesRegexp(TypeError, 'pip install netcdf4'):
open_dataset(tmp_file, engine='scipy')


class ScipyFilePathTestAutocloseTrue(ScipyFilePathTest):
autoclose = True
Expand Down

0 comments on commit f517be7

Please sign in to comment.