Degrees, wind and precipitation: normalize units without losing meaning

Combine means and amounts over different intervals while preserving time boundaries, coverage and provenance.

A droplet, a bowl and ribbons form a conceptual illustration of weather quantities and their intervals.
A droplet, a bowl and ribbons form a conceptual illustration of weather quantities and their intervals.

A weather card displays “6”, “14” and “2.5”. The first number might be a temperature at a particular instant, the second a mean wind speed over ninety minutes, and the third a precipitation amount over a completed interval. Hide all three behind value and unit, and the next developer can easily apply the same averaging operation to them. The calculation will run, but the meaning will change.

Converting degrees or kilometres per hour is only part of normalization. We need to preserve the answer to this question: what quantity, where, and over what time does the number represent? We will use an independent exercise with synthetic data. It retrieves no provider forecast and does not assess weather accuracy. Its purpose is to accept compatible intervals and stop a calculation when the information it needs is missing.

One timestamp can conceal different temporal supports

Three records labelled 12:00 do not necessarily describe the same time span. Temperature may belong to the instant 12:00, a mean to the preceding interval, and precipitation to accumulation over that interval. The current Open-Meteo documentation defines hourly precipitation as a sum over the preceding hour. That is a rule for a particular API variable, not a universal property of a field named precipitation.

The teaching contract explicitly records three types of temporal support:

Kind What the value means Operation allowed in the exercise
instant A value at a specified instant Convert its unit; do not declare it a mean
mean A mean over a complete, known interval Combine compatible intervals using duration weights
amount An accumulated quantity over an interval Add adjacent intervals without double counting

This distinction agrees with CF Conventions 1.12, in its section on cell methods: interpreting a value requires the method that produced it and the temporal cell boundaries. We are not implementing the CF format. We use the underlying distinction to describe our own small JSON contract.

Start with two boundaries, rather than the label “hour”

For interval values, the exercise accepts start and end: integer seconds from a shared synthetic origin on a UTC timeline. Duration is end - start. An instantaneous record has equal boundaries; an interval record has strictly positive duration. This is a deliberately limited exercise format, not a recommendation to replace absolute timestamps in a real API with relative numbers.

The quantity name, location, source, model, model run, original unit and value are retained alongside them. All records in one aggregation must share these identities, except for units that can be converted to a common unit. Height is already included in air_temperature_2m and wind_speed_10m. This contract does not support other heights.

The rule is intentionally strict: it neither mixes two model runs nor selects the best provider. A product that needs such composition must define its meaning and the result’s provenance separately. Matching timestamps do not prove that the model, grid cell or calculation method matches.

The mean of means depends on duration

Suppose the mean temperature from 10:00 to 10:30 is 6 °C, and from 10:30 to 12:00 it is 14 °C. Both values are means, the intervals are adjacent, and together they cover the required two hours. The arithmetic calculation (6 + 14) / 2 gives 10 °C, but assigns half an hour the same weight as ninety minutes.

The appropriate combination for this example is:

(6 × 1800 + 14 × 5400) / 7200 = 12 °C

If 6 and 14 were two instantaneous measurements, that formula would no longer have the same justification. Anything might have happened between the points. We would need a separate interpolation model or measurements from which to estimate the integral. The exercise rejects attempts to pass points off as interval means.

For wind speed, the same arithmetic applies only to scalar speed. A mean of speeds is not a mean wind vector; direction and gusts require other rules. Accepting wind_speed_10m does not mean accepting every field containing the word “wind”.

Change units while preserving temporal support

Temperatures of 42.8 °F and 287.15 K correspond to 6 °C and 14 °C. If the former again describes half an hour and the latter ninety minutes, the result remains 12 °C. Conversion uses the standard relationships between temperature scales listed in the NIST guide.

An affine conversion a × x + b can be applied to this weighted mean because the weights sum to one. That statement should not be extended to every statistic. Temperature differences, variance and sums of values require their own rules. Our contract handles absolute air temperature and its interval mean; temperature differences are outside its scope.

