Deltares / xugrid

Xarray and unstructured grids
https://deltares.github.io/xugrid/
MIT License
64 stars 8 forks source link

Contourf (and other) plotting fails with newer xarray versions and chunks argument #117

Closed veenstrajelmer closed 1 year ago

veenstrajelmer commented 1 year ago

In issue https://github.com/Deltares/xugrid/issues/78 (https://github.com/Deltares/xugrid/commit/21cb424cd2ded9d6ca048de3b46e8842c6a42709) there was a fix for newer xarray versions. When plotting a dataset that was opened with a chunks argument, an error was raised: "KeyError: 'Indexing with a boolean dask array is not allowed. This will result in a dask array of unknown shape. Such arrays are unsupported by Xarray.Please compute the indexer first using .compute()'". This was solved for uda.ugrid.plot(), but not for other plot methods. For instance uda.ugrid.plot.contourf() still fails.

This should be a useful testcase to add (copied and edited from the one that was added in the fixing commit):

def test_plot_with_chunks(self, tmp_path):
    time = xr.DataArray([0.0, 1.0, 2.0], coords={"time": [0, 1, 2]})
    uda = (self.uda * time).transpose()
    uda.name = "test"

    path = tmp_path / "test.nc"
    uda.ugrid.to_netcdf(path)
    back = xugrid.open_dataarray(path, chunks={"time": 1})
    primitive = back.isel(time=0).ugrid.plot.contourf()
    assert primitive is not None

My suggestion is to add darray = darray.squeeze().compute() that was added to plot() one level higher, so to _PlotMethods.__init__. There might be something I am overlooking here