Skip to main content

TrerraQT Query Client

Project description

TerraQT Client

Python 3.11+ License: MIT Ruff Type Checked

A professional, type-safe Python client for querying TerraQT weather forecast data sources.

Features

  • 🚀 Async-first: Built on aiohttp for efficient concurrent API requests
  • 🔧 Flexible Grouping: Group locations by any criteria (solar, wind, custom categories)
  • 📊 Multiple Variable Sets: Configure different variable sets per model and group
  • 🛡️ Type-safe: Full type hints with strict mypy compliance
  • 🔄 Retry Logic: Automatic retry with exponential backoff
  • 📦 Modern Packaging: UV-compatible with PEP 621 compliance

Installation

Using UV (Recommended)

# Install from source
uv pip install .

# Install with development dependencies
uv pip install -e ".[dev]"

Using pip

pip install terraqt

Quick Start

import asyncio
from terraqt import TerraQT, SOLAR, WIND

async def main():
    # Initialize client
    tqt = TerraQT(api_token="your_api_token")

    # Register locations with groups
    tqt.locations.register(
        "station_1", lon=106.5, lat=37.5, weight=0.6, groups=SOLAR
    )
    tqt.locations.register(
        "station_2", lon=105.5, lat=36.5, weight=0.4, groups=SOLAR
    )

    # Fetch data using fluent query builder
    async with tqt.client() as client:
        # Option 1: Use variable group from model config
        collection = await (
            tqt.query(client)
            .models(["gfs_surface", "aifs_surface"])
            .locations_by_group(SOLAR)
            .execute()
        )
        
        # Option 2: Specify raw variables directly
        collection = await (
            tqt.query(client)
            .models(["gfs_surface"])
            .locations(["station_1", "station_2"])
            .variables(["ws100m", "tcc"])
            .execute()
        )

    # Access forecast data
    for model in collection.models():
        for location in collection.locations(model):
            df = collection.get(model, location)
            print(f"{model} at {location}: {df.shape}")

asyncio.run(main())

Location Management

Registering Locations

from terraqt import TerraQT, SOLAR, WIND

tqt = TerraQT(api_token="your_token")

# Single group
tqt.locations.register("solar_site_1", lon=106.5, lat=37.5, groups=SOLAR)

# Multiple groups (location can belong to multiple categories)
tqt.locations.register(
    "hybrid_site", 
    lon=105.5, 
    lat=36.5, 
    groups=[SOLAR, WIND, "priority"]
)

# With weights for aggregation
tqt.locations.register(
    "station_1", 
    lon=107.0, 
    lat=38.0, 
    weight=0.7, 
    groups=WIND
)

Loading from CSV

# CSV file with columns: lon, lat, weight
tqt.locations.load_from_csv(
    "locations.csv",
    prefix="site",
    groups=SOLAR  # or groups=[SOLAR, "priority"]
)

Querying Locations

# Get all locations in a group
solar_locs = tqt.locations.get_locations(SOLAR)

# Get all registered groups
all_groups = tqt.locations.get_groups()

# Get location objects
location_objs = tqt.locations.get_location_objects(SOLAR)

Model Configuration

Default Models

The client comes with pre-configured models:

from terraqt import DEFAULT_MODEL_CONFIGS

# Available models:
# - aifs_surface
# - gfs_surface  
# - cfs_h6_surface

Custom Model Configuration

from terraqt import TerraQT, ModelConfig, SOLAR, WIND

tqt = TerraQT(api_token="your_token")

# Register a custom model with variable groups
custom_model = ModelConfig(
    name="my_model",
    variable_groups={
        WIND: ("ws100m", "wd100m"),
        SOLAR: ("ssrd", "tcc", "dswrf"),
        "temperature": ("t2m", "tmax", "tmin"),
    },
    model_type="forecast"
)

tqt.config.register_model(custom_model)

Query Builder

Basic Usage

