Skip to content

Commit

Permalink
Update _grib2io.py
Browse files Browse the repository at this point in the history
When looking for the GRIB2 header string "GRIB", changed the
struct.unpack format from ">i" to ">I", an unsigned integer. The
integer representation of "GRIB" should never be negative.

This fixes cases where other data could be in the file and a given
4 bytes represented as ">i" could be negative which raises an error.
Instead, we just want to skip ahead and continue looking for "GRIB".
  • Loading branch information
EricEngle-NOAA committed Jun 10, 2024
1 parent 68e2103 commit 2ae039d
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/grib2io/_grib2io.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def _build_index(self):
# loop when "test_offset" is 0.
for test_offset in range(2048):
self._filehandle.seek(pos + test_offset)
header = struct.unpack(">i", self._filehandle.read(4))[0]
header = struct.unpack(">I", self._filehandle.read(4))[0]
if header.to_bytes(4, "big") == b"GRIB":
pos = pos + test_offset
break
Expand Down

0 comments on commit 2ae039d

Please sign in to comment.