# Requires a live HTTP call to the Open-Meteo API; cannot execute offline.
import tempfile
from pathlib import Path
from spotforecast2_safe.data.fetch_data import fetch_weather_data
with tempfile.TemporaryDirectory() as tmp:
weather_df = fetch_weather_data(
cov_start='2023-01-01T00:00',
cov_end='2023-01-03T00:00',
latitude=51.5136,
longitude=7.4653,
timezone='UTC',
freq='h',
fallback_on_failure=True,
cache_home=Path(tmp) / 'weather_cache',
)
print(weather_df.shape)data.fetch_data.fetch_weather_data
data.fetch_data.fetch_weather_data(
cov_start,
cov_end,
latitude=51.5136,
longitude=7.4653,
timezone='UTC',
freq='h',
fallback_on_failure=True,
cache_home=None,
fill_missing=False,
)Fetch weather data for the dataset period plus forecast horizon.
Creates a weather DataFrame using the Open-Meteo API with optional caching. Caching is controlled solely by the cache_home argument: when a path is provided the service reads from / writes to a parquet cache file inside that directory; when None (the default) no caching is performed.
Parameters
| Name | Type | Description | Default |
|---|---|---|---|
| cov_start | str | Start date for covariate data. | required |
| cov_end | str | End date for covariate data. | required |
| latitude | float | Latitude of the location for weather data. Default is 51.5136 (Dortmund). | 51.5136 |
| longitude | float | Longitude of the location for weather data. Default is 7.4653 (Dortmund). | 7.4653 |
| timezone | str | Timezone for the weather data. | 'UTC' |
| freq | str | Frequency of the weather data. | 'h' |
| fallback_on_failure | bool | Whether to use fallback data in case of failure. | True |
| cache_home | Optional[Union[str, Path]] | Optional path to cache directory. When provided, fetched weather data is cached in <cache_home>/weather_cache.parquet. When None (default), no caching is performed. |
None |
| fill_missing | bool | Whether to forward- and back-fill remaining NaN gaps (default False). Forwarded to WeatherService.get_dataframe; see its docstring. |
False |
Returns
| Name | Type | Description |
|---|---|---|
| pd.DataFrame | pd.DataFrame: DataFrame containing weather information. |