Selecting fields from a weather file: the index, byte ranges and source version

Read selected parts of a weather file without mixing versions or accepting incomplete responses: an executed transport exercise and a separate plan for real GRIB data.

Layered ribbons and a selected window suggest reading a subset of weather data.

A forecast page may need temperature and precipitation while the source file contains many more fields. Downloading the entire file is a straightforward starting point. As the number of forecast runs grows, however, a more precise question becomes useful: which bytes do we actually need, and how do we establish that they belong to a consistent set of data?

Reducing the download size is only half the task. We can calculate the range correctly, receive a response and still combine an index from one version with data from another. That mistake is more dangerous than an explicit rejection: the outcome looks successful even though its provenance is ambiguous.

This article examines the transport layer through an independent Python example. It uses an invented LAB1 container and responses held in memory. This is an executed byte-selection exercise, not real GRIB decoding or a request to ECMWF. At the end, we define a separate experiment with an actual file and ecCodes that still needs to be performed.

The index answers “where”, but does not explain all the data

ECMWF documents accompanying .index files: each JSON line describes a field and provides its _offset and _length. Offsets must be read for the particular forecast run because they change. This makes it possible to retrieve an individual message through an HTTP byte range. ECMWF’s official download documentation.

The teaching container has three records. Their names resemble weather parameters, but they contain short text placeholders rather than meteorological values:

Field First byte Length Last byte
2t 0 17 16
10u 17 18 34
tp 35 17 51

We calculate the inclusive end as offset + length - 1. For tp, that is 35 + 17 - 1 = 51. A city identifier, a geographical coordinate and a byte position are different things: this step selects a message from a file, not an individual forecast-grid point.

Our index is deliberately simple: one fictional run, one record per field name and complete coverage of 52 bytes without gaps. A real selection needs further qualifiers, including the run, forecast step, level type and level value. A matching short parameter name is insufficient for a general weather catalogue.

Before fetching data, the example checks its entire index. Negative offsets, zero lengths, overlaps, ranges outside the object and duplicate names cause rejection. Even a JSON member repeated within the same line cannot pass unnoticed. This is the exercise’s chosen strict profile, not a universal description of every possible index.

Pin the versions of two objects

The index and container are separate objects. For each, we retain an “ETag, complete size” pair. Initial checks in the exercise return "index-v1" and "data-v1". These strings are opaque version identifiers. We should not interpret them as content checksums.

For subsequent reads, we use If-Match to request the pinned version. We check a successful partial response against Content-Range, rather than its status alone. The relevant rules are defined in RFC 9110.

The request for our second selected record looks like this:

Range: bytes=35-51
If-Match: "data-v1"
Accept-Encoding: identity
Cache-Control: no-cache

no-cache requests validation of a stored response before reuse; it does not promise an absence of caches along the path. RFC 9111 explains the semantics. Our in-memory object does not model caches, so checking that this header is present does not establish how a real intermediary behaves.

We do not add a silent transition to a new version. If the source changes between two ranges, the entire current candidate is rejected. A separate new pass can start with the new index. It has its own validation history; new bytes are not appended to the old selection.

Build a reader with a narrow contract

The example consists of three files: selection.py, the tests in test_selection.py, and run_demo.py. It has no dependencies outside Python’s standard library. MemoryTransport returns objects shaped like HTTP responses but opens no sockets.

One pass follows this sequence:

  1. Read metadata for the index and container, and pin their versions.
  2. Retrieve the index with its corresponding If-Match and validate its structure.
  3. Confirm that every requested field exists before reading any field bytes.
  4. Read each required range, checking the response and the teaching record’s structure.
  5. Check both versions again, then return the complete result.

The exercise uses small limits: objects up to 4096 bytes, up to eight records and a five-second verification window measured by the supplied clock. These values make rejection cases understandable and reproducible. They are not recommended limits for an ECMWF downloader.

The reader accepts only a single partial 206 response. For tp, it expects exactly Content-Range: bytes 35-51/52, a declared length of 17 and exactly 17 body bytes. A 200 response, a different start, an unknown complete size, an extra byte or a truncated body does not satisfy this contract.

