Skip to content

Optimize latlons() for regular_ll and regular_gg grids for grib2#283

Open
JRPdata wants to merge 1 commit into
jswhit:masterfrom
JRPdata:optimize-latlons-regular-grids
Open

Optimize latlons() for regular_ll and regular_gg grids for grib2#283
JRPdata wants to merge 1 commit into
jswhit:masterfrom
JRPdata:optimize-latlons-regular-grids

Conversation

@JRPdata

@JRPdata JRPdata commented Jun 17, 2026

Copy link
Copy Markdown

Description

Speed-up (~10x) patch for latlon(), for narrow case of regular_ll and regular_gg in GRIB2 files. This patch does it by replacing the slower distinctLatitudes and distinctLongitudes calls with direct calculations.

Details

It's my understanding that in eccodes the distinctLatitudes and distinctLongitudes first gets the lats and lons from the entire grid of lats and lons and then filtering them to the unique values; this is significantly slower with larger and larger grids.

Generating it ourselves requires some careful handling for wrapping conditions, when wrapping is actually intended, given the irregular nature of how sometimes the keys are set by the various centers.

For instance, in a few of the gribs I tested had a first lon point of +180.0 going eastward, but with the last gridpoint of +179.75 (the original code generates -180 to 179.75). This is handled by the block of if abs(lon_start) == 180.0 etc.
I also put handling for starting at -180 for the reverse case of this going westward even though I don't have a sample of it to confirm.

Testing

I tested it against 24 varied GRIB files I had scattered about from some various products (i.e. from ECMWF,NCEP, CMC, UKMET, NAVGEM), but it definitely doesn't cover all the centers

It is not exhaustive so use with care if you apply this patch.

Further edge-case reports welcome.

Further notes

For another project I had originally disk-cached (h5) the latlons() on all the relevant grib keys to access the latlons() faster to get the 10x speedup (i.e. benchmarked 10x from ECMWF 0.25 deg -- for larger grids it will be a greater speedup, and conversely for smaller grids the speedup will be more modest). It was worthwhile enough to share this PR.

- Tries to skip distinctLatitudes and distinctLongitudes by calculating them manually
- Yields ~10x speedup for higher res global grids (i.e. 0.25 deg ECMWF).
- Try/except that falls back to original code (use ecCodes decoding for GRIB1 files and column-major layouts).
- Added numerical and GRIB1 fallback tests.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant