A city between grid nodes: choosing a forecast point

An original coordinate-to-node exercise covering exact ties, longitude seams, poles and the distinction between axis rounding and minimum distance.

A conceptual scene for choosing a forecast point, rather than an exact grid map.
A conceptual scene for choosing a forecast point, rather than an exact grid map.

The city has already been found in the catalogue: its identity is known, and latitude and longitude are stored in separate fields. A weather model, however, does not have to contain a node at that exact location. Another decision sits between the location's coordinates and a temperature value: which element of the model grid should we read, and why that one?

In the earlier discussion of geographical catalogues, we separated names, places and coordinate revisions. Here we move to the next relationship: coordinate → grid node. We will build a small original Python example, define its boundary behaviour and run the checks. Every point and grid is synthetic; the exercise contains no forecasts, real cities or network calls.

The most important limitation comes first: rounding latitude and longitude separately is a particular selection rule. It does not guarantee the shortest distance over the Earth's surface. The example will demonstrate that difference numerically instead of hiding it behind a function named nearest.

Describe the grid first

Take a global regular grid with a 10° step. It is deliberately coarse so that its indices are easy to check by hand. This is not a recommended resolution for a weather product. A second teaching version will use a 5° step to show how changing the grid affects a binding.

Property First teaching version
Identifier teaching-10deg-v1
Latitudes −90° through +90°, both included, in 10° steps
Longitudes −180° included, +180° excluded, in 10° steps
Logical dimensions 19 rows and 36 columns
Index direction Rows northward, columns eastward
Poles One canonical point per pole, at longitude 0°

The description “regular grid” alone is insufficient for reading an external file. For example, GRIB2 template 3.0 specifies dimensions, first and last points, increments, Earth shape and scanning mode separately. Our rows are logical indices for this exercise. We do not turn them into offsets in a real GRIB file or assume its storage order.

The grid identifier also has meaning. In the example's closed registry, teaching-10deg-v1 is allowed only with a 10° step. Passing a 5° step under that name is rejected. The same row and column must not silently refer to a different point after the geometry changes.

Preserve decimal input until the node decision

The select function receives coordinate strings such as "12.340" and "45.670". The grammar permits a sign, one to three integer digits and up to six digits after the decimal point. Whitespace, exponential notation, slash-separated fractions, NaN, infinity and additional decimal places are rejected. Numeric float values and booleans are outside this contract too.

This is a chosen boundary of the teaching interface, not a universal geographical coordinate format. It lets us state exactly which value was rounded. We do not discard a seventh decimal place or repair an invalid latitude by moving it to the nearest permitted value.

After validation, the string becomes a Fraction. The Python 3.12 documentation explains the distinction between a fraction constructed from decimal text and one constructed from an already computed binary float. Here, "5.000001" becomes exactly 5,000,001 / 1,000,000. Values on opposite sides of a midpoint therefore do not collapse because of earlier rounding.

For each axis, we first find the fractional index between nodes. Integer numerator and denominator arithmetic then makes the decision:

quotient, remainder = divmod(value.numerator, value.denominator)
index = quotient + int(2 * remainder >= value.denominator)

If the remainder is less than half the denominator, the lower index remains. Otherwise, the higher index is selected. An exact tie deliberately goes toward the increasing coordinate: north for latitude and east for longitude. This is neither ties-to-even nor rounding away from zero.

For example, latitude +5° maps to +10°, while −5° maps to 0°. A value of 4.999999° still maps to 0°, whereas 5.000001° maps to +10°. This asymmetry at exact midpoints is not a hidden mistake: it is recorded in the policy identifier and checked explicitly.

The longitude seam does not add a column

We accept latitude only within [−90°, +90°]. The teaching longitude input range is deliberately wider: [−540°, +540°]. It allows equivalent representations after one additional turn. Values outside that range are rejected instead of accepting an arbitrarily large number.

Before choosing a column, longitude is normalized into [−180°, +180°):

canonical = (longitude + 180) % 360 - 180

Thus +180° and −180° share a canonical representation. Longitudes 0°, +360° and −360° produce the same result too. After rounding, the column wraps modulo the number of columns: the exact midpoint +175° selects the eastward node +180°, represented as −180°. The table does not contain two different nodes for the same seam.

