Deltares / xugrid

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

regridding of regular grid to unstructured grid provides flipped values on y axis #157

Closed JoerivanEngelen closed 1 month ago

JoerivanEngelen commented 1 year ago

We've got a user running into this issue.

When trying to regrid an structured grid to an unstructured one, the values are flipped on the y axis. Original code:

regridder = xu.CentroidLocatorRegridder(source=riv_bot, target=uds_grid) riv_bot = regridder.regrid(riv_bot)

It gets fixed by reversing the y axis before the regridding:

riv_bot = riv_bot.reindex(y=list(reversed(riv_bot.y)))

image

Huite commented 1 month ago

Some temporary test stuff:

# %%
import xugrid as xu
import numpy as np
import xarray as xr
# %%

uda = xu.data.elevation_nl()
xmin, xmax, ymin, ymax = uda.grid.bounds
# %%
x = np.arange(xmin, xmax, 1000.0)
y = np.arange(ymin, ymax, 1000.0)
da = xr.DataArray(
    data=np.zeros((y.size, x.size)),
    coords={"y": y, "x": x, "dx": ("x", np.full(x.size, 1000.0)), "dy": ("y", np.full(y.size, 1000.0))},
    dims=["y", "x"]
)
# %%

regridder = xu.OverlapRegridder(source=uda, target=da, method="mean")
# %%
result = regridder.regrid(uda)
# %%

decreasing_y = result.isel(y=slice(None, None, -1))
regridder = xu.OverlapRegridder(source=decreasing_y, target=uda, method="mean")
new = regridder.regrid(decreasing_y)

# %%

# Structured to structured
regridder = xu.OverlapRegridder(source=decreasing_y, target=result)
new = regridder.regrid(decreasing_y)
# %%

Structured to structured is okay, it takes the flipping into account properly.

I'm pretty sure it's the structured -> unstructured conversion that happens that's causing the disconnect.

Huite commented 1 month ago

We should add a test with four equivalent datasets:

And regrid them to a single target unstructured grid / target structured grid. All the results should be the same then.