Skip to content

grib2: consolidate scaled value decoding - #1605

Merged
lesserwhirls merged 1 commit into
Unidata:maint-5.xfrom
nullschool-tech:grib2-scaled-value-decoding
Sep 23, 2026
Merged

lesserwhirls merged 1 commit into
Unidata:maint-5.xfrom
nullschool-tech:grib2-scaled-value-decoding

Conversation

@cambecc

@cambecc cambecc commented Sep 22, 2026 •

Copy link
Copy Markdown
Contributor

Description of Changes

The code for decoding GRIB2 scaled values is duplicated in a few places. The decoding process also accumulates more floating point rounding error than it needs to.

This change:

  • introduces a utility method (and tests) for scaled value decoding
  • changes all locations that do scaled value decoding in Grib2Gds and Grib2Pds to use this utility method
  • reduces floating point rounding error when decoding

PR Checklist

  • Link to any issues that the PR addresses
  • Add labels
  • Open as a draft PR
    until ready for review
  • Make sure GitHub tests pass
  • Mark PR as "Ready for Review"

Comment thread grib/src/main/java/ucar/nc2/grib/grib2/Grib2Gds.java
Comment thread grib/src/main/java/ucar/nc2/grib/grib2/Grib2Gds.java
Comment thread grib/src/main/java/ucar/nc2/grib/grib2/Grib2Pds.java
Comment thread grib/src/main/java/ucar/nc2/grib/grib2/Grib2Pds.java
Comment thread grib/src/main/java/ucar/nc2/grib/GribNumbers.java
Comment thread grib/src/main/java/ucar/nc2/grib/GribNumbers.java
Comment thread grib/src/test/java/ucar/nc2/grib/TestGribNumbers.java
Comment thread grib/src/test/java/ucar/nc2/grib/TestGribNumbers.java
Comment thread grib/src/test/java/ucar/nc2/grib/TestGribNumbers.java
@cambecc
cambecc marked this pull request as ready for review September 22, 2026 15:11
@lesserwhirls lesserwhirls added the iosp: grib grib file format label Sep 22, 2026
@lesserwhirls lesserwhirls added this to the 5.11.0 milestone Sep 22, 2026
@lesserwhirls

Copy link
Copy Markdown
Member

This looks great - thank you for the cleanup! We do have one new failing test in our extended test suite. In cdm-test/src/test/java/ucar/nc2/iosp/grib/TestGribSpheroids.java:

public void code7_oblate_specified_m() throws IOException {
String filename = dir + "TT_FC_INCA.grb2";
try (NetcdfFile ncfile = NetcdfFiles.open(filename, null)) {
Variable v = ncfile.findVariable("LambertConformal_Projection");
Attribute axis = v.findAttribute("semi_major_axis");
Assert.assertEquals(6377397., axis.getNumericValue().doubleValue(), 0.1);
}
}

the assert now throws:

java.lang.AssertionError: expected:<6377397.0> but was:<6377397.16>. I think this is easily explained by the reduced floating point rounding error when decoding. If you agree that is the case, would you mind updating the check in the test. I've attached the test file (that is not available in the codebase) in case you are interested in the specific case (compressed with zip so that I can attach it to the issue).

TT_FC_INCA.grb2.zip

@cambecc

cambecc commented Sep 23, 2026

Copy link
Copy Markdown
Contributor Author

The change from 6377397.0 to 6377397.16 is due to widening the Grib2Gds earthRadius, majorAxis, and minorAxis fields from float to double (and also using double for scaled value decoding). I checked the grib octets and they have the majorAxis as 637739716 with a scale factor of 2. So I think this increase in precision is correct. I will update the test.

@cambecc
cambecc force-pushed the grib2-scaled-value-decoding branch from 74649a7 to 1f3c8e7 Compare September 23, 2026 03:47
@lesserwhirls

Copy link
Copy Markdown
Member

Looks great - thank you!

@lesserwhirls
lesserwhirls merged commit d1dfc2d into Unidata:maint-5.x Sep 23, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

iosp: grib grib file format

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants