Skip to content

feat(data): read GRIB2's complex packing (#2428, part 3) - #2809

Draft
elpidioassinaturas wants to merge 1 commit into
opengeos:mainfrom
elpidioassinaturas:feat/grib-complex
Draft

elpidioassinaturas wants to merge 1 commit into
opengeos:mainfrom
elpidioassinaturas:feat/grib-complex

Conversation

@elpidioassinaturas

@elpidioassinaturas elpidioassinaturas commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part 3 of #2428, its "complex packing" item: GRIB2 fields in complex packing, without (template 5.2) and with spatial differencing (5.3, order 1 and 2), are read. NCEP's GFS and CPTEC's MERGE (Brazil's merged satellite and gauge rain) are packed so; today they open as "GRIB2 complex packing (template 5.3), not read yet".

crates/data/src/raster/grib/complex.rs (new):

  • Groups: each with its own reference, width and length, checked before a value is read. The groups must hold exactly the field's values and the data section must hold them all; otherwise the field is left out as damaged.
  • Missing values (management 1 and 2, marked by the value's bits or by the reference of a group of no bits) are NaN, as points a bitmap masks are. They are neither counted as rounded nor said as past f32's range. The primary and the secondary missing value both become NaN.
  • Spatial differencing is undone as the values are read. Missing values aren't differenced. The first values are unsigned and the least difference is a sign bit and magnitude, as GDAL reads them.
  • A field of no groups is a constant one (NCEP's libraries write constant fields so).
  • Nothing held apart from the field: the values are read one at a time, so a field still costs 4 bytes a point. A field of more groups than values is refused before its groups are walked.
  • Never silent: splitting methods other than the general one, other missing value managements, differencing of another order and descriptors of more than 4 octets are left out and said, by name, with the existing count. Differences that add up past 2^53 are damage.

grib.rs: unpack's scaling, (R + X × 2^E) / 10^D, is moved unchanged into scaling, shared by both packings, and field_values takes a missing value.

Reaches: every path the GRIB reader already has (desktop, browser upload, command line, Python, layer.addUrl with and without "bytes"). No new command, parameter or dependency.

Left for later: JPEG 2000, PNG, CCSDS, IEEE and GRIB1's second-order packing; other grids; bytes in the browser.

Where code comes from: written from WMO's FM 92 GRIB specification. No code was copied or translated from another project.

Testing

  • 7 new tests: groups of different widths, with and without a bitmap; primary and secondary missing values, in values and in groups of no bits; both orders of differencing, with missing values; a constant field of no groups; each refused variant by name; each kind of damage by name; every prefix and every octet changed without a panic.
  • Values compared with GDAL 3.9.3's GRIB driver (through rasterio):
    • The MERGE daily file of 2026-11-22 (two fields of 1001 × 924) is identical, value by value and mask by mask.
    • 12 records of the GFS 0.25° run of 2026-11-23 00 UTC (pressure, wind, temperature, soil, rain, vegetation): the same points without a value, and values within 2 steps of f32 (4 of them identical; GDAL unpacks in f32, this reader in f64).
    • 12 files written by GDAL in complex packing, without and with differencing of order 1 and 2, with and without missing values: the same points without a value, and values within 1 step of f32. Constant fields are identical.
    • The messages the new tests build: the same values. GDAL gives the secondary missing value as 9998; here it has no value.
  • cargo test -p rustgis-data (1,810 unit tests and 70 integration tests pass), clippy with -D warnings, cargo fmt --check, cargo xtask test-hygiene and cargo xtask layers. The rest of the workspace and the wasm build are left to CI.

UI evidence

No visible UI change.

Related issue

Part of #2428.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • New Features
    • Added support for reading GRIB2 data using complex packing, including fields with spatial differencing.
    • Bitmap-masked points and points marked as missing by complex packing are reported as having no value.
  • Documentation
    • Updated the data format guides to describe GRIB2 complex packing support and clarify which packing formats are unsupported.

Data representation templates 5.2 and 5.3: groups of their own widths,
missing values, and spatial differencing of order 1 and 2.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 23, 2026 •

Copy link
Copy Markdown
Contributor

Explain Change →

No actionable comments were generated in the recent review.

Walkthrough

The GRIB reader adds support for GRIB2 complex packing with and without spatial differencing. It handles missing values as NaN, rejects unsupported or malformed packing data, and adds tests and documentation for the new support.

Changes: GRIB2 complex packing

Layer / File(s) Summary
Complex packing decoder
crates/data/src/raster/grib/complex.rs
Adds parsing and iteration for complex-packed values. The decoder handles group metadata, missing markers, spatial differencing, and validation of packed data.
GRIB reader integration
crates/data/src/raster/grib.rs
The GRIB2 parser now accepts templates 5.2 and 5.3 and dispatches complex-packed fields to the new decoder. Shared field handling represents missing values as NaN.
Decoder validation and format documentation
crates/data/src/raster/grib_tests.rs, README.md, docs/user-guide/data-formats.md, docs/user-guide/data.md
Tests cover supported decoding cases, unsupported options, malformed data, and truncated inputs. The README and user guides document complex-packing support and unsupported formats.

Estimated code review effort: 4 (Complex) | ~45 minutes

Change: Feature

Sequence Diagram(s)

sequenceDiagram
  participant Parser as GRIB2 parser
  participant Packing as grib2_packing
  participant Decoder as Complex::of
  participant Unpacker as unpack_complex
  participant Values as Complex::values
  participant Field as field_values
  Parser->>Packing: parse packing template
  Packing->>Decoder: parse complex-packing metadata
  Parser->>Unpacker: dispatch complex-packed field
  Unpacker->>Values: create value iterator
  Values->>Field: provide decoded or missing values
Loading

Possibly related issues

  • opengeos/RustGIS#2428 — The issue identifies GRIB2 complex packing as part of the native GRIB reader work; this PR adds decoding for templates 5.2 and 5.3.

Possibly related PRs

  • opengeos/RustGIS#2430 — That PR adds the simple-packing GRIB reader that this PR extends with complex-packing support.

Suggested reviewers: giswqs

Merge Risk: Minimal · up to ea2d1

The GRIB reader now decodes GRIB2 complex packing (templates 5.2 and 5.3), and unsupported or malformed variants are reported as errors. No concrete merge-blocking risk was found in the supplied evidence.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title "feat(data): read GRIB2's complex packing (#2428, part 3)" matches the main change: the PR adds GRIB2 complex packing reading (templates 5.2 and 5.3) in crates/data, as the description and o…
Description check Passed The description follows the template's Summary, Testing, UI evidence, and Related issue sections. Summary explains the new GRIB2 complex packing reader, its groups, missing values, spatial differencin…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Title check

Explanation

The title "feat(data): read GRIB2's complex packing (#2428, part 3)" matches the main change: the PR adds GRIB2 complex packing reading (templates 5.2 and 5.3) in crates/data, as the description and objectives state. It names the scope and the part of the tracked work, so a reviewer scanning history can see the primary change.

Full details: Description check

Explanation

The description follows the template's Summary, Testing, UI evidence, and Related issue sections. Summary explains the new GRIB2 complex packing reader, its groups, missing values, spatial differencing, and refused variants. Testing lists new tests, GDAL comparisons, and the commands run. UI evidence states 'No visible UI change.' as the template allows. Related issue states 'Part of #2428.' The template's placeholder comments were replaced with content.

Finishing Touches

Generate unit tests (beta)

  • Create a new PR
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

A rabbit reads the weather sky,
Through packed-up bits that drift on by.
Groups unfold, and values gleam,
Missing points fade from the stream.
The map takes shape beneath the beam.

Recent review info

Run configuration

  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 0b576b30-b895-493c-b20c-2ef92678af4d
Commits

Reviewing files that changed from the base of the PR and between 691e265 and ea2d118.

Files selected for processing: 6

  • README.md
  • crates/data/src/raster/grib.rs
  • crates/data/src/raster/grib/complex.rs
  • crates/data/src/raster/grib_tests.rs
  • docs/user-guide/data-formats.md
  • docs/user-guide/data.md

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 6 remain after this review.

Comment thread crates/data/src/raster/grib/complex.rs Resolved
Comment thread crates/data/src/raster/grib_tests.rs Resolved
@github-actions

Copy link
Copy Markdown
Contributor

Code review

I read the diff, the base-branch AGENTS.md, and the changed source. I did not build or run the tests, since this review had no build tool. The field-layout arithmetic (octet offsets, bit reads, list padding, differencing recurrences) checks out by hand against the spec layout the code uses. I also checked the test vectors by hand, and they match the code.

Prior reviews: none on this PR, so no earlier concerns were left alone.

Bugs

  • Spatial differencing with missing values (crates/data/src/raster/grib/complex.rs:212, low confidence). Missing points are skipped before the differencing state updates, so when a missing value falls among the first points, the descriptor value lands on a later point and shifts every later difference. The test spatial_differencing_is_undone asserts this. The only oracle is GDAL, which can't settle what the WMO text says. The author should cite the clause, or confirm it against eccodes or NCEP output.

Security

  • None found. Hostile input is bounded: groups ≤ count ≤ points, the lists and values are checked against the data length before reading, and differences are capped at 2^53.

Performance

  • None worth raising. The per-value path is the same shape as the simple-packing path, and the field stays at 4 bytes a point with no second copy.

Quality

  • Weak assertion in complex_packing_marks_missing_values (crates/data/src/raster/grib_tests.rs:1429). r.lost.len() == 1 does not check the past-range notice the comment describes. A suggestion is attached.

AGENTS.md

  • None found. Every unsupported variant is named and counted, the reader and both packings are covered by tests, and the change needs no new commands, lists, or dependencies. The per-point loop in field_values has no Cancel check, but it sits inside a field-level check in to_raster and the simple path already works the same way, so I did not raise it.

Change size

Compared with main (merge base 1d194ab to PR head ea2d118). Six files: 1 added, 5 modified, 0 removed, 0 renamed. Size before 407.8 KB, size after 430.0 KB, change +22.2 KB. The stats file reports bytes_change as 22713 bytes, which is +22.2 KB.

Measure Value
Files changed 6 (added 1, modified 5, removed 0, renamed 0)
Size before 407.8 KB
Size after 430.0 KB
Total size change +22.2 KB
Path Before After Change
crates/data/src/raster/grib_tests.rs 52.4 KB 63.7 KB +11.3 KB
crates/data/src/raster/grib/complex.rs 0 B 8.8 KB +8.8 KB
crates/data/src/raster/grib.rs 59.2 KB 61.2 KB +1.9 KB
docs/user-guide/data-formats.md 116.4 KB 116.5 KB +165 B
README.md 27.8 KB 27.8 KB +30 B
docs/user-guide/data.md 152.0 KB 152.0 KB +28 B

@github-actions

Copy link
Copy Markdown
Contributor

📖 Docs preview

Commit ea2d118 from a fork built, but isn't published until a maintainer adds the preview label (once per commit).

@elpidioassinaturas
elpidioassinaturas marked this pull request as draft November 23, 2026 21:36

This branch has not been deployed

No deployments
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.

2 participants