Skip to content
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

Add region selection from file #981

Merged
merged 8 commits into from
Sep 18, 2023
Merged

Add region selection from file #981

merged 8 commits into from
Sep 18, 2023

Conversation

acordonez
Copy link
Collaborator

@acordonez acordonez commented Sep 12, 2023

This PR adds a function that takes an xarray dataset and returns the dataset selected over a region read from file. Grid cells outside this region are converted to NaN.

Geopandas read_file() is used to load the input file. While geopandas.read_file() can load many different file types, this has only been tested with a shapefile (.shp).

The regionmask library is used to extract the gridded data over the region of interest.

Example:

import xcdat
from pcmdi_metrics.io.region_from_file import region_from_file

# An xarray dataset to work with
ds = xc.open_dataset("my_test_dataset.nc")
fshp = "ne_110m_admin_0_countries/ne_110m_admin_0_countries.shp"
attr = "NAME_EN"
feature = "Tanzania"
ds_rg = region_from_file(ds,fshp,feature,attr)

@lee1043
Copy link
Contributor

lee1043 commented Sep 18, 2023

@acordonez thank you for the PR. Will we need to add geopandas as one of dependencies in dev.yml?

@acordonez
Copy link
Collaborator Author

@lee1043 It's probably a good idea since I'm calling it explicitly. Geopandas is already a dependency via regionmask and probably some of the other packages we use.

@lee1043
Copy link
Contributor

lee1043 commented Sep 18, 2023

@acordonez thanks for clarifying. Thanks for adding this capability. Please feel free to merge it!

@acordonez
Copy link
Collaborator Author

Thanks @lee1043!

@acordonez acordonez merged commit d628fc1 into main Sep 18, 2023
5 checks passed
@acordonez acordonez deleted the add_shapefile_region_ao branch September 18, 2023 21:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants