Skip to content

Commit

Permalink
Keep attributes for "bounds" variables
Browse files Browse the repository at this point in the history
Issue #2921 is about mismatching time units between a time variable and
its "bounds" companion.
However, #2965 does more than fixing #2921, it removes all double
attributes from "bounds" variables which has the undesired side effect
that there is currently no way to save them to netcdf with xarray.
Since the mentioned link is a recommendation and not a hard requirement
for CF compliance, these attributes should be left to the caller to
prepare the dataset variables appropriately if required.
Reduces the amount of surprise that attributes are not written to disk
and fixes #8368.
  • Loading branch information
st-bender committed Apr 10, 2024
1 parent 07c7f96 commit e8aa5c5
Showing 1 changed file with 0 additions and 20 deletions.
20 changes: 0 additions & 20 deletions xarray/conventions.py
Original file line number Diff line number Diff line change
Expand Up @@ -794,24 +794,4 @@ def cf_encoder(variables: T_Variables, attributes: T_Attrs):

new_vars = {k: encode_cf_variable(v, name=k) for k, v in variables.items()}

# Remove attrs from bounds variables (issue #2921)
for var in new_vars.values():
bounds = var.attrs["bounds"] if "bounds" in var.attrs else None
if bounds and bounds in new_vars:
# see http://cfconventions.org/cf-conventions/cf-conventions.html#cell-boundaries
for attr in [
"units",
"standard_name",
"axis",
"positive",
"calendar",
"long_name",
"leap_month",
"leap_year",
"month_lengths",
]:
if attr in new_vars[bounds].attrs and attr in var.attrs:
if new_vars[bounds].attrs[attr] == var.attrs[attr]:
new_vars[bounds].attrs.pop(attr)

return new_vars, attributes

0 comments on commit e8aa5c5

Please sign in to comment.