Deltares / xugrid

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

Associate data from one aspect (node, face, edge) to another aspect (node, face, edge) #173

Open Huite opened 9 months ago

Huite commented 9 months ago

E.g. if we have data defined on the nodes, and not on the faces, we can "interpolate" the data from the nodes to the edges. The easiest way to do so is via the connectivity arrays. The most straightforward way is by directly indexing, and then reducing.

For the rectangular connectivities, this could be done directly, although the indexing operation is extremely slow in delayed operations (due to a dask bug) -- but this can be worked around by stacking and unstacking xarray arrays.

However, such operations do not work as nicely for the more irregular connectivities like the node_face_connectivity, which is now returned as sparse array. These can be converted to dense rectangular arrays, but an alternative is converting to COO format, to get the row numbers. The row numbers can then be used in a groupby reduction.

This opens another nice option, which is to parametrize the reduce operation. Like pandas / xarray groupby or resample, the objects has associated reduction methods, e.g. groupby("...").sum() or groupby("...").mean(). By returning the groupby object (or something similar), the user can make their own choice in the reduction method.

The methods would be defined on the UgridDataArray accessor, since the data is by definition associated with only one aspect.

API could look something like:

uda.to_face().mean()  # only valid for node and edge data
uda.to_node().sum()  # only valid for edge and face data
uda.to_edge().max()  # only valid for node and face data