Skip to main content

Open-Meteo Python Library

Project description

Open-Meteo Python API Client

An API client to get weather data from the Open-Meteo Weather API based on the Python library requests.

Instead of using JSON, the API client uses FlatBuffers to transfer data. Encoding data in FlatBuffers is more efficient for long time-series data. Data can be transferred to numpy or pandas using Zero-Copy to analyze large amount of data quickly.

TODO:

  • Document data structure
  • Consider dedicated pandas library

Basic Usage

# pip install openmeteo-requests

import openmeteo_requests

om = openmeteo_requests.Client()
params = {
    "latitude": 52.54,
    "longitude": 13.41,
    "hourly": ["temperature_2m", "precipitation"],
    "current": ["temperature_2m"]
}

results = om.weather_api("https://api.open-meteo.com/v1/forecast", params=params)
result = results[0]

print(f"Coordinates {result.Latitude()}°E {result.Longitude()}°N {result.Elevation()} m asl")
print(f"Timezone {result.Timezone()} {result.TimezoneAbbreviation()} Offset={result.UtcOffsetSeconds()}s")

print(f"Current temperature is {result.Current().Temperature2m().Value() °C}")

# Accessing hourly forecasts as numpy arrays
hourly = result.Hourly()
temperature_2m = hourly.Temperature2m().ValuesAsNumpy()
precipitation = hourly.Temperature2m().ValuesAsNumpy()

# Usage with Pandas Dataframes
import pandas as pd
date = pd.date_range(
    start=pd.to_datetime(hourly.Time().Start(), unit="s"),
    end=pd.to_datetime(hourly.Time().End(), unit="s"),
    freq=pd.Timedelta(seconds=hourly.Time().Interval()),
    inclusive="left"
)
df = pd.DataFrame(
    data={
        "date": date,
        "temperature_2m": hourly.Temperature2m().ValuesAsNumpy(),
        "precipitation": hourly.Precipitation().ValuesAsNumpy()
    }
)
print(df)
#date  temperature_2m  precipitation
#0  2023-08-01 00:00:00       16.945999            1.7
#1  2023-08-01 01:00:00       16.996000            2.1
#2  2023-08-01 02:00:00       16.996000            1.0
#3  2023-08-01 03:00:00       16.846001            0.2

Caching Data

If you are working with large amounts of data, caching data can make it easier to develop. You can pass a cached session from the library requests-cache to the Open-Meteo API client.

The following example stores all data indefinitely (expire_after=-1) in a SQLite database called .cache.sqlite. For more options read the requests-cache documentation.

Additionally, retry-requests to automatically retry failed API calls in case there has been any unexpected network or server error.

# pip install openmeteo-requests
# pip install requests-cache retry-requests

import openmeteo_requests
import requests_cache
from retry_requests import retry

# Setup the Open-Meteo API client with a cache and retry mechanism
cache_session = requests_cache.CachedSession('.cache', expire_after=-1)
retry_session = retry(cache_session, retries=5, backoff_factor=0.2)
om = openmeteo_requests.Client(session=retry_session)

# Using the client object `om` will now cache all weather data

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

openmeteo_requests-1.0.0.tar.gz (5.8 MB view details)

Uploaded Source

Built Distribution

openmeteo_requests-1.0.0-py3-none-any.whl (4.8 kB view details)

Uploaded Python 3

File details

Details for the file openmeteo_requests-1.0.0.tar.gz.

File metadata

  • Download URL: openmeteo_requests-1.0.0.tar.gz
  • Upload date:
  • Size: 5.8 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.5

File hashes

Hashes for openmeteo_requests-1.0.0.tar.gz
Algorithm Hash digest
SHA256 0d5899a250aa5bd33e33f2020972c0678f85a6784aed5e41e9ad1a52b1875de1
MD5 a220c7ccce6434f5fc9a2a0d66efe34c
BLAKE2b-256 b6c9b9400fa4ece2f7e54564b3e775d2a249544ccccd31deb28ae5b72fe81f06

See more details on using hashes here.

File details

Details for the file openmeteo_requests-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for openmeteo_requests-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 423a4ac08a0f7349b2389ebadf26f90eeb033d3f03cbda37cc7493f2c932a1d3
MD5 c54110c129828115b1616ccf3fb76a0c
BLAKE2b-256 a66d7be37439076523b37d9c1f612b48ed984e569c2bdc4457f6c3b6806eda49

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page