Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

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

Effeciantly subset existing ugrid to a smaller ugrid given a bbox or polygon #200

Closed
saeed-moghimi-noaa opened this issue Jan 20, 2024 · 1 comment

Comments

@saeed-moghimi-noaa
Copy link

Hi There,

Is there a suggested tool to perform efficient subset of a large ugrid data set to a new ugrid (xarray) with the consistent connectivity given a bbox or polygon as an input using xugrid tool set? I would like to retain all the properties of the parent mesh such as node coordinates and connectivity and values.

something like:

new_ugrid = subset (large_input_ugrid, bbox)

Thanks.

@Huite
Copy link
Collaborator

Huite commented Jan 22, 2024

Hi @saeed-moghimi-noaa

The user guide part on selection should give relevant information on this: https://deltares.github.io/xugrid/examples/selection.html

The .ugrid.sel method will return subsets with valid topologies.

Note that this currently selects by the face coordinates, which may not be what you desire. See issue #182 for a workaround (although to be clear: we should tackle it with a proper solution so it doesn't require a workaround).

Selecting by polygon can be done relatively easily, but also requires more on than step. I'll open an issue for the functionality.

For now, one could use this multi-step approach:

import xugrid as xu
import geopandas as gpd

# Read a polygon into a geodataframe
gdf = gdp.read_file("my-polygon.shp")

# Read the ugrid file containing a single variable
uda = xu.open_dataarray("my-ugrid.nc")

# Burn the vector geometry in to the unstructured topology
burned = xu.burn_vector_geometry(gdf, uda, column="id")  # let's assume there's an id colum with a value of 1

# Use the burned result to select only the values in the polygon, then drop the faces outside of the polygon
selection = uda.where(burned == 1).dropna(uda.ugrid.grid.face_dimension)

There's a bit more info on "burning" vector geometries in this part of the user guide: https://deltares.github.io/xugrid/examples/vector_conversion.html

In all cases, this results in a new, consistent, geometry. In case you want to preserve the original topology, you would use .where instead and "mask" all values outside of the selection by NaN.

@Deltares Deltares locked and limited conversation to collaborators May 1, 2024
@JoerivanEngelen JoerivanEngelen converted this issue into discussion #239 May 1, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants