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
with rasterio.open("land_cover.tif") as src:
clipped_data = clip_raster_to_roi(
src,
bbox=(-101.0, 38.0, -94.0, 40.0),
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
¶
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
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 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 | |
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. |