1
1
"""Collection of miscellaneous minor tests."""
2
2
3
+ import os
4
+ from tempfile import NamedTemporaryFile
5
+
6
+ import numpy as np
3
7
import pygrib
4
8
import pytest
5
9
@@ -9,3 +13,89 @@ def test_internal_value_type_of_runtime_error():
9
13
with pytest .raises (RuntimeError ) as e :
10
14
grbindx .write ("nonexistent/path" )
11
15
assert type (e .value .args [0 ]) is str
16
+
17
+
18
+ @pytest .mark .parametrize (
19
+ "data_fname,scanning_mode,expected_values" ,
20
+ [
21
+ (
22
+ "scanning_mode.grib2" ,
23
+ 0b00000000 ,
24
+ np .array ([[0 , 1 ], [2 , 3 ], [4 , 5 ]], dtype = float ),
25
+ ),
26
+ (
27
+ "scanning_mode.grib2" ,
28
+ 0b10000000 ,
29
+ np .array ([[0 , 1 ], [2 , 3 ], [4 , 5 ]], dtype = float ),
30
+ ),
31
+ (
32
+ "scanning_mode.grib2" ,
33
+ 0b01000000 ,
34
+ np .array ([[0 , 1 ], [2 , 3 ], [4 , 5 ]], dtype = float ),
35
+ ),
36
+ (
37
+ "scanning_mode.grib2" ,
38
+ 0b00100000 ,
39
+ np .array ([[0 , 3 ], [1 , 4 ], [2 , 5 ]], dtype = float ),
40
+ ),
41
+ (
42
+ "scanning_mode.grib2" ,
43
+ 0b00010000 ,
44
+ np .array ([[0 , 1 ], [3 , 2 ], [4 , 5 ]], dtype = float ),
45
+ ),
46
+ (
47
+ "scanning_mode.grib2" ,
48
+ 0b00110000 ,
49
+ np .array ([[0 , 3 ], [4 , 1 ], [2 , 5 ]], dtype = float ),
50
+ ),
51
+ (
52
+ "scanning_mode_with_bitmap.grib2" ,
53
+ 0b00000000 ,
54
+ np .array ([[np .nan , 1 ], [2 , 3 ], [4 , 5 ]], dtype = float ),
55
+ ),
56
+ (
57
+ "scanning_mode_with_bitmap.grib2" ,
58
+ 0b10000000 ,
59
+ np .array ([[np .nan , 1 ], [2 , 3 ], [4 , 5 ]], dtype = float ),
60
+ ),
61
+ (
62
+ "scanning_mode_with_bitmap.grib2" ,
63
+ 0b01000000 ,
64
+ np .array ([[np .nan , 1 ], [2 , 3 ], [4 , 5 ]], dtype = float ),
65
+ ),
66
+ (
67
+ "scanning_mode_with_bitmap.grib2" ,
68
+ 0b00100000 ,
69
+ np .array ([[np .nan , 3 ], [1 , 4 ], [2 , 5 ]], dtype = float ),
70
+ ),
71
+ (
72
+ "scanning_mode_with_bitmap.grib2" ,
73
+ 0b00010000 ,
74
+ np .array ([[np .nan , 1 ], [3 , 2 ], [4 , 5 ]], dtype = float ),
75
+ ),
76
+ (
77
+ "scanning_mode_with_bitmap.grib2" ,
78
+ 0b00110000 ,
79
+ np .array ([[np .nan , 3 ], [4 , 1 ], [2 , 5 ]], dtype = float ),
80
+ ),
81
+ ],
82
+ )
83
+ def test_scanning_mode (data_fname , scanning_mode , expected_values ):
84
+ template_path = f"../sampledata/{ data_fname } "
85
+ with open (template_path , "rb" ) as f :
86
+ template = f .read ()
87
+ scanning_mode_index = 0x6C
88
+ bytes_ = (
89
+ template [:scanning_mode_index ]
90
+ + scanning_mode .to_bytes (1 , "big" )
91
+ + template [scanning_mode_index + 1 :]
92
+ )
93
+ with NamedTemporaryFile (mode = "wb" , delete = False ) as temp :
94
+ temp .write (bytes_ )
95
+
96
+ grbs = pygrib .open (temp .name )
97
+ actual = grbs [1 ].values
98
+ np .testing .assert_array_equal (actual , expected_values )
99
+
100
+ grbs .close ()
101
+ os .unlink (temp .name )
0 commit comments