This rule must not be transferred to latitude. A value of +91° is invalid under our contract. Wrapping it, reflecting it across a pole or clamping it to +90° would change the meaning of the request.

Poles have a separate rule. An exact +90° input with any admitted longitude is normalized to (+90°, 0°). If a +86° point rounds to the pole, the canonical node also uses longitude 0°, but the distance from the original point remains nonzero. Our logical index array does not promise that every polar column represents a distinct physical location.

Distance explains the selection; it does not repair it

After selection, we calculate the distance from the input point to the node on an explicitly chosen sphere with a radius of 6,371 km. The trigonometry uses float, and the displayed result is rounded to three decimal places. This is approximate geometric diagnostics; the number of decimal places does not establish that precision for the coordinates or Earth model.

The PROJ documentation distinguishes an ellipsoidal geodesic from the spherical case, where the path follows a great circle. Our example calculates only the latter. It implements no ellipsoidal geodesy, terrain model or elevation correction.

For the synthetic point (12.340°, 45.670°), the rule selected (10°, 50°), and the program returned 539.241 km. Reversing both coordinate signs selected (−10°, −50°) at the same distance. These large numbers emphasize how coarse the teaching grid is; they are not measurements of a weather service.

Now consider an awkward case:

Query point Node Distance on the teaching sphere
(44.99°, 5°) (40°, 10°), selected by rounding the axes 689.566 km
The same point (50°, 0°), another node in the same grid 671.562 km

The other node is closer. Our function therefore implements a binding based on two angular coordinates, but it does not find the minimum spherical distance. A product promising that minimum needs an appropriate algorithm and verification of its metric. Renaming the function is insufficient.

Keep the explanation of the binding

The result contains the normalized input point, node coordinates, row, column, grid version, policy and a separate distance. The identifier teaching-10deg-v1/r10/c23 belongs to a node, not a city. It does not replace a settlement's stable ID or its coordinate revision.

For the same point, the second grid with a 5° step selected (10°, 45°); the calculated distance was 270.265 km. This is a different binding with a different grid_id. Even when both versions select physically identical coordinates, their node keys remain different in the exercise. Sharing data across versions would require separately established compatibility.

A future catalogue binding should also retain the place ID and the coordinate revision it used. A coordinate or grid change can then trigger an explicit recalculation. The example itself does not maintain a catalogue, store a forecast or transfer old weather values between nodes.

Check boundaries against independent expectations

The local check used the Python standard library.

The local execution completed 28 test methods successfully. A separate file contains 33 manually specified cases with expected indices and coordinates. Another 162 combinations cover every midpoint on both axes and adjacent decimal values in the 10° grid.

Expected results are not calculated by the selection function itself. A second method enumerates permitted node coordinates and compares angular deviations. Distance has an independent check based on the dot product of unit vectors, alongside known geometrical cases: zero, a quarter circle, antipodal points, both poles and crossing the longitude seam.

Negative cases check invalid types, infinity, bounds, an unknown grid and an attempt to change spacing under an old name. A separate test retains the counterexample to the “nearest node” claim. The successful execution establishes this program's behaviour for defined inputs; it does not establish the geographical suitability of a real source.

What rounding cannot decide

For coasts and mountains, distance alone is insufficient to assess how representative a value is. We have no land mask, node elevations or local terrain model. The code therefore invents no correction and makes no promise that a closer node predicts the weather more accurately. Even an exact coordinate match is not a forecast-quality assessment.

Working with a real product begins with its grid description, its selection or interpolation policy, and its handling of unavailable values. The public ecCodes example illustrates a search result containing node coordinates, index and distance. These are useful explanatory fields; our exercise neither runs ecCodes nor reproduces its algorithm.

The practical result is a transparent contract. We know which coordinates were admitted, what happened at a midpoint and seam, which grid version was used and which distance was actually calculated. With that record, the next stage can load data for a specified node while keeping decisions about place identity, geometry and forecast quality separate.

  1. Weather Moving from 0.25° to 0.1°: what to establish before expanding
  2. Weather 77 thousand cities: how many distinct forecast points need processing
  3. Weather A town is more than its name: identity in a geographic catalogue

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