Deltares / xugrid

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

Create from numpy constructors, e.g. from_face_data, from_node_data, from_edge_data #265

Closed Huite closed 1 month ago

Huite commented 1 month ago

Currently, instantiating a UgridDataArray requires an xarray DataArray. If you data is available in numpy form, this is somewhat roundabout. Assuming we have a grid and some np_values:

da = xr.DataArray(data=np_values, dim=(grid.face_dimension,))
uda = xu.UgridDataArray(da, grid)

Ideally, this could be provided as an alternative constructor. I'm still a little uncertain about the best form: xu.UgridDataArray.from_numpy(grid, np_values) is insufficiently specific, because it does not specify the grid facet (node, edge, face).

So a better option would perhaps be: xu.UgridDataArray.from_node_data(grid, values). values likely wouldn't even need to be a numpy array, but anything that could be converted (list, tuples, DataArrays, etc.).

Alternatively, we could add a method on the Ugrid object via associate_node_data or something which would then return a UgridDataArray. It doesn't feel quite right though, since it's obviously an alternative constructor.