For wind, 36 km/h becomes 10 m/s, while 72 km/h becomes 20 m/s. If the first mean covers one hour and the second two hours, the combined mean is 50 / 3 m/s. Rounding for the card happens after the calculation. The code uses Decimal, but that does not create physical precision absent from the source, nor does it make repeating decimals terminate.

Millimetres and millimetres per hour answer different questions

A precipitation amount of 2 mm in the first hour and 0.5 mm in the second gives 2.5 mm over two hours. This adds amounts on adjacent supports. An interval mean intensity of 2 mm/h over ninety minutes gives 3 mm: multiply the mean accumulation rate by the known duration.

An instantaneous value of 2 mm/h, however, does not establish that 3 mm will fall in the following ninety minutes. That would require an assumption of constant intensity. The exercise has no such model, so mean_rate_to_amount accepts only mean with a complete interval. CF explains accumulation and rate separately, including the need to know temporal support.

Likewise, a two-hour amount cannot be split equally between two hours without explanation. All the rain might have fallen in the first ten minutes. The exercise function handles only whole input intervals: requests for part of an interval are rejected. A real product may adopt a distribution model, but then the result needs to be identified as derived using that model, with its version retained.

A cumulative counter is not a ready-made hourly amount

Another hazardous input is a series in which each value contains all accumulation since the beginning of a run. Suppose a synthetic counter reads 13,15 and 18 mm. Adding gives 46 mm, but each later record already includes the preceding contribution. Differences of 2 and 3 mm can describe the next two intervals only if the accumulation origin and run are shared, the boundaries are known, and no reset occurred between readings.

The first reading of 13 mm also requires an initial support: without it, the accumulation period is unknown. If the counter suddenly decreases, simply clamping the negative difference to zero conceals a run change, reset or data correction. A particular provider may document a policy for those cases, but that policy belongs in its adapter.

Our example accepts amounts already assigned to definite intervals. It does not convert cumulative counters: an unknown kind is rejected. This preserves the responsibility boundary—first establish that the input is an amount for this exact interval, then add adjacent intervals.

Completeness is part of the answer

Before summing or averaging, records are sorted and checked for duplicates, overlap and extension beyond the requested window. A gap or null makes the result unavailable for the whole window. The response retains the duration covered by known values and the required duration. Unknown numbers are not replaced by zero, and a mean over the known half is not labelled as a two-hour mean.

Zero, in contrast, is data. A complete interval with 0 mm can be included in a sum. If one of two hourly records is missing, the available 2 mm does not become a two-hour total. An interface may show partial data separately when needed, but that is a different product with an explicit label, rather than this function’s result.

A daily window also requires exact boundaries. The exercise includes separate windows lasting 23 and 25 hours: with one millimetre for every fully covered hour, the results are 23 and 25 mm. This checks handling of the supplied duration. It neither determines a time zone nor proves the correctness of a particular daylight-saving transition. A separate layer must construct local calendar boundaries.

What the reproducible example provides

The package contains quantities.py and test_quantities.py; run python test_quantities.py from the example directory. In the local execution on Python 3.12.3, all 25 unittest methods completed successfully using the unchanged original fixtures. They checked the numbers above, incomplete coverage, zero, overlap, incompatible identities, unsuitable units and the prohibition on integrating instantaneous intensity. This is a result of the synthetic exercise specifically.

The code preserves original records in inputs and records the applied rule name. It is not a weather adapter, does not cover every quantity, does not repair real provider artifacts, and does not compare forecasts with observations. Accepting a real dataset starts with its specification, followed by separate examples and checks for each rule.

The label beneath a number should explain its meaning: “mean over two hours”, “amount over a completed interval”, or “value at an instant”. Retained boundaries, provenance and coverage information make that label honest—and keep its meaning intact through the next conversion.

  1. Weather The process finished, but was the whole forecast updated?
  2. Weather Multiple weather providers: a shared contract without losing important differences
  3. Weather Forecasts and observations: what weather data does the user see?

A project by the author · Weather Solution200

After reading, a look outside.

Temperature, rain and wind in your city. Check the forecast and plan the hours ahead.

Check the forecast
Next article