Skip to content

Commit 40558bf

Browse files
authored
Merge pull request #213 from noritada/fix/scanning-mode-processing
Fix an issue that the 3rd bit of scanning mode in GRIB does not affect processing
2 parents 9c11f03 + 2ae2271 commit 40558bf

File tree

5 files changed

+99
-13
lines changed

5 files changed

+99
-13
lines changed

Changelog

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ since version 2.1.4 release
66
* allow pygrib.open to take an io.BufferedReader object as an argument
77
(issue #83, PR #206)
88
* ensure that RuntimeError contains a message of type `str` (PR #212)
9+
* fix an issue that the 3rd bit of scanning mode does not affect processing (PR #213)
910

1011
version 2.1.4 (git tag v2.1.4rel)
1112
================================

sampledata/scanning_mode.grib2

191 Bytes
Binary file not shown.
190 Bytes
Binary file not shown.

src/pygrib/_pygrib.pyx

+8-13
Original file line numberDiff line numberDiff line change
@@ -1185,12 +1185,7 @@ cdef class gribmessage(object):
11851185
raise RuntimeError(_get_error_message(err))
11861186
return longval
11871187
else: # array
1188-
if self.has_key('jPointsAreConsecutive') and\
1189-
self['jPointsAreConsecutive']:
1190-
storageorder='F'
1191-
else:
1192-
storageorder='C'
1193-
datarr = np.zeros(size, int, order=storageorder)
1188+
datarr = np.zeros(size, int)
11941189
err = grib_get_long_array(self._gh, name, <long *>datarr.data, &size)
11951190
if err:
11961191
raise RuntimeError(_get_error_message(err))
@@ -1205,12 +1200,7 @@ cdef class gribmessage(object):
12051200
raise RuntimeError(_get_error_message(err))
12061201
return doubleval
12071202
else: # array
1208-
if self.has_key('jPointsAreConsecutive') and\
1209-
self['jPointsAreConsecutive']:
1210-
storageorder='F'
1211-
else:
1212-
storageorder='C'
1213-
datarr = np.zeros(size, np.double, order=storageorder)
1203+
datarr = np.zeros(size, np.double)
12141204
err = grib_get_double_array(self._gh, name, <double *>datarr.data, &size)
12151205
if err:
12161206
raise RuntimeError(_get_error_message(err))
@@ -1336,7 +1326,12 @@ cdef class gribmessage(object):
13361326
return datarr
13371327
if ny != GRIB_MISSING_LONG and nx != GRIB_MISSING_LONG and\
13381328
self.expand_reduced:
1339-
datarr.shape = (ny,nx)
1329+
if self.has_key('jPointsAreConsecutive') and\
1330+
self['jPointsAreConsecutive']:
1331+
storageorder='F'
1332+
else:
1333+
storageorder='C'
1334+
datarr = datarr.reshape(ny, nx, order=storageorder)
13401335
# check scan modes for rect grids.
13411336
if datarr.ndim == 2:
13421337
# columns scan in the -y direction (so flip)

test/test_misc.py

+90
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"""Collection of miscellaneous minor tests."""
22

3+
import os
4+
from tempfile import NamedTemporaryFile
5+
6+
import numpy as np
37
import pygrib
48
import pytest
59

@@ -9,3 +13,89 @@ def test_internal_value_type_of_runtime_error():
913
with pytest.raises(RuntimeError) as e:
1014
grbindx.write("nonexistent/path")
1115
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

Comments
 (0)