Replies: 1 comment
-
Hi @lpilz, I was wondering when a question would pop up! https://deltares.github.io/xugrid/examples/regridder_overview.html#regridding-overview If you already have the original data in a geodataframe, with refined polygons you should be able to use it to make an xugrid dataset directly. You can the relevant variable into a UgridDataArray: import xugrid
uds = xugrid.UgridDataset.from_geodataframe(gdf)
uda = uds["E_CO2"] This data can be reprojected in a straightforward manner. This doesn't change the data, it simply reprojects all the vertices of the grid to the new coordinate system: uda_reprojected = uda.ugrid.to_crs(epsg=...) # can also be a pyproj crs object You should also define your target topology in way that xugrid will understand it. I'm guessing this is a structured grid in your preferred coordinate system? To compute all the overlaps, we instantiate an OverlapRegridder: regridder = xugrid.OverlapRegridder(source=uda_reprojected, target=da_target) This will immediately run the intersection logic and compute all overlaps. These are stored in a sparse matrix, you can get the values with the overlap_matrix = regridder.weights This'll return a Dataset with all the relevant data for the regridder, the fields basically match what you need for a scipy CSR matrix. In general though, the idea is that the regridder can do the remapping for you, so you don't have to deal with these weights matrices. regridder = xugrid.OverlapRegridder(source=uda_reprojected, target=da_target, method="...")
remapped_da = regridder.regrid(source=uda_reprojected) There is machinery to do this in a delayed manner for orthogonal dimensions (e.g. a time dimensions, or a vertical dimension layer), so it'll process large datasets as well (provided they are properly chunked). I'd be curious to hear what method you need and whether you can use one of the provided methods. See: https://deltares.github.io/xugrid/api/xugrid.OverlapRegridder.html#xugrid.OverlapRegridder Note that it's also possible to provide your own custom reduction method, see the custom reductions part in the docs: |
Beta Was this translation helpful? Give feedback.
-
Hi there and hi again Huite!
Huite and I met at EGU24 (which was very nice) and already talked a bit about this usecase, so here is the discussion thread as promised.
I am working on a problem where I have to remap emissions conservatively between grids of two different geographical projections (from lat/lon to LCC projection). I think there are two solutions to this problem. The first one is a monte-carlo approach (which I haven't looked at yet but might in the future for curiosity's sake). And the second one an approach constructing polygons (with additional vertices on the edges in order to represent the curvilinear form) from those projections and intersecting them.
I already implemented the second approach using
geopandas
andshapely
but unfortunately it's awfully slow. So I was wondering whether this could be solved more efficiently by usingxugrid
.Here is the Setup: I have two
xarray
datasets which look similar to these:I would like to do something like this:
Can somebody maybe tell me if this is a sensible approach at all? Does this work with what functionality
xugrid
is providing at the moment or would you have to add additional functionality? And if this is easy: Is there a way to save the interpolation matrix mapping between lat,lon to x,y as a sparse matrix somehow?Thanks a lot!
Lukas
Beta Was this translation helpful? Give feedback.
All reactions