async with tqt.client() as client:
    # Query by group (uses variable group from model config)
    collection = await (
        tqt.query(client)
        .models(["gfs_surface"])
        .locations_by_group(SOLAR)  # Fetches solar variables
        .execute()
    )
    
    # Query specific locations
    collection = await (
        tqt.query(client)
        .models(["gfs_surface"])
        .locations(["site_1", "site_2"])
        .variable_group(WIND)  # Specify which variable group to use
        .execute()
    )
    
    # Query with raw variables (bypasses model config)
    collection = await (
        tqt.query(client)
        .models(["gfs_surface"])
        .locations(["site_1"])
        .variables(["ws100m", "tcc", "t2m"])  # Custom variables
        .execute()
    )

Configuration

From Environment Variables

from terraqt import TerraQT

# Reads TERRAQT_API_TOKEN from environment
tqt = TerraQT.from_env()

Programmatic Configuration

from terraqt import TerraQT, TerraQTConfig

config = TerraQTConfig(
    api_token="your_token",
    api_base_url="https://api-pro-openet.terraqt.com/v1",
    timeout_total=120.0,
    timeout_connect=10.0,
    timeout_read=60.0,
    max_concurrency=20,
    retry_attempts=5,
    retry_backoff=1.0,
)

tqt = TerraQT(config=config)

Architecture

src/terraqt/
├── __init__.py      # Public API exports
├── _version.py      # Version information
├── core.py          # Main TerraQT entry point
├── models.py        # Data models (Location, ForecastResult, etc.)
├── locations.py     # Location registry management
├── client.py        # Async HTTP client
├── config.py        # Configuration management
└── py.typed         # PEP 561 marker

Development

Setup

# Clone the repository
git clone https://github.com/terraqt/terraqt-client.git
cd terraqt-client

# Create virtual environment and install dependencies
uv venv
source .venv/bin/activate
uv pip install -e ".[dev]"

# Install pre-commit hooks
pre-commit install

Running Tests

# Run all tests
pytest

# Run with coverage
pytest --cov=src/terraqt --cov-report=html

# Run specific test file
pytest tests/test_processing.py -v

Code Quality

# Format code
ruff format

# Lint code
ruff check --fix

# Type check
mypy src/terraqt

License

MIT License - see LICENSE for details.

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

terraqt_client-0.0.1.tar.gz (17.8 kB view details)

Uploaded Source

Built Distribution

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

terraqt_client-0.0.1-py3-none-any.whl (17.4 kB view details)

Uploaded Python 3

File details

Details for the file terraqt_client-0.0.1.tar.gz.

File metadata

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

File hashes

Hashes for terraqt_client-0.0.1.tar.gz
Algorithm Hash digest
SHA256 d3fb4d055380aa302b9fe2e87c4eedaa05a130ed9546729d15e9fd34f721b974
MD5 e18415620a8b18e746c53209a7f01703
BLAKE2b-256 dc049b2001974ea6db34d2017feb1506f733d7984fba70e7afd4b147a0ea5573

See more details on using hashes here.

Provenance

The following attestation bundles were made for terraqt_client-0.0.1.tar.gz:

Publisher: release.yml on jed-cheng/terraqt-client

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

File details

Details for the file terraqt_client-0.0.1-py3-none-any.whl.

File metadata

  • Download URL: terraqt_client-0.0.1-py3-none-any.whl
  • Upload date:
  • Size: 17.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for terraqt_client-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 9f7c1bfda5cb1a31e438ddff103551f02e445ce8f055901366c61fc40748d4b4
MD5 29015d2cf505b1b91addfd13da4fd7d0
BLAKE2b-256 4385a043389f70d2aa60b6f908102542f32e5d399bb2a33dd0627af883f606b1

See more details on using hashes here.

Provenance

The following attestation bundles were made for terraqt_client-0.0.1-py3-none-any.whl:

Publisher: release.yml on jed-cheng/terraqt-client

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