Skip to main content

Python client and CSV parser for the Sustax User API (SUA)

Project description

sustax-client

PyPI package: sustax-client · PyPI

A small Python package for the Sustax User API and for parsing the Sustax global climate models by means of CSV files exported by Sustax (see https://sustax.earth). This repository is prepared in a standard src/ layout so it can be:

  • developed locally with pip install -e .
  • installed from PyPI
  • built as a source distribution and wheel

What is included

  • SustaxClient for authentication, catalog lookup, pricing, purchases, downloads, status polling, coordinates, and balance checks
  • load_sustax_file() to load Sustax CSV files into pandas DataFrames or nested numpy-friendly dictionaries
  • parser, parsing, and reranking helpers for Sustax CSV outputs
  • Python examples for catalog inspection, coordinate downloads, and postal-code downloads
  • unit tests plus marked live API and purchase workflow tests

Repository layout

sustax-client/
├── .gitignore
├── CONTRIBUTING.md
├── LICENSE
├── MANIFEST.in
├── README.md
├── pytest.ini
├── pyproject.toml
├── src/
│   └── sustax_client/
│       ├── __init__.py
│       ├── client.py
│       ├── parser.R
│       ├── parser.py
│       ├── parsing.py
│       └── reranking.py
├── tests/
│   ├── test_reranking.py
│   ├── test_sustax_downloaded_csv_values.py
│   └── test_sustax_workflow.py
└── examples/
    ├── _helpers.py
    ├── download_coords.py
    ├── download_zip_code.py
    └── inspect_catalog.py

Installation

Local development

python -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
python -m pip install -e .[dev]

From PyPI

Install the official package from sustax-client · PyPI:

pip install sustax-client

Quick start

1. Authenticate and inspect the catalog

from sustax_client import SustaxClient

with SustaxClient() as client:
    client.authenticate("your_username", "your_password")
    catalog = client.get_variables_id()
    print(catalog)

2. Preview pricing for a point request

from sustax_client import SustaxClient

with SustaxClient() as client:
    client.authenticate("your_username", "your_password")
    quote = client.view_price_request(
        lat=41.3874,
        lon=2.1686,
        year_from=2020,
        year_to=2030,
        sustax_code_ids=[12345],
    )
    print(quote)

3. Buy, download, and unzip a request

This example purchases data and may spend Sustax coins.

from sustax_client import SustaxClient

with SustaxClient() as client:
    client.authenticate("your_username", "your_password")
    request_info = client.buy_data(
        lat=41.3874,
        lon=2.1686,
        year_from=2020,
        year_to=2030,
        sustax_code_ids=[12345],
        acceptance_sustax_disclaimer=True,
    )
    zip_path = client.download_file(request_info["url"], dest_dir="downloads")
    extracted = client.unzip_download(dest_dir="downloads/unzipped")
    print(zip_path)
    print(extracted)

4. Load a Sustax CSV with pandas

from sustax_client import load_sustax_file

climate_df, metrics_df, metadata = load_sustax_file(
    "downloads/unzipped/example.csv",
    return_pandas_df=True,
    return_metadata=True,
)

print(climate_df.head())
print(metrics_df.head())
print(metadata)

Supported request modes

The packaged client supports the three location modes described in the Sustax documentation:

  • point coordinates: lat, lon
  • ROI / bounding box: lat=[north, south], lon=[west, east]
  • postal code lookup: postal_code, country

CSV parser behavior

load_sustax_file() assumes the exported file is a Sustax CSV with three main blocks:

  • metadata
  • accuracy metrics
  • climate payload time series

By default it returns two pandas DataFrames:

  • climate payload indexed by time
  • metrics indexed by scenario

Set return_pandas_df=False if you prefer nested dictionaries and a numpy datetime array.

Development

Run tests:

pytest

Build distributions locally:

python -m build

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

sustax_client-0.3.6.tar.gz (26.1 kB view details)

Uploaded Source

Built Distribution

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

sustax_client-0.3.6-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

Details for the file sustax_client-0.3.6.tar.gz.

File metadata

  • Download URL: sustax_client-0.3.6.tar.gz
  • Upload date:
  • Size: 26.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for sustax_client-0.3.6.tar.gz
Algorithm Hash digest
SHA256 67efbeb1872046f4f03879dc7eb5e33b83e3a0a612751bbb6a5e4a5b105f27d2
MD5 5a563bf2647a3d095837c4aa36dcf8fa
BLAKE2b-256 0d07c23835645fc6475ee70efe3fbcfe7ad107ae456b07297f5a587553faacb7

See more details on using hashes here.

File details

Details for the file sustax_client-0.3.6-py3-none-any.whl.

File metadata

  • Download URL: sustax_client-0.3.6-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for sustax_client-0.3.6-py3-none-any.whl
Algorithm Hash digest
SHA256 1d1a63df5a1e2c25fc5480c156c9485f0d74ba4654cb32c86545c98905cbc14d
MD5 637805e6f72734e8fd9472697f0028a9
BLAKE2b-256 c7f7655984c4d9e6890f12eb2d4301844c413835d380017862747fd225cac2f1

See more details on using hashes here.

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