Skip to content

[Feature]: Read GRIB without GDAL, and a byte range of a remote GRIB file (layer.addUrl "bytes") #2428

Open

Description

@elpidioassinaturas

Before you start

What problem does this solve?

RustGIS can't open GRIB, the format almost every weather and climate model publishes in. It isn't among the rasters GDAL reads either (gdal::EXTENSIONS has no .grb), so today a GRIB file doesn't open at all, on any platform.

The case behind this: the BrasilData plugin wants the rain and temperature forecast of CPTEC/INPE's BAM model for an area of Brazil. The only source is the model's raw output:

  • one folder per run, such as https://dataserver.cptec.inpe.br/dataserver_modelos/bam/TQ0666L064/brutos/2026/11/18/00/;
  • one global GRIB1 file every 6 hours out to 11 days, GPOSNMC{run}{valid}P.fct.TQ0666L064.grb, 1.5 to 1.7 GB each (487 records);
  • beside each, a wgrib inventory (.inv, 52 KB of text) with each record's offset, such as 450:1562037716:d=26111800:prec:kpds5=61:kpds6=1:kpds7=0:TR=4:P1=2:P2=3:TimeU=11:sfc:12-18hr acc:NAve=0, and a GrADS .ctl;
  • a regular 2000 × 1000 grid of 0.18° from 0° east, simple packing, 16 bits a value: each record is 4,000,084 bytes;
  • the fields wanted are PREC (kpds5=61, kg/m²/day, the mean over the 6-hour window) and TP2M (kpds5=128, K).

The server answers ranges (Accept-Ranges: bytes): curl -r 1562037716-1566037799 on the 18-hour file returns 206 and exactly that record, a complete GRIB1 message. So a plugin needs to read two 4 MB records, not two 1.6 GB files. Today it can't:

  • host.fetch returns UTF-8 text of at most 2 MB, and can't ask for a range (crates/engine/src/cmd/plugin_fetch.rs);
  • layer.addUrl downloads the whole file, and nothing reads GRIB once it is here.

Reading the inventory with host.fetch already works: it is small text.

Proposed solution

Two parts, each its own pull request, no new dependency.

1. A native GRIB reader in crates/data (raster::grib), without GDAL

  • Editions 1 and 2, simple packing: GRIB1's grid point data with simple packing, GRIB2's data representation template 5.0. Regular latitude/longitude grids only: GRIB1 data representation type 0, GRIB2 grid definition template 3.0.
  • Any scanning order (i and j directions, j consecutive, alternating rows), laid out north up; a grid round the whole globe is laid out from −180°, as GDAL lays one out. Bitmaps: masked points have no value (NaN).
  • Each field on the grid of the first field read is a band, in file order, named from its message: d=2026111800:kpds5=61:kpds6=1:kpds7=0:TR=4:P1=2:P2=3:TimeU=11 in GRIB1 (the fields of wgrib's inventory lines, so a band can be matched with its .inv line), discipline, category, number, template, surface and forecast time in GRIB2.
  • Values (R + X × 2^E) / 10^D in the file's units (kelvin stays kelvin), held as f32 like every raster.
  • Never silent: complex packing, JPEG 2000, PNG, CCSDS and IEEE fields, other grids (Gaussian, rotated, projected, quasi-regular), predefined grids and bitmaps are left out with one notice a reason and a count ("1 field left out: GRIB2 complex packing (template 5.3), not read yet"); fields on another grid, fields past the memory limit and a message cut short are counted too. A file with nothing readable is an error naming why. A sphere's or another earth's latitudes and longitudes are read as WGS 84's, and a notice says so.
  • BAM's files give La1 = -90° and La2 = 90°, the grid's edges, but scan north to south (scanning mode 0) in 1000 rows of 0.18° whose centres the .ctl puts at ±89.91° (options yrev). When the first and last latitudes run against the scanning mode, rows follow the scanning mode, centred between the two, and a notice says so. (Checked on BAM's land-sea mask: the Arctic Ocean on the first row, the Ross and Weddell ice shelves on the last.)
  • Read message by message, never whole: a 1.6 GB file isn't held in memory.
  • Reaches Add Data (layer.addData), the open dialogs and the Catalog's listing on the desktop, the command line, Python, and uploads in the browser (raster::read_bytes). A downloaded .grb from layer.addUrl opens too.
  • The other packings, complex packing first (used by NCEP's GFS), are left for a later part.

2. layer.addUrl reads a byte range: "bytes": [first, last]

  • "bytes": [first, last], octets counted from 0, both included, as HTTP's Range and curl -r write them (BAM's PREC above: [1562037716, 1566037799], the next line's offset minus one). The app asks for that range only, with Range: bytes=first-last, and opens what comes back as the file.
  • For a GRIB file: an address whose file name ends in .grb, .grib, .grb2 or .grib2, or any address with "format": "grib". Other formats refuse bytes (a range of a GeoTIFF or a GeoPackage isn't a file).
  • A server that ignores the range and answers 200 with the whole file is refused once the answer passes the range's length, rather than downloaded.
  • At most 64 MB a range; a range must be answered with 206 and the length asked for.
  • The same reach rules as any layer.addUrl and host.fetch from a plugin: http(s):// on the internet only, never this computer, the local network or a forbidden address, no redirect there, read anonymously from a plugin.
  • Desktop app (off the frame, through the existing download path, with progress and Cancel) and the command line. A plugin can already call layer.addUrl, so plugins get it with nothing more. In the browser, bytes is refused with a message in this part and comes in a later one: a cross-origin Range request needs the server's CORS, which CPTEC's sends (Access-Control-Allow-Headers: Content-Type, Range).
  • The way back: the layer's source keeps the address and the range, so a saved project reopens the same record.

3. Later parts, if review wants them: bytes in the browser; complex packing; a local GRIB file's fields picked by "layer".

What a plugin then does for BAM's rain: host.fetch the .inv; find the prec line; layer.addUrl {"url": "…/GPOSNMC20261118002026111818P.fct.TQ0666L064.grb", "bytes": [1562037716, 1566037799], "name": "BAM rain 12–18 h"}; then the zonal statistics tools over its area.

Crates: data (the reader), engine (layer.addUrl's bytes and format), ui-egui and rustgis-web (the extension lists). No new dependency.

Testing: part 1 builds GRIB1 and GRIB2 messages in the tests (packings, scanning modes, bitmaps, constant fields, other grids, cut messages, every octet of a file flipped and every prefix read without a panic), and checks real BAM records fetched by range against an independent decoder. Part 2 tests the range against a local server (a 206, a 200 the range refuses, a short answer, the size cap, the reach rules) and a plugin command's layer.addUrl with bytes.

Estimated scope

Medium: a broader feature that is feasible in a reasonable timeframe

Alternatives considered

  • terrio's GRIB driver. opengeos/terrio reads GRIB1 and GRIB2 as GDAL does (most packings and grids, remote files by range). RustGIS doesn't depend on terrio yet (only on terrio-crs), and [Feature]: Native NetCDF-4/HDF5 reading, without GDAL and in the browser: how would you like it architected? #396 raised the build time and size of bringing it in. This proposal keeps a small reader with no dependency for the common case; when RustGIS adopts terrio for data I/O, raster::grib gives way to its driver, and layer.addUrl's bytes stays as it is.
  • RustGIS's GDAL path. Desktop only, needs GDAL installed, local files only, and GRIB isn't among its types today.
  • Binary host.fetch. A plugin would then decode GRIB itself, in every plugin that wants it, and the 2 MB limit is too small for a record.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions