Skip to main content

Python library for the Current Lab API

Project description

currentlab-python

The Current Lab Python library provides convenient access to the Current Lab API from Python applications. Load gridded forecast files as xarray datasets with a single function call.

Installation

pip install currentlab

Authentication

Set your API key as an environment variable:

export CURRENT_LAB_API_KEY="your-api-key"

Or use a .env file (see .env.example) and load it with python-dotenv.

You can also pass the key directly to any function call via the api_key= argument.

Quick start

import currentlab

ds = currentlab.load_dataset(
    region="oahu",
    dataset_type="currents_surface",
)
print(ds)

API Reference

load_dataset(...)

currentlab.load_dataset(
    region,
    dataset_type,
    dataset_source="auto",
    file_format="netcdf",
    run_date="latest",
    *,
    save_dir=None,
    save_path=None,
    api_key=None,
    base_url="https://api.current-lab.com",
)
Parameter Type Description
region str Region identifier, e.g. "oahu"
dataset_type str Dataset type, e.g. "currents_surface", "wave", "wind_10m", "ocean_3d"
dataset_source str Model/source selection. Default "auto" lets the server pick the best available source
file_format str File format to request. Default "netcdf"
run_date str Forecast run date as "YYYY-MM-DD", or "latest". Default "latest"
save_dir str | Path Directory to save the downloaded file. Filename is derived from the API response. File is reused on subsequent calls if it already exists
save_path str | Path Exact path to save the file to. Reused on subsequent calls if it already exists. Mutually exclusive with save_dir
api_key str API key override. Falls back to CURRENT_LAB_API_KEY env var
base_url str API base URL override (useful for testing)

Returns an xr.Dataset.

Usage patterns

Stream into memory (no file saved)

A temporary file is created and streamed into memory. The file is deleted after the dataset is closed.

ds = currentlab.load_dataset(region="oahu", dataset_type="wave")

Save to a directory (cached)

File name is derived from the API response.

ds = currentlab.load_dataset(
    region="oahu",
    dataset_type="wave",
    run_date="2025-03-01",
    save_dir="./data",
)

Save to a specific file path (cached)

ds = currentlab.load_dataset(
    region="oahu",
    dataset_type="currents_surface",
    save_path="./data/oahu_currents_latest.nc",
)

Request a specific run date

ds = currentlab.load_dataset(
    region="oahu",
    dataset_type="wind_10m",
    run_date="2026-03-09",
)

Working with the dataset

The returned object is a standard xarray Dataset, so the full xarray API is available:

import numpy as np
import currentlab

ds = currentlab.load_dataset(region="oahu", dataset_type="currents_surface")

# Compute current speed from u/v components
ds["speed"] = np.hypot(ds["u"], ds["v"])
ds["speed"].attrs["long_name"] = "Current Speed"
ds["speed"].attrs["units"] = "m/s"

print(ds["speed"].max().item())

Error handling

from currentlab import CurrentLabAuthError, CurrentLabAPIError, CurrentLabDownloadError

try:
    ds = currentlab.load_dataset(region="oahu", dataset_type="currents_surface")
except CurrentLabAuthError:
    print("Check your CURRENT_LAB_API_KEY.")
except CurrentLabAPIError as e:
    print(f"API error: {e}")
except CurrentLabDownloadError as e:
    print(f"Download failed: {e}")

All exceptions inherit from currentlab.CurrentLabError.

Development

git clone https://github.com/current-lab/currentlab-python
cd currentlab-python
python3 -m venv .venv
source .venv/bin/activate
pip install -e ".[dev]"
pytest

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

currentlab-0.1.0.tar.gz (11.8 kB view details)

Uploaded Source

Built Distribution

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

currentlab-0.1.0-py3-none-any.whl (8.4 kB view details)

Uploaded Python 3

File details

Details for the file currentlab-0.1.0.tar.gz.

File metadata

  • Download URL: currentlab-0.1.0.tar.gz
  • Upload date:
  • Size: 11.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for currentlab-0.1.0.tar.gz
Algorithm Hash digest
SHA256 bedd8dbb0d16690b2f2c0456e1bf2866699c811f2cdf2e523d7a843c72979362
MD5 cdad0b149fb555a63ab9c5e8c40394b8
BLAKE2b-256 c040c834469562e030a4ef078e710a629db5b66eb320b46380cf11404da79d30

See more details on using hashes here.

File details

Details for the file currentlab-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: currentlab-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for currentlab-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 293103308a6e993ca8f195adc98af99ecfdaf9a776f9d98368d18f19822dcb95
MD5 8ddfc56925e0c95811000c4038e488f6
BLAKE2b-256 b44cdab2f1a30c2a2f7d2ccdf2056ca31cca68f6e4e3a44bcb4081e3a64369ea

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