terraflow.climate¶
The climate module provides spatial interpolation and index-based matching for aligning climate observations to raster cells.
Overview¶
Per-cell climate interpolation via configurable spatial algorithms:
"linear"(default):scipy.interpolate.griddata— fast, no extra dependencies"kriging": Ordinary Kriging viapykrige— geostatistically optimal; also produces per-cell uncertainty ({var}_krig_stdcolumns)"idw": Inverse Distance Weighting (power=2) — faster than kriging, no uncertainty output- Index-based matching: Row order or explicit cell ID matching for pre-aligned data
- Graceful fallbacks: Global mean values for cells outside interpolation range or with sparse data
Quick Example¶
import pandas as pd
from terraflow.climate import ClimateInterpolator
# Load climate data
climate_df = pd.read_csv("weather_stations.csv")
# Create spatial interpolator with kriging (produces uncertainty columns)
interpolator = ClimateInterpolator(
climate_df=climate_df,
strategy="spatial",
interpolation_method="kriging",
fallback_to_mean=True
)
# Interpolate values for raster cell locations
import numpy as np
cell_lats = np.array([39.14, 38.55])
cell_lons = np.array([-100.82, -99.20])
interpolated = interpolator.interpolate(cell_lats, cell_lons)
Use Cases
- Spatial: Weather station networks, satellite gridded data
- Index: Pre-processed per-cell climate datasets
API Reference¶
climate
¶
Climate data handling with spatial interpolation and matching strategies.
This module provides tools for aligning climate data with raster cells using index-based lookup or spatial interpolation. Three spatial methods are supported:
- linear (default) — Delaunay-triangulation linear interpolation via
scipy.interpolate.griddata. Fast, zero extra dependencies, no uncertainty estimate. - kriging — Ordinary Kriging via
pykrige. Geostatistically optimal (Best Linear Unbiased Predictor) with automatic variogram model selection (spherical / exponential / Gaussian) based on Leave-One-Out Cross-Validation (LOOCV). Returns per-cell prediction standard deviation as{var}_krig_stdcolumns. Requires ≥MIN_KRIGING_STATIONSstations. - idw — Inverse Distance Weighting (power=2). Faster than kriging, no extra dependencies beyond numpy, no uncertainty estimate.
Cross-validation (LOOCV) is computed at initialisation time for the kriging
method and stored in ClimateInterpolator.cv_metrics. The pipeline writes
these metrics to report.json under interpolation_cv.
Example
.. code-block:: python
import pandas as pd
from terraflow.climate import ClimateInterpolator
climate_df = pd.read_csv("climate.csv")
cell_lats = [40.5, 40.6, 40.7]
cell_lons = [-74.5, -74.4, -74.3]
interpolator = ClimateInterpolator(
climate_df=climate_df,
strategy="spatial",
interpolation_method="kriging",
fallback_to_mean=True,
)
cell_climate = interpolator.interpolate(cell_lats, cell_lons)
# Returns DataFrame with mean_temp, total_rain,
# mean_temp_krig_std, total_rain_krig_std columns.
ClimateInterpolator(climate_df, strategy='spatial', interpolation_method='linear', variogram_mode='standard', cell_id_column=None, fallback_to_mean=True)
¶
Spatially interpolate or index-match climate data to raster cells.
Supports two top-level strategies for aligning climate observations to raster cells:
"spatial"— interpolate from weather-station coordinates to cell centroids. Three algorithms are available viainterpolation_method:"linear"(default),"kriging","idw"."index"— match cells to climate records by row order or cell ID.
When interpolation_method="kriging", the interpolator:
- Selects the best variogram model (spherical / exponential / Gaussian) by Leave-One-Out Cross-Validation (LOOCV) RMSE on the first climate variable.
- Stores per-variable LOOCV RMSE and MAE in
cv_metrics. - Returns
{var}_krig_stdcolumns alongside the point estimates.
Attributes:
| Name | Type | Description |
|---|---|---|
climate_df |
DataFrame
|
Climate data with |
strategy |
str
|
Top-level matching strategy ( |
interpolation_method |
str
|
Spatial algorithm ( |
cell_id_column |
str or None
|
Optional column for explicit cell-ID matching ( |
fallback_to_mean |
bool
|
If True, replace NaN predictions with the global variable mean. |
cv_metrics |
dict
|
LOOCV results populated at init when |
climate_columns |
list[str]
|
Numeric climate variable columns (excludes |
Initialise the interpolator and (for kriging) select variogram model.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
climate_df
|
DataFrame
|
DataFrame with climate data. Must have |
required |
strategy
|
Literal['spatial', 'index']
|
|
'spatial'
|
interpolation_method
|
Literal['linear', 'kriging', 'idw']
|
Spatial interpolation algorithm. |
'linear'
|
cell_id_column
|
Optional[str]
|
Column name for cell-ID matching ( |
None
|
fallback_to_mean
|
bool
|
Replace NaN / extrapolated values with the global variable mean. |
True
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
ImportError
|
If |
Source code in terraflow/climate.py
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 | |
interpolate(cell_lats, cell_lons)
¶
Interpolate or match climate data to cell locations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cell_lats
|
ndarray
|
Array of cell latitudes (WGS84). |
required |
cell_lons
|
ndarray
|
Array of cell longitudes (WGS84). |
required |
Returns:
| Type | Description |
|---|---|
DataFrame
|
One row per cell, columns for each climate variable. When
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If array lengths do not match. |
Source code in terraflow/climate.py
CoordinateRange
¶
Bases: BaseModel
Validated geographic coordinate pair.
Ensures latitude is in [-90, 90] and longitude in [-180, 180]. Pydantic provides automatic validation and type coercion.
validate_latitude(v)
classmethod
¶
Ensure latitude is in valid range [-90, 90].
Source code in terraflow/climate.py
validate_longitude(v)
classmethod
¶
Ensure longitude is in valid range [-180, 180].