Skip to content

Commit c3f9192

Browse files
committed
Hack to preserve the existing order of attributes on saved Coords and Cubes.
1 parent 292ca9f commit c3f9192

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

lib/iris/fileformats/netcdf/saver.py

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,12 +1683,26 @@ def _set_cf_var_attributes(self, cf_var, element):
16831683
else:
16841684
units_str = str(element.units)
16851685

1686-
if cf_units.as_unit(units_str).is_udunits():
1687-
_setncattr(cf_var, "units", units_str)
1686+
# NB this bit is a nasty hack to preserve existing behaviour through a refactor:
1687+
# The attributes for Coords are created in the order units, standard_name,
1688+
# whereas for data-variables (aka Cubes) it is the other way around.
1689+
# Needed now that this routine is also called from _create_cf_data_variable.
1690+
# TODO: when we can break things, rationalise these to be the same.
1691+
def add_units():
1692+
if cf_units.as_unit(units_str).is_udunits():
1693+
_setncattr(cf_var, "units", units_str)
1694+
1695+
def add_stdname():
1696+
standard_name = element.standard_name
1697+
if standard_name is not None:
1698+
_setncattr(cf_var, "standard_name", standard_name)
16881699

1689-
standard_name = element.standard_name
1690-
if standard_name is not None:
1691-
_setncattr(cf_var, "standard_name", standard_name)
1700+
if isinstance(element, Cube):
1701+
add_stdname()
1702+
add_units()
1703+
else:
1704+
add_units()
1705+
add_stdname()
16921706

16931707
long_name = element.long_name
16941708
if long_name is not None:

0 commit comments

Comments
 (0)