terraflow.ingest¶
The ingest module contains IO helpers for loading raster and climate inputs.
Quick Example¶
import rasterio
from terraflow.ingest import load_climate_csv
from terraflow.geo import clip_raster_to_roi
# Load climate data with validation
climate_df = load_climate_csv("weather_stations.csv")
print(f"Loaded {len(climate_df)} weather stations")
# Load and clip raster to region of interest
roi = {"xmin": -101.0, "ymin": 38.0, "xmax": -94.0, "ymax": 40.0}
with rasterio.open("land_cover.tif") as src:
clipped_data, transform = clip_raster_to_roi(
src,
roi=roi,
roi_crs="EPSG:4326"
)
Validation
All ingest functions perform automatic validation:
- Climate CSVs must have
lat,loncolumns - Coordinate ranges are checked (lat: [-90, 90], lon: [-180, 180])
- Missing values and duplicates trigger warnings
API Reference¶
ingest
¶
ClimateLayer
¶
Bases: BaseModel
Metadata for a single climate/tabular input layer.
lat_range
instance-attribute
¶
Observed latitude range: (min, max).
lon_range
instance-attribute
¶
Observed longitude range: (min, max).
n_rows
instance-attribute
¶
Row count after coordinate validation and NaN-dropping.
path
instance-attribute
¶
Resolved absolute path to the CSV file.
sha256 = None
class-attribute
instance-attribute
¶
SHA-256 hex digest of the file contents (content fingerprint).
variables
instance-attribute
¶
Climate variable column names (excludes 'lat' and 'lon').
DataCatalog
¶
Bases: BaseModel
Immutable metadata snapshot of all resolved input datasets.
Produced by :func:build_data_catalog after local file resolution and
availability checks. The pipeline orchestrator depends only on
DataCatalog — not on dataset-specific glob logic.
The catalog does NOT read raster pixel data or orchestrate any pipeline step; it is a pure metadata/provenance object.
raster_by_name(layer_name)
¶
Return the first RasterLayer matching layer_name, or None.
RasterLayer
¶
Bases: BaseModel
Metadata for a single raster input layer.
bounds
instance-attribute
¶
Spatial extent in native CRS: (left, bottom, right, top).
crs
instance-attribute
¶
CRS as a proj-string or EPSG code (e.g. 'EPSG:4326').
dtype
instance-attribute
¶
Numpy dtype string of band 1 (e.g. 'float32').
layer_name
instance-attribute
¶
Logical name for this layer (e.g. 'soil', 'vegetation').
nodata
instance-attribute
¶
Nodata sentinel value, or None if unset in the file.
path
instance-attribute
¶
Resolved absolute path to the raster file.
sha256 = None
class-attribute
instance-attribute
¶
SHA-256 hex digest of the file contents (content fingerprint).
shape
instance-attribute
¶
Raster grid dimensions (height, width).
build_data_catalog(raster_path, climate_csv_path, *, raster_layer_name='primary')
¶
Resolve local input files, collect metadata, and return a DataCatalog.
This function performs availability checks, reads raster metadata (CRS, bounds, nodata, shape), parses climate CSV headers, and computes SHA-256 fingerprints. It does not load raster pixel arrays or orchestrate any downstream step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
raster_path
|
str | Path
|
Absolute or resolvable path to the input GeoTIFF raster. |
required |
climate_csv_path
|
str | Path
|
Absolute or resolvable path to the climate CSV. |
required |
raster_layer_name
|
str
|
Logical name assigned to the raster in the catalog (default
|
'primary'
|
Returns:
| Type | Description |
|---|---|
DataCatalog
|
Immutable metadata snapshot for use in provenance writing. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If either input file does not exist. |
ValueError
|
If the CSV is missing required coordinate columns. |
Source code in terraflow/ingest.py
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 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 | |
load_climate_csv(path)
¶
Load and validate climate data from CSV.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the CSV file. Must contain 'lat' and 'lon' columns for spatial interpolation, plus climate variables (e.g., 'mean_temp', 'total_rain'). |
required |
Returns:
| Type | Description |
|---|---|
pd.DataFrame:
|
Climate data with validated coordinates and variables. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError:
|
If the file does not exist. |
pd.errors.ParserError:
|
If the CSV is malformed. |
ValueError:
|
If required columns are missing, coordinates are invalid, or climate data has NaN values in critical fields. |
Notes
Validates: - File existence - Required 'lat' and 'lon' columns - Latitude range [-90, 90] - Longitude range [-180, 180] - At least one climate variable column (not lat/lon) - NaN values in coordinates (drops rows with missing lat/lon)
Source code in terraflow/ingest.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 | |
load_raster(path)
¶
Load a raster dataset (e.g., GeoTIFF).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the raster file. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
DatasetReader |
DatasetReader
|
Open rasterio dataset. Caller is responsible for closing the dataset using a context manager or calling .close(). |
Raises:
| Type | Description |
|---|---|
FileNotFoundError:
|
If the file does not exist. |
rasterio.errors.RasterioIOError:
|
If the file cannot be opened as a raster. |