diff --git a/doc/whats-new.rst b/doc/whats-new.rst index a77a0334de7..1df3a79f0fb 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -21,6 +21,10 @@ v0.12.2 (unreleased) Enhancements ~~~~~~~~~~~~ + +- netCDF chunksizes are now only dropped when original_shape is different, + not when it isn't found. (:issue:`2207`) + By `Karel van de Plassche `_. - Enable `@` operator for DataArray. This is equivalent to :py:meth:`DataArray.dot` By `Maximilian Roos `_. - Add ``fill_value`` argument for reindex, align, and merge operations diff --git a/xarray/backends/netCDF4_.py b/xarray/backends/netCDF4_.py index e411fd3a80e..2396523dca7 100644 --- a/xarray/backends/netCDF4_.py +++ b/xarray/backends/netCDF4_.py @@ -206,7 +206,9 @@ def _extract_nc4_variable_encoding(variable, raise_on_invalid=False, chunks_too_big = any( c > d and dim not in unlimited_dims for c, d, dim in zip(chunksizes, variable.shape, variable.dims)) - changed_shape = encoding.get('original_shape') != variable.shape + has_original_shape = 'original_shape' in encoding + changed_shape = (has_original_shape and + encoding.get('original_shape') != variable.shape) if chunks_too_big or changed_shape: del encoding['chunksizes'] diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index e63cf45b5ed..ad66ecf1286 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -1134,6 +1134,18 @@ def test_encoding_kwarg_compression(self): assert ds.x.encoding == {} + def test_keep_chunksizes_if_no_original_shape(self): + ds = Dataset({'x': [1, 2, 3]}) + chunksizes = (2, ) + ds.variables['x'].encoding = { + 'chunksizes': chunksizes + } + + with self.roundtrip(ds) as actual: + assert_identical(ds, actual) + assert_array_equal(ds['x'].encoding['chunksizes'], + actual['x'].encoding['chunksizes']) + def test_encoding_chunksizes_unlimited(self): # regression test for GH1225 ds = Dataset({'x': [1, 2, 3], 'y': ('x', [2, 3, 4])})