Compression and multipart responses are also outside the exercise. Each additional capability introduces another processing path. For an initial reader, an explicit rejection of an unsupported case is more useful than accepting something that merely resembles the expected result.

In memory, responses already exist as complete byte strings. Checking their size here therefore does not test bounded reading from a network stream. A real transport needs a read limit before a large body has been fully allocated, along with timeouts and connection cleanup. That additional work remains separate.

Correct bytes can still represent the wrong field

The most useful negative scenario does not change the container’s size. We swap the offsets of 2t and tp in the index. Both records are 17 bytes long; coverage remains complete, every byte is available and the range response is correct.

The transport itself does not know what the selected fragment contains. Each of our LAB1 records therefore includes its own field name and payload length. When we request 2t but receive a frame labelled tp, validation ends with field_mismatch.

This is a specially written check for an invented format. It cannot be turned into a real GRIB validator by replacing four leading characters. Real messages require separate checks of their structure and meteorological metadata using an appropriate decoder. The index locates a candidate; the retrieved message must confirm that the candidate matches the request.

There is another boundary: repeating ETag checks on two files does not make them a single atomic transaction. Those checks record what particular objects returned at particular moments. If the publisher produced an inconsistent pair of stable files, the transport cannot create the missing relationship between them. Run metadata and validation of the selected messages are still necessary.

What the local run showed

The local check used the Python standard library.

The exercise executed 41 test methods on Python 3.12.14. The successful scenario selected 2t and tp, returned 34 record bytes from the 52-byte container and made seven simulated requests: two initial checks, one index read, two ranges and two final checks.

This is not a measurement of overall traffic savings. The exercise’s index itself occupies 174 bytes, so the simulated GET bodies total 208 bytes. With such a tiny container, the index is larger than the data. The purpose of the run is to test the reader’s decisions; the value of partial downloading should be assessed on a real workload, including metadata and request counts.

Deliberate fault Expected behaviour
The version changes before a range or final check No candidate is returned
The server ignores Range, truncates or enlarges the body The response is rejected
The first record is correct but the second is unavailable No partial result is returned
The index belongs to a different requested run No data ranges are fetched
The verification window expires No candidate is returned
The range is correct but the frame contains another name Field-identity validation fails

The timing test advances its clock explicitly; it does not wait for five actual seconds. This checks the procedure’s completion deadline, not the freshness of a meteorological forecast. Similarly, cycle-42 is a fictional label for the selected run, not an algorithm for finding the latest weather.

The tests did not run an HTTP client, TLS, a proxy or a real GRIB decoder. They did not evaluate forecast accuracy or change any running website. This boundary makes the result useful: we know which assumptions have been checked and which require a different experiment.

The next experiment: one real run and two fields

The next step requires an available public ECMWF file and its index, one pinned forecast run and one explicit forecast step. We select two fields and record the exact URLs, versions, sizes and relevant index entries. The ranges must be recalculated from that index; the numbers in the teaching table are unsuitable.

After retrieval through a real HTTP client, we check the same transport conditions and then open each fragment with ecCodes. grib_get can retrieve metadata and fails when a required key is missing; grib_ls does not itself provide that behaviour. See the grib_get documentation and grib_ls documentation.

We must also decode the values and compare the parameter, level, initialization time and forecast step, rather than stopping at a recognizable header. grib_get_data provides a way to inspect coordinates and values. The commands, input hashes and actual responses from that experiment should be retained together.

This stage has not been executed in the current preparation: ecCodes tools are absent, and no real weather files were downloaded. The completed transport exercise supplies a foundation: an exact selection, a pinned version, and either a complete result or an explicit rejection. The next step must add genuine confirmation of the selected messages’ contents.

  1. Weather One weather field for thousands of cities: batch reading and decoding
  2. Weather Importing weather data again: checkpoints and revisions instead of duplicates
  3. Weather Degrees, wind and precipitation: normalize units without losing meaning