Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Fix indexing error when mask_and_scale=True was combined with band dim chunking #388

Merged
merged 3 commits into from
Aug 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: Fix indexing error when `mask_and_scale=True` was combined with band dim chunking (issue #387, pull #388)

0.6.0
------
Expand Down
8 changes: 3 additions & 5 deletions rioxarray/_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,11 +288,9 @@ def _getitem(self, key):
if self.masked:
out = np.ma.filled(out.astype(self.dtype), self.fill_value)
if self.mask_and_scale:
for band in np.atleast_1d(band_key):
band_iii = band - 1
out[band_iii] = (
out[band_iii] * riods.scales[band_iii]
+ riods.offsets[band_iii]
for iii, band_iii in enumerate(np.atleast_1d(band_key) - 1):
out[iii] = (
out[iii] * riods.scales[band_iii] + riods.offsets[band_iii]
)

if squeeze_axis:
Expand Down
17 changes: 17 additions & 0 deletions test/integration/test_integration__io.py
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,23 @@ def test_chunks():
assert_allclose(ac, ex)


def test_chunks_with_mask_and_scale():
with create_tmp_geotiff(
10, 10, 4, transform_args=[1, 2, 0.5, 2.0], crs="+proj=latlong"
) as (tmp_file, expected):
# Chunk at open time
with rioxarray.open_rasterio(
tmp_file, mask_and_scale=True, chunks=(1, 2, 2)
) as actual:
assert isinstance(actual.data, dask.array.Array)
assert "open_rasterio" in actual.data.name

# do some arithmetic
ac = actual.mean().compute()
ex = expected.mean()
assert_allclose(ac, ex)


def test_pickle_rasterio():
# regression test for https://github.com/pydata/xarray/issues/2121
with create_tmp_geotiff() as (tmp_file, expected):
Expand Down