Skip to content

Commit

Permalink
BUG: Compatibility changes with xarray 0.17 (issue corteva#254)
Browse files Browse the repository at this point in the history
  • Loading branch information
snowman2 committed Feb 26, 2021
1 parent 0804791 commit 775b2a3
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 14 deletions.
1 change: 1 addition & 0 deletions docs/history.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ History

Latest
------
- BUG: Compatibility changes with xarray 0.17 (issue #254)

0.3.0
------
Expand Down
10 changes: 8 additions & 2 deletions rioxarray/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,12 @@ def open_rasterio(
result = _prepare_dask(result, riods, filename, chunks)

# Make the file closeable
result._file_obj = manager

try:
# xarray 0.17 +
result.set_close(manager.close)
except AttributeError:
result._file_obj = manager
result.rio._manager = manager
# add file path to encoding
result.encoding["source"] = riods.name
return result
2 changes: 1 addition & 1 deletion rioxarray/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def __init__(self, xds: DataArray):
self.res = (abs(res[0]), abs(res[1]))
self.transform = xds.rio.transform(recalc=True)
try:
rio_file = xds._file_obj.acquire()
rio_file = xds.rio._manager.acquire()
self.profile = rio_file.profile
self.colormap = rio_file.colormap
except AttributeError:
Expand Down
7 changes: 4 additions & 3 deletions rioxarray/raster_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ def _clip_from_disk(xds, geometries, all_touched, drop, invert):
"""
try:
out_image, out_transform = rasterio.mask.mask(
xds._file_obj.acquire(),
xds.rio._manager.acquire(),
geometries,
all_touched=all_touched,
invert=invert,
Expand Down Expand Up @@ -199,6 +199,7 @@ def __init__(self, xarray_obj):
super().__init__(xarray_obj)
# properties
self._nodata = None
self._manager = None # https://github.com/corteva/rioxarray/issues/254

def set_nodata(self, input_nodata, inplace=True):
"""
Expand Down Expand Up @@ -278,7 +279,7 @@ def nodata(self):
# look in places used by `xarray.open_rasterio`
if self._nodata is None:
try:
self._nodata = self._obj._file_obj.acquire().nodata
self._nodata = self._manager.acquire().nodata
except AttributeError:
try:
self._nodata = self._obj.attrs["nodatavals"][0]
Expand Down Expand Up @@ -874,7 +875,7 @@ def to_raster(
# get the output profile from the rasterio object
# if opened with xarray.open_rasterio()
try:
out_profile = self._obj._file_obj.acquire().profile
out_profile = self._manager.acquire().profile
except AttributeError:
out_profile = {}
out_profile.update(profile_kwargs)
Expand Down
31 changes: 23 additions & 8 deletions test/integration/test_integration__io.py
Original file line number Diff line number Diff line change
Expand Up @@ -265,7 +265,9 @@ def test_open_rasterio_mask_chunk_clip():
assert str(type(xdi.data)) == "<class 'dask.array.core.Array'>"
assert xdi.chunks == ((1,), (245,), (574,))
assert np.isnan(xdi.values).sum() == 52119
assert xdi.encoding == {"_FillValue": 0.0}
test_encoding = dict(xdi.encoding)
assert test_encoding.pop("source").endswith("small_dem_3m_merged.tif")
assert test_encoding == {"_FillValue": 0.0}
attrs = dict(xdi.attrs)
assert_almost_equal(
tuple(xdi.rio._cached_transform())[:6],
Expand Down Expand Up @@ -301,15 +303,19 @@ def test_open_rasterio_mask_chunk_clip():
# test data array
clipped = xdi.rio.clip(geometries, comp_subset.rio.crs)
_assert_xarrays_equal(clipped, comp_subset)
assert clipped.encoding == {"_FillValue": 0.0}
test_encoding = dict(clipped.encoding)
assert test_encoding.pop("source").endswith("small_dem_3m_merged.tif")
assert test_encoding == {"_FillValue": 0.0}

# test dataset
clipped_ds = xdi.to_dataset(name="test_data").rio.clip(
geometries, subset.rio.crs
)
comp_subset_ds = comp_subset.to_dataset(name="test_data")
_assert_xarrays_equal(clipped_ds, comp_subset_ds)
assert clipped_ds.test_data.encoding == {"_FillValue": 0.0}
test_encoding = dict(clipped.encoding)
assert test_encoding.pop("source").endswith("small_dem_3m_merged.tif")
assert test_encoding == {"_FillValue": 0.0}


##############################################################################
Expand Down Expand Up @@ -873,13 +879,17 @@ def test_open_cog():


def test_mask_and_scale():
test_file = os.path.join(TEST_INPUT_DATA_DIR, "tmmx_20190121.nc")
with pytest.warns(SerializationWarning):
with rioxarray.open_rasterio(
os.path.join(TEST_INPUT_DATA_DIR, "tmmx_20190121.nc"), mask_and_scale=True
) as rds:
with rioxarray.open_rasterio(test_file, mask_and_scale=True) as rds:
assert np.nanmin(rds.air_temperature.values) == 248.7
assert np.nanmax(rds.air_temperature.values) == 302.1
assert rds.air_temperature.encoding == {
test_encoding = dict(rds.air_temperature.encoding)
source = test_encoding.pop("source")
assert source.startswith("netcdf:") and source.endswith(
"tmmx_20190121.nc:air_temperature"
)
assert test_encoding == {
"_Unsigned": "true",
"add_offset": 220.0,
"scale_factor": 0.1,
Expand All @@ -905,7 +915,12 @@ def test_no_mask_and_scale():
) as rds:
assert np.nanmin(rds.air_temperature.values) == 287
assert np.nanmax(rds.air_temperature.values) == 821
assert rds.air_temperature.encoding == {
test_encoding = dict(rds.air_temperature.encoding)
source = test_encoding.pop("source")
assert source.startswith("netcdf:") and source.endswith(
"tmmx_20190121.nc:air_temperature"
)
assert test_encoding == {
"_FillValue": 32767.0,
"missing_value": 32767,
}
Expand Down

0 comments on commit 775b2a3

Please sign in to comment.