File: test_bufr_data.py

package info (click to toggle)
eccodes-python 2%3A2.44.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,048 kB
  • sloc: python: 7,750; ansic: 280; sh: 94; makefile: 81; cpp: 30
file content (334 lines) | stat: -rw-r--r-- 13,261 bytes parent folder | download
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
# Copyright 2022- ECMWF.
#
# This software is licensed under the terms of the Apache Licence Version 2.0
# which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
# In applying this licence, ECMWF does not waive the privileges and immunities
# granted to it by virtue of its status as an intergovernmental organisation
# nor does it submit to any jurisdiction.

# flake8: noqa: F403
# flake8: noqa: F405
import sys
import warnings

import numpy as np
import pytest as pt

from eccodes import *
from eccodes.highlevel._bufr.message import *

if sys.platform.startswith("win32"):
    pt.skip("Not applicable on Windows", allow_module_level=True)


@pt.fixture(autouse=True)
def fixture(monkeypatch):
    import os

    this_dir = os.path.dirname(__file__)
    monkeypatch.chdir(this_dir)  # always run tests from the tests directory


def test_data_key():
    assert Key.from_string("year") == Key(name="year")
    assert Key.from_string("year->code") == Key(name="year", attribute="code")
    assert Key.from_string("year->units") == Key(name="year", attribute="units")
    assert Key.from_string("year->reference") == Key(name="year", attribute="reference")
    assert Key.from_string("year->scale") == Key(name="year", attribute="scale")
    assert Key.from_string("year->width") == Key(name="year", attribute="width")
    assert Key.from_string("year->percentConfidence") == Key(
        name="year->percentConfidence", primary="year", secondary="percentConfidence"
    )
    assert Key.from_string("year->percentConfidence->code") == Key(
        name="year->percentConfidence",
        primary="year",
        secondary="percentConfidence",
        attribute="code",
    )
    assert Key.from_string("#1#year") == Key(name="year", rank=1)
    assert Key.from_string("#1#year->code") == Key(
        name="year", rank=1, attribute="code"
    )
    assert Key.from_string("#1#year->percentConfidence") == Key(
        name="year->percentConfidence",
        rank=1,
        primary="year",
        secondary="percentConfidence",
    )
    assert Key.from_string("#1#year->percentConfidence->code") == Key(
        name="year->percentConfidence",
        rank=1,
        primary="year",
        secondary="percentConfidence",
        attribute="code",
    )
    assert "year" == Key(name="year").string
    assert "year->code" == Key(name="year", attribute="code").string
    assert "year->units" == Key(name="year", attribute="units").string
    assert "year->reference" == Key(name="year", attribute="reference").string
    assert "year->scale" == Key(name="year", attribute="scale").string
    assert "year->width" == Key(name="year", attribute="width").string
    assert (
        "year->percentConfidence"
        == Key(
            name="year->percentConfidence",
            primary="year",
            secondary="percentConfidence",
        ).string
    )
    assert (
        "year->percentConfidence->code"
        == Key(
            name="year->percentConfidence",
            primary="year",
            secondary="percentConfidence",
            attribute="code",
        ).string
    )
    assert "#1#year" == Key(name="year", rank=1).string
    assert "#1#year->code" == Key(name="year", rank=1, attribute="code").string
    assert (
        "#1#year->percentConfidence"
        == Key(
            name="year->percentConfidence",
            rank=1,
            primary="year",
            secondary="percentConfidence",
        ).string
    )
    assert (
        "#1#year->percentConfidence->code"
        == Key(
            name="year->percentConfidence",
            rank=1,
            primary="year",
            secondary="percentConfidence",
            attribute="code",
        ).string
    )
    with pt.raises(NotFoundError):
        Key.from_string("/pressure=85000/airTemperature")


