Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/iris/fileformats/_nc_load_rules/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -433,9 +433,9 @@ def build_cube_metadata(engine):
# Set the cube global attributes.
for attr_name, attr_value in cf_var.cf_group.global_attributes.items():
try:
cube.attributes[str(attr_name)] = attr_value
cube.attributes.globals[str(attr_name)] = attr_value
except ValueError as e:
msg = "Skipping global attribute {!r}: {}"
msg = "Skipping disallowed global attribute {!r}: {}"
warnings.warn(msg.format(attr_name, str(e)))


Expand Down
7 changes: 6 additions & 1 deletion lib/iris/fileformats/netcdf/loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,13 @@ def attribute_predicate(item):
return item[0] not in _CF_ATTRS

tmpvar = filter(attribute_predicate, cf_var.cf_attrs_unused())
attrs_dict = iris_object.attributes
if hasattr(attrs_dict, "locals"):
# Treat cube attributes (i.e. a CubeAttrsDict) as a special case.
# These attrs are "local" (i.e. on the variable), so record them as such.
attrs_dict = attrs_dict.locals
for attr_name, attr_value in tmpvar:
_set_attributes(iris_object.attributes, attr_name, attr_value)
_set_attributes(attrs_dict, attr_name, attr_value)


def _get_actual_dtype(cf_var):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def _make_engine(global_attributes=None, standard_name=None, long_name=None):
return engine


class TestInvalidGlobalAttributes(tests.IrisTest):
class TestGlobalAttributes(tests.IrisTest):
def test_valid(self):
global_attributes = {
"Conventions": "CF-1.5",
Expand All @@ -51,7 +51,7 @@ def test_valid(self):
engine = _make_engine(global_attributes)
build_cube_metadata(engine)
expected = global_attributes
self.assertEqual(engine.cube.attributes, expected)
self.assertEqual(engine.cube.attributes.globals, expected)

def test_invalid(self):
global_attributes = {
Expand All @@ -65,13 +65,14 @@ def test_invalid(self):
# Check for a warning.
self.assertEqual(warn.call_count, 1)
self.assertIn(
"Skipping global attribute 'calendar'", warn.call_args[0][0]
"Skipping disallowed global attribute 'calendar'",
warn.call_args[0][0],
)
# Check resulting attributes. The invalid entry 'calendar'
# should be filtered out.
global_attributes.pop("calendar")
expected = global_attributes
self.assertEqual(engine.cube.attributes, expected)
self.assertEqual(engine.cube.attributes.globals, expected)


class TestCubeName(tests.IrisTest):
Expand Down