Deltares / xugrid

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

Add support for multi-dimensional coordinates in from_structured #202

Closed Huite closed 8 months ago

Huite commented 8 months ago

@roeldegoede:

I realized the name rotated isn't appropriate: supporting multi-dimensional coordinates is more general, e.g. if you have a curvilinear grid (which is approximated by straight edges). I also ran into the trouble of finding out how the coordinates are called. In your example, they are called xc and yc, but in many other cases they will have different names.

This implementation currently assumes that the last two logical dimensions are always called ("y", "x"), but the multi-dimensional coordinates may be arbitrarily named. They could technically be identified by their attrs (if set), but this seems much simpler.

Taking your example in the issue:

uda = xu.UgridDataArray.from_structured(da)
uda.ugrid.plot()

This doesn't specifiy the x and y coordinates, so it works as before: it looks at the x and y dimensions/coordinates. In this case, they are unlabeled, so they are just a range of 0...N.

image

To succesfully use the multi-dimensional coordinates, specify them explicitly:

uda = xu.UgridDataArray.from_structured(da, x="xc", y="yc")
uda.ugrid.plot()

image

This seems to work, if we check with a plot of the original:

da.plot(x="xc", y="yc")

image

The .ugrid.plot seems a bit sharper, maybe due to how to matplotlib treats a PolyCollection (bit of a surprise to me).