weather
weather
#
Weather forecasting via the Open-Meteo API.
Fetches hourly weather forecasts from the Open-Meteo API (free, no API key
required), plus fetch_past_weather for historical backfill via Meteostat.
Both return the canonical cg.Weather.
fetch_past_weather(location, dt)
#
Fetch historical weather for a location and time using Meteostat.
Useful for backfilling weather on captures that did not record it. Fields
Meteostat does not provide (apparent temperature, visibility, UV) come back
as nan.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
Location
|
Location with latitude, longitude, and altitude. |
required |
dt
|
datetime
|
Date and time to fetch weather for (naive is treated as local). |
required |
Returns:
| Type | Description |
|---|---|
Weather
|
A |
Raises:
| Type | Description |
|---|---|
ImportError
|
If meteostat is not installed. |
ValueError
|
If no weather data is available for the location/time. |
Example
import capturegraph as cg
from capturegraph.scheduling.forecast.weather import fetch_past_weather
from datetime import datetime
loc = cg.Location(latitude=42.445, longitude=-76.480, altitude_meters=261.5)
weather = fetch_past_weather(loc, datetime(2024, 6, 15, 12, 0))
weather.temperature_celsius # 22.5
Source code in capturegraph-lib/capturegraph/scheduling/forecast/weather.py
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 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 | |
hourly_weather(location, days=3)
#
Fetch hourly weather forecast from Open-Meteo.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
location
|
Location
|
Location with latitude and longitude attributes. |
required |
days
|
int
|
Number of days to forecast (1-16). Defaults to 3. |
3
|
Returns:
| Type | Description |
|---|---|
Array[Weather]
|
List of Weather objects for each hour. |
Source code in capturegraph-lib/capturegraph/scheduling/forecast/weather.py
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 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 | |
nearest_weather(weather, times)
#
Find the nearest weather entry for each timestamp.
Sorts the weather list by capture time and uses binary search to find the closest weather entry for each query time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weather
|
Array[Weather]
|
List of Weather objects with time attributes. |
required |
times
|
Array[datetime]
|
List of datetimes to find weather for. |
required |
Returns:
| Type | Description |
|---|---|
Array[Weather]
|
List of Weather objects, one for each query time. |