def test_data_accessors():
    bufr = BUFRMessage(open("./sample-data/synop.bufr"))
    # Header keys (should not be accessible from the Data class)
    assert "edition" not in bufr.data
    with pt.raises(NotFoundError):
        bufr.data["edition"]
    with pt.raises(NotFoundError):
        bufr.data["edition"] = 3
    # Data keys: defined
    assert "year" in bufr.data
    assert "year->code" in bufr.data
    assert "year->percentConfidence" in bufr.data
    assert "year->percentConfidence->code" in bufr.data
    assert "#1#year" in bufr.data
    assert "#1#year->code" in bufr.data
    assert "#1#year->percentConfidence" in bufr.data
    assert "#1#year->percentConfidence->code" in bufr.data
    assert bufr.data["year"] == 2009
    assert bufr.data["year->code"] == 4001
    assert bufr.data["year->percentConfidence"] == 70
    assert np.all(
        bufr.data["cloudAmount"] == np.ma.masked_values([8, -1, -1, -1, -1], -1)
    )
    # assert bufr.data['year->percentConfidence->code'] == TODO
    assert bufr.data["#1#year"] == 2009
    assert bufr.data["#1#year->code"] == 4001
    assert bufr.data["#1#year->percentConfidence"] == 70
    assert bufr.data["#1#cloudAmount"] == 8
    # assert bufr.data['#1#year->percentConfidence->code'] == TODO
    bufr.data["year"] = 2025
    bufr.data["year->percentConfidence"] = 100
    bufr.data["cloudAmount"] = [1, 2, 3, 4, 5]
    bufr.data["#1#year"] = 2025
    bufr.data["#1#year->percentConfidence"] = 100
    bufr.data["#1#cloudAmount"] = 8
    with pt.raises(ReadOnlyError):
        bufr.data["year->code"] = 0
    with pt.raises(ReadOnlyError):
        bufr.data["#1#year->code"] = 0
    # with pt.raises(ReadOnlyError): bufr.data['#1#year->percentConfidence->code'] = TODO
    # Data keys: undefined
    assert "year->code->code" not in bufr.data
    assert "year->percentConfidence->code->code" not in bufr.data
    assert "#2#year" not in bufr.data
    assert "#2#year->percentConfidence" not in bufr.data
    assert "#2#year->percentConfidence->code" not in bufr.data
    # Negative subscripts
    bufr = BUFRMessage(open("./sample-data/temp.bufr"))
    assert "centre" in bufr.data[-1]
    assert np.all(bufr.data[-1]["centre"] == bufr.data[2]["centre"])
    assert "pressure" in bufr.data[-2]
    assert np.all(bufr.data[-2]["pressure"] == bufr.data[1]["pressure"])
    assert "blockNumber" in bufr.data[-3]
    assert bufr.data[-3]["blockNumber"] == bufr.data[0]["blockNumber"]
    with pt.raises(IndexError):
        bufr.data[-4]


def test_data_get_count():
    bufr = BUFRMessage(open("./sample-data/synop.bufr"))
    with pt.raises(KeyValueNotFoundError):
        bufr.data.get_count("edition")
    assert bufr.get_count("year") == 1
    assert bufr.get_count("year->code") == 1
    assert bufr.get_count("year->percentConfidence") == 1
    assert bufr.get_count("year->percentConfidence->code") == 1
    assert bufr.get_count("#1#year") == 1
    assert bufr.get_count("#1#year->code") == 1
    assert bufr.get_count("#1#year->percentConfidence") == 1
    assert bufr.get_count("#1#year->percentConfidence->code") == 1
    with pt.raises(KeyValueNotFoundError):
        bufr.get_count("#2#year")
    assert bufr.get_count("cloudAmount") == 5
    assert bufr.get_count("cloudAmount->code") == 5
    assert bufr.get_count("cloudAmount->percentConfidence") == 5
    assert bufr.get_count("cloudAmount->percentConfidence->code") == 5
    assert bufr.get_count("#1#cloudAmount") == 1
    assert bufr.get_count("#1#cloudAmount->code") == 1
    assert bufr.get_count("#1#cloudAmount->percentConfidence") == 1
    assert bufr.get_count("#1#cloudAmount->percentConfidence->code") == 1
    with pt.raises(KeyValueNotFoundError):
        bufr.get_count("#6#cloudAmount")
    bufr = BUFRMessage(open("./sample-data/terra-modis-aerosol.bufr"))
    assert bufr.get_count("opticalDepth") == 10
    assert bufr.data[0].get_count("opticalDepth") == 2
    assert bufr.data[1].get_count("opticalDepth") == 8
    assert bufr.data.get_count("#1#opticalDepth") == 1
    assert bufr.data[0].get_count("#1#opticalDepth") == 1
    assert bufr.data[1].get_count("#1#opticalDepth") == 1
    with pt.raises(KeyValueNotFoundError):
        bufr.data.get_count("#11#opticalDepth")
    with pt.raises(KeyValueNotFoundError):
        bufr.data[0].get_count("#3#opticalDepth")
    with pt.raises(KeyValueNotFoundError):
        bufr.data[1].get_count("#9#opticalDepth")


def test_data_is_missing():
    bufr = BUFRMessage("BUFR3_local_satellite")
    bufr["numberOfSubsets"] = 3
    with pt.raises(KeyValueNotFoundError):
        bufr.data.is_missing("edition")
    assert bufr.is_missing("pressure")
    assert bufr.is_missing("#1#pressure")
    assert bufr.is_missing("#2#pressure")
    array = bufr["pressure"]
    assert np.all(array.mask)
    assert array.shape[0] > 1
    array[0, :] = 1013.25
    assert not np.all(array.mask)
    assert not bufr.is_missing("pressure")
    assert not bufr.is_missing("#1#pressure")
    assert bufr.is_missing("#2#pressure")
    bufr.pack()
    assert not bufr.is_missing("pressure")
    assert not bufr.is_missing("#1#pressure")
    assert bufr.is_missing("#2#pressure")


