-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added spatial subset for both messages and grib2io xarray.
- Loading branch information
1 parent
d9552d2
commit 03c861a
Showing
4 changed files
with
348 additions
and
321 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,65 +1,86 @@ | ||
import itertools | ||
from pathlib import Path | ||
|
||
import grib2io | ||
import pytest | ||
import xarray as xr | ||
from numpy.testing import assert_allclose, assert_array_equal | ||
from numpy.testing import assert_array_equal | ||
|
||
import grib2io | ||
|
||
def _del_list_inplace(input_list, indices): | ||
for index in sorted(indices, reverse=True): | ||
del input_list[index] | ||
return input_list | ||
|
||
@pytest.fixture() | ||
def inp_ds(request): | ||
datadir = request.config.rootdir / "tests" / "data" / "gfs_20221107" | ||
|
||
def _test_any_differences(da1, da2, atol=0.005, rtol=0): | ||
"""Test if two DataArrays are equal, including most attributes.""" | ||
assert_array_equal( | ||
da1.attrs["GRIB2IO_section0"][:-1], da2.attrs["GRIB2IO_section0"][:-1] | ||
) | ||
assert_array_equal(da1.attrs["GRIB2IO_section1"], da2.attrs["GRIB2IO_section1"]) | ||
assert_array_equal(da1.attrs["GRIB2IO_section2"], da2.attrs["GRIB2IO_section2"]) | ||
assert_array_equal(da1.attrs["GRIB2IO_section3"], da2.attrs["GRIB2IO_section3"]) | ||
assert_array_equal(da1.attrs["GRIB2IO_section4"], da2.attrs["GRIB2IO_section4"]) | ||
skip = [2, 9, 10, 11, 16, 17] | ||
assert_array_equal( | ||
_del_list_inplace(list(da1.attrs["GRIB2IO_section5"]), skip), | ||
_del_list_inplace(list(da2.attrs["GRIB2IO_section5"]), skip), | ||
filters = { | ||
"typeOfFirstFixedSurface": 103, | ||
"valueOfFirstFixedSurface": 2, | ||
"productDefinitionTemplateNumber": 0, | ||
"shortName": "TMP", | ||
} | ||
|
||
ids = xr.open_mfdataset( | ||
[ | ||
datadir / "gfs.t00z.pgrb2.1p00.f009_subset", | ||
datadir / "gfs.t00z.pgrb2.1p00.f012_subset", | ||
], | ||
combine="nested", | ||
concat_dim="leadTime", | ||
engine="grib2io", | ||
filters=filters, | ||
) | ||
assert_allclose(da1.data, da2.data, atol=atol, rtol=rtol) | ||
|
||
yield ids | ||
|
||
def test_da_write(tmp_path, request): | ||
"""Test writing a single DataArray to a single grib2 message.""" | ||
target_dir = tmp_path / "test_to_grib2" | ||
target_dir.mkdir() | ||
target_file = target_dir / "test_to_grib2_da.grib2" | ||
|
||
@pytest.fixture() | ||
def inp_msgs(request): | ||
datadir = request.config.rootdir / "tests" / "data" / "gfs_20221107" | ||
|
||
with grib2io.open(datadir / "gfs.t00z.pgrb2.1p00.f012_subset") as inp: | ||
print(inp[0].section3) | ||
newmsg = inp[0].subset(lats=(43, 32.7), lons=(117, 79)) | ||
with grib2io.open(datadir / "gfs.t00z.pgrb2.1p00.f012_subset") as imsgs: | ||
yield imsgs | ||
|
||
|
||
print(inp[0]) | ||
print(newmsg) | ||
print(newmsg.section0) | ||
print(inp[0].section0) | ||
print(newmsg.section1) | ||
print(inp[0].section1) | ||
print(newmsg.section2) | ||
print(inp[0].section2) | ||
print(newmsg.section3) | ||
print(inp[0].section3) | ||
print(newmsg.section4) | ||
print(inp[0].section4) | ||
print(newmsg.section5) | ||
print(inp[0].section5) | ||
@pytest.mark.parametrize( | ||
"lats, lons, expected_section3", | ||
[ | ||
pytest.param( | ||
(43, 32.7), | ||
(117, 79), | ||
[ | ||
0, | ||
380, | ||
0, | ||
0, | ||
0, | ||
6, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
0, | ||
38, | ||
10, | ||
0, | ||
-1, | ||
43000000, | ||
79000000, | ||
48, | ||
33000000, | ||
117000000, | ||
1000000, | ||
1000000, | ||
0, | ||
], | ||
id="subset_1", | ||
), | ||
], | ||
) | ||
def test_message_subset(inp_msgs, inp_ds, lats, lons, expected_section3): | ||
"""Test subsetting a single DataArray to a single grib2 message.""" | ||
newmsg = inp_msgs[0].subset(lats=lats, lons=lons) | ||
assert_array_equal(newmsg.section3, expected_section3) | ||
|
||
print(inp[0].data.shape) | ||
print(newmsg.data.shape) | ||
newds = inp_ds["TMP"].grib2io.subset(lats=lats, lons=lons) | ||
assert_array_equal(newds.attrs["GRIB2IO_section3"], expected_section3) | ||
|
||
with grib2io.open(target_file, mode="w") as out: | ||
out.write(newmsg) | ||
assert False | ||
newds = inp_ds.grib2io.subset(lats=lats, lons=lons) | ||
assert_array_equal(newds["TMP"].attrs["GRIB2IO_section3"], expected_section3) |