-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extension proposal: extension for UGRID and unstructured mesh utils #4222
Comments
I put a note in the gridded issue, but in short: I think the way to get this is to use xarray in gridded, rather than to, well, reimplement gridded in xarray :-) |
My basic understanding of this is that your data essentially can be represented as an xarray dataset as long as you cart around an extra variable (or attributes) to describe it, and there is a common set of methods you would then like to apply to that data? If that's the case then this seems like a powerful and clear proposal for a widely-useful package which would leverage xarray, but perhaps would not live within xarray itself? We encourage composition over inheritance (i.e. wrap xarray.Dataset with your new |
@ChrisBarker-NOAA: thanks, I'll reply in the gridded issue: NOAA-ORR-ERD/gridded#55 @TomNicholas Thanks for the comment! I've put a clarification in the post, that I don't intend this to live in Xarray. The stuff I suggest is specifically about mesh topology, which I'd agree on as being outside of the scope of xarray. I realize the post wasn't that clear in that regard. I was indeed thinking of the accessor methods, but failed to explicitly mention them. Hopefully should be bit clearer now. |
In order to maintain a list of currently relevant issues, we mark issues as stale after a period of inactivity If this issue remains relevant, please comment here or remove the |
I think we can move the conversation to https://github.com/UXARRAY/uxarray now. |
Disclaimer: I'm not a 100% sure this is necessarily the right place to discuss this. I think some of the things I propose could be broadly useful. At any rate, I appreciate feedback!
EDIT: to clarify, I don't think my original wording was entirely clear: I'm not suggesting adding this to xarray, but I'm thinking about a dataset accessor or a wrapping object in a separate package.
Introduction
Xarray is a great tool for structured data, but there isn't anything quite like it for unstructured meshes of convex cells. I'm coming from a hydrological background, and I believe many of my colleagues working with unstructured meshes could greatly benefit from xarray.
In terms of data models and formats, there are many unstructured mesh formats, I think UGRID is the most widely shared (outside of specific model formats) and it supports many aspects (e.g. data on faces, nodes, edges). Although Gridded exists and provides a number of features, I really need something that consumes and produces xarray Datasets (I don't want to go without e.g. dask, or deal with the netcdf4 library myself).
My most immediate need is some utilities for 2D unstructured meshes (y, x), and layered meshes (layer, y, x). This is also what UGRID is mostly used for now, as there don't seem to be many examples of the 3D unstructured mesh definitions in the wild. Sticking to 2D simplies things somewhat, so I'll ignore the third dimension for now -- but I don't see any reason why one couldn't generalize many ideas to 3D.
I currently use xarray a great deal with structured data, in dealing with "regular" netCDFs. In my thinking, it's possible to "mirror" a few methods which by and large behave similarly to xarray, using the UGRID data model for the mesh topology.
A simple UGRID example
To clarify what I'm talking about, this is what UGRID looks like in xarray, there's a Dataset with a dummy variable which describes in which variables the mesh topology is stored, in this case for a triangle and a quad.
It's only the topology that is special cased. A time dependent variable looks the same as it would otherwise, except it would have dimensions
("time", "face")
rather than("time", "y", "x")
.UGRID netCDFs can in principle be read and written without issue. I would prefer to work with standard xarray Datasets like this if possible, because it means there's no need to touch any IO methods, as well as maintaining the full flexibility of xarray.
Some methods
I'm thinking of a few methods which behave similarly as the xarray methods except that they know about the mesh topology (in x and y) and the dependencies between the different variables. So, you could do:
And it would slice based on the x coordinate of the nodes, but update all the other variables to produce a consistent mesh (and renumber the
face_nodes
appriopriately).xugrid.isel
would behave in the same way.xugrid.plot
would e.g. perform triangulation automatically (if the mesh isn't triangular already), and create amatplotlib.tri.Triangulation
object, and so forth.Regridding
I think one of the major needs for dealing with unstructured grids is being able to map data from one mesh to another, go from (GIS) raster data to unstructured, or go from unstructured model output to GIS raster data. For my use case, I often require area weighted methods, which requires computing overlap between source and destination meshes. I haven't really found a suitable package, e.g. xemsf looks very useful, but doesn't have quite the methods I'd want. Also troubling is that I can't really get it installed on Windows.
Existing GIS routines for vector data are far too slow (they have to take too many special cases into account, I think), see some benchmarks here.
I think numba provides an interesting opportunity here, not just for ease of development and distribution, but also because of JIT-compilation, the user can provide their own regridding methods. E.g.
The best way to find overlapping cells seems via a cell tree, as implemented here, and requires searching for bounding boxes rather than points. I've ported the CellTree2D to numba, and added a bounding box search.
The serial version is a little bit slower than C++ for point searches, but with numba's
prange
(and enough processors) searches are a few times faster. (In retrospect, I could've just re-used the C++ tree rather than porting it, but numba does have some benefits...)Interpolation on an unstructured grids can probably be handled by pyinterp, and can be made available via the regridder. (Scipy interpolate takes too much memory in my experience for large meshes.)
API proposal
Most of these methods require dealing with numpy arrays (or numpy arrays representing sparse arrays), so I think numba is very suitable for the algorithms (if they don't already exist).
For most methods, 3D meshes would be possible too. It requires some changes in e.g. the CellTree, and plotting would first require a
slice_horizontal
or something to get a plan view image.Some methods might not be very needed, I'm basing it off of my impression what I currently use xarray for in combination with some other tools that assume structured data.
Resources
(Have to check for non-permissive / copyleft licenses...)
Thoughts?
Does this sound useful? Does it belong somewhere else / does it already exist?
(I'm aware a number e.g. the tree structures are available in vtk as well, but the Python API unfortunately doesn't expose them; having a numba version also makes changes a lot easier.)
Something that popped up:
Some of the algorithms benefit from some persistent data structures (e.g. a cell tree or connectivity matrix), can these be maintained via an xarray-extension?
I'm assuming mesh topologies that fit within memory, that seems challenging enough for now...
The text was updated successfully, but these errors were encountered: