Skip to content

Commit 3877ed7

Browse files
author
Will Benfold
committed
Add warnings to function, and test them
1 parent 53b54b8 commit 3877ed7

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

lib/iris/fileformats/netcdf.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,8 +238,11 @@ def _split_cell_methods(nc_cell_methods: str) -> List[re.Match]:
238238
elif cha == ")":
239239
bracket_depth -= 1
240240
if bracket_depth < 0:
241-
pass
242-
# TODO: Raise meaningful warning or error to indicate badly formatted cell method
241+
msg = (
242+
"Cell methods may be incorrectly parsed due to mismatched "
243+
"brackets"
244+
)
245+
warnings.warn(msg, UserWarning, stacklevel=2)
243246
if bracket_depth > 0 and ind in name_start_inds:
244247
name_start_inds.remove(ind)
245248

@@ -255,8 +258,11 @@ def _split_cell_methods(nc_cell_methods: str) -> List[re.Match]:
255258
nc_cell_method_str = nc_cell_methods[start_ind:end_ind]
256259
nc_cell_method_match = _CM_PARSE.match(nc_cell_method_str.strip())
257260
if not nc_cell_method_match:
258-
pass
259-
# TODO: Raise meaningful warning or error to indicate badly formatted cell method
261+
msg = (
262+
f"Failed to fully parse cell method string: {nc_cell_methods}"
263+
)
264+
warnings.warn(msg, UserWarning, stacklevel=2)
265+
continue
260266
nc_cell_methods_matches.append(nc_cell_method_match)
261267

262268
return nc_cell_methods_matches

lib/iris/tests/unit/fileformats/netcdf/test_parse_cell_methods.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,36 @@ def test_comment_brackets(self):
116116
res = parse_cell_methods(cell_method_str)
117117
self.assertEqual(res, expected)
118118

119+
def test_comment_bracket_mismatch_warning(self):
120+
cell_method_strings = [
121+
"time: minimum within days (comment: 18h day-1)-18h)",
122+
"time : minimum within days (comment: 18h day-1)-18h)",
123+
]
124+
for cell_method_str in cell_method_strings:
125+
with mock.patch("warnings.warn") as warn:
126+
_ = parse_cell_methods(cell_method_str)
127+
self.assertIn(
128+
"Cell methods may be incorrectly parsed due to mismatched brackets",
129+
warn.call_args[0][0],
130+
)
131+
132+
def test_badly_formatted_warning(self):
133+
cell_method_strings = [
134+
# "time: maximum (interval: 1 hr comment: first bit "
135+
# "time: mean (interval: 1 day comment: second bit)",
136+
"time: (interval: 1 hr comment: first bit) "
137+
"time: mean (interval: 1 day comment: second bit)",
138+
"time: maximum (interval: 1 hr comment: first bit) "
139+
"time: (interval: 1 day comment: second bit)",
140+
]
141+
for cell_method_str in cell_method_strings:
142+
with mock.patch("warnings.warn") as warn:
143+
_ = parse_cell_methods(cell_method_str)
144+
self.assertIn(
145+
f"Failed to fully parse cell method string: {cell_method_str}",
146+
warn.call_args[0][0],
147+
)
148+
119149
def test_portions_of_cells(self):
120150
cell_method_strings = [
121151
"area: mean where sea_ice over sea",

0 commit comments

Comments
 (0)