Repository: pangeo-data/xESMF

Issue:
xe.Regridder does not work when paralell is set to true
I have an input xarray dataset I want to regrid
<xarray.Dataset>
Dimensions:    (longitude: 1440, latitude: 721, time: 132)
Coordinates:
  * longitude  (longitude) float32 0.0 0.25 0.5 0.75 ... 359.0 359.2 359.5 359.8
  * latitude   (latitude) float32 90.0 89.75 89.5 89.25 ... -89.5 -89.75 -90.0
  * time       (time) datetime64[ns] 2000-01-01 2000-02-01 ... 2010-12-01
Data variables:
    d2m        (time, latitude, longitude) float32 dask.array<chunksize=(12, 721, 1440), meta=np.ndarray>

Discussion:
huard: Maybe a quick test to exercise the error catching ?

Patch:
diff --git a/CHANGES.rst b/CHANGES.rst
index a2ed8db..fa1169a 100644
--- a/CHANGES.rst
+++ b/CHANGES.rst
@@ -6,6 +6,7 @@ What's new
 
 Bug fixes
 ~~~~~~~~~
+* Raise a meaningful error messages when the output grid has no chunks with `parallel=True` (:issue:`299`, :pull:`304`). By `Pascal Bourgault <https://github.com/aulemahal>`_.
 * Correct guess of output chunks for the :``SpatialAverager``.
 
 0.8.1 (2023-09-05)
diff --git a/xesmf/frontend.py b/xesmf/frontend.py
index 2b79f59..267ed3d 100644
--- a/xesmf/frontend.py
+++ b/xesmf/frontend.py
@@ -963,6 +963,11 @@ class Regridder(BaseRegridder):
             ds_out = ds_out.set_coords(['lon_b', 'lat_b'])
         if 'lon_b' in ds_in.data_vars:
             ds_in = ds_in.set_coords(['lon_b', 'lat_b'])
+        if not (set(self.out_horiz_dims) - {'dummy'}).issubset(ds_out.chunksizes.keys()):
+            raise ValueError(
+                'Using `parallel=True` requires the output grid to have chunks along all spatial dimensions. '
+                'If the dataset has no variables, consider adding an all-True spatial mask with appropriate chunks.'
+            )
         # Drop everything in ds_out except mask or create mask if None. This is to prevent map_blocks loading unnecessary large data
         if self.sequence_out:
             ds_out_dims_drop = set(ds_out.variables).difference(ds_out.data_vars)


Test patch:
diff --git a/xesmf/tests/test_frontend.py b/xesmf/tests/test_frontend.py
index 0246574..0d0b9d8 100644
--- a/xesmf/tests/test_frontend.py
+++ b/xesmf/tests/test_frontend.py
@@ -649,6 +649,11 @@ def test_para_weight_gen():
     assert all(regridder_locs.w.data.data == para_regridder_locs.w.data.data)
 
 
+def test_para_weight_gen_errors():
+    with pytest.raises(ValueError, match='requires the output grid to have chunks'):
+        xe.Regridder(ds_in, ds_out, 'conservative', parallel=True)
+
+
 def test_regrid_dataset():
     # xarray.Dataset containing in-memory numpy array
     regridder = xe.Regridder(ds_in, ds_out, 'conservative')