Skip to content

Commit

Permalink
Add tests for roi (#115)
Browse files Browse the repository at this point in the history
* Add tests for roi

* Add load

* Add close

* Acheck something

* back
  • Loading branch information
fmaussion committed Jul 24, 2018
1 parent 53b43ce commit b1b1a8c
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
6 changes: 5 additions & 1 deletion docs/whats-new.rst
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,16 @@ Enhancements
~~~~~~~~~~~~

- new :py:func:`~Grid.extent_as_polygon` method, which creates a polygon
drawing the contours of a Grid.
outlining the contours of a Grid.


Bug fixes
~~~~~~~~~

- the xarray accessor method ``roi()`` now preserves encoding (:issue:`109`).
By `Johannes Landmann <https://github.com/jlandmann>`_



v0.2.2 (21 June 2018)
---------------------
Expand Down
4 changes: 2 additions & 2 deletions salem/sio.py
Original file line number Diff line number Diff line change
Expand Up @@ -512,15 +512,15 @@ def roi(self, ds=None, **kwargs):
out.attrs = self._obj.attrs
out.encoding = self._obj.encoding
if isinstance(out, xr.Dataset):
for v in self._obj.variables.keys():
for v in self._obj.variables:
out[v].encoding = self._obj[v].encoding
if isinstance(out, xr.DataArray):
out.name = self._obj.name

# add pyproj string everywhere
out.attrs['pyproj_srs'] = self.grid.proj.srs
if isinstance(out, xr.Dataset):
for v in out.variables:
for v in out.data_vars:
out.variables[v].attrs = self._obj.variables[v].attrs
out.variables[v].attrs['pyproj_srs'] = self.grid.proj.srs
return out
Expand Down
12 changes: 12 additions & 0 deletions salem/tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,18 @@ def test_era(self):

np.testing.assert_almost_equal(dss.longitude, [90.0, 90.75, 91.5])

@requires_xarray
def test_roi(self):
import xarray as xr
# Check that all attrs are preserved
with sio.open_xr_dataset(get_demo_file('era_interim_tibet.nc')) as ds:
ds.encoding = {'_FillValue': np.NaN}
ds['t2m'].encoding = {'_FillValue': np.NaN}
ds_ = ds.salem.roi(roi=np.ones_like(ds.t2m.values[0, ...]))
xr.testing.assert_identical(ds, ds_)
assert ds.encoding == ds_.encoding
assert ds.t2m.encoding == ds_.t2m.encoding

@requires_xarray
@requires_geopandas # because of the grid tests, more robust with GDAL
def test_basic_wrf(self):
Expand Down

0 comments on commit b1b1a8c

Please sign in to comment.