Skip to content

Commit 3b8b33c

Browse files
ukmo-ccbunneypre-commit-ci[bot]trexfeathersESadek-MO
authored
Update to cell_method parsing (#6083)
* Provided error message for malformed `cell_method` attribute * Made `cell_method` pattern matching more lenient w.r.t. space after colon separator * Changed `cell_methods` regex slightly to fix failures in unit tests. Also added new unit test to check cell_methods with: 1. Name only (no colon and method) 2. Not space between colon separator and method. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Added WhatsNew entry * Update docs/src/whatsnew/latest.rst Co-authored-by: Elias <[email protected]> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Martin Yeo <[email protected]> Co-authored-by: Elias <[email protected]>
1 parent d3071ff commit 3b8b33c

File tree

3 files changed

+14
-3
lines changed

3 files changed

+14
-3
lines changed

docs/src/whatsnew/latest.rst

+3
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ This document explains the changes made to Iris for this release
5252
#. `@rcomer`_ enabled partial collapse of multi-dimensional string coordinates,
5353
fixing :issue:`3653`. (:pull:`5955`)
5454

55+
#. `@ukmo-ccbunney`_ improved error handling for malformed `cell_method`
56+
attribute. Also made cell_method string parsing more lenient w.r.t.
57+
whitespace. (:pull:`6083`)
5558

5659
💣 Incompatible Changes
5760
=======================

lib/iris/fileformats/_nc_load_rules/helpers.py

+9-3
Original file line numberDiff line numberDiff line change
@@ -202,11 +202,11 @@
202202
_CM_INTERVAL = "interval"
203203
_CM_METHOD = "method"
204204
_CM_NAME = "name"
205-
_CM_PARSE_NAME = re.compile(r"([\w_]+\s*?:\s+)+")
205+
_CM_PARSE_NAME = re.compile(r"([\w_]+\s*?:\s*)+")
206206
_CM_PARSE = re.compile(
207207
r"""
208-
(?P<name>([\w_]+\s*?:\s+)+)
209-
(?P<method>[\w_\s]+(?![\w_]*\s*?:))\s*
208+
(?P<name>([\w_]+\s*?:\s*)+)
209+
(?P<method>[^\s][\w_\s]+(?![\w_]*\s*?:))\s*
210210
(?:
211211
\(\s*
212212
(?P<extra>.+)
@@ -296,6 +296,12 @@ def _split_cell_methods(nc_cell_methods: str) -> List[re.Match]:
296296
for m in _CM_PARSE_NAME.finditer(nc_cell_methods):
297297
name_start_inds.append(m.start())
298298

299+
# No matches? Must be malformed cell_method string; warn and return
300+
if not name_start_inds:
301+
msg = f"Failed to parse cell method string: {nc_cell_methods}"
302+
warnings.warn(msg, category=iris.warnings.IrisCfLoadWarning, stacklevel=2)
303+
return []
304+
299305
# Remove those that fall inside brackets
300306
bracket_depth = 0
301307
for ind, cha in enumerate(nc_cell_methods):

lib/iris/tests/unit/fileformats/nc_load_rules/helpers/test_parse_cell_methods.py

+2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class Test(tests.IrisTest):
1919
def test_simple(self):
2020
cell_method_strings = [
2121
"time: mean",
22+
"time:mean",
2223
"time : mean",
2324
]
2425
expected = (CellMethod(method="mean", coords="time"),)
@@ -125,6 +126,7 @@ def test_badly_formatted_warning(self):
125126
cell_method_strings = [
126127
# "time: maximum (interval: 1 hr comment: first bit "
127128
# "time: mean (interval: 1 day comment: second bit)",
129+
"time",
128130
"time: (interval: 1 hr comment: first bit) "
129131
"time: mean (interval: 1 day comment: second bit)",
130132
"time: maximum (interval: 1 hr comment: first bit) "

0 commit comments

Comments
 (0)