diff --git a/virtualizarr/manifests/array.py b/virtualizarr/manifests/array.py index f6d93819..6430dc64 100644 --- a/virtualizarr/manifests/array.py +++ b/virtualizarr/manifests/array.py @@ -13,6 +13,7 @@ expand_dims, ) from virtualizarr.manifests.manifest import ChunkManifest +from virtualizarr.utils import determine_chunk_grid_shape class ManifestArray: @@ -64,7 +65,16 @@ def __init__( f"chunkmanifest arg must be of type ChunkManifest or dict, but got type {type(chunkmanifest)}" ) - # TODO check that the metadata shape and chunkmanifest shape are consistent with one another + # check that the metadata shape and chunkmanifest shape are consistent with one another + metadata_chunk_grid_shape = determine_chunk_grid_shape( + shape=metadata.shape, chunks=metadata.chunks + ) + if _chunkmanifest.shape_chunk_grid != metadata_chunk_grid_shape: + raise ValueError( + "Set of virtual chunk keys in manifest do not match shape of chunk grid implied by array metadata. \n" + f"Keys in chunkmanifest imply a chunk grid shape of {_chunkmanifest.shape_chunk_grid} but the metadata contains shape={_metadata.shape} and chunks={_metadata.chunks} which imply a chunk grid shape of {metadata_chunk_grid_shape}" + ) + # TODO also cover the special case of scalar arrays self._metadata = _metadata diff --git a/virtualizarr/tests/test_manifests/test_array.py b/virtualizarr/tests/test_manifests/test_array.py index 57535782..f8c948dd 100644 --- a/virtualizarr/tests/test_manifests/test_array.py +++ b/virtualizarr/tests/test_manifests/test_array.py @@ -49,6 +49,21 @@ def test_manifest_array_dict_v3_metadata(self, array_v3_metadata): assert marr.size == 5 * 2 * 20 assert marr.ndim == 3 + def test_consistency_checks(self, array_v3_metadata): + # create a manifest with only one chunk + chunks_dict = { + "0": {"path": "s3://bucket/foo.nc", "offset": 100, "length": 100}, + } + manifest = ChunkManifest(entries=chunks_dict) + + # but array metadata implying there should be two chunks + chunks = (1,) + shape = (2,) + metadata = array_v3_metadata(shape=shape, chunks=chunks) + + with pytest.raises(ValueError, match="do not match shape of chunk grid"): + ManifestArray(metadata=metadata, chunkmanifest=manifest) + class TestResultType: def test_idempotent(self, manifest_array):