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
|
from io import BufferedReader, BytesIO, SEEK_CUR
from pathlib import Path
import os
import pygrib
import pytest
filename = os.path.join(
os.path.dirname(os.path.abspath(__file__)), "../sampledata/flux.grb"
)
message_lines_expected_for_data_with_zero_offset = """
1:Precipitation rate:kg m**-2 s**-1 (avg):regular_gg:surface:level 0:fcst time 108-120 hrs (avg):from 200402291200
2:Surface pressure:Pa (instant):regular_gg:surface:level 0:fcst time 120 hrs:from 200402291200
3:Maximum temperature:K (instant):regular_gg:heightAboveGround:level 2 m:fcst time 108-120 hrs:from 200402291200
4:Minimum temperature:K (instant):regular_gg:heightAboveGround:level 2 m:fcst time 108-120 hrs:from 200402291200
""".lstrip()
message_lines_expected_for_data_with_4bytes_offset = """
1:Surface pressure:Pa (instant):regular_gg:surface:level 0:fcst time 120 hrs:from 200402291200
2:Maximum temperature:K (instant):regular_gg:heightAboveGround:level 2 m:fcst time 108-120 hrs:from 200402291200
3:Minimum temperature:K (instant):regular_gg:heightAboveGround:level 2 m:fcst time 108-120 hrs:from 200402291200
""".lstrip()
def assert_message_lines(grbs, capsys, expected):
for grb in grbs:
print(grb)
captured = capsys.readouterr()
assert captured.out == expected
def test_open_for_filename(capsys):
grbs = pygrib.open(filename)
assert type(grbs.name) == str
assert_message_lines(grbs, capsys, message_lines_expected_for_data_with_zero_offset)
assert_message_lines(grbs, capsys, "")
grbs.seek(0)
assert_message_lines(grbs, capsys, message_lines_expected_for_data_with_zero_offset)
grbs.close()
def test_open_for_path_object(capsys):
path = Path(filename)
grbs = pygrib.open(path)
assert type(grbs.name) == str
assert_message_lines(grbs, capsys, message_lines_expected_for_data_with_zero_offset)
assert_message_lines(grbs, capsys, "")
grbs.seek(0)
assert_message_lines(grbs, capsys, message_lines_expected_for_data_with_zero_offset)
grbs.close()
def test_open_for_bufferedreader_object(capsys):
# BufferedReader with underlying file.
f = open(filename, "rb")
grbs = pygrib.open(f)
assert type(grbs.name) == str
assert_message_lines(grbs, capsys, message_lines_expected_for_data_with_zero_offset)
assert_message_lines(grbs, capsys, "")
grbs.seek(0)
assert_message_lines(grbs, capsys, message_lines_expected_for_data_with_zero_offset)
grbs.close()
f.close()
# BufferedReader with no fileno (just a byte stream)
if pygrib.__has_fmemopen__: # not implemented on Windows
f = open(filename, "rb")
fb = BufferedReader(BytesIO(f.read()))
grbs = pygrib.open(fb)
assert grbs.name == None
assert_message_lines(grbs, capsys, message_lines_expected_for_data_with_zero_offset)
assert_message_lines(grbs, capsys, "")
grbs.seek(0)
assert_message_lines(grbs, capsys, message_lines_expected_for_data_with_zero_offset)
grbs.close()
f.close(); fb.close()
@pytest.mark.xfail(reason="unimplemented")
def test_open_for_bytesio_object(capsys):
with open(filename, "rb") as f:
buffer = f.read()
f = BytesIO(buffer)
grbs = pygrib.open(f)
assert type(grbs.name) == str
assert_message_lines(grbs, capsys, message_lines_expected_for_data_with_zero_offset)
assert_message_lines(grbs, capsys, "")
grbs.seek(0)
assert_message_lines(grbs, capsys, message_lines_expected_for_data_with_zero_offset)
grbs.close()
f.close()
def test_open_for_textiowrapper_object():
f = open(filename, "r")
with pytest.raises(TypeError) as e:
pygrib.open(f)
assert str(e.value) == "expected bytes, _io.TextIOWrapper found"
INDICATOR = b"GRIB"
def read_indicator(f):
assert f.read(len(INDICATOR)) == INDICATOR
def read_indicator_and_seek_to_starting_point(f):
read_indicator(f)
f.seek(-len(INDICATOR), SEEK_CUR)
def reread_end_section_using_raw_file_access(f):
end_section_bytes = b"7777"
f.seek(-len(end_section_bytes), SEEK_CUR)
assert f.read(len(end_section_bytes)) == end_section_bytes
@pytest.mark.parametrize(
"preprocess, message_lines_expected, postprocess",
[
(read_indicator, message_lines_expected_for_data_with_4bytes_offset, None,),
(
read_indicator_and_seek_to_starting_point,
message_lines_expected_for_data_with_zero_offset,
None,
),
(
None,
message_lines_expected_for_data_with_zero_offset,
reread_end_section_using_raw_file_access,
),
(
read_indicator_and_seek_to_starting_point,
message_lines_expected_for_data_with_zero_offset,
reread_end_section_using_raw_file_access,
),
],
)
def test_open_for_bufferedreader_object_with_raw_file_reading(
capsys, preprocess, message_lines_expected, postprocess
):
f = open(filename, "rb")
if preprocess is not None:
preprocess(f)
grbs = pygrib.open(f)
assert_message_lines(grbs, capsys, message_lines_expected)
grbs.seek(0)
assert_message_lines(grbs, capsys, message_lines_expected)
grbs.close()
if postprocess is not None:
postprocess(f)
f.close()
@pytest.mark.parametrize(
"preprocess, message_lines_expected, postprocess",
[
(read_indicator, message_lines_expected_for_data_with_4bytes_offset, None,),
(
read_indicator_and_seek_to_starting_point,
message_lines_expected_for_data_with_zero_offset,
None,
),
(
None,
message_lines_expected_for_data_with_zero_offset,
reread_end_section_using_raw_file_access,
),
(
read_indicator_and_seek_to_starting_point,
message_lines_expected_for_data_with_zero_offset,
reread_end_section_using_raw_file_access,
),
],
)
@pytest.mark.xfail(reason="unimplemented")
def test_open_for_bytesio_object_with_raw_file_reading(
capsys, preprocess, message_lines_expected, postprocess
):
with open(filename, "rb") as f:
buffer = f.read()
f = BytesIO(buffer)
if preprocess is not None:
preprocess(f)
grbs = pygrib.open(f)
assert_message_lines(grbs, capsys, message_lines_expected)
grbs.seek(0)
assert_message_lines(grbs, capsys, message_lines_expected)
grbs.close()
if postprocess is not None:
postprocess(f)
f.close()
|