From baffce6b453e280bda2c6c93594305944243a3a0 Mon Sep 17 00:00:00 2001 From: Hauke Schulz <43613877+observingClouds@users.noreply.github.com> Date: Wed, 9 Feb 2022 15:45:30 +0100 Subject: [PATCH 1/6] check only data variables' dimensions with region selection --- xarray/backends/api.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/xarray/backends/api.py b/xarray/backends/api.py index 0ca82555c8f..c531c22f259 100644 --- a/xarray/backends/api.py +++ b/xarray/backends/api.py @@ -1263,7 +1263,7 @@ def _validate_region(ds, region): ) non_matching_vars = [ - k for k, v in ds.variables.items() if not set(region).intersection(v.dims) + k for k, v in ds.data_vars.items() if not set(region).intersection(v.dims) ] if non_matching_vars: raise ValueError( From ca7ab7e520bfae837daea1e5a29e8b780f1e242e Mon Sep 17 00:00:00 2001 From: Hauke Schulz <43613877+observingClouds@users.noreply.github.com> Date: Wed, 9 Feb 2022 17:38:50 +0100 Subject: [PATCH 2/6] add tests for region write with coordinates --- xarray/tests/test_backends.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 23c48369989..3e853317746 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -2347,6 +2347,30 @@ def setup_and_verify_store(expected=data): ): data2.to_zarr(store, region={"x": slice(3)}) + def test_write_region_w_coords(self): + data = Dataset( + {"u": (("x","y",), np.array([[10], [11], [12]]))}, + coords={'x':[0,1,2],'y':[0]} + ) + data2 = Dataset( + {"u": (("x","y",), np.array([[13], [14]]))}, + coords={'x':[3,4],'y':[0]} + ) + + @contextlib.contextmanager + def setup_and_verify_store(expected=data): + with self.create_zarr_target() as store: + data.to_zarr(store) + yield store + with self.open(store) as actual: + assert_identical(actual, expected) + + # verify the base case works + expected = Dataset({"u": (("x","y"), np.array([[13], [14], [12]]))}, + coords={'x':[3,4,2],'y':[0]}) + with setup_and_verify_store(expected) as store: + data2.to_zarr(store, region={"x": slice(2)}) + @requires_dask def test_encoding_chunksizes(self): # regression test for GH2278 From 9caa8a4466526d8899c6740b4df2c61d66bf0125 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Feb 2022 17:22:56 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- xarray/tests/test_backends.py | 30 ++++++++++++++++++++++++------ 1 file changed, 24 insertions(+), 6 deletions(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 3e853317746..aa48413703c 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -2349,12 +2349,28 @@ def setup_and_verify_store(expected=data): def test_write_region_w_coords(self): data = Dataset( - {"u": (("x","y",), np.array([[10], [11], [12]]))}, - coords={'x':[0,1,2],'y':[0]} + { + "u": ( + ( + "x", + "y", + ), + np.array([[10], [11], [12]]), + ) + }, + coords={"x": [0, 1, 2], "y": [0]}, ) data2 = Dataset( - {"u": (("x","y",), np.array([[13], [14]]))}, - coords={'x':[3,4],'y':[0]} + { + "u": ( + ( + "x", + "y", + ), + np.array([[13], [14]]), + ) + }, + coords={"x": [3, 4], "y": [0]}, ) @contextlib.contextmanager @@ -2366,8 +2382,10 @@ def setup_and_verify_store(expected=data): assert_identical(actual, expected) # verify the base case works - expected = Dataset({"u": (("x","y"), np.array([[13], [14], [12]]))}, - coords={'x':[3,4,2],'y':[0]}) + expected = Dataset( + {"u": (("x", "y"), np.array([[13], [14], [12]]))}, + coords={"x": [3, 4, 2], "y": [0]}, + ) with setup_and_verify_store(expected) as store: data2.to_zarr(store, region={"x": slice(2)}) From 9581f03f25380ba1b7225dfef90aaf1040949fb5 Mon Sep 17 00:00:00 2001 From: Hauke Schulz <43613877+observingClouds@users.noreply.github.com> Date: Wed, 9 Feb 2022 18:23:53 +0100 Subject: [PATCH 4/6] add changes to whats-new.rst --- doc/whats-new.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/doc/whats-new.rst b/doc/whats-new.rst index a8cd952609c..db8136d68de 100644 --- a/doc/whats-new.rst +++ b/doc/whats-new.rst @@ -37,6 +37,9 @@ Deprecations Bug fixes ~~~~~~~~~ +- Fix :py:meth:`Dataset.to_zarr` to allow writing datasets when some coordinates of the dataset do not + share a dimension with those given in ``region`` (:issue: `6069`, :pull:`6260`). + By `Hauke Schulz `_. Documentation ~~~~~~~~~~~~~ From cb172ddce994e3954d14880c56940f9cdc671892 Mon Sep 17 00:00:00 2001 From: Hauke Schulz <43613877+observingClouds@users.noreply.github.com> Date: Wed, 9 Feb 2022 18:42:47 +0100 Subject: [PATCH 5/6] change formatting --- xarray/tests/test_backends.py | 20 ++------------------ 1 file changed, 2 insertions(+), 18 deletions(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index aa48413703c..35b4db88547 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -2349,27 +2349,11 @@ def setup_and_verify_store(expected=data): def test_write_region_w_coords(self): data = Dataset( - { - "u": ( - ( - "x", - "y", - ), - np.array([[10], [11], [12]]), - ) - }, + {"u": (("x", "y"), np.array([[10], [11], [12]]))}, coords={"x": [0, 1, 2], "y": [0]}, ) data2 = Dataset( - { - "u": ( - ( - "x", - "y", - ), - np.array([[13], [14]]), - ) - }, + {"u": (("x", "y"), np.array([[13], [14]]))}, coords={"x": [3, 4], "y": [0]}, ) From fa4d28a9f7c2407643b28726613ece9f6da6b514 Mon Sep 17 00:00:00 2001 From: Hauke Schulz <43613877+observingClouds@users.noreply.github.com> Date: Wed, 9 Feb 2022 20:55:59 +0100 Subject: [PATCH 6/6] include non-dimensional coordinates in test --- xarray/tests/test_backends.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/xarray/tests/test_backends.py b/xarray/tests/test_backends.py index 35b4db88547..b2ecb3abdf5 100644 --- a/xarray/tests/test_backends.py +++ b/xarray/tests/test_backends.py @@ -2350,11 +2350,11 @@ def setup_and_verify_store(expected=data): def test_write_region_w_coords(self): data = Dataset( {"u": (("x", "y"), np.array([[10], [11], [12]]))}, - coords={"x": [0, 1, 2], "y": [0]}, + coords={"x": [0, 1, 2], "y": [0], "z": ("x", [10, 11, 12])}, ) data2 = Dataset( {"u": (("x", "y"), np.array([[13], [14]]))}, - coords={"x": [3, 4], "y": [0]}, + coords={"x": [3, 4], "y": [0], "z": ("x", [13, 14])}, ) @contextlib.contextmanager @@ -2368,7 +2368,7 @@ def setup_and_verify_store(expected=data): # verify the base case works expected = Dataset( {"u": (("x", "y"), np.array([[13], [14], [12]]))}, - coords={"x": [3, 4, 2], "y": [0]}, + coords={"x": [3, 4, 2], "y": [0], "z": ("x", [13, 14, 12])}, ) with setup_and_verify_store(expected) as store: data2.to_zarr(store, region={"x": slice(2)})