NOAA-EMC / NCEPLIBS-g2c

This library contains C decoder/encoder routines for GRIB edition 2.
Other
18 stars 11 forks source link

change template values internally to signed 64-bit ints #379

Closed edwardhartnett closed 1 year ago

edwardhartnett commented 1 year ago

GRIB2 sometimes stores 4-byte unsigned floats in templates.

To handle this, internally we need to store template values as long long int. Interestingly, the degrib2.F90 prints every template value as a signed int, even if it is an unsigned int. So some numbers show up as negative in degrib2 output. In g2cdegrib2.c I cast back to int to match the behavior of degrib2.F90.

Part of #209

AlexanderRichert-NOAA commented 1 year ago

Build/tests look good to me. Is there a one-sentence explanation for the connection between the use of 4-byte floats in g2 files and the need for long long ints?

edwardhartnett commented 1 year ago

@AlexanderRichert-NOAA here's your one-sentence explanation: "GRIB2 is dumb."

So some 4-byte ints may be negative, some positive. Are the positive ones small enough to fit into a signed int? There is no guarantee. So in order to ensure that any unsigned 4-byte ints which are larger than the largest signed 4-byte ints are handled properly, the whole template arrray needs to be stored as 8-bytes signed long long int. ;-)