Skip to content

Commit 5343ccc

Browse files
jmccreightdcherian
authored andcommitted
Contiguous store with unlim dim bug fix (#2941)
* Contiguous store with unlim dim bug fix * It is new: Bug fix to 1849, need chunking for vars with unlim dims in netcdf4. * pep8 spoke * white space addition * rm warning, combine if * check invalid encoding * pep8 strikes * Update netCDF4_.py * Line wrapping
1 parent f84c514 commit 5343ccc

File tree

3 files changed

+14
-0
lines changed

3 files changed

+14
-0
lines changed

doc/whats-new.rst

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,9 @@ Enhancements
5555
Bug fixes
5656
~~~~~~~~~
5757

58+
- NetCDF4 output: variables with unlimited dimensions must be chunked (not
59+
contiguous) on output. (:issue:`1849`)
60+
By `James McCreight <https://github.com/jmccreight>`_.
5861
- indexing with an empty list creates an object with zero-length axis (:issue:`2882`)
5962
By `Mayeul d'Avezac <https://github.com/mdavezac>`_.
6063
- Return correct count for scalar datetime64 arrays (:issue:`2770`)

xarray/backends/netCDF4_.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ def _extract_nc4_variable_encoding(variable, raise_on_invalid=False,
210210
if chunks_too_big or changed_shape:
211211
del encoding['chunksizes']
212212

213+
var_has_unlim_dim = any(dim in unlimited_dims for dim in variable.dims)
214+
if (not raise_on_invalid and var_has_unlim_dim
215+
and 'contiguous' in encoding.keys()):
216+
del encoding['contiguous']
217+
213218
for k in safe_to_drop:
214219
if k in encoding:
215220
del encoding[k]
@@ -445,6 +450,7 @@ def prepare_variable(self, name, variable, check_encoding=False,
445450
encoding = _extract_nc4_variable_encoding(
446451
variable, raise_on_invalid=check_encoding,
447452
unlimited_dims=unlimited_dims)
453+
448454
if name in self.ds.variables:
449455
nc4_var = self.ds.variables[name]
450456
else:

xarray/tests/test_backends.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3525,6 +3525,11 @@ def test_extract_nc4_variable_encoding(self):
35253525
encoding = _extract_nc4_variable_encoding(var, raise_on_invalid=True)
35263526
assert {'shuffle': True} == encoding
35273527

3528+
# Variables with unlim dims must be chunked on output.
3529+
var = xr.Variable(('x',), [1, 2, 3], {}, {'contiguous': True})
3530+
encoding = _extract_nc4_variable_encoding(var, unlimited_dims=('x',))
3531+
assert {} == encoding
3532+
35283533
def test_extract_h5nc_encoding(self):
35293534
# not supported with h5netcdf (yet)
35303535
var = xr.Variable(('x',), [1, 2, 3], {},

0 commit comments

Comments
 (0)