-
Notifications
You must be signed in to change notification settings - Fork 41
Add support for flag variables. #252
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
Conversation
Closes xarray-contrib#199 Implements rich comparisons and .isin.
Codecov Report
@@ Coverage Diff @@
## main #252 +/- ##
==========================================
+ Coverage 96.38% 96.52% +0.13%
==========================================
Files 16 16
Lines 1854 1926 +72
==========================================
+ Hits 1787 1859 +72
Misses 67 67
Flags with carried forward coverage won't be shown. Click here to find out more.
Continue to review full report at Codecov.
|
You are an absolute hero! Will test this right away. |
Works like a charm! I modfied my code slightly (I had originally encoded the flags as string, which was bad): from cmip6_preprocessing.regionmask import merged_mask, _default_merge_dict
from cmip6_preprocessing.utils import _maybe_make_list
def basin_mask(ds):
basins = regionmask.defined_regions.natural_earth.ocean_basins_50
basins = merged_mask(basins, ds)
# add cf compatible metadata
basins.name = 'basin'
flag_meanings = [m.replace(' ','_').lower() for m in list(_default_merge_dict().keys())]
flag_values = np.arange(len(flag_meanings))
basins.attrs.update({'flag_values':flag_values, 'flag_meanings':' '.join(flag_meanings)})
return basins
def mask_basin(ds, basin, drop=False):
basin = _maybe_make_list(basin)
mask = basin_mask(ds)
ds_masked = ds.where(mask.cf.isin(basin)) #this is nice and clean!
return ds_masked Then I can mask out a single basin: ds = data_dict['CMIP.MIROC.MIROC6.historical.Omon.gn']
ds_masked = mask_basin(ds, 'southern_ocean')
plt.figure()
ds_masked.thetao.isel(time=0, lev=0, member_id=0).plot() and even with any combination of multiple basins: ds_masked = mask_basin(ds, ['southern_ocean', 'north_atlantic_ocean', 'south_atlantic_ocean'])
plt.figure()
ds_masked.thetao.isel(time=0, lev=0, member_id=0).plot() Thanks again! This is super neat! Ill integrate this with cmip6_pp once this is merged? |
xref #199
Implements rich comparisons and .isin. I changed my mind and dropped the
.flag
.@jbusecke I copied your code over but had to make some changes to match the conventions. Try it out and let me know how it works!
Quoting the docs: