Skip to content
Merged
Changes from 4 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
35 changes: 15 additions & 20 deletions lib/iris/fileformats/_nc_load_rules/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ class UnknownCellMethodWarning(iris.exceptions.IrisUnknownCellMethodWarning):
pass


def parse_cell_methods(nc_cell_methods):
def parse_cell_methods(nc_cell_methods, cf_name=None):
"""Parse a CF cell_methods attribute string into a tuple of zero or
more CellMethod instances.

Expand All @@ -345,6 +345,7 @@ def parse_cell_methods(nc_cell_methods):
results are not affected.

"""
msg = None
cell_methods = []
if nc_cell_methods is not None:
for m in _split_cell_methods(nc_cell_methods):
Expand All @@ -356,10 +357,15 @@ def parse_cell_methods(nc_cell_methods):
method_words = method.split()
if method_words[0].lower() not in _CM_KNOWN_METHODS:
msg = "NetCDF variable contains unknown cell method {!r}"
warnings.warn(
msg.format("{}".format(method_words[0])),
category=UnknownCellMethodWarning,
)
msg = msg.format(method_words[0])
if cf_name:
name = "{}".format(cf_name)
msg = msg.replace("variable", "variable {!r}".format(name))
else:
warnings.warn(
msg,
category=UnknownCellMethodWarning,
)
Comment thread
stephenworsley marked this conversation as resolved.
d[_CM_METHOD] = method
name = d[_CM_NAME]
name = name.replace(" ", "")
Expand Down Expand Up @@ -417,6 +423,9 @@ def parse_cell_methods(nc_cell_methods):
comments=d[_CM_COMMENT],
)
cell_methods.append(cell_method)
# only prints one warning, rather than each loop
if msg:
warnings.warn(msg, category=UnknownCellMethodWarning)
return tuple(cell_methods)


Expand Down Expand Up @@ -447,21 +456,7 @@ def build_cube_metadata(engine):

# Incorporate cell methods
nc_att_cell_methods = getattr(cf_var, CF_ATTR_CELL_METHODS, None)
with warnings.catch_warnings(record=True) as warning_records:
cube.cell_methods = parse_cell_methods(nc_att_cell_methods)
# Filter to get the warning we are interested in.
warning_records = [
record
for record in warning_records
if issubclass(record.category, UnknownCellMethodWarning)
]
if len(warning_records) > 0:
# Output an enhanced warning message.
warn_record = warning_records[0]
name = "{}".format(cf_var.cf_name)
msg = warn_record.message.args[0]
msg = msg.replace("variable", "variable {!r}".format(name))
warnings.warn(message=msg, category=UnknownCellMethodWarning)
cube.cell_methods = parse_cell_methods(nc_att_cell_methods, cf_var.cf_name)

# Set the cube global attributes.
for attr_name, attr_value in cf_var.cf_group.global_attributes.items():
Expand Down