Skip to content

Commit

Permalink
grib2 tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ghidalgo3 committed Jul 15, 2024
1 parent 9b5fe7f commit 572f62d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
1 change: 1 addition & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,5 @@ jobs:
- name: Test with pytest
shell: bash -l {0}
run: |
export ZARR_V3_EXPERIMENTAL_API=1
pytest -v --cov
8 changes: 6 additions & 2 deletions kerchunk/grib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
import xarray
import numpy as np

from kerchunk.utils import class_factory, _encode_for_JSON
from kerchunk.utils import class_factory, _encode_for_JSON, zarr_init_group_and_store
from kerchunk.codecs import GRIBCodec
from kerchunk.combine import MultiZarrToZarr, drop

Expand Down Expand Up @@ -113,6 +113,7 @@ def scan_grib(
inline_threshold=100,
skip=0,
filter={},
zarr_version=None,
):
"""
Generate references for a GRIB2 file
Expand All @@ -134,6 +135,9 @@ def scan_grib(
the exact value or is in the given set, are processed.
E.g., the cf-style filter ``{'typeOfLevel': 'heightAboveGround', 'level': 2}``
only keeps messages where heightAboveGround==2.
zarr_version: int
The desired zarr spec version to target (currently 2 or 3). The default
of None will use the default zarr version.
Returns
-------
Expand Down Expand Up @@ -192,7 +196,7 @@ def scan_grib(
if good is False:
continue

z = zarr.open_group(store)
z, store = zarr_init_group_and_store(store, zarr_version=zarr_version)
global_attrs = {
f"GRIB_{k}": m[k]
for k in cfgrib.dataset.GLOBAL_ATTRIBUTES_KEYS
Expand Down
11 changes: 8 additions & 3 deletions kerchunk/tests/test_grib.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,19 @@
here = os.path.dirname(__file__)


def test_one():
@pytest.mark.parametrize("zarr_version", [2, 3])
def test_one(zarr_version):
# from https://dd.weather.gc.ca/model_gem_regional/10km/grib2/00/000
fn = os.path.join(here, "CMC_reg_DEPR_ISBL_10_ps10km_2022072000_P000.grib2")
out = scan_grib(fn)
out = scan_grib(fn, zarr_version=zarr_version)
ds = xr.open_dataset(
"reference://",
engine="zarr",
backend_kwargs={"consolidated": False, "storage_options": {"fo": out[0]}},
backend_kwargs={
"consolidated": False,
"zarr_version": zarr_version,
"storage_options": {"fo": out[0]},
},
)

assert ds.attrs["GRIB_centre"] == "cwao"
Expand Down

0 comments on commit 572f62d

Please sign in to comment.