currentlab 0.1.0__tar.gz → 0.2.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.2.0/.github/workflows/publish.yml +38 -0
- {currentlab-0.1.0 → currentlab-0.2.0}/PKG-INFO +19 -19
- {currentlab-0.1.0 → currentlab-0.2.0}/README.md +17 -17
- {currentlab-0.1.0 → currentlab-0.2.0}/examples/basic_usage.py +7 -7
- {currentlab-0.1.0 → currentlab-0.2.0}/pyproject.toml +1 -1
- {currentlab-0.1.0 → currentlab-0.2.0}/src/currentlab/__init__.py +1 -1
- currentlab-0.2.0/src/currentlab/_api.py +105 -0
- {currentlab-0.1.0 → currentlab-0.2.0}/src/currentlab/_client.py +35 -46
- currentlab-0.2.0/tests/test_api.py +190 -0
- currentlab-0.2.0/tests/test_client.py +220 -0
- {currentlab-0.1.0 → currentlab-0.2.0}/tests/test_download.py +18 -6
- currentlab-0.1.0/AGENTS.md +0 -113
- currentlab-0.1.0/src/currentlab/_api.py +0 -115
- currentlab-0.1.0/tests/test_api.py +0 -254
- currentlab-0.1.0/tests/test_client.py +0 -148
- {currentlab-0.1.0 → currentlab-0.2.0}/.gitignore +0 -0
- {currentlab-0.1.0 → currentlab-0.2.0}/LICENSE +0 -0
- {currentlab-0.1.0 → currentlab-0.2.0}/src/currentlab/_download.py +0 -0
- {currentlab-0.1.0 → currentlab-0.2.0}/src/currentlab/_exceptions.py +0 -0
- {currentlab-0.1.0 → currentlab-0.2.0}/src/currentlab/py.typed +0 -0
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
name: Publish to PyPI
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
push:
|
|
5
|
+
tags: ["v*"]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
build:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
steps:
|
|
11
|
+
- uses: actions/checkout@v4
|
|
12
|
+
- uses: actions/setup-python@v5
|
|
13
|
+
with:
|
|
14
|
+
python-version: "3.12"
|
|
15
|
+
- run: pip install -e ".[dev]" build
|
|
16
|
+
- run: pytest
|
|
17
|
+
- name: Check tag matches package version
|
|
18
|
+
run: |
|
|
19
|
+
version=$(python -c "import tomllib; print(tomllib.load(open('pyproject.toml','rb'))['project']['version'])")
|
|
20
|
+
test "v$version" = "$GITHUB_REF_NAME" || { echo "Tag $GITHUB_REF_NAME does not match pyproject version $version"; exit 1; }
|
|
21
|
+
- run: python -m build
|
|
22
|
+
- uses: actions/upload-artifact@v4
|
|
23
|
+
with:
|
|
24
|
+
name: dist
|
|
25
|
+
path: dist/
|
|
26
|
+
|
|
27
|
+
publish:
|
|
28
|
+
needs: build
|
|
29
|
+
runs-on: ubuntu-latest
|
|
30
|
+
environment: pypi
|
|
31
|
+
permissions:
|
|
32
|
+
id-token: write
|
|
33
|
+
steps:
|
|
34
|
+
- uses: actions/download-artifact@v4
|
|
35
|
+
with:
|
|
36
|
+
name: dist
|
|
37
|
+
path: dist/
|
|
38
|
+
- uses: pypa/gh-action-pypi-publish@release/v1
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: currentlab
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Python library for the Current Lab API
|
|
5
5
|
Project-URL: Homepage, https://current-lab.com
|
|
6
6
|
Project-URL: Documentation, https://github.com/current-lab/currentlab-python#readme
|
|
@@ -59,7 +59,7 @@ import currentlab
|
|
|
59
59
|
|
|
60
60
|
ds = currentlab.load_dataset(
|
|
61
61
|
region="oahu",
|
|
62
|
-
|
|
62
|
+
product="currents_surface",
|
|
63
63
|
)
|
|
64
64
|
print(ds)
|
|
65
65
|
```
|
|
@@ -71,9 +71,8 @@ print(ds)
|
|
|
71
71
|
```python
|
|
72
72
|
currentlab.load_dataset(
|
|
73
73
|
region,
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
file_format="netcdf",
|
|
74
|
+
product,
|
|
75
|
+
source="auto",
|
|
77
76
|
run_date="latest",
|
|
78
77
|
*,
|
|
79
78
|
save_dir=None,
|
|
@@ -85,16 +84,17 @@ currentlab.load_dataset(
|
|
|
85
84
|
|
|
86
85
|
| Parameter | Type | Description |
|
|
87
86
|
|---|---|---|
|
|
88
|
-
| `region` | `str` | Region identifier, e.g. `"oahu"` |
|
|
89
|
-
| `
|
|
90
|
-
| `
|
|
91
|
-
| `
|
|
92
|
-
| `
|
|
93
|
-
| `
|
|
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` |
|
|
87
|
+
| `region` | `str` | Region identifier, e.g. `"oahu"`. Your accessible regions are listed by `/v1/auth/permissions` |
|
|
88
|
+
| `product` | `str` | One of `"currents_surface"`, `"wind_10m"`, `"wave"`, `"ocean_3d"` |
|
|
89
|
+
| `source` | `str` | Dataset source. Default `"auto"` lets the server pick the best available source |
|
|
90
|
+
| `run_date` | `str \| date \| datetime` | Forecast run date as `"YYYY-MM-DD"` or `"latest"`. Default `"latest"` |
|
|
91
|
+
| `save_dir` | `str \| Path` | Directory to save the downloaded file. Filename comes from the API response. File is reused on subsequent calls if it already exists |
|
|
92
|
+
| `save_path` | `str \| Path` | Exact path to save the file to. If the file exists it is opened directly without calling the API. Mutually exclusive with `save_dir` |
|
|
95
93
|
| `api_key` | `str` | API key override. Falls back to `CURRENT_LAB_API_KEY` env var |
|
|
96
94
|
| `base_url` | `str` | API base URL override (useful for testing) |
|
|
97
95
|
|
|
96
|
+
Files are always requested in NetCDF format.
|
|
97
|
+
|
|
98
98
|
Returns an `xr.Dataset`.
|
|
99
99
|
|
|
100
100
|
## Usage patterns
|
|
@@ -104,7 +104,7 @@ Returns an `xr.Dataset`.
|
|
|
104
104
|
A temporary file is created and streamed into memory. The file is deleted after the dataset is closed.
|
|
105
105
|
|
|
106
106
|
```python
|
|
107
|
-
ds = currentlab.load_dataset(region="oahu",
|
|
107
|
+
ds = currentlab.load_dataset(region="oahu", product="wave")
|
|
108
108
|
```
|
|
109
109
|
|
|
110
110
|
### Save to a directory (cached)
|
|
@@ -114,7 +114,7 @@ File name is derived from the API response.
|
|
|
114
114
|
```python
|
|
115
115
|
ds = currentlab.load_dataset(
|
|
116
116
|
region="oahu",
|
|
117
|
-
|
|
117
|
+
product="wave",
|
|
118
118
|
run_date="2025-03-01",
|
|
119
119
|
save_dir="./data",
|
|
120
120
|
)
|
|
@@ -125,7 +125,7 @@ ds = currentlab.load_dataset(
|
|
|
125
125
|
```python
|
|
126
126
|
ds = currentlab.load_dataset(
|
|
127
127
|
region="oahu",
|
|
128
|
-
|
|
128
|
+
product="currents_surface",
|
|
129
129
|
save_path="./data/oahu_currents_latest.nc",
|
|
130
130
|
)
|
|
131
131
|
```
|
|
@@ -135,7 +135,7 @@ ds = currentlab.load_dataset(
|
|
|
135
135
|
```python
|
|
136
136
|
ds = currentlab.load_dataset(
|
|
137
137
|
region="oahu",
|
|
138
|
-
|
|
138
|
+
product="wind_10m",
|
|
139
139
|
run_date="2026-03-09",
|
|
140
140
|
)
|
|
141
141
|
```
|
|
@@ -148,7 +148,7 @@ The returned object is a standard xarray Dataset, so the full xarray API is avai
|
|
|
148
148
|
import numpy as np
|
|
149
149
|
import currentlab
|
|
150
150
|
|
|
151
|
-
ds = currentlab.load_dataset(region="oahu",
|
|
151
|
+
ds = currentlab.load_dataset(region="oahu", product="currents_surface")
|
|
152
152
|
|
|
153
153
|
# Compute current speed from u/v components
|
|
154
154
|
ds["speed"] = np.hypot(ds["u"], ds["v"])
|
|
@@ -164,7 +164,7 @@ print(ds["speed"].max().item())
|
|
|
164
164
|
from currentlab import CurrentLabAuthError, CurrentLabAPIError, CurrentLabDownloadError
|
|
165
165
|
|
|
166
166
|
try:
|
|
167
|
-
ds = currentlab.load_dataset(region="oahu",
|
|
167
|
+
ds = currentlab.load_dataset(region="oahu", product="currents_surface")
|
|
168
168
|
except CurrentLabAuthError:
|
|
169
169
|
print("Check your CURRENT_LAB_API_KEY.")
|
|
170
170
|
except CurrentLabAPIError as e:
|
|
@@ -27,7 +27,7 @@ import currentlab
|
|
|
27
27
|
|
|
28
28
|
ds = currentlab.load_dataset(
|
|
29
29
|
region="oahu",
|
|
30
|
-
|
|
30
|
+
product="currents_surface",
|
|
31
31
|
)
|
|
32
32
|
print(ds)
|
|
33
33
|
```
|
|
@@ -39,9 +39,8 @@ print(ds)
|
|
|
39
39
|
```python
|
|
40
40
|
currentlab.load_dataset(
|
|
41
41
|
region,
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
file_format="netcdf",
|
|
42
|
+
product,
|
|
43
|
+
source="auto",
|
|
45
44
|
run_date="latest",
|
|
46
45
|
*,
|
|
47
46
|
save_dir=None,
|
|
@@ -53,16 +52,17 @@ currentlab.load_dataset(
|
|
|
53
52
|
|
|
54
53
|
| Parameter | Type | Description |
|
|
55
54
|
|---|---|---|
|
|
56
|
-
| `region` | `str` | Region identifier, e.g. `"oahu"` |
|
|
57
|
-
| `
|
|
58
|
-
| `
|
|
59
|
-
| `
|
|
60
|
-
| `
|
|
61
|
-
| `
|
|
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` |
|
|
55
|
+
| `region` | `str` | Region identifier, e.g. `"oahu"`. Your accessible regions are listed by `/v1/auth/permissions` |
|
|
56
|
+
| `product` | `str` | One of `"currents_surface"`, `"wind_10m"`, `"wave"`, `"ocean_3d"` |
|
|
57
|
+
| `source` | `str` | Dataset source. Default `"auto"` lets the server pick the best available source |
|
|
58
|
+
| `run_date` | `str \| date \| datetime` | Forecast run date as `"YYYY-MM-DD"` or `"latest"`. Default `"latest"` |
|
|
59
|
+
| `save_dir` | `str \| Path` | Directory to save the downloaded file. Filename comes from the API response. File is reused on subsequent calls if it already exists |
|
|
60
|
+
| `save_path` | `str \| Path` | Exact path to save the file to. If the file exists it is opened directly without calling the API. Mutually exclusive with `save_dir` |
|
|
63
61
|
| `api_key` | `str` | API key override. Falls back to `CURRENT_LAB_API_KEY` env var |
|
|
64
62
|
| `base_url` | `str` | API base URL override (useful for testing) |
|
|
65
63
|
|
|
64
|
+
Files are always requested in NetCDF format.
|
|
65
|
+
|
|
66
66
|
Returns an `xr.Dataset`.
|
|
67
67
|
|
|
68
68
|
## Usage patterns
|
|
@@ -72,7 +72,7 @@ Returns an `xr.Dataset`.
|
|
|
72
72
|
A temporary file is created and streamed into memory. The file is deleted after the dataset is closed.
|
|
73
73
|
|
|
74
74
|
```python
|
|
75
|
-
ds = currentlab.load_dataset(region="oahu",
|
|
75
|
+
ds = currentlab.load_dataset(region="oahu", product="wave")
|
|
76
76
|
```
|
|
77
77
|
|
|
78
78
|
### Save to a directory (cached)
|
|
@@ -82,7 +82,7 @@ File name is derived from the API response.
|
|
|
82
82
|
```python
|
|
83
83
|
ds = currentlab.load_dataset(
|
|
84
84
|
region="oahu",
|
|
85
|
-
|
|
85
|
+
product="wave",
|
|
86
86
|
run_date="2025-03-01",
|
|
87
87
|
save_dir="./data",
|
|
88
88
|
)
|
|
@@ -93,7 +93,7 @@ ds = currentlab.load_dataset(
|
|
|
93
93
|
```python
|
|
94
94
|
ds = currentlab.load_dataset(
|
|
95
95
|
region="oahu",
|
|
96
|
-
|
|
96
|
+
product="currents_surface",
|
|
97
97
|
save_path="./data/oahu_currents_latest.nc",
|
|
98
98
|
)
|
|
99
99
|
```
|
|
@@ -103,7 +103,7 @@ ds = currentlab.load_dataset(
|
|
|
103
103
|
```python
|
|
104
104
|
ds = currentlab.load_dataset(
|
|
105
105
|
region="oahu",
|
|
106
|
-
|
|
106
|
+
product="wind_10m",
|
|
107
107
|
run_date="2026-03-09",
|
|
108
108
|
)
|
|
109
109
|
```
|
|
@@ -116,7 +116,7 @@ The returned object is a standard xarray Dataset, so the full xarray API is avai
|
|
|
116
116
|
import numpy as np
|
|
117
117
|
import currentlab
|
|
118
118
|
|
|
119
|
-
ds = currentlab.load_dataset(region="oahu",
|
|
119
|
+
ds = currentlab.load_dataset(region="oahu", product="currents_surface")
|
|
120
120
|
|
|
121
121
|
# Compute current speed from u/v components
|
|
122
122
|
ds["speed"] = np.hypot(ds["u"], ds["v"])
|
|
@@ -132,7 +132,7 @@ print(ds["speed"].max().item())
|
|
|
132
132
|
from currentlab import CurrentLabAuthError, CurrentLabAPIError, CurrentLabDownloadError
|
|
133
133
|
|
|
134
134
|
try:
|
|
135
|
-
ds = currentlab.load_dataset(region="oahu",
|
|
135
|
+
ds = currentlab.load_dataset(region="oahu", product="currents_surface")
|
|
136
136
|
except CurrentLabAuthError:
|
|
137
137
|
print("Check your CURRENT_LAB_API_KEY.")
|
|
138
138
|
except CurrentLabAPIError as e:
|
|
@@ -15,7 +15,7 @@ from currentlab import CurrentLabAPIError, CurrentLabAuthError, CurrentLabDownlo
|
|
|
15
15
|
# ---------------------------------------------------------------------------
|
|
16
16
|
ds = currentlab.load_dataset(
|
|
17
17
|
region="oahu",
|
|
18
|
-
|
|
18
|
+
product="currents_surface",
|
|
19
19
|
)
|
|
20
20
|
print(ds)
|
|
21
21
|
|
|
@@ -24,7 +24,7 @@ print(ds)
|
|
|
24
24
|
# ---------------------------------------------------------------------------
|
|
25
25
|
ds = currentlab.load_dataset(
|
|
26
26
|
region="oahu",
|
|
27
|
-
|
|
27
|
+
product="currents_surface",
|
|
28
28
|
save_dir="./data",
|
|
29
29
|
)
|
|
30
30
|
|
|
@@ -33,7 +33,7 @@ ds = currentlab.load_dataset(
|
|
|
33
33
|
# ---------------------------------------------------------------------------
|
|
34
34
|
ds = currentlab.load_dataset(
|
|
35
35
|
region="oahu",
|
|
36
|
-
|
|
36
|
+
product="currents_surface",
|
|
37
37
|
save_path="./data/oahu_currents_latest.nc",
|
|
38
38
|
)
|
|
39
39
|
|
|
@@ -42,8 +42,8 @@ ds = currentlab.load_dataset(
|
|
|
42
42
|
# ---------------------------------------------------------------------------
|
|
43
43
|
ds = currentlab.load_dataset(
|
|
44
44
|
region="oahu",
|
|
45
|
-
|
|
46
|
-
|
|
45
|
+
product="wave",
|
|
46
|
+
source="highres",
|
|
47
47
|
run_date="2026-03-09",
|
|
48
48
|
save_dir="./data",
|
|
49
49
|
)
|
|
@@ -51,7 +51,7 @@ ds = currentlab.load_dataset(
|
|
|
51
51
|
# ---------------------------------------------------------------------------
|
|
52
52
|
# 5. Working with the dataset
|
|
53
53
|
# ---------------------------------------------------------------------------
|
|
54
|
-
ds = currentlab.load_dataset(region="oahu",
|
|
54
|
+
ds = currentlab.load_dataset(region="oahu", product="currents_surface")
|
|
55
55
|
|
|
56
56
|
# Compute current speed from u/v components
|
|
57
57
|
ds["speed"] = np.hypot(ds["u"], ds["v"])
|
|
@@ -65,7 +65,7 @@ print(ds["speed"].max().item(), "m/s max surface current speed")
|
|
|
65
65
|
# ---------------------------------------------------------------------------
|
|
66
66
|
|
|
67
67
|
try:
|
|
68
|
-
ds = currentlab.load_dataset(region="fake_region",
|
|
68
|
+
ds = currentlab.load_dataset(region="fake_region", product="currents_surface")
|
|
69
69
|
except CurrentLabAuthError:
|
|
70
70
|
print("Check your CURRENT_LAB_API_KEY.")
|
|
71
71
|
except CurrentLabAPIError as e:
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
import requests
|
|
7
|
+
|
|
8
|
+
from ._exceptions import CurrentLabAPIError, CurrentLabAuthError
|
|
9
|
+
|
|
10
|
+
API_KEY_ENV_VAR = "CURRENT_LAB_API_KEY"
|
|
11
|
+
DEFAULT_BASE_URL = "https://api.current-lab.com"
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _resolve_api_key(api_key: str | None) -> str:
|
|
15
|
+
if api_key:
|
|
16
|
+
return api_key
|
|
17
|
+
key = os.environ.get(API_KEY_ENV_VAR)
|
|
18
|
+
if not key:
|
|
19
|
+
raise CurrentLabAuthError(
|
|
20
|
+
f"No API key found. Set the {API_KEY_ENV_VAR} environment variable "
|
|
21
|
+
"or pass api_key= to load_dataset()."
|
|
22
|
+
)
|
|
23
|
+
return key
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def _error_detail(response: requests.Response) -> str:
|
|
27
|
+
"""Build a readable message from a v1 error body: {"detail": str, "code": str}."""
|
|
28
|
+
try:
|
|
29
|
+
payload = response.json()
|
|
30
|
+
except ValueError:
|
|
31
|
+
return response.text
|
|
32
|
+
|
|
33
|
+
if not isinstance(payload, dict):
|
|
34
|
+
return response.text
|
|
35
|
+
|
|
36
|
+
detail = payload.get("detail")
|
|
37
|
+
code = payload.get("code")
|
|
38
|
+
|
|
39
|
+
errors = payload.get("errors")
|
|
40
|
+
if isinstance(errors, list):
|
|
41
|
+
messages = [
|
|
42
|
+
e["message"] for e in errors if isinstance(e, dict) and "message" in e
|
|
43
|
+
]
|
|
44
|
+
if messages:
|
|
45
|
+
detail = "; ".join(messages)
|
|
46
|
+
|
|
47
|
+
if not isinstance(detail, str):
|
|
48
|
+
return response.text
|
|
49
|
+
return f"{detail} (code: {code})" if code else detail
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def request_gridded_files(
|
|
53
|
+
region: str,
|
|
54
|
+
product: str,
|
|
55
|
+
source: str = "auto",
|
|
56
|
+
file_format: str = "netcdf",
|
|
57
|
+
run_date: str | None = None,
|
|
58
|
+
api_key: str | None = None,
|
|
59
|
+
base_url: str = DEFAULT_BASE_URL,
|
|
60
|
+
) -> dict[str, Any]:
|
|
61
|
+
"""POST to /v1/gridded_files and return the parsed ``meta`` and ``files``."""
|
|
62
|
+
key = _resolve_api_key(api_key)
|
|
63
|
+
|
|
64
|
+
body: dict[str, Any] = {
|
|
65
|
+
"region": region,
|
|
66
|
+
"product": product,
|
|
67
|
+
"source": source,
|
|
68
|
+
"file_format": file_format,
|
|
69
|
+
}
|
|
70
|
+
if run_date is not None:
|
|
71
|
+
body["run_date"] = run_date
|
|
72
|
+
|
|
73
|
+
try:
|
|
74
|
+
response = requests.post(
|
|
75
|
+
url=f"{base_url}/v1/gridded_files",
|
|
76
|
+
json=body,
|
|
77
|
+
headers={"X-API-Key": key},
|
|
78
|
+
timeout=30,
|
|
79
|
+
)
|
|
80
|
+
except requests.ConnectionError as exc:
|
|
81
|
+
raise CurrentLabAPIError(
|
|
82
|
+
f"Could not reach the Current Lab API at {base_url}. "
|
|
83
|
+
"Check your network connection."
|
|
84
|
+
) from exc
|
|
85
|
+
except requests.Timeout as exc:
|
|
86
|
+
raise CurrentLabAPIError("Request to the Current Lab API timed out.") from exc
|
|
87
|
+
|
|
88
|
+
status = response.status_code
|
|
89
|
+
if status in (401, 403):
|
|
90
|
+
raise CurrentLabAuthError(
|
|
91
|
+
f"Authentication failed (HTTP {status}): {_error_detail(response)}"
|
|
92
|
+
)
|
|
93
|
+
|
|
94
|
+
if not response.ok:
|
|
95
|
+
raise CurrentLabAPIError(
|
|
96
|
+
f"API request failed (HTTP {status}): {_error_detail(response)}"
|
|
97
|
+
)
|
|
98
|
+
|
|
99
|
+
payload = response.json()
|
|
100
|
+
if not payload.get("files"):
|
|
101
|
+
raise CurrentLabAPIError(
|
|
102
|
+
"The API returned no files for the given parameters. "
|
|
103
|
+
"Check that region, product, source, and run_date are valid."
|
|
104
|
+
)
|
|
105
|
+
return payload
|
|
@@ -6,7 +6,7 @@ from typing import Optional, Union
|
|
|
6
6
|
|
|
7
7
|
import xarray as xr
|
|
8
8
|
|
|
9
|
-
from ._api import DEFAULT_BASE_URL,
|
|
9
|
+
from ._api import DEFAULT_BASE_URL, request_gridded_files
|
|
10
10
|
from ._download import download_to_path, temp_download
|
|
11
11
|
|
|
12
12
|
RunDate = Union[str, date, datetime]
|
|
@@ -15,21 +15,18 @@ RunDate = Union[str, date, datetime]
|
|
|
15
15
|
def _coerce_run_date(run_date: RunDate) -> str:
|
|
16
16
|
"""Normalise run_date to the YYYY-MM-DD string the API expects.
|
|
17
17
|
|
|
18
|
-
``datetime`` is
|
|
19
|
-
|
|
18
|
+
``datetime`` is a subclass of ``date``, so both are handled by the same branch
|
|
19
|
+
and the time component is dropped.
|
|
20
20
|
"""
|
|
21
|
-
if isinstance(run_date, datetime):
|
|
22
|
-
return run_date.strftime("%Y-%m-%d")
|
|
23
21
|
if isinstance(run_date, date):
|
|
24
22
|
return run_date.strftime("%Y-%m-%d")
|
|
25
|
-
return run_date
|
|
23
|
+
return run_date
|
|
26
24
|
|
|
27
25
|
|
|
28
26
|
def load_dataset(
|
|
29
27
|
region: str,
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
file_format: str = "netcdf",
|
|
28
|
+
product: str,
|
|
29
|
+
source: str = "auto",
|
|
33
30
|
run_date: RunDate = "latest",
|
|
34
31
|
*,
|
|
35
32
|
save_dir: Optional[Union[str, Path]] = None,
|
|
@@ -37,31 +34,29 @@ def load_dataset(
|
|
|
37
34
|
api_key: Optional[str] = None,
|
|
38
35
|
base_url: str = DEFAULT_BASE_URL,
|
|
39
36
|
) -> xr.Dataset:
|
|
40
|
-
"""Load a Current Lab gridded forecast
|
|
37
|
+
"""Load a Current Lab gridded forecast file as an xarray Dataset.
|
|
41
38
|
|
|
42
39
|
Parameters
|
|
43
40
|
----------
|
|
44
41
|
region : str
|
|
45
|
-
Region identifier, e.g. ``"oahu"``.
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
File format to request. Defaults to ``"netcdf"``.
|
|
42
|
+
Region identifier, e.g. ``"oahu"``. Your accessible regions are listed by
|
|
43
|
+
the ``/v1/auth/permissions`` endpoint.
|
|
44
|
+
product : str
|
|
45
|
+
One of ``"currents_surface"``, ``"wind_10m"``, ``"wave"``, ``"ocean_3d"``.
|
|
46
|
+
source : str, optional
|
|
47
|
+
Dataset source. Defaults to ``"auto"`` (server-selected best source).
|
|
52
48
|
run_date : str, date, or datetime, optional
|
|
53
|
-
Forecast run date
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
``"latest"``.
|
|
49
|
+
Forecast run date as a ``datetime.date``, ``datetime.datetime`` (time
|
|
50
|
+
component is ignored), ``"YYYY-MM-DD"`` string, or ``"latest"`` for the
|
|
51
|
+
most recent run. Defaults to ``"latest"``.
|
|
57
52
|
save_dir : str or Path, optional
|
|
58
53
|
Directory in which to save the downloaded file. The filename is taken from
|
|
59
54
|
the API response. If the file already exists it is loaded without
|
|
60
55
|
re-downloading. Mutually exclusive with *save_path*.
|
|
61
56
|
save_path : str or Path, optional
|
|
62
57
|
Exact file path to save the download to. If the file already exists it is
|
|
63
|
-
loaded without
|
|
64
|
-
|
|
58
|
+
loaded directly, without calling the API. Mutually exclusive with
|
|
59
|
+
*save_dir*.
|
|
65
60
|
api_key : str, optional
|
|
66
61
|
Current Lab API key. Defaults to the ``CURRENT_LAB_API_KEY`` environment
|
|
67
62
|
variable.
|
|
@@ -77,45 +72,39 @@ def load_dataset(
|
|
|
77
72
|
--------
|
|
78
73
|
Stream directly into memory (no file saved):
|
|
79
74
|
|
|
80
|
-
>>> ds = currentlab.load_dataset(region="oahu",
|
|
75
|
+
>>> ds = currentlab.load_dataset(region="oahu", product="currents_surface")
|
|
81
76
|
|
|
82
77
|
Save to a directory and reuse on subsequent calls:
|
|
83
78
|
|
|
84
|
-
>>> ds = currentlab.load_dataset(
|
|
85
|
-
... region="oahu",
|
|
86
|
-
... dataset_type="waves",
|
|
87
|
-
... save_dir="./data",
|
|
88
|
-
... )
|
|
79
|
+
>>> ds = currentlab.load_dataset(region="oahu", product="wave", save_dir="./data")
|
|
89
80
|
"""
|
|
90
81
|
if save_dir is not None and save_path is not None:
|
|
91
82
|
raise ValueError("Provide either save_dir or save_path, not both.")
|
|
92
83
|
|
|
93
|
-
|
|
84
|
+
if save_path is not None and Path(save_path).exists():
|
|
85
|
+
return xr.open_dataset(Path(save_path), engine="netcdf4")
|
|
86
|
+
|
|
87
|
+
response = request_gridded_files(
|
|
94
88
|
region=region,
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
file_format=
|
|
89
|
+
product=product,
|
|
90
|
+
source=source,
|
|
91
|
+
file_format="netcdf",
|
|
98
92
|
run_date=_coerce_run_date(run_date),
|
|
99
93
|
api_key=api_key,
|
|
100
94
|
base_url=base_url,
|
|
101
95
|
)
|
|
102
|
-
|
|
103
|
-
download_url: str = file_info["
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
return xr.open_dataset(dest, engine="netcdf4")
|
|
111
|
-
|
|
112
|
-
if save_dir is not None:
|
|
113
|
-
dest = Path(save_dir) / remote_filename
|
|
96
|
+
file_info = min(response["files"], key=lambda f: f.get("sequence_id", 0))
|
|
97
|
+
download_url: str = file_info["url"]
|
|
98
|
+
|
|
99
|
+
if save_path is not None or save_dir is not None:
|
|
100
|
+
if save_path is not None:
|
|
101
|
+
dest = Path(save_path)
|
|
102
|
+
else:
|
|
103
|
+
dest = Path(save_dir) / file_info["file_name"]
|
|
114
104
|
if not dest.exists():
|
|
115
105
|
download_to_path(download_url, dest)
|
|
116
106
|
return xr.open_dataset(dest, engine="netcdf4")
|
|
117
107
|
|
|
118
|
-
# No save location — stream to a temp file, load fully into memory, then clean up.
|
|
119
108
|
with temp_download(download_url) as tmp_path:
|
|
120
109
|
with xr.open_dataset(tmp_path, engine="netcdf4") as ds:
|
|
121
110
|
return ds.load()
|