def test_data_set_missing():
    bufr = BUFRMessage("BUFR3_local_satellite")
    bufr["numberOfSubsets"] = 3
    with pt.raises(KeyValueNotFoundError):
        bufr.data.set_missing("edition")
    with pt.raises(ValueCannotBeMissingError):
        bufr.set_missing("pressure->units")
    bufr["pressure"] = 1013.25
    bufr.pack()
    bufr.set_missing("pressure")
    array = bufr["pressure"]
    assert np.all(array.mask)
    bufr["pressure"] = 1013.25
    bufr.pack()
    bufr.set_missing("#1#pressure")
    array = bufr["pressure"]
    assert np.all(array[0, :].mask)
    assert not np.any(array[1:, :].mask)
    bufr.set_missing("#2#pressure")
    assert np.all(array[1, :].mask)
    assert not np.any(array[2:, :].mask)
    bufr.set_missing("pressure")
    assert np.all(array.mask)


def test_data_type_of_seconds():
    # With default, zero scale -> int
    bufr = BUFRMessage("BUFR3_local_satellite")
    bufr["numberOfSubsets"] = 3
    bufr["unexpandedDescriptors"] = [4006]
    assert bufr["second->scale"] == 0
    assert bufr["second"].dtype == np.dtype(int)
    assert bufr["second"].data[0] == CODES_MISSING_LONG
    assert bufr["second"][0] is np.ma.masked
    # With changed, non-zero scale -> float
    bufr = BUFRMessage("BUFR3_local_satellite")
    bufr["numberOfSubsets"] = 3
    bufr["unexpandedDescriptors"] = [202131, 4006, 202000]
    assert bufr["second->scale"] == 3
    assert bufr["second"].dtype == np.dtype(float)
    assert bufr["second"].data[0] == CODES_MISSING_DOUBLE
    assert bufr["second"][0] is np.ma.masked


def test_data_assumed_scalar_elements():
    bufr = BUFRMessage("BUFR3_local_satellite")
    bufr["numberOfSubsets"] = 3
    bufr["unexpandedDescriptors"] = [1007]
    bufr.data["satelliteIdentifier"] = [1, 2, 3]
    bytes = bufr.get_buffer()
    bufr = BUFRMessage(bytes)
    with change_behaviour() as behaviour:
        behaviour.assumed_scalar_elements.add("satelliteIdentifier")
        with pt.warns(UserWarning):
            assert np.all(bufr.data["satelliteIdentifier"] == [1, 2, 3])
        with change_behaviour() as behaviour:
            behaviour.on_assumed_scalar_element_invalid_size = "raise"
            bufr = BUFRMessage(bytes)
            with pt.raises(ValueError):
                assert np.all(bufr.data["satelliteIdentifier"] == [1, 2, 3])
        with change_behaviour() as behaviour:
            behaviour.on_assumed_scalar_element_invalid_size = "ignore"
            bufr = BUFRMessage(bytes)
            with warnings.catch_warnings(record=True) as ws:
                assert np.all(bufr.data["satelliteIdentifier"] == [1, 2, 3])
                assert not ws
    with change_behaviour() as behaviour:
        behaviour.assumed_scalar_elements.add("satelliteIdentifier")
        # Single rank
        bufr = BUFRMessage("BUFR3_local_satellite")
        bufr["numberOfSubsets"] = 3
        bufr["unexpandedDescriptors"] = [1007]
        assert bufr.data["satelliteIdentifier"] is np.ma.masked
        assert bufr.data["#1#satelliteIdentifier"] is np.ma.masked
        bufr.data["satelliteIdentifier"] = 1
        bufr.data["#1#satelliteIdentifier"] = 1
        bufr.pack()
        assert bufr.data["satelliteIdentifier"] == 1
        with pt.raises(ValueError):
            bufr.data["satelliteIdentifier"] = [1, 2, 3]
        # Multiple ranks
        bufr = BUFRMessage("BUFR3_local_satellite")
        bufr["numberOfSubsets"] = 3
        bufr["unexpandedDescriptors"] = [1007, 1007]
        value = bufr.data["satelliteIdentifier"]
        assert isinstance(value, np.ndarray)
        assert value.shape == (2,)
        assert value.all() is np.ma.masked
        value1 = bufr.data["#1#satelliteIdentifier"]
        assert value1 is np.ma.masked
        bufr.data["satelliteIdentifier"] = [0, 0]
        assert np.all(bufr.data["satelliteIdentifier"] == 0)
        bufr.data["#1#satelliteIdentifier"] = 1
        bufr.data["#2#satelliteIdentifier"] = 2
        assert np.all(bufr.data["satelliteIdentifier"] == [1, 2])


def test_data_assignment_with_non_native_types():
    bufr = BUFRMessage("BUFR3_local_satellite")
    bufr["satelliteIdentifier"] = np.int8(0)
    bufr["satelliteIdentifier"] = np.int16(0)
    bufr["satelliteIdentifier"] = np.int32(0)
    bufr["latitude"] = np.float32(0)