-
Notifications
You must be signed in to change notification settings - Fork 280
/
test_parse_cell_methods.py
205 lines (189 loc) · 7.29 KB
/
test_parse_cell_methods.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# Copyright Iris contributors
#
# This file is part of Iris and is released under the LGPL license.
# See COPYING and COPYING.LESSER in the root of the repository for full
# licensing details.
"""
Unit tests for :func:`iris.fileformats.netcdf.parse_cell_methods`.
"""
import pytest
from iris.coords import CellMethod
from iris.fileformats.netcdf import (
UnknownCellMethodWarning,
parse_cell_methods,
)
class TestParseCellMethods:
def _check_answers(self, test_strings, result):
"""
Compare a list of test strings against a single expected result.
Done this way so that any failures produce intelligible Pytest messages.
"""
expected_tests_and_results = [
(cell_method_str, result) for cell_method_str in test_strings
]
actual_tests_and_results = [
(cell_method_str, parse_cell_methods(cell_method_str))
for cell_method_str in test_strings
]
assert actual_tests_and_results == expected_tests_and_results
def test_simple(self):
# Some simple testcases which should all have the same result
cell_method_strings = [
"time: mean",
"time : mean",
]
expected = (CellMethod(method="mean", coords="time"),)
self._check_answers(cell_method_strings, expected)
def test_with_interval(self):
cell_method_strings = [
"time: variance (interval: 1 hr)",
"time : variance (interval: 1 hr)",
]
expected = (
CellMethod(method="variance", coords="time", intervals="1 hr"),
)
self._check_answers(cell_method_strings, expected)
def test_multiple_axes(self):
cell_method_strings = [
"lat: lon: standard_deviation",
"lat: lon : standard_deviation",
"lat : lon: standard_deviation",
"lat : lon : standard_deviation",
]
expected = (
CellMethod(method="standard_deviation", coords=["lat", "lon"]),
)
self._check_answers(cell_method_strings, expected)
def test_multiple(self):
cell_method_strings = [
"time: maximum (interval: 1 hr) time: mean (interval: 1 day)",
"time : maximum (interval: 1 hr) time: mean (interval: 1 day)",
"time: maximum (interval: 1 hr) time : mean (interval: 1 day)",
"time : maximum (interval: 1 hr) time : mean (interval: 1 day)",
]
expected = (
CellMethod(method="maximum", coords="time", intervals="1 hr"),
CellMethod(method="mean", coords="time", intervals="1 day"),
)
self._check_answers(cell_method_strings, expected)
def test_comment(self):
cell_method_strings = [
(
"time: maximum (interval: 1 hr comment: first bit) "
"time: mean (interval: 1 day comment: second bit)"
),
(
"time : maximum (interval: 1 hr comment: first bit) "
"time: mean (interval: 1 day comment: second bit)"
),
(
"time: maximum (interval: 1 hr comment: first bit) "
"time : mean (interval: 1 day comment: second bit)"
),
(
"time : maximum (interval: 1 hr comment: first bit) "
"time : mean (interval: 1 day comment: second bit)"
),
]
expected = (
CellMethod(
method="maximum",
coords="time",
intervals="1 hr",
comments="first bit",
),
CellMethod(
method="mean",
coords="time",
intervals="1 day",
comments="second bit",
),
)
self._check_answers(cell_method_strings, expected)
def test_comment_brackets(self):
cell_method_strings = [
"time: minimum within days (comment: 18h(day-1)-18h)",
"time : minimum within days (comment: 18h(day-1)-18h)",
]
expected = (
CellMethod(
method="minimum within days",
coords="time",
intervals=None,
comments="18h(day-1)-18h",
),
)
self._check_answers(cell_method_strings, expected)
def test_comment_bracket_mismatch_warning(self):
cell_method_strings = [
"time: minimum within days (comment: 18h day-1)-18h)",
"time : minimum within days (comment: 18h day-1)-18h)",
]
expected = (
CellMethod(
method="minimum within days",
coords="time",
intervals=None,
comments="18h day-1)-18h",
),
)
msg = (
"Cell methods may be incorrectly parsed due to mismatched brackets"
)
for cell_method_str in cell_method_strings:
with pytest.warns(UserWarning, match=msg):
self._check_answers(cell_method_strings, expected)
def test_badly_formatted_warning(self):
cell_method_strings = [
(
"time: (interval: 1 hr comment: first bit) "
"time: mean (interval: 1 day comment: second bit)"
),
(
"time: maximum (interval: 1 hr comment: first bit) "
"time: (interval: 1 day comment: second bit)"
),
]
for cell_method_str in cell_method_strings[1:]:
with pytest.warns(
UserWarning,
match="Failed to fully parse cell method string: time: ",
):
_ = parse_cell_methods(cell_method_str)
def test_portions_of_cells(self):
cell_method_strings = [
"area: mean where sea_ice over sea",
"area : mean where sea_ice over sea",
]
expected = (
CellMethod(method="mean where sea_ice over sea", coords="area"),
)
self._check_answers(cell_method_strings, expected)
def test_climatology(self):
cell_method_strings = [
"time: minimum within days time: mean over days",
"time : minimum within days time: mean over days",
"time: minimum within days time : mean over days",
"time : minimum within days time : mean over days",
]
expected = (
CellMethod(method="minimum within days", coords="time"),
CellMethod(method="mean over days", coords="time"),
)
self._check_answers(cell_method_strings, expected)
def test_climatology_with_unknown_method(self):
cell_method_strings = [
"time: min within days time: mean over days",
"time : min within days time: mean over days",
"time: min within days time : mean over days",
"time : min within days time : mean over days",
]
expected = (
CellMethod(method="min within days", coords="time"),
CellMethod(method="mean over days", coords="time"),
)
msg = "NetCDF variable contains unknown cell method 'min'"
for cell_method_str in cell_method_strings:
with pytest.warns(UnknownCellMethodWarning, match=msg):
res = parse_cell_methods(cell_method_str)
assert res == expected