Repository navigation
Description
Before you start
- I searched the existing issues and the roadmap and did not find a duplicate request.
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 as450: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) andTP2M(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.fetchreturns UTF-8 text of at most 2 MB, and can't ask for a range (crates/engine/src/cmd/plugin_fetch.rs);layer.addUrldownloads 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=11in GRIB1 (the fields of wgrib's inventory lines, so a band can be matched with its.invline), discipline, category, number, template, surface and forecast time in GRIB2. - Values
(R + X × 2^E) / 10^Din the file's units (kelvin stays kelvin), held asf32like 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°andLa2 = 90°, the grid's edges, but scan north to south (scanning mode 0) in 1000 rows of 0.18° whose centres the.ctlputs 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.grbfromlayer.addUrlopens 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'sRangeandcurl -rwrite them (BAM's PREC above:[1562037716, 1566037799], the next line's offset minus one). The app asks for that range only, withRange: bytes=first-last, and opens what comes back as the file.- For a GRIB file: an address whose file name ends in
.grb,.grib,.grb2or.grib2, or any address with"format": "grib". Other formats refusebytes(a range of a GeoTIFF or a GeoPackage isn't a file). - A server that ignores the range and answers
200with 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
206and the length asked for. - The same reach rules as any
layer.addUrlandhost.fetchfrom 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,bytesis refused with a message in this part and comes in a later one: a cross-originRangerequest 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::gribgives way to its driver, andlayer.addUrl'sbytesstays 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
fix(data): GRIB review round 1 (opengeos#2428)
fix(data): GRIB review round 2 (opengeos#2428)
fix(data): GRIB review round 3 (opengeos#2428)
fix(data): GRIB review round 4 (opengeos#2428)
fix(data): GRIB review round 5 (opengeos#2428)
fix(data): GRIB review round 6 (opengeos#2428)
fix(data): GRIB review round 7 (opengeos#2428)
fix(data): GRIB review round 8 (opengeos#2428)
fix(data): GRIB review round 9 (opengeos#2428)
fix(data): GRIB values past f32's range have no value, and are said (o…
fix(data): GRIB review round 10 (opengeos#2428)
fix(data): GRIB review round 12 (opengeos#2428)
fix(data): GRIB review round 13 (opengeos#2428)
fix(data): GRIB review round 14 (opengeos#2428)
fix(data,ui-egui): GRIB review round 15 (opengeos#2428)
fix(data): GRIB review round 16 (opengeos#2428)
feat(data): read GRIB files without GDAL (opengeos#2428, part 1) (ope…
feat(engine): layer.addUrl reads a byte range of a GRIB file (opengeo…
feat(data): read GRIB2's complex packing (opengeos#2428) (opengeos#2809)
fix(plugins): a GRIB byte range a plugin command asked for lands for …