Skip to content

HSLUDEM/MeteoSwiss_Data_Factory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

MeteoSwiss Data Factory

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.


What the script does

meteoswiss_api.py defines a class MeteoSwissDataFactory that:

  1. Takes latitude and longitude coordinates.

  2. Selects the nearest MeteoSwiss station using the Haversine distance formula.

  3. Automatically downloads metadata:

    • Station list
    • Data inventory
    • Parameter catalogue
  4. Downloads historical data for a user-defined year range.

  5. Supports multiple temporal resolutions:

    • 10-minute data
    • Hourly data
    • Daily data
    • Monthly data
    • Annual data
  6. Automatically:

    • Handles decade-based file segmentation (where applicable)
    • Parses timestamps
    • Filters data to the requested year range
  7. Provides convenience accessors for:

    • Raw datasets
    • Temperature-only datasets
    • Irradiation-only datasets
    • Station and parameter metadata

Data Source

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.csv
  • ogd-smn_meta_datainventory.csv
  • ogd-smn_meta_parameters.csv

Official documentation:


Installation

Dependencies:

pip install pandas numpy requests

Python ≥ 3.9 recommended.


Basic Usage

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()

API Overview

MeteoSwissDataFactory(latitude, longitude, year_start, year_end)

Initialises the class and:

  • Loads metadata tables
  • Determines the closest station
  • Stores requested year range
  • Prepares lazy-loading of datasets

Raw Data Access

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.


Convenience Data Access

Hourly Temperature

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

Hourly Irradiation

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)

Daily Temperature

df_T_daily = weather.get_daily_data_temperature()

Daily Irradiation

df_I_daily = weather.get_daily_data_irradiation()

Metadata Access

  • 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

Internal Logic

Decade Handling

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.


Timestamp Handling

  • 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)

Notes

  • 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.

Example Run

The __main__ section demonstrates:

  • Data retrieval at different resolutions
  • Extraction of temperature and irradiation subsets
  • Inspection of metadata tables

Run:

python meteoswiss_api.py

About

Query meteorological data from the MeteoSwiss API.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages