Skip to main content

Weather API Client for Python

Project description

Frequenz Weather API Client

Build Status PyPI Package Docs

Introduction

Weather API Client for Python providing access to historical and live weather forecast data.

Supported Platforms

The following platforms are officially supported (tested):

  • Python: 3.11
  • Operating System: Ubuntu Linux 20.04
  • Architectures: amd64, arm64

Contributing

If you want to know how to build this project and contribute to it, please check out the Contributing Guide.

Usage

Installation

pip install frequenz-client-weather

Available Features

The available features are listed here.

Initialize the client

The Client can optionally be initialized with keep alive.

from frequenz.client.weather import Client
from frequenz.client.base.channel import ChannelOptions, KeepAliveOptions, SslOptions
from datetime import timedelta

client = Client(
    service_address,
    channel_defaults=ChannelOptions(
        ssl=SslOptions(
            enabled=False,
        ),
        keep_alive=KeepAliveOptions(
            enabled=True,
            timeout=timedelta(minutes=5),
            interval=timedelta(seconds=20),
        ),
    ),
)

Get historical weather forecast

from datetime import datetime
import pandas as pd
from frequenz.client.weather._types import ForecastFeature, Location

# Define a list of locations, features and a time range to request historical forecasts for
locations = [Location(latitude=46.2276, longitude=15.2137, country_code="DE")]
features = [ForecastFeature.TEMPERATURE_2_METRE, ForecastFeature.V_WIND_COMPONENT_10_METRE]
start = datetime(2024, 1, 1)
end = datetime(2024, 1, 31)

forecast_iterator = client.hist_forecast_iterator(
    features=features, locations=locations, start=start, end=end
)

# Collect and flatten forecasts
flat_forecasts = [f.flatten() async for f in forecast_iterator]
forecast_records = [record for batch in flat_forecasts for record in batch]

# E.g. convert to DataFrame and sort
forecast_df = pd.DataFrame(forecast_records).sort_values(["creation_ts", "validity_ts", "latitude", "longitude"])
print(forecast_df)

Get live weather forecast

import pandas as pd
from frequenz.client.weather._types import ForecastFeature, Location

# Define a list of locations and features to request live forecasts for
locations = [Location(latitude=46.2276, longitude=15.2137, country_code="DE")]
features = [ForecastFeature.TEMPERATURE_2_METRE, ForecastFeature.V_WIND_COMPONENT_10_METRE]

# Returns a Receiver object that can be iterated over asynchronously
stream = await client.stream_live_forecast(
    locations=locations,
    features=features,
)

# Process incoming forecasts as they arrive
async for forecast in stream:
    # The to_ndarray_vlf method converts the forecast data to a 3D numpy array,
    # where the dimensions correspond to validity_ts, location, feature
    # The method can also take filters for validity_ts, locations and features
    # E.g. filter the forecast for wind features
    wind_forecast = forecast.to_ndarray_vlf(features=[ForecastFeature.V_WIND_COMPONENT_10_METRE])
    print(wind_forecast)

Command Line Interface

The package also provides a command line interface to get weather forecast data. Use -h to see the available options.

Get historical weather forecast

weather-cli \
    --url <service-address> \
    --location "40,15" \
    --feature U_WIND_COMPONENT_100_METRE \
    --start 2024-03-14 \
    --end 2024-03-15 \
    --mode historical

Get live weather forecast

weather-cli \
    --url <service-address> \
    --location "40, 15" \
    --feature TEMPERATURE_2_METRE \
    --mode live

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

frequenz_client_weather-0.2.3.tar.gz (16.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

frequenz_client_weather-0.2.3-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file frequenz_client_weather-0.2.3.tar.gz.

File metadata

  • Download URL: frequenz_client_weather-0.2.3.tar.gz
  • Upload date:
  • Size: 16.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for frequenz_client_weather-0.2.3.tar.gz
Algorithm Hash digest
SHA256 8b2cd66646550e032739b2eaa5e1a05bf03354ca72e71c3a670505a40175f873
MD5 387ea21a32ac40bb92f8ef8f15fa8287
BLAKE2b-256 2bf9860dff4ecf7e860d72eb637e42ff07dcdf29cba227051a07f75965903e77

See more details on using hashes here.

Provenance

The following attestation bundles were made for frequenz_client_weather-0.2.3.tar.gz:

Publisher: ci.yaml on frequenz-floss/frequenz-client-weather-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file frequenz_client_weather-0.2.3-py3-none-any.whl.

File metadata

File hashes

Hashes for frequenz_client_weather-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3b059fc104328ef3b55c1b6f8166a3393145920875ae3e84a83959c83b3634d6
MD5 1b07a2ecab3c0fb7b50ad46f847a35a1
BLAKE2b-256 2f7d2f553d6404e96b45d7f978b5ec0556ebb284196e44b2f944f243e9ab7831

See more details on using hashes here.

Provenance

The following attestation bundles were made for frequenz_client_weather-0.2.3-py3-none-any.whl:

Publisher: ci.yaml on frequenz-floss/frequenz-client-weather-python

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

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