Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for small GRIB files #451

Merged
merged 7 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions kerchunk/grib2.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,11 @@ def _split_file(f: io.FileIO, skip=0):
while f.tell() < size:
logger.debug(f"extract part {part + 1}")
head = f.read(1024)
if len(head) < 1024:
break # EOF
if b"GRIB" not in head:
f.seek(-4, 1)
continue
ind = head.index(b"GRIB")
start = f.tell() - 1024 + ind
start = f.tell() - len(head) + ind
part_size = int.from_bytes(head[ind + 12 : ind + 16], "big")
f.seek(start)
yield start, part_size, f.read(part_size)
Expand Down
6 changes: 6 additions & 0 deletions kerchunk/tests/test_grib.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,12 @@ def test_subhourly():
assert len(result) == 2, "Expected two grib messages"


def test_tiny_grib():
fpath = os.path.join(here, "tinygrib.grb2")
result = scan_grib(fpath)
assert len(result) == 1, "Expected one grib message"


def test_grib_tree():
"""
End-to-end test from grib file to zarr hierarchy
Expand Down
Binary file added kerchunk/tests/tinygrib.grb2
Binary file not shown.
Loading