currentlab 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- currentlab-0.1.0/.gitignore +38 -0
- currentlab-0.1.0/AGENTS.md +113 -0
- currentlab-0.1.0/LICENSE +7 -0
- currentlab-0.1.0/PKG-INFO +188 -0
- currentlab-0.1.0/README.md +156 -0
- currentlab-0.1.0/examples/basic_usage.py +74 -0
- currentlab-0.1.0/pyproject.toml +54 -0
- currentlab-0.1.0/src/currentlab/__init__.py +21 -0
- currentlab-0.1.0/src/currentlab/_api.py +115 -0
- currentlab-0.1.0/src/currentlab/_client.py +121 -0
- currentlab-0.1.0/src/currentlab/_download.py +46 -0
- currentlab-0.1.0/src/currentlab/_exceptions.py +14 -0
- currentlab-0.1.0/src/currentlab/py.typed +0 -0
- currentlab-0.1.0/tests/test_api.py +254 -0
- currentlab-0.1.0/tests/test_client.py +148 -0
- currentlab-0.1.0/tests/test_download.py +90 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
# Python
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[cod]
|
|
4
|
+
*.pyo
|
|
5
|
+
*.pyd
|
|
6
|
+
.Python
|
|
7
|
+
*.egg-info/
|
|
8
|
+
dist/
|
|
9
|
+
build/
|
|
10
|
+
.eggs/
|
|
11
|
+
*.egg
|
|
12
|
+
|
|
13
|
+
# Virtual environments
|
|
14
|
+
.venv/
|
|
15
|
+
venv/
|
|
16
|
+
env/
|
|
17
|
+
|
|
18
|
+
# Environment / secrets
|
|
19
|
+
.env
|
|
20
|
+
|
|
21
|
+
# Testing
|
|
22
|
+
.pytest_cache/
|
|
23
|
+
.coverage
|
|
24
|
+
htmlcov/
|
|
25
|
+
|
|
26
|
+
# Editors
|
|
27
|
+
.vscode/
|
|
28
|
+
.idea/
|
|
29
|
+
*.swp
|
|
30
|
+
|
|
31
|
+
# macOS
|
|
32
|
+
.DS_Store
|
|
33
|
+
|
|
34
|
+
# Data files (downloaded datasets)
|
|
35
|
+
*.nc
|
|
36
|
+
*.grib
|
|
37
|
+
*.grib2
|
|
38
|
+
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
# Project notes for AI assistants
|
|
2
|
+
|
|
3
|
+
This file captures context, decisions, and open questions for anyone (human or AI) picking up this project.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## What this is
|
|
8
|
+
|
|
9
|
+
A lightweight Python SDK for the **Current Lab** API (https://current-lab.com), a startup that sells ocean/wind/wave forecast data. The API serves gridded datasets split by forecast run date, available in several file formats (NetCDF recommended). Before this SDK existed, users had to write their own code to call the API, download the file, and open it with xarray. The SDK collapses that to a single function call.
|
|
10
|
+
|
|
11
|
+
---
|
|
12
|
+
|
|
13
|
+
## Current state (as of initial build)
|
|
14
|
+
|
|
15
|
+
The package is fully scaffolded and ready for logic refinement. It has not yet been published to PyPI. No tests have been written yet.
|
|
16
|
+
|
|
17
|
+
### What works
|
|
18
|
+
|
|
19
|
+
- `currentlab.load_dataset(...)` — the single public entry point. Returns an `xr.Dataset`.
|
|
20
|
+
- Auth via `CURRENT_LAB_API_KEY` environment variable, with an optional `api_key=` override.
|
|
21
|
+
- Three download modes handled transparently:
|
|
22
|
+
1. **No save location** — streams to a temp file, calls `ds.load()` to pull into memory, cleans up the temp file via context manager.
|
|
23
|
+
2. **`save_dir=`** — filename taken from `file_info["file_name"]` in the API response. Skips download if file already exists (local caching).
|
|
24
|
+
3. **`save_path=`** — explicit path. Same skip-if-exists caching.
|
|
25
|
+
- `run_date` accepts `str` (`"YYYY-MM-DD"` or `"latest"`), `datetime.date`, or `datetime.datetime` (time component stripped). Coercion handled in `_coerce_run_date()`.
|
|
26
|
+
- Structured exception hierarchy: `CurrentLabError` → `CurrentLabAPIError` / `CurrentLabAuthError` / `CurrentLabDownloadError`.
|
|
27
|
+
|
|
28
|
+
### File structure
|
|
29
|
+
|
|
30
|
+
```
|
|
31
|
+
src/currentlab/
|
|
32
|
+
__init__.py # exports: load_dataset, RunDate, exception classes, __version__
|
|
33
|
+
_client.py # load_dataset() + RunDate type alias + _coerce_run_date()
|
|
34
|
+
_api.py # request_gridded_file() — POSTs to /v0/gridded_files
|
|
35
|
+
_download.py # _stream_to_file(), download_to_path(), temp_download() context manager
|
|
36
|
+
_exceptions.py # CurrentLabError hierarchy
|
|
37
|
+
py.typed # PEP 561 marker
|
|
38
|
+
examples/
|
|
39
|
+
basic_usage.py
|
|
40
|
+
pyproject.toml # hatchling build, requires-python>=3.9
|
|
41
|
+
README.md
|
|
42
|
+
.env.example
|
|
43
|
+
.gitignore
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
### API shape (based on existing internal code)
|
|
47
|
+
|
|
48
|
+
```python
|
|
49
|
+
# Request
|
|
50
|
+
POST https://api.current-lab.com/v0/gridded_files
|
|
51
|
+
Headers: {"X-API-Key": "<key>"}
|
|
52
|
+
Body (JSON):
|
|
53
|
+
{
|
|
54
|
+
"region": "oahu",
|
|
55
|
+
"dataset_type": "currents_surface", # also: "waves", "wind"
|
|
56
|
+
"dataset_source": "auto",
|
|
57
|
+
"file_format": "netcdf",
|
|
58
|
+
"run_date": "latest", # or "YYYY-MM-DD"
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
# Response shape (assumed — owner should verify)
|
|
62
|
+
{
|
|
63
|
+
"data": [
|
|
64
|
+
{
|
|
65
|
+
"download_url": "<presigned-url>",
|
|
66
|
+
"file_name": "<filename>.nc",
|
|
67
|
+
...
|
|
68
|
+
}
|
|
69
|
+
]
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
`_api.py` currently takes `data[0]` from the response. If the API can return multiple files, this logic needs revisiting.
|
|
74
|
+
|
|
75
|
+
---
|
|
76
|
+
|
|
77
|
+
## Design decisions (and why)
|
|
78
|
+
|
|
79
|
+
- **Functional, not class-based.** Owner preference — no client object to instantiate, just call `load_dataset()`.
|
|
80
|
+
- **`src/` layout** with `pyproject.toml` + hatchling. Modern standard, plays well with editable installs and conda-forge submission.
|
|
81
|
+
- **Dual-use intent.** The package is pip-installable, but the owner also wants users to be able to copy `src/currentlab/` directly into their project. Runtime deps are intentionally minimal (`requests`, `xarray`, `netcdf4`) to support this.
|
|
82
|
+
- **Streaming download** uses `requests` with `stream=True` in 64 KB chunks to handle large NetCDF files without memory spikes.
|
|
83
|
+
- **In-memory temp file cleanup** uses `xr.open_dataset` as a context manager (`with xr.open_dataset(...) as ds: return ds.load()`) to close the file handle before the temp file is deleted.
|
|
84
|
+
- **`datetime` checked before `date`** in `_coerce_run_date` because `datetime` is a subclass of `date`.
|
|
85
|
+
|
|
86
|
+
---
|
|
87
|
+
|
|
88
|
+
## Things the owner still needs to verify / tune
|
|
89
|
+
|
|
90
|
+
- **Exact API response shape.** The `data[0]` assumption and field names (`download_url`, `file_name`) were inferred from the internal example code. Confirm these match production.
|
|
91
|
+
- **Valid values for `region` and `dataset_type`.** Once there's a stable list, consider adding them as `Literal` type hints or at least documenting them in the docstring.
|
|
92
|
+
- **Whether `data[]` can contain multiple files.** If so, `load_dataset` may need a way to expose or iterate over them.
|
|
93
|
+
- **Any other top-level API fields** in the response worth surfacing (e.g. model metadata, run timestamp).
|
|
94
|
+
- **Timeout values.** API request timeout is 30s, download timeout is 300s. Adjust if needed.
|
|
95
|
+
|
|
96
|
+
---
|
|
97
|
+
|
|
98
|
+
## What hasn't been done yet
|
|
99
|
+
|
|
100
|
+
- **Tests.** No `tests/` directory exists. The natural next step after the owner verifies the API shape.
|
|
101
|
+
- **PyPI publish.** Package is scaffolded but not released. Workflow: `pip install build && python -m build && twine upload dist/*`, or set up a GitHub Actions release workflow.
|
|
102
|
+
- **conda-forge.** Can be submitted after the first PyPI release using `grayskull pypi currentlab` to auto-generate the `meta.yaml` feedstock recipe.
|
|
103
|
+
- **CI.** No GitHub Actions workflows yet.
|
|
104
|
+
- **Changelog / versioning.** Currently at `0.1.0` hardcoded in both `pyproject.toml` and `__init__.py`. Consider `hatch-vcs` to drive version from git tags.
|
|
105
|
+
|
|
106
|
+
---
|
|
107
|
+
|
|
108
|
+
## Owner preferences / style notes
|
|
109
|
+
|
|
110
|
+
- Functional API style preferred over class-based.
|
|
111
|
+
- Keep dependencies light — this is a thin SDK, not a framework.
|
|
112
|
+
- The repo will likely be renamed to `currentlab-python` (from `current-lab-python`) to match conventions like `stripe-python`, `openai-python`, etc. The pip package name (`currentlab`) is unaffected.
|
|
113
|
+
- No emojis, no filler comments in code.
|
currentlab-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
Copyright 2026 Current Lab Technologies, Inc.
|
|
2
|
+
|
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
4
|
+
|
|
5
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
6
|
+
|
|
7
|
+
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: currentlab
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Python library for the Current Lab API
|
|
5
|
+
Project-URL: Homepage, https://current-lab.com
|
|
6
|
+
Project-URL: Documentation, https://github.com/current-lab/currentlab-python#readme
|
|
7
|
+
Project-URL: Repository, https://github.com/current-lab/currentlab-python
|
|
8
|
+
Project-URL: Bug Tracker, https://github.com/current-lab/currentlab-python/issues
|
|
9
|
+
Author-email: Current Lab <info@current-lab.com>
|
|
10
|
+
License: LICENSE
|
|
11
|
+
License-File: LICENSE
|
|
12
|
+
Keywords: currents,forecast,netcdf,ocean,waves,wind,xarray
|
|
13
|
+
Classifier: Development Status :: 3 - Alpha
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Atmospheric Science
|
|
22
|
+
Classifier: Topic :: Scientific/Engineering :: Oceanography
|
|
23
|
+
Requires-Python: >=3.9
|
|
24
|
+
Requires-Dist: netcdf4>=1.6
|
|
25
|
+
Requires-Dist: requests>=2.28
|
|
26
|
+
Requires-Dist: xarray>=2023.1
|
|
27
|
+
Provides-Extra: dev
|
|
28
|
+
Requires-Dist: pytest-cov>=4.0; extra == 'dev'
|
|
29
|
+
Requires-Dist: pytest>=7.0; extra == 'dev'
|
|
30
|
+
Requires-Dist: python-dotenv>=1.0; extra == 'dev'
|
|
31
|
+
Description-Content-Type: text/markdown
|
|
32
|
+
|
|
33
|
+
# currentlab-python
|
|
34
|
+
|
|
35
|
+
The Current Lab Python library provides convenient access to the [Current Lab](https://current-lab.com) API from Python applications. Load gridded forecast files as [xarray](https://docs.xarray.dev) datasets with a single function call.
|
|
36
|
+
|
|
37
|
+
## Installation
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
pip install currentlab
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
## Authentication
|
|
44
|
+
|
|
45
|
+
Set your API key as an environment variable:
|
|
46
|
+
|
|
47
|
+
```bash
|
|
48
|
+
export CURRENT_LAB_API_KEY="your-api-key"
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
Or use a `.env` file (see `.env.example`) and load it with [python-dotenv](https://pypi.org/project/python-dotenv/).
|
|
52
|
+
|
|
53
|
+
You can also pass the key directly to any function call via the `api_key=` argument.
|
|
54
|
+
|
|
55
|
+
## Quick start
|
|
56
|
+
|
|
57
|
+
```python
|
|
58
|
+
import currentlab
|
|
59
|
+
|
|
60
|
+
ds = currentlab.load_dataset(
|
|
61
|
+
region="oahu",
|
|
62
|
+
dataset_type="currents_surface",
|
|
63
|
+
)
|
|
64
|
+
print(ds)
|
|
65
|
+
```
|
|
66
|
+
|
|
67
|
+
## API Reference
|
|
68
|
+
|
|
69
|
+
### `load_dataset(...)`
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
currentlab.load_dataset(
|
|
73
|
+
region,
|
|
74
|
+
dataset_type,
|
|
75
|
+
dataset_source="auto",
|
|
76
|
+
file_format="netcdf",
|
|
77
|
+
run_date="latest",
|
|
78
|
+
*,
|
|
79
|
+
save_dir=None,
|
|
80
|
+
save_path=None,
|
|
81
|
+
api_key=None,
|
|
82
|
+
base_url="https://api.current-lab.com",
|
|
83
|
+
)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
| Parameter | Type | Description |
|
|
87
|
+
|---|---|---|
|
|
88
|
+
| `region` | `str` | Region identifier, e.g. `"oahu"` |
|
|
89
|
+
| `dataset_type` | `str` | Dataset type, e.g. `"currents_surface"`, `"wave"`, `"wind_10m"`, `"ocean_3d"` |
|
|
90
|
+
| `dataset_source` | `str` | Model/source selection. Default `"auto"` lets the server pick the best available source |
|
|
91
|
+
| `file_format` | `str` | File format to request. Default `"netcdf"` |
|
|
92
|
+
| `run_date` | `str` | Forecast run date as `"YYYY-MM-DD"`, or `"latest"`. Default `"latest"` |
|
|
93
|
+
| `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 |
|
|
94
|
+
| `save_path` | `str \| Path` | Exact path to save the file to. Reused on subsequent calls if it already exists. Mutually exclusive with `save_dir` |
|
|
95
|
+
| `api_key` | `str` | API key override. Falls back to `CURRENT_LAB_API_KEY` env var |
|
|
96
|
+
| `base_url` | `str` | API base URL override (useful for testing) |
|
|
97
|
+
|
|
98
|
+
Returns an `xr.Dataset`.
|
|
99
|
+
|
|
100
|
+
## Usage patterns
|
|
101
|
+
|
|
102
|
+
### Stream into memory (no file saved)
|
|
103
|
+
|
|
104
|
+
A temporary file is created and streamed into memory. The file is deleted after the dataset is closed.
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
ds = currentlab.load_dataset(region="oahu", dataset_type="wave")
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
### Save to a directory (cached)
|
|
111
|
+
|
|
112
|
+
File name is derived from the API response.
|
|
113
|
+
|
|
114
|
+
```python
|
|
115
|
+
ds = currentlab.load_dataset(
|
|
116
|
+
region="oahu",
|
|
117
|
+
dataset_type="wave",
|
|
118
|
+
run_date="2025-03-01",
|
|
119
|
+
save_dir="./data",
|
|
120
|
+
)
|
|
121
|
+
```
|
|
122
|
+
|
|
123
|
+
### Save to a specific file path (cached)
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
ds = currentlab.load_dataset(
|
|
127
|
+
region="oahu",
|
|
128
|
+
dataset_type="currents_surface",
|
|
129
|
+
save_path="./data/oahu_currents_latest.nc",
|
|
130
|
+
)
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
### Request a specific run date
|
|
134
|
+
|
|
135
|
+
```python
|
|
136
|
+
ds = currentlab.load_dataset(
|
|
137
|
+
region="oahu",
|
|
138
|
+
dataset_type="wind_10m",
|
|
139
|
+
run_date="2026-03-09",
|
|
140
|
+
)
|
|
141
|
+
```
|
|
142
|
+
|
|
143
|
+
### Working with the dataset
|
|
144
|
+
|
|
145
|
+
The returned object is a standard xarray Dataset, so the full xarray API is available:
|
|
146
|
+
|
|
147
|
+
```python
|
|
148
|
+
import numpy as np
|
|
149
|
+
import currentlab
|
|
150
|
+
|
|
151
|
+
ds = currentlab.load_dataset(region="oahu", dataset_type="currents_surface")
|
|
152
|
+
|
|
153
|
+
# Compute current speed from u/v components
|
|
154
|
+
ds["speed"] = np.hypot(ds["u"], ds["v"])
|
|
155
|
+
ds["speed"].attrs["long_name"] = "Current Speed"
|
|
156
|
+
ds["speed"].attrs["units"] = "m/s"
|
|
157
|
+
|
|
158
|
+
print(ds["speed"].max().item())
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
## Error handling
|
|
162
|
+
|
|
163
|
+
```python
|
|
164
|
+
from currentlab import CurrentLabAuthError, CurrentLabAPIError, CurrentLabDownloadError
|
|
165
|
+
|
|
166
|
+
try:
|
|
167
|
+
ds = currentlab.load_dataset(region="oahu", dataset_type="currents_surface")
|
|
168
|
+
except CurrentLabAuthError:
|
|
169
|
+
print("Check your CURRENT_LAB_API_KEY.")
|
|
170
|
+
except CurrentLabAPIError as e:
|
|
171
|
+
print(f"API error: {e}")
|
|
172
|
+
except CurrentLabDownloadError as e:
|
|
173
|
+
print(f"Download failed: {e}")
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
All exceptions inherit from `currentlab.CurrentLabError`.
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
## Development
|
|
180
|
+
|
|
181
|
+
```bash
|
|
182
|
+
git clone https://github.com/current-lab/currentlab-python
|
|
183
|
+
cd currentlab-python
|
|
184
|
+
python3 -m venv .venv
|
|
185
|
+
source .venv/bin/activate
|
|
186
|
+
pip install -e ".[dev]"
|
|
187
|
+
pytest
|
|
188
|
+
```
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
# currentlab-python
|
|
2
|
+
|
|
3
|
+
The Current Lab Python library provides convenient access to the [Current Lab](https://current-lab.com) API from Python applications. Load gridded forecast files as [xarray](https://docs.xarray.dev) datasets with a single function call.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pip install currentlab
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Authentication
|
|
12
|
+
|
|
13
|
+
Set your API key as an environment variable:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
export CURRENT_LAB_API_KEY="your-api-key"
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Or use a `.env` file (see `.env.example`) and load it with [python-dotenv](https://pypi.org/project/python-dotenv/).
|
|
20
|
+
|
|
21
|
+
You can also pass the key directly to any function call via the `api_key=` argument.
|
|
22
|
+
|
|
23
|
+
## Quick start
|
|
24
|
+
|
|
25
|
+
```python
|
|
26
|
+
import currentlab
|
|
27
|
+
|
|
28
|
+
ds = currentlab.load_dataset(
|
|
29
|
+
region="oahu",
|
|
30
|
+
dataset_type="currents_surface",
|
|
31
|
+
)
|
|
32
|
+
print(ds)
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
## API Reference
|
|
36
|
+
|
|
37
|
+
### `load_dataset(...)`
|
|
38
|
+
|
|
39
|
+
```python
|
|
40
|
+
currentlab.load_dataset(
|
|
41
|
+
region,
|
|
42
|
+
dataset_type,
|
|
43
|
+
dataset_source="auto",
|
|
44
|
+
file_format="netcdf",
|
|
45
|
+
run_date="latest",
|
|
46
|
+
*,
|
|
47
|
+
save_dir=None,
|
|
48
|
+
save_path=None,
|
|
49
|
+
api_key=None,
|
|
50
|
+
base_url="https://api.current-lab.com",
|
|
51
|
+
)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
| Parameter | Type | Description |
|
|
55
|
+
|---|---|---|
|
|
56
|
+
| `region` | `str` | Region identifier, e.g. `"oahu"` |
|
|
57
|
+
| `dataset_type` | `str` | Dataset type, e.g. `"currents_surface"`, `"wave"`, `"wind_10m"`, `"ocean_3d"` |
|
|
58
|
+
| `dataset_source` | `str` | Model/source selection. Default `"auto"` lets the server pick the best available source |
|
|
59
|
+
| `file_format` | `str` | File format to request. Default `"netcdf"` |
|
|
60
|
+
| `run_date` | `str` | Forecast run date as `"YYYY-MM-DD"`, or `"latest"`. Default `"latest"` |
|
|
61
|
+
| `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 |
|
|
62
|
+
| `save_path` | `str \| Path` | Exact path to save the file to. Reused on subsequent calls if it already exists. Mutually exclusive with `save_dir` |
|
|
63
|
+
| `api_key` | `str` | API key override. Falls back to `CURRENT_LAB_API_KEY` env var |
|
|
64
|
+
| `base_url` | `str` | API base URL override (useful for testing) |
|
|
65
|
+
|
|
66
|
+
Returns an `xr.Dataset`.
|
|
67
|
+
|
|
68
|
+
## Usage patterns
|
|
69
|
+
|
|
70
|
+
### Stream into memory (no file saved)
|
|
71
|
+
|
|
72
|
+
A temporary file is created and streamed into memory. The file is deleted after the dataset is closed.
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
ds = currentlab.load_dataset(region="oahu", dataset_type="wave")
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
### Save to a directory (cached)
|
|
79
|
+
|
|
80
|
+
File name is derived from the API response.
|
|
81
|
+
|
|
82
|
+
```python
|
|
83
|
+
ds = currentlab.load_dataset(
|
|
84
|
+
region="oahu",
|
|
85
|
+
dataset_type="wave",
|
|
86
|
+
run_date="2025-03-01",
|
|
87
|
+
save_dir="./data",
|
|
88
|
+
)
|
|
89
|
+
```
|
|
90
|
+
|
|
91
|
+
### Save to a specific file path (cached)
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
ds = currentlab.load_dataset(
|
|
95
|
+
region="oahu",
|
|
96
|
+
dataset_type="currents_surface",
|
|
97
|
+
save_path="./data/oahu_currents_latest.nc",
|
|
98
|
+
)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Request a specific run date
|
|
102
|
+
|
|
103
|
+
```python
|
|
104
|
+
ds = currentlab.load_dataset(
|
|
105
|
+
region="oahu",
|
|
106
|
+
dataset_type="wind_10m",
|
|
107
|
+
run_date="2026-03-09",
|
|
108
|
+
)
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
### Working with the dataset
|
|
112
|
+
|
|
113
|
+
The returned object is a standard xarray Dataset, so the full xarray API is available:
|
|
114
|
+
|
|
115
|
+
```python
|
|
116
|
+
import numpy as np
|
|
117
|
+
import currentlab
|
|
118
|
+
|
|
119
|
+
ds = currentlab.load_dataset(region="oahu", dataset_type="currents_surface")
|
|
120
|
+
|
|
121
|
+
# Compute current speed from u/v components
|
|
122
|
+
ds["speed"] = np.hypot(ds["u"], ds["v"])
|
|
123
|
+
ds["speed"].attrs["long_name"] = "Current Speed"
|
|
124
|
+
ds["speed"].attrs["units"] = "m/s"
|
|
125
|
+
|
|
126
|
+
print(ds["speed"].max().item())
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Error handling
|
|
130
|
+
|
|
131
|
+
```python
|
|
132
|
+
from currentlab import CurrentLabAuthError, CurrentLabAPIError, CurrentLabDownloadError
|
|
133
|
+
|
|
134
|
+
try:
|
|
135
|
+
ds = currentlab.load_dataset(region="oahu", dataset_type="currents_surface")
|
|
136
|
+
except CurrentLabAuthError:
|
|
137
|
+
print("Check your CURRENT_LAB_API_KEY.")
|
|
138
|
+
except CurrentLabAPIError as e:
|
|
139
|
+
print(f"API error: {e}")
|
|
140
|
+
except CurrentLabDownloadError as e:
|
|
141
|
+
print(f"Download failed: {e}")
|
|
142
|
+
```
|
|
143
|
+
|
|
144
|
+
All exceptions inherit from `currentlab.CurrentLabError`.
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
## Development
|
|
148
|
+
|
|
149
|
+
```bash
|
|
150
|
+
git clone https://github.com/current-lab/currentlab-python
|
|
151
|
+
cd currentlab-python
|
|
152
|
+
python3 -m venv .venv
|
|
153
|
+
source .venv/bin/activate
|
|
154
|
+
pip install -e ".[dev]"
|
|
155
|
+
pytest
|
|
156
|
+
```
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Basic usage examples for the currentlab Python library.
|
|
3
|
+
|
|
4
|
+
Set CURRENT_LAB_API_KEY in your environment before running:
|
|
5
|
+
export CURRENT_LAB_API_KEY="your-api-key"
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
import numpy as np
|
|
9
|
+
|
|
10
|
+
import currentlab
|
|
11
|
+
from currentlab import CurrentLabAPIError, CurrentLabAuthError, CurrentLabDownloadError
|
|
12
|
+
|
|
13
|
+
# ---------------------------------------------------------------------------
|
|
14
|
+
# 1. Stream directly into memory — no file is saved to disk
|
|
15
|
+
# ---------------------------------------------------------------------------
|
|
16
|
+
ds = currentlab.load_dataset(
|
|
17
|
+
region="oahu",
|
|
18
|
+
dataset_type="currents_surface",
|
|
19
|
+
)
|
|
20
|
+
print(ds)
|
|
21
|
+
|
|
22
|
+
# ---------------------------------------------------------------------------
|
|
23
|
+
# 2. Save to a directory — file is reused on subsequent calls
|
|
24
|
+
# ---------------------------------------------------------------------------
|
|
25
|
+
ds = currentlab.load_dataset(
|
|
26
|
+
region="oahu",
|
|
27
|
+
dataset_type="currents_surface",
|
|
28
|
+
save_dir="./data",
|
|
29
|
+
)
|
|
30
|
+
|
|
31
|
+
# ---------------------------------------------------------------------------
|
|
32
|
+
# 3. Save to a specific path
|
|
33
|
+
# ---------------------------------------------------------------------------
|
|
34
|
+
ds = currentlab.load_dataset(
|
|
35
|
+
region="oahu",
|
|
36
|
+
dataset_type="currents_surface",
|
|
37
|
+
save_path="./data/oahu_currents_latest.nc",
|
|
38
|
+
)
|
|
39
|
+
|
|
40
|
+
# ---------------------------------------------------------------------------
|
|
41
|
+
# 4. Request a specific run date
|
|
42
|
+
# ---------------------------------------------------------------------------
|
|
43
|
+
ds = currentlab.load_dataset(
|
|
44
|
+
region="oahu",
|
|
45
|
+
dataset_type="wave",
|
|
46
|
+
dataset_source="highres",
|
|
47
|
+
run_date="2026-03-09",
|
|
48
|
+
save_dir="./data",
|
|
49
|
+
)
|
|
50
|
+
|
|
51
|
+
# ---------------------------------------------------------------------------
|
|
52
|
+
# 5. Working with the dataset
|
|
53
|
+
# ---------------------------------------------------------------------------
|
|
54
|
+
ds = currentlab.load_dataset(region="oahu", dataset_type="currents_surface")
|
|
55
|
+
|
|
56
|
+
# Compute current speed from u/v components
|
|
57
|
+
ds["speed"] = np.hypot(ds["u"], ds["v"])
|
|
58
|
+
ds["speed"].attrs["long_name"] = "Current Speed"
|
|
59
|
+
ds["speed"].attrs["units"] = "m/s"
|
|
60
|
+
|
|
61
|
+
print(ds["speed"].max().item(), "m/s max surface current speed")
|
|
62
|
+
|
|
63
|
+
# ---------------------------------------------------------------------------
|
|
64
|
+
# 6. Error handling
|
|
65
|
+
# ---------------------------------------------------------------------------
|
|
66
|
+
|
|
67
|
+
try:
|
|
68
|
+
ds = currentlab.load_dataset(region="fake_region", dataset_type="currents_surface")
|
|
69
|
+
except CurrentLabAuthError:
|
|
70
|
+
print("Check your CURRENT_LAB_API_KEY.")
|
|
71
|
+
except CurrentLabAPIError as e:
|
|
72
|
+
print(f"API error: {e}")
|
|
73
|
+
except CurrentLabDownloadError as e:
|
|
74
|
+
print(f"Download failed: {e}")
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["hatchling"]
|
|
3
|
+
build-backend = "hatchling.build"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "currentlab"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Python library for the Current Lab API"
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
license = { text = "LICENSE" }
|
|
11
|
+
authors = [
|
|
12
|
+
{ name = "Current Lab", email = "info@current-lab.com" },
|
|
13
|
+
]
|
|
14
|
+
requires-python = ">=3.9"
|
|
15
|
+
keywords = ["ocean", "waves", "wind", "currents", "xarray", "netcdf", "forecast"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 3 - Alpha",
|
|
18
|
+
"Intended Audience :: Science/Research",
|
|
19
|
+
"License :: OSI Approved :: MIT License",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3.9",
|
|
22
|
+
"Programming Language :: Python :: 3.10",
|
|
23
|
+
"Programming Language :: Python :: 3.11",
|
|
24
|
+
"Programming Language :: Python :: 3.12",
|
|
25
|
+
"Topic :: Scientific/Engineering :: Atmospheric Science",
|
|
26
|
+
"Topic :: Scientific/Engineering :: Oceanography",
|
|
27
|
+
]
|
|
28
|
+
dependencies = [
|
|
29
|
+
"requests>=2.28",
|
|
30
|
+
"xarray>=2023.1",
|
|
31
|
+
"netcdf4>=1.6",
|
|
32
|
+
]
|
|
33
|
+
|
|
34
|
+
[project.optional-dependencies]
|
|
35
|
+
dev = [
|
|
36
|
+
"pytest>=7.0",
|
|
37
|
+
"pytest-cov>=4.0",
|
|
38
|
+
"python-dotenv>=1.0",
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[project.urls]
|
|
42
|
+
Homepage = "https://current-lab.com"
|
|
43
|
+
Documentation = "https://github.com/current-lab/currentlab-python#readme"
|
|
44
|
+
Repository = "https://github.com/current-lab/currentlab-python"
|
|
45
|
+
"Bug Tracker" = "https://github.com/current-lab/currentlab-python/issues"
|
|
46
|
+
|
|
47
|
+
[tool.hatch.build.targets.wheel]
|
|
48
|
+
packages = ["src/currentlab"]
|
|
49
|
+
|
|
50
|
+
[tool.pytest.ini_options]
|
|
51
|
+
testpaths = ["tests"]
|
|
52
|
+
|
|
53
|
+
[tool.ruff.lint]
|
|
54
|
+
select = ["E", "F", "I"]
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
"""currentlab — Python library for the Current Lab API."""
|
|
2
|
+
|
|
3
|
+
from ._client import RunDate, load_dataset
|
|
4
|
+
from ._exceptions import (
|
|
5
|
+
CurrentLabAPIError,
|
|
6
|
+
CurrentLabAuthError,
|
|
7
|
+
CurrentLabDownloadError,
|
|
8
|
+
CurrentLabError,
|
|
9
|
+
)
|
|
10
|
+
|
|
11
|
+
__version__ = "0.1.0"
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"load_dataset",
|
|
15
|
+
"RunDate",
|
|
16
|
+
"CurrentLabError",
|
|
17
|
+
"CurrentLabAPIError",
|
|
18
|
+
"CurrentLabAuthError",
|
|
19
|
+
"CurrentLabDownloadError",
|
|
20
|
+
"__version__",
|
|
21
|
+
]
|