Skip to content

Commit

Permalink
Check that template dims and zarr_chunks match.
Browse files Browse the repository at this point in the history
Otherwise, mismatched chunks can hide surprising silent errors.

PiperOrigin-RevId: 638036862
  • Loading branch information
shoyer authored and Xarray-Beam authors committed May 28, 2024
1 parent 0d0cdf9 commit 347d954
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
5 changes: 5 additions & 0 deletions xarray_beam/_src/zarr.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,11 @@ def _infer_zarr_chunks(dataset: xarray.Dataset) -> Dict[str, int]:
def _setup_zarr(template, store, zarr_chunks):
"""Setup a Zarr store."""
if zarr_chunks is not None:
if zarr_chunks.keys() != template.dims.keys():
raise ValueError(
'template dimensions do not match keys in zarr_chunks: '
f'{template.dims.keys()} vs. {zarr_chunks.keys()}'
)
template = _override_chunks(template, zarr_chunks)
_verify_template_is_lazy(template)
# inconsistent chunks in encoding can lead to spurious failures in xarray:
Expand Down
7 changes: 7 additions & 0 deletions xarray_beam/_src/zarr_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,13 @@ def test_chunks_to_zarr(self):
):
inputs2 | xbeam.ChunksToZarr(temp_dir, template)

temp_dir = self.create_tempdir().full_path
with self.assertRaisesRegex(
ValueError,
'template dimensions do not match keys in zarr_chunks',
):
xbeam.ChunksToZarr(temp_dir, chunked, zarr_chunks={})

def test_multiple_vars_chunks_to_zarr(self):
dataset = xarray.Dataset(
{
Expand Down

0 comments on commit 347d954

Please sign in to comment.