From f63ad3bfbc5ab7cecf00f4b012d18253e9c5e733 Mon Sep 17 00:00:00 2001 From: Julien Seguinot <1186928+juseg@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:33:58 +0200 Subject: [PATCH 1/2] Add accessor method cf.assign(). --- cf_xarray/accessor.py | 48 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 48 insertions(+) diff --git a/cf_xarray/accessor.py b/cf_xarray/accessor.py index a10c4886..34583611 100644 --- a/cf_xarray/accessor.py +++ b/cf_xarray/accessor.py @@ -2633,6 +2633,54 @@ def grid_mapping_names(self) -> dict[str, list[str]]: results[v].append(k) return results + def assign(self, **standard_variables: Any) -> Dataset: + """Assign new variables by standard_name. + + Parameters + ---------- + standard_variables : mapping + A mapping from netCDF Climate and Forecast compliant standard names + (http://cfconventions.org/standard-names.html), to the new values. + The new values are passed to ``:meth:Dataset.assign``. + + Returns + ------- + dataset : Dataset + A new dataset with the new variables added, replacing any existing + variable with the same standard name. New variables are labelled + according to their input (short) names, or standard names if short + names are missing, with added trailing underscores in the event of + duplicates. + + See Also + -------- + DataSet.assign + """ + + # initalize dictionary mapping short names to the new variables + variables = {} + + # 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) + + # add trailing underscores until we find a free slot + while name in self._obj.data_vars or name in variables: + emit_user_level_warning( + f"found existing variable {name}, using {name}_ instead", + UserWarning) + name += '_' + + # map name to values (without coords, see #513, xarray#6447) + values = xr.as_variable(values) + values.attrs.update(standard_name=standard_name) + variables[name] = values + + # assign new variables and return a new dataset + return self._obj.assign(variables) + def decode_vertical_coords(self, *, outnames=None, prefix=None): """ Decode parameterized vertical coordinates in place. From 2a54d922259c8d1a6694f3958ba1ccbe9c2122c1 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 11 Jun 2024 11:53:45 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- cf_xarray/accessor.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/cf_xarray/accessor.py b/cf_xarray/accessor.py index 34583611..1795b726 100644 --- a/cf_xarray/accessor.py +++ b/cf_xarray/accessor.py @@ -2662,7 +2662,6 @@ def assign(self, **standard_variables: Any) -> Dataset: # 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) @@ -2670,8 +2669,9 @@ def assign(self, **standard_variables: Any) -> Dataset: while name in self._obj.data_vars or name in variables: emit_user_level_warning( f"found existing variable {name}, using {name}_ instead", - UserWarning) - name += '_' + UserWarning, + ) + name += "_" # map name to values (without coords, see #513, xarray#6447) values = xr.as_variable(values)