Skip to content

Commit

Permalink
Fixes for xarray 0.11 (#128)
Browse files Browse the repository at this point in the history
  • Loading branch information
fmaussion committed Nov 9, 2018
1 parent c3a4ef0 commit e8e441b
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 22 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ matrix:
include:
- env: CONDA_ENV=py27-all MPL=--mpl
- env: CONDA_ENV=py36-all MPL=--mpl
- env: CONDA_ENV=py36-all-xr10 MPL=--mpl
- env: CONDA_ENV=py36-min MPL=
- env: CONDA_ENV=py36-xr MPL=
- env: CONDA_ENV=py36-xarray-dev MPL=--mpl
Expand Down
27 changes: 27 additions & 0 deletions ci/requirements-py36-all-xr10.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
name: test_env
channels:
- conda-forge
dependencies:
- python=3.6
- six
- numpy
- scipy
- pyproj
- joblib
- netCDF4
- shapely
- geopandas
- rasterio
- pandas
- xarray=0.10.9
- dask
- matplotlib<3.0.0
- scikit-image
- Pillow
- descartes
- cartopy
- pip:
- coveralls
- pytest-cov
- pytest-mpl
- motionless
25 changes: 14 additions & 11 deletions salem/graphics.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,17 +472,20 @@ def _check_data(self, data=None, crs=None, interp='nearest',
interp = 1
elif interp.lower() == 'spline':
interp = 3
try:
data = imresize(data.filled(np.NaN),
(self.grid.ny, self.grid.nx),
order=interp, mode='edge',
anti_aliasing=True)
except RuntimeError:
# For some order anti_aliasing doesnt work with 'edge'
data = imresize(data.filled(np.NaN),
(self.grid.ny, self.grid.nx),
order=interp, mode='edge',
anti_aliasing=False)
with warnings.catch_warnings():
mess = "invalid value encountered in reduce"
warnings.filterwarnings("ignore", message=mess)
try:
data = imresize(data.filled(np.NaN),
(self.grid.ny, self.grid.nx),
order=interp, mode='edge',
anti_aliasing=True)
except RuntimeError:
# For some order anti_aliasing doesnt work with 'edge'
data = imresize(data.filled(np.NaN),
(self.grid.ny, self.grid.nx),
order=interp, mode='edge',
anti_aliasing=False)

elif isinstance(crs, Grid):
# Remap
Expand Down
23 changes: 16 additions & 7 deletions salem/sio.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,6 @@

try:
import xarray as xr
from xarray.backends.netCDF4_ import NetCDF4DataStore
from xarray.core.pycompat import basestring
from xarray.backends.api import _MultiFileCloser, _default_lock
from xarray.core import dtypes
has_xarray = True
except ImportError:
has_xarray = False
Expand All @@ -40,6 +36,19 @@ def __call__(self, func):
except ImportError:
pass

if has_xarray:
from xarray.backends.netCDF4_ import NetCDF4DataStore
from xarray.core.pycompat import basestring
from xarray.backends.api import _MultiFileCloser
from xarray.core import dtypes
try:
from xarray.backends.locks import (NETCDFC_LOCK, HDF5_LOCK,
combine_locks)
NETCDF4_PYTHON_LOCK = combine_locks([NETCDFC_LOCK, HDF5_LOCK])
except ImportError:
# xarray < v0.11
from xarray.backends.api import _default_lock as NETCDF4_PYTHON_LOCK


def read_shapefile(fpath, cached=False):
"""Reads a shapefile using geopandas.
Expand Down Expand Up @@ -981,10 +990,10 @@ def open_wrf_dataset(file, **kwargs):
time = netcdf_time(ds)
if time is not None:
ds['Time'] = time
ds.rename({'Time':'time'}, inplace=True)
ds = ds.rename({'Time':'time'})
tr = {'Time': 'time', 'XLAT': 'lat', 'XLONG': 'lon', 'XTIME': 'xtime'}
tr = {k: tr[k] for k in tr.keys() if k in ds.variables}
ds.rename(tr, inplace=True)
ds = ds.rename(tr)

# drop ugly vars
vns = ['Times', 'XLAT_V', 'XLAT_U', 'XLONG_U', 'XLONG_V']
Expand Down Expand Up @@ -1133,7 +1142,7 @@ def open_mf_wrf_dataset(paths, chunks=None, compat='no_conflicts', lock=None,
dask.config.set(scheduler='single-threaded')

if lock is None:
lock = _default_lock(paths[0], 'netcdf4')
lock = NETCDF4_PYTHON_LOCK
datasets = [open_wrf_dataset(p, chunks=chunks or {}, lock=lock)
for p in paths]
file_objs = [ds._file_obj for ds in datasets]
Expand Down
8 changes: 4 additions & 4 deletions salem/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,16 +173,16 @@ def test_read_to_grid(self):
grid=g.grid,
**d)
try:
h1 = res.argument_hash
except AttributeError:
h1 = res.timestamp
except AttributeError:
h1 = res.argument_hash
res = _memory_shapefile_to_grid.call_and_shelve(shape_cpath,
grid=g.grid,
**d2)
try:
h2 = res.argument_hash
except AttributeError:
h2 = res.timestamp
except AttributeError:
h2 = res.argument_hash
self.assertEqual(h1, h2)


Expand Down

0 comments on commit e8e441b

Please sign in to comment.