This repository contains a Python utility to automatically download MeteoSwiss station data from the Automatic Weather Stations (SMN) Open Data via the official STAC API hosted at data.geo.admin.ch.
The routine is implemented as a class and is designed for integration into larger scientific or modelling workflows.
Only the module meteoswiss_api.py is required. A runnable example is included in the __main__ section.
meteoswiss_api.py defines a class MeteoSwissDataFactory that:
-
Takes latitude and longitude coordinates.
-
Selects the nearest MeteoSwiss station using the Haversine distance formula.
-
Automatically downloads metadata:
- Station list
- Data inventory
- Parameter catalogue
-
Downloads historical data for a user-defined year range.
-
Supports multiple temporal resolutions:
- 10-minute data
- Hourly data
- Daily data
- Monthly data
- Annual data
-
Automatically:
- Handles decade-based file segmentation (where applicable)
- Parses timestamps
- Filters data to the requested year range
-
Provides convenience accessors for:
- Raw datasets
- Temperature-only datasets
- Irradiation-only datasets
- Station and parameter metadata
The code uses the official STAC API endpoint:
https://data.geo.admin.ch/api/stac/v1
Collection used:
ch.meteoschweiz.ogd-smn
The following metadata assets are retrieved automatically:
ogd-smn_meta_stations.csvogd-smn_meta_datainventory.csvogd-smn_meta_parameters.csv
Official documentation:
- https://opendatadocs.meteoswiss.ch/a-data-groundbased/a1-automatic-weather-stations
- https://opendatadocs.meteoswiss.ch/general/download
- https://www.geo.admin.ch/en/rest-interface-stac-api
Dependencies:
pip install pandas numpy requestsPython ≥ 3.9 recommended.
from meteoswiss_api import MeteoSwissDataFactory
latitude = 46.947144
longitude = 7.444751
year_start = 2005
year_end = 2016
weather = MeteoSwissDataFactory(
latitude=latitude,
longitude=longitude,
year_start=year_start,
year_end=year_end,
)
df_hourly = weather.get_raw_data_hourly()
df_daily = weather.get_raw_data_daily()
df_monthly = weather.get_raw_data_monthly()Initialises the class and:
- Loads metadata tables
- Determines the closest station
- Stores requested year range
- Prepares lazy-loading of datasets
The following methods return copies of the internally cached data:
get_raw_data_10min()get_raw_data_hourly()get_raw_data_daily()get_raw_data_monthly()get_raw_data_yearly()
Internally, datasets are fetched only once per resolution and then cached.
df_T = weather.get_hourly_data_temperature()Returns:
| Column | Description |
|---|---|
T_mean_degC |
Hourly mean air temperature (2 m) |
T_min_degC |
Hourly minimum temperature |
T_max_degC |
Hourly maximum temperature |
df_I = weather.get_hourly_data_irradiation()Returns:
| Column | Description |
|---|---|
I_global_mean_W_per_m2 |
Global radiation (hourly mean) |
I_diff_mean_W_per_m2 |
Diffuse radiation (hourly mean) |
df_T_daily = weather.get_daily_data_temperature()df_I_daily = weather.get_daily_data_irradiation()info_stations_meta_data()info_parameters_meta_data()info_selected_station()info_selected_station_available_data()
These provide full transparency regarding:
- Available stations
- Parameter definitions
- Data coverage of the selected station
Hourly and 10-minute historical data are stored in decade-based files (e.g. 2000–2009).
The class automatically:
- Determines the required decades
- Downloads and concatenates the relevant files
- Filters data to
[year_start, year_end]
Daily, monthly, and yearly data are retrieved via their respective single-asset endpoints.
- Timestamps are parsed from
reference_timestamp - Stored as pandas
datetime - No timezone is attached (naive datetime)
- Data is filtered to the requested year range (inclusive)
- HTTP errors are surfaced via
raise_for_status(). - Returned DataFrames are copies to avoid accidental mutation of internal cache.
- Not all parameters are available at all stations; consult metadata functions for coverage.
The __main__ section demonstrates:
- Data retrieval at different resolutions
- Extraction of temperature and irradiation subsets
- Inspection of metadata tables
Run:
python meteoswiss_api.py