Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
38 changes: 38 additions & 0 deletions lib/iris/tests/integration/netcdf/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -484,5 +484,43 @@ def test_path_string_save_same(self):
self.assertCDL(tempfile_frompath)


@tests.skip_data
class TestWarningRepeats(tests.IrisTest):
def test_datum_once(self):
fnames = [
"false_east_north_merc.nc",
"non_unit_scale_factor_merc.nc",
"toa_brightness_temperature.nc",
Comment thread
stephenworsley marked this conversation as resolved.
Outdated
]
fpaths = [
tests.get_data_path(("NetCDF", "mercator", fname)) for fname in fnames
]

with warnings.catch_warnings():
warnings.simplefilter("default")
with warnings.catch_warnings(record=True) as record:
Comment thread
stephenworsley marked this conversation as resolved.
Outdated
for fpath in fpaths:
iris.load(fpath)
Comment thread
stephenworsley marked this conversation as resolved.
Outdated
assert len(record) == 1

def test_datum_default(self):
Comment thread
stephenworsley marked this conversation as resolved.
Outdated
fnames = [
"false_east_north_merc.nc",
"non_unit_scale_factor_merc.nc",
"toa_brightness_temperature.nc",
]
fpaths = [
tests.get_data_path(("NetCDF", "mercator", fname)) for fname in fnames
]

with warnings.catch_warnings():
warnings.simplefilter("default")
with pytest.warns() as record:
iris.load_cube(fpaths[0])
iris.load_cube(fpaths[1])
iris.load_cube(fpaths[2])
assert len(record) == 3


if __name__ == "__main__":
tests.main()