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

Added support for NAVGEM files which include a text header #115

Merged
merged 1 commit into from
Jan 5, 2024
Merged
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
17 changes: 16 additions & 1 deletion grib2io/_grib2io.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,22 @@ def _build_index(self, no_data=False):
try:
# Read first 4 bytes and decode...looking for "GRIB"
pos = self._filehandle.tell()
header = struct.unpack('>i',self._filehandle.read(4))[0]

# Ignore headers (usually text) that are not part of the GRIB2
# file. For example, NAVGEM files have a http header at the
# beginning that needs to be ignored.

# Read a byte at a time until "GRIB" is found. Using
# "wgrib2" on a NAVGEM file, the header was 421 bytes and
# decided to go to 2048 bytes to be safe. For normal GRIB2
# files this should be quick and break out of the first
# 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]
if header.to_bytes(4, "big") == b"GRIB":
pos = pos + test_offset
break

# Test header. Then get information from GRIB2 Section 0: the discipline
# number, edition number (should always be 2), and GRIB2 message size.
Expand Down