Skip to content

Commit 254b7a6

Browse files
committed
Add 'global_attributes' field into CubeMetadata.
1 parent e06f7c8 commit 254b7a6

File tree

2 files changed

+116
-49
lines changed

2 files changed

+116
-49
lines changed

lib/iris/common/metadata.py

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,7 @@ def _difference_strict_attributes(left, right):
595595
def _is_attributes(field, left, right):
596596
"""Determine whether we have two 'attributes' dictionaries."""
597597
return (
598-
field == "attributes"
598+
field in ("attributes", "global_attributes")
599599
and isinstance(left, Mapping)
600600
and isinstance(right, Mapping)
601601
)
@@ -1081,7 +1081,7 @@ class CubeMetadata(BaseMetadata):
10811081
10821082
"""
10831083

1084-
_members = "cell_methods"
1084+
_members = ("cell_methods", "global_attributes")
10851085

10861086
__slots__ = ()
10871087

@@ -1101,7 +1101,11 @@ def __lt__(self, other):
11011101
def _sort_key(item):
11021102
keys = []
11031103
for field in item._fields:
1104-
if field not in ("attributes", "cell_methods"):
1104+
if field not in (
1105+
"attributes",
1106+
"global_attributes",
1107+
"cell_methods",
1108+
):
11051109
value = getattr(item, field)
11061110
keys.extend((value is not None, value))
11071111
return tuple(keys)
@@ -1121,16 +1125,30 @@ def _combine_lenient(self, other):
11211125
A list of combined metadata member values.
11221126
11231127
"""
1128+
# Perform lenient combination of parent (BaseMetadata) members.
1129+
result = super()._combine_lenient(other)
11241130
# Perform "strict" combination for "cell_methods".
11251131
value = (
11261132
self.cell_methods
11271133
if self.cell_methods == other.cell_methods
11281134
else None
11291135
)
1130-
# Perform lenient combination of the other parent members.
1131-
result = super()._combine_lenient(other)
11321136
result.append(value)
1133-
1137+
# Perform lenient combination for "global_attributes"
1138+
left = self.global_attributes
1139+
right = other.global_attributes
1140+
value = None
1141+
if self._is_attributes("global_attributes", left, right):
1142+
value = self._combine_lenient_attributes(left, right)
1143+
else:
1144+
# Default logic is the general fallback for member comparison
1145+
if left == right:
1146+
value = left
1147+
elif left is None:
1148+
value = right
1149+
elif right is None:
1150+
value = left
1151+
result.append(value)
11341152
return result
11351153

11361154
def _compare_lenient(self, other):
@@ -1149,8 +1167,13 @@ def _compare_lenient(self, other):
11491167
# Perform "strict" comparison for "cell_methods".
11501168
result = self.cell_methods == other.cell_methods
11511169
if result:
1170+
# Perform lenient comparison of global_attributes.
1171+
result = self._compare_lenient_attributes(
1172+
self.global_attributes, other.global_attributes
1173+
)
1174+
if result:
1175+
# Compare BaseMetadata aspects.
11521176
result = super()._compare_lenient(other)
1153-
11541177
return result
11551178

11561179
def _difference_lenient(self, other):
@@ -1166,16 +1189,20 @@ def _difference_lenient(self, other):
11661189
A list of difference metadata member values.
11671190
11681191
"""
1169-
# Perform "strict" difference for "cell_methods".
1170-
value = (
1192+
# Perform lenient difference of the other parent members.
1193+
result = super()._difference_lenient(other)
1194+
# Calculate + add "strict" difference for "cell_methods".
1195+
cms_value = (
11711196
None
11721197
if self.cell_methods == other.cell_methods
11731198
else (self.cell_methods, other.cell_methods)
11741199
)
1175-
# Perform lenient difference of the other parent members.
1176-
result = super()._difference_lenient(other)
1177-
result.append(value)
1200+
# Calculate + add "lenient" difference for "global_attributes".
1201+
gattrs_value = self._difference_lenient_attributes(
1202+
self.global_attributes, other.global_attributes
1203+
)
11781204

1205+
result.extend([cms_value, gattrs_value])
11791206
return result
11801207

11811208
@property

0 commit comments

Comments
 (0)