Skip to content

Commit

Permalink
Merge pull request #451 from kmpaul/kpaul/issue-448
Browse files Browse the repository at this point in the history
Allow for small GRIB files
  • Loading branch information
martindurant authored Apr 12, 2024
2 parents 95f333c + fb21134 commit dee5938
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
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.

0 comments on commit dee5938

Please sign in to comment.