Skip to content

Comments

Fix parsing of GRIB2 files without Section 2 (Local Use Section)#2

Open
SebastienN15 wants to merge 1 commit intoUdSAES:masterfrom
SebastienN15:fix-optional-section2
Open

Fix parsing of GRIB2 files without Section 2 (Local Use Section)#2
SebastienN15 wants to merge 1 commit intoUdSAES:masterfrom
SebastienN15:fix-optional-section2

Conversation

@SebastienN15
Copy link

Summary

This PR fixes a bug where the library fails to parse valid GRIB2 files that omit Section 2 (Local Use Section), which is optional according to the WMO GRIB2 specification.

Problem

The library was always attempting to parse Section 2, even though it's optional in GRIB2 files. According to the GRIB2 specification, Section 2 (Local Use Section) is optional and recommended not to be used for international data exchange.

When parsing GRIB2 files that omit Section 2 (such as Météo-France Arome files), the parser would read subsequent sections with incorrect offsets, leading to errors like:

PRODUCT_DEFINITION_TEMPLATE_NOT_SUPPORTED: 31747

The bogus template number (31747 in this case) is actually random bytes from a later section being misinterpreted as the Product Definition Template number.

Solution

Modified the parseAllSections() function to:

  1. Check if Section 2 is present by reading the section number byte (at offset +4 after the section length)
  2. Only parse Section 2 if the section number is 2
  3. Properly adjust offsets for Section 3 and subsequent sections whether or not Section 2 is present

Compatibility

  • ✅ All existing tests pass
  • ✅ Backward compatible with GRIB2 files that include Section 2 (like DWD files)
  • ✅ Now supports GRIB2 files that omit Section 2 (like Météo-France Arome files and other standard GRIB2 files)

Testing

Tested with:

  • Existing test suite (all tests pass)
  • Météo-France Arome GRIB2 file (78 MB, 20 messages) - now parses successfully
  • The file that previously failed now correctly identifies Product Definition Template 0, which is supported by the library

Files Changed

  • index.js: Modified parseAllSections() function (lines 302-314)

References

According to WMO GRIB2 specification:

  • Section 2 (Local Use Section) is optional
  • It is recommended not to use Section 2 for international data exchange
  • Many GRIB2 files from different meteorological agencies omit this section

Section 2 (Local Use Section) is optional in GRIB2 files according to the
WMO GRIB2 specification, and it is recommended not to use it for international
data exchange. However, the library was always attempting to parse Section 2,
causing incorrect section offsets when it was absent.

This resulted in the parser reading subsequent sections with wrong offsets,
leading to errors like "PRODUCT_DEFINITION_TEMPLATE_NOT_SUPPORTED" with bogus
template numbers.

Changes:
- Modified parseAllSections() to check if Section 2 is present by reading the
  section number byte (offset +4 after the section length)
- Only parse Section 2 if the section number is 2
- Properly adjust offsets for Section 3 and subsequent sections whether or not
  Section 2 is present

This fix maintains backward compatibility with GRIB2 files that include
Section 2 (like DWD files) while also supporting files that omit it (like
Météo-France Arome files and other standard GRIB2 files).

All existing tests pass with this change.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
@SebastienN15
Copy link
Author

@florianwagner this would unblock reading grib files from Météo France, let me know if that's good for you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant