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 a FlagGrouper to handle grouping by a flag variable #472

Open
dcherian opened this issue Oct 10, 2023 · 3 comments · May be fixed by #556
Open

Add a FlagGrouper to handle grouping by a flag variable #472

dcherian opened this issue Oct 10, 2023 · 3 comments · May be fixed by #556

Comments

@dcherian
Copy link
Contributor

This would be cool and particularly useful with regionmask

cc @mathause

@mathause
Copy link
Contributor

That would be super cool! Would need to decide if the flag_values are kept as well. I assume you would add this in da.cf.groupby(groups)?

A short example (using regionmask - could of course be simplified to only using xarray):

import cf_xarray
import regionmask
import xarray as xr

air = xr.tutorial.open_dataset("air_temperature")
mask = regionmask.defined_regions.ar6.land.mask(air)

regional_mean = air.air.groupby(mask).mean()

regional_mean = regional_mean.assign_coords({mask.name: mask.attrs["flag_values"]})
regional_mean = regional_mean.assign_coords({"flags": mask.attrs["flag_meanings"]})

# or
regional_mean = regional_mean.assign_coords({mask.name: mask.attrs["flag_meanings"]})

@dcherian
Copy link
Contributor Author

With our new Grouper API, this should just be a FlagGrouper, it should have all the hooks necessary. We can treat the values as being factorized already (so quite cheap), and then set the output coordinate using flag_meanings. Will need to be careful to not assume sortedness.

@dcherian dcherian changed the title when grouping by a flag array, set output coordinate using flag_meanings Add a FlagGrouper to handle grouping by a flag variable Feb 26, 2025
@dcherian
Copy link
Contributor Author

How does this look?

Image

dcherian added a commit that referenced this issue Feb 27, 2025
dcherian added a commit that referenced this issue Feb 27, 2025
@dcherian dcherian linked a pull request Feb 27, 2025 that will close this issue
dcherian added a commit that referenced this issue Feb 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants