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

Assign new variables by standard names #516

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

juseg
Copy link

@juseg juseg commented Jun 11, 2024

This pull request implements ds.cf.assign(**standard_variables). The new method should:

  • Assign variables by standard name as **kwargs or dictionary.
  • Decide on the variable short names without user input (see below).
  • Either handle or reject duplicate standard names (see below).
  • Workaround xarray incompatibilities, currently CF accessor adds grid mapping as coordinate #513.

Also needed:

  • Add documentation, where should it go?
  • Add tests, including name duplicates.

I would welcome opinions on the two points below.

1. Decide about the variable short name

I propose this algorithm:

  • If variable has a short name, absent from the dataset, use it.
ds.cf.assign(air_temperature=DataArray(name='tas')) -> 'tas'
  • Otherwise, use the standard name as a short variable name.
ds.cf.assign(air_temperature=DataArray()) -> 'air_temperature'
  • If name is already used in dataset, warn user, and add trailing underscores.
ds.cf.assign(
   air_temperature=DataArray(name=tas)).cf.assign(
   total_precipitation=DataArray(name=tas)) -> tas, tas_
  • If name is taken by another variable in call, also add trailing underscores.
ds.cf.assign(
   air_temperature=DataArray(name=tas),
   total_precipitation=DataArray(name=tas)) -> tas, tas_

2. When dataset already contains standard name

ds = xr.Dataset()
ds = ds.cf.assign(air_temperature=0)
ds = ds.cf.assign(air_temperature=1)

I find it more difficult to decide what the method should do here.

  • Override existing variable (same as Dataset.assign).
  • Override but raise a warning.
  • Assign new variable with same standard name.
  • Assign new variable but raise a warning.
  • Customize via keyword e.g. existing: ignore, override, raise, warn

Note: if we allow multiple variables with the same standard name, the resulting Dataset is technically valid, and ds.cf shows several variables associated with one standard name, while ds.cf[standard_name] fails with a KeyError`.

@dcherian
Copy link
Contributor

I think the cf-xarray version of this only really makes sense when the assigned name is a standard name on one of the present variables, so (2) in your listing.

For (1), we should just forward on to Xarray, as usual.

if we allow multiple variables with the same standard name, the resulting Dataset is technically valid, and ds.cf shows several variables associated with one standard name, while ds.cf[standard_name] fails with a KeyError`

Yes, this is intentional. ds.cf[standard_name] will raise an error unless there is only one result, since that is the only way to return a DataArray. to get all, use ds.cf[[standard_name]]. Then you will get a dataset with all dataarrays with that standard name.

# for each standard name and value pair
for standard_name, values in standard_variables.items():
# default to using existing short name or standard name
name = getattr(values, "name", standard_name)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something like

apply_mapper(_get_all, self._obj, key, error=False, default=[key])

should be used here. So

mapped_name, = apply_mapper(_get_all, self._obj, standard_name, error=False, default=[standard_name])

will do the translation to actual name in the dataset, if possible. Use this to create a new dictionary that you can then pass to self._obj.assign

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the feedback, I will be looking at this next week.

Copy link

codecov bot commented Aug 21, 2024

Codecov Report

Attention: Patch coverage is 23.07692% with 10 lines in your changes missing coverage. Please review.

Project coverage is 85.39%. Comparing base (a9cebee) to head (e156aef).
Report is 25 commits behind head on main.

Files Patch % Lines
cf_xarray/accessor.py 23.07% 8 Missing and 2 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #516      +/-   ##
==========================================
- Coverage   85.78%   85.39%   -0.39%     
==========================================
  Files          13       13              
  Lines        2364     2623     +259     
  Branches      183      241      +58     
==========================================
+ Hits         2028     2240     +212     
- Misses        303      341      +38     
- Partials       33       42       +9     
Flag Coverage Δ
mypy 41.07% <15.38%> (+2.54%) ⬆️
unittests 93.09% <9.09%> (-0.90%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

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 this pull request may close these issues.

2 participants