Open-Meteo Python SDK

1.26.0 · active · verified Wed Apr 15

The Open-Meteo Python SDK provides FlatBuffer data models for interacting with the Open-Meteo weather API. It allows developers to construct API requests and parse API responses using highly efficient binary serialization. The library is actively maintained with frequent updates (often monthly or bi-monthly) to support new weather models and variables as the Open-Meteo API evolves.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `openmeteo-sdk` to build a FlatBuffer request for weather data, send it to the Open-Meteo API using the `requests` library, and then parse the binary FlatBuffer response to extract hourly temperatures and daily sunshine duration. It highlights the use of `flatbuffers.Builder` for request creation and direct access to data via the generated `WeatherApiResponse` classes.

import requests
import flatbuffers
from openmeteo_sdk.WeatherApiRequest import WeatherApiRequest
from openmeteo_sdk.WeatherApiResponse import WeatherApiResponse
from openmeteo_sdk.Variables import Variables

def get_weather_data(latitude: float, longitude: float):
    builder = flatbuffers.Builder(0)
    request = WeatherApiRequest.WeatherApiRequest.StartWeatherApiRequest(builder)
    WeatherApiRequest.WeatherApiRequest.AddLatitude(request, latitude)
    WeatherApiRequest.WeatherApiRequest.AddLongitude(request, longitude)
    WeatherApiRequest.WeatherApiRequest.AddHourly(request, Variables.Variables().TEMPERATURE_2M)
    WeatherApiRequest.WeatherApiRequest.AddDaily(request, Variables.Variables().SUNSHINE_DURATION)
    WeatherApiRequest.WeatherApiRequest.AddTimezone(request, builder.CreateString("Europe/Berlin"))
    request_object = WeatherApiRequest.WeatherApiRequest.EndWeatherApiRequest(builder)
    builder.Finish(request_object)

    # Send request to Open-Meteo API
    response = requests.post(
        "https://api.open-meteo.com/v1/forecast?models=gfs_seamless",
        data=builder.Output(),
        headers={
            "Content-Type": "application/flatbuffers",
            "Accept": "application/flatbuffers"
        }
    )
    response.raise_for_status()

    # Parse response
    weather_api_response = WeatherApiResponse.WeatherApiResponse.GetRootAsWeatherApiResponse(response.content)

    # Access hourly data
    if weather_api_response.Hourly() is not None:
        hourly_temperatures = []
        for i in range(weather_api_response.Hourly().VariablesLength()):
            variable = weather_api_response.Hourly().Variables(i)
            if variable.Variable() == Variables.Variables().TEMPERATURE_2M:
                for j in range(variable.ValuesLength()):
                    hourly_temperatures.append(variable.Values(j))
        print(f"Hourly Temperatures (2m): {hourly_temperatures[:5]}...") # Print first 5 for brevity

    # Access daily data
    if weather_api_response.Daily() is not None:
        daily_sunshine = []
        for i in range(weather_api_response.Daily().VariablesLength()):
            variable = weather_api_response.Daily().Variables(i)
            if variable.Variable() == Variables.Variables().SUNSHINE_DURATION:
                for j in range(variable.ValuesLength()):
                    daily_sunshine.append(variable.Values(j))
        print(f"Daily Sunshine Duration: {daily_sunshine[:5]}...") # Print first 5 for brevity

if __name__ == '__main__':
    get_weather_data(latitude=52.52, longitude=13.41)

view raw JSON →