diff --git a/docs/history.rst b/docs/history.rst index dfe640f7..732d0f9e 100644 --- a/docs/history.rst +++ b/docs/history.rst @@ -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 ------ diff --git a/rioxarray/_io.py b/rioxarray/_io.py index 44c378d3..e376254f 100644 --- a/rioxarray/_io.py +++ b/rioxarray/_io.py @@ -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: diff --git a/test/integration/test_integration__io.py b/test/integration/test_integration__io.py index 0156f510..2b4da10f 100644 --- a/test/integration/test_integration__io.py +++ b/test/integration/test_integration__io.py @@ -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):