About Custom Calculations

Introduction

A custom calculation uses a formula you write with weather forecast data. You choose variables such as temperature, rain, or wind, then describe how to combine or change their values. WindBorne runs the calculation at each location in the forecast grid and returns the result.

  1. Choose Your Grids
    The WM-6 max_temperature_2m_3h and min_temperature_2m_3h over the same three-hour window.
  2. Apply Your Calculation
    Subtract the minimum from the maximum at every grid cell.
  3. Get a New Weather Field
    One output grid showing the three-hour temperature range.

For example, subtracting the three-hour minimum temperature from the three-hour maximum gives the temperature range at each location. More detailed calculations can compare variables, average conditions over time, or combine pressure levels.

Available Endpoints

Validate a calculation, calculate a grid, return values at geographic coordinates, or generate threshold polygons.

The Gridded, Point, and Threshold Calculation endpoints currently support WeatherMesh 6 global and 3 km CONUS. They check that the required forecast data is available when you run the calculation.

1. Setup

For a gridded calculation, send the following request parameters:

For requests with multiple forecast hours, each hour is calculated separately. Hours that don’t have all the required input data are omitted from the result and reported in forecast_hour_errors. Results for the remaining hours are still returned. If every requested hour fails, the request returns HTTP 400 with error and forecast_hour_errors.

In the example below, the calculation finds the maximum temperature over the next 72 hours at each grid cell. The input covers hours 0–360, so output grids can be calculated for hours 0–288. Later hours are skipped because their 72-hour window extends beyond the available input.

Full forecast input and custom calculation output from hour 0 to hour 360 All 121 input steps are available. Blue output covers hours 0–288, giving 97 output grids. The output at hour 12 uses 25 input samples from hours 12–84. Forecast inputs 121 forecast steps Custom calculation output 97 output grids 0h72h144h216h288h360h

Hour 12 · Inputs 12–84h (25 samples) → 1 grid

The output uses the same grid as the input data. When you set bounds on a supported grid, only data for that area is loaded and calculated. The output may include extra cells around the edges.

2. Variables

Forecast variables are lowercase snake_case references enclosed in square brackets. A reference without selectors, such as [temperature_2m], uses the ensemble mean. Selectors inside the brackets choose a distribution, level, or member; for example, [temperature_2m(members=42)] selects ensemble member 42. Forecast inputs retain their native units.

See Variables for the variables and pressure levels available for a model. Its distribution_details field shows whether each variable supports deterministic, percentile, threshold-probability, or ensemble-member selections. Custom Calculations use the forecast variable values in their documented native units and do not convert them automatically.

2.1 Accumulated Variable Timing

Variables like [snowfall_3h] and [total_precipitation_3h] represent totals over the three hours ending at their valid time rather than values at a single instant. A value valid at 06:00 therefore covers 03:00–06:00. The calculation language uses the API's valid time directly and does not shift these variables.

3. Arithmetic Operators and Precedence

Calculations support these basic math operations:

  • Add values with +
  • Subtract values with -
  • Multiply values with *
  • Divide values with /
  • Keep a value’s sign with +value, or reverse it with -value
  • Group calculations with parentheses: (expression)

Calculations follow Python’s usual order: parentheses and function calls first, then positive or negative signs, multiplication and division, and finally addition and subtraction. Operations with the same priority run from left to right.

Here, multiplication happens before addition:

Use parentheses to change the order. Here, addition happens first:

You can combine arithmetic with functions:

To calculate a reciprocal, divide 1 by the value:

Floor division (//), modulo (%), bitwise operators, and shortcuts such as += are not supported. Use = for assignments, explained in section 4. Calculations do not support loops, imports, or other Python statements.

4. Calculation Structure and Evaluation

The last expression in a calculation describes the expected result at each grid cell, forecast hour, and selected pressure level.

A calculation must include at least one forecast variable, which defines the grid to calculate on. Expressions containing only numbers, such as 2 or tan(radians(45)), pass syntax validation but cannot be calculated.

Numbers in a calculation apply to every input value. For example, the following expression subtracts 2 from the temperature at every grid cell and forecast hour:

When an expression contains multiple forecast variables, they are matched by location and valid time before the calculation is performed:

A calculation may contain zero or more assignments followed by a final expression. Each assignment binds a lowercase snake_case name to an expression so it can be reused later in the calculation. Assigned names are bare identifiers; square brackets identify forecast references.

Every assignment ends with a semicolon. Line breaks are formatting whitespace and do not separate statements.

Assignments are evaluated from left to right. You can assign a name more than once. A reassignment uses the name’s previous value to calculate its new value. Later references use the new value, while earlier assignments keep the expression they originally captured. You cannot use a name before it has been assigned, including in its own first assignment.

This constant-only example illustrates assignment syntax; execution still requires a forecast reference:

4.1 Multi-Level Expressions

An upper-air variable without a level= selector returns one grid for each available pressure level. Select one level, such as level=850, to get a single grid. Use a list like level=[850, 700, 500] to get a separate grid for each of those levels.

The examples below show how different level selections affect the result:

5. Core Functions

Core functions take numbers, forecast variables, or calculations as inputs. They calculate a result separately at each grid cell.

To combine values across pressure levels, time, forecast hours, or ensemble members, use aggregate_level(...), aggregate_time(...), aggregate_forecast_hours(...), or aggregate_members(...), explained in section 9. Each supports min, max, sum, and mean. aggregate_members(...) also supports p10, p50, and p90.

Core functions do not combine values from different locations. To average values across longitudes, use zonal_mean(...), explained in section 9.5.

5.1 Comparing and Combining Values

5.2 Scores and Limits

5.3 Math Functions

5.4 Angles and Trigonometry

5.5 Rounding

You can use functions inside other functions and combine them with math operators. This example combines warm temperatures and calm winds into a mosquito-suitability score:

Use the function names and inputs listed above. Version 1 does not support custom functions.

6. Conditional Expressions

Conditions compare values and choose between results. A calculation that ends with a condition returns 1 where it is true and 0 where it is false.

AND is evaluated before OR, so a OR b AND c means a OR (b AND c). Both sides are always evaluated, so every variable they use must be available. Point Calculation interpolates values to each coordinate before checking a condition, so a point's result can differ from its nearest grid cell.

To use a condition in arithmetic, convert it to a number with if. This example returns the share of ensemble members below freezing:

6.1 Checking for Finite Values

is_finite(value) is false where a value is missing (NaN) or infinite, and true everywhere else. This example replaces missing or infinite temperatures with 0:

6.2 Membership in Sets and Ranges

A set is a discrete collection of numbers, while a range is an interval of numbers that is by default continuous or optionally limited to evenly spaced values. Use uppercase IN to check whether a value is in a set or range, and NOT IN to check that it is not:

The syntax and behavior of sets and ranges are detailed below.

6.2.1 Ranges

A range, written range(start, end), contains every value from start up to, but not including, end. include_start and include_end control each bound separately. Without a step, the start must not be greater than the end.

Add an optional step to contain only evenly spaced values: start, start + step, start + 2 * step, and so on, within the bounds. A value must equal one of them exactly: 0.3 is in range(0, 1, step=0.1), but 0.30001 is not.

This example checks whether the temperature rounds to a multiple of 5°C from 0°C to 35°C:

In a stepped range, the start and step can have at most nine decimal places. The start, end, and step must be within ±224 when the start and step are whole numbers, and within ±106 ÷ 10d otherwise, where d is the larger number of decimal places in the start or step. For example, a step of 0.1 allows ends up to ±105.

6.2.2 Sets

A set, written set(...), contains only the listed values. A value must equal one of them exactly. Forecast values rarely do, so be sure to round them first. This example checks whether the temperature rounds to 0°C, 10°C, or 20°C:

Each set can contain at most 1,000 distinct values. Duplicates within the set count once. Values are stored as 32-bit floats, so 1 and 1.00000001 count as the same value.

6.2.3 Using Sets and Ranges

Set values and the start, end, and step of a range must be numbers written in the calculation, not variables, forecast values, or arithmetic. To reuse a set or range, assign it to a name; you can only use that name after IN or NOT IN. This example checks the same range now and 24 hours later:

Missing and infinite values are never in a set or range, so NOT IN is true for them. Add is_finite to exclude them: is_finite(value) AND value NOT IN range(0, 30).

7. Selecting Another Forecast

The request parameter forecast_hours chooses which output hours to calculate. The selectors in this section choose which forecast data a variable reads while each output hour is calculated.

By default, a variable uses data for the forecast hour being calculated. Time selectors let you choose another hour or a list of hours from the same forecast run. Selecting one hour gives one grid that you can use directly in a calculation. Selecting several hours keeps their grids separate; you can combine them, pick one, or calculate with another input that uses the same hours in the same order.

Use offset_hours to select hours relative to the hour being calculated. For example, offset_hours=24 selects 24 hours later, and offset_hours=-6 selects 6 hours earlier. A list such as offset_hours=[-6, 0, 6] selects all three times.

Use forecast_hour to select hours counted from forecast_zero. The valid time is forecast_zero + forecast_hour; forecast_zero can differ from initialization_time. See Run Information for a run's forecast_zero and available hours. For example, forecast_hour=24 always selects hour 24. It also accepts a list of hours. You cannot use offset_hours and forecast_hour together in the same variable reference.

Selecting one hour gives one grid, so you can use it directly in a calculation. This example calculates the temperature change over the next 24 hours:

You can also combine time and distribution selectors in a function. This example picks the higher of the 90th-percentile temperature 24 hours later and the mean temperature at the current hour:

Selecting a list keeps each hour’s grid separate:

For a calculation at hour 12, this selects temperature at hours 6, 12, and 18, returning three grids in that order. The next section explains how to work with these layers.

8. Working with Multi-Grid Results

A calculation can return multiple grids. For example, it may return one grid for each selected pressure level or forecast hour. The following functions let you arrange grids in a specific order, select a grid by its pressure level or time, or aggregate multiple grids into one result. Each function processes every grid cell separately.

8.1 Stack Results

Use stack(first, ...) to combine results into an ordered stack of separate grids.

This creates two grids: temperature at 850 hPa, followed by dewpoint at 700 hPa. Each grid keeps its own values.

8.2 Select a Pressure Layer

Use pressure_layer(value, index) to select one pressure layer from a result. Indices start at 0 and follow the order of the selected levels.

This calculates temperature minus dewpoint at two pressure levels and three times. Index 1 selects the result at 500 hPa, keeping all three times.

8.3 Select a Time Layer

Use time_layer(value, index) to select one time layer from a result. Indices start at 0 and follow the order of the selected times.

This calculates the temperature change from the current hour at two pressure levels for offsets 0, 6, and 12 hours. Index 1 selects the change after 6 hours, keeping both pressure levels.

8.4 Reduce Layers

Use reduce(value, operation) to combine values across all times and vertical levels into one grid. The calculation happens separately at each grid cell.

This example finds the highest temperature at each grid cell across two vertical levels and two times: 850 and 500 hPa, at the hour being calculated and 6 hours later.

Choose min, max, sum, or mean as the operation. For example, reduce(value, mean) averages values across all times and vertical levels at each grid cell. Write the operation name without parentheses; mean(a, b) is a separate function that averages its inputs.

To combine only vertical levels while keeping times separate, use aggregate_level(...) in section 9.1. To combine values across time, use aggregate_time(...) in section 9.2. To calculate cumulative results across forecast hours, use aggregate_forecast_hours(...) in section 9.3. To combine ensemble members while keeping times separate, use aggregate_members(...) in section 9.4.

9. Aggregate Across Levels, Times, Forecast Hours, Members, or Longitudes

Aggregation combines multiple values into one result. It can work across any one of five dimensions: pressure levels, times, forecast hours, ensemble members, or longitudes.

Each aggregation uses a value and an operation, except zonal_mean(...), which always averages. Every other aggregation supports min, max, sum, and mean. aggregate_members(...) also supports p10, p50, and p90.

9.1 Pressure Levels

Use aggregate_level(value, operation) to combine values across pressure levels at each grid cell. It returns one grid for each selected time.

You can combine all levels in the input or specify a pressure range using aggregate_level(value, operation, level_start, level_end).

Both ends of the range are included. Write the pressure bounds as numbers, rather than variables or calculations.

The range must stay within the available pressure levels and include at least one level. Invalid ranges reject the request. If a level inside the range is missing, it is left out. The range is not shortened automatically.

9.2 Time

Use aggregate_time(value, operation, hours_before, hours_after) to combine values across a time window at each grid cell. Both ends of the window are included.

If the input already selects a list of times, use aggregate_time(value, operation) to combine those times.

Use 0 to start or end the window at the hour being calculated. Write both bounds as whole numbers of zero or more. Fractions, variables, calculations, and decimal forms such as 6.0 are not supported.

The calculation includes every available input sample within the window. A variable’s own time coverage stays the same: each value of [total_precipitation_3h] still represents a three-hour total.

9.3 Forecast Hours

Use aggregate_forecast_hours(value, operation, forecast_hour) to calculate cumulative results across forecast hours.

9.4 Members

Use aggregate_members(value, operation) to combine values across all ensemble members at each grid cell. It returns one grid for each selected time.

The input must use the members selector so the calculation contains all available ensemble members.

Member aggregation uses every ensemble member available for the selected model and initialization time. To use one member without aggregating, select it directly, such as [temperature_2m(members=42)].

9.5 Longitudes

Use zonal_mean(value) to average values across all longitudes at each latitude. Every grid cell at a latitude gets the same result. Pressure levels, times, and ensemble members stay separate.

The average uses every longitude in the forecast grid, even when the request covers a smaller area. If any value at a latitude is NaN, the result for that latitude is NaN. zonal_mean(...) is not available for wm-6-3km.

10. Errors

Invalid calculations or level selections reject the request. For requests with multiple forecast hours, hours without all the required input data are skipped. Results are returned for the remaining hours.

Errors identify the affected expression and show the available pressure levels or forecast hours when relevant.