forcingkit-client 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.
- {forcingkit_client-0.1.0 → forcingkit_client-0.2.0}/PKG-INFO +2 -2
- {forcingkit_client-0.1.0 → forcingkit_client-0.2.0}/README.md +1 -1
- {forcingkit_client-0.1.0 → forcingkit_client-0.2.0}/pyproject.toml +1 -1
- {forcingkit_client-0.1.0 → forcingkit_client-0.2.0}/src/forcingkit_client/__init__.py +1 -1
- {forcingkit_client-0.1.0 → forcingkit_client-0.2.0}/src/forcingkit_client/client.py +60 -8
- {forcingkit_client-0.1.0 → forcingkit_client-0.2.0}/src/forcingkit_client/schema.py +3 -3
- {forcingkit_client-0.1.0 → forcingkit_client-0.2.0}/src/forcingkit_client/trajectory.py +6 -1
- {forcingkit_client-0.1.0 → forcingkit_client-0.2.0}/tests/test_client.py +87 -8
- {forcingkit_client-0.1.0 → forcingkit_client-0.2.0}/uv.lock +1 -1
- {forcingkit_client-0.1.0 → forcingkit_client-0.2.0}/.gitignore +0 -0
- {forcingkit_client-0.1.0 → forcingkit_client-0.2.0}/LICENSE +0 -0
- {forcingkit_client-0.1.0 → forcingkit_client-0.2.0}/src/forcingkit_client/store.py +0 -0
- {forcingkit_client-0.1.0 → forcingkit_client-0.2.0}/tests/conftest.py +0 -0
- {forcingkit_client-0.1.0 → forcingkit_client-0.2.0}/tests/test_schema.py +0 -0
- {forcingkit_client-0.1.0 → forcingkit_client-0.2.0}/tests/test_trajectory.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: forcingkit-client
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.2.0
|
|
4
4
|
Summary: Lightweight Python client for a forcingkit service: ocean, atmosphere and station forcing as xarray, and forcing interpolated along a trajectory.
|
|
5
5
|
Project-URL: Documentation, https://forcingkit.docs.lhzn.io/client.html
|
|
6
6
|
Project-URL: Issues, https://github.com/lhzn-io/forcingkit/issues
|
|
@@ -35,7 +35,7 @@ from forcingkit_client import Client
|
|
|
35
35
|
|
|
36
36
|
fk = Client("http://localhost:9598")
|
|
37
37
|
box = (-70.9, 41.3, -70.6, 41.6) # west, south, east, north
|
|
38
|
-
ocean = fk.ocean(box, "2025-07-01", 24) # u, v, temp, salt, zeta
|
|
38
|
+
ocean = fk.ocean(box, "2025-07-01", 24) # u, v, temp, salt, zeta; w from NECOFS
|
|
39
39
|
air = fk.atmosphere(box, "2025-07-01", 24) # HRRR u10, v10, t2m, ...
|
|
40
40
|
|
|
41
41
|
# lon, lat, time, z: one value per point; z in metres, positive up
|
|
@@ -11,7 +11,7 @@ from forcingkit_client import Client
|
|
|
11
11
|
|
|
12
12
|
fk = Client("http://localhost:9598")
|
|
13
13
|
box = (-70.9, 41.3, -70.6, 41.6) # west, south, east, north
|
|
14
|
-
ocean = fk.ocean(box, "2025-07-01", 24) # u, v, temp, salt, zeta
|
|
14
|
+
ocean = fk.ocean(box, "2025-07-01", 24) # u, v, temp, salt, zeta; w from NECOFS
|
|
15
15
|
air = fk.atmosphere(box, "2025-07-01", 24) # HRRR u10, v10, t2m, ...
|
|
16
16
|
|
|
17
17
|
# lon, lat, time, z: one value per point; z in metres, positive up
|
|
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "forcingkit-client"
|
|
7
|
-
version = "0.
|
|
7
|
+
version = "0.2.0"
|
|
8
8
|
description = "Lightweight Python client for a forcingkit service: ocean, atmosphere and station forcing as xarray, and forcing interpolated along a trajectory."
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
requires-python = ">=3.11"
|
|
@@ -56,9 +56,16 @@ def _bbox(bbox: BBox) -> Dict[str, float]:
|
|
|
56
56
|
|
|
57
57
|
|
|
58
58
|
def _ocean_request(
|
|
59
|
-
bbox,
|
|
59
|
+
bbox,
|
|
60
|
+
start,
|
|
61
|
+
hours,
|
|
62
|
+
vertical_spacing_m,
|
|
63
|
+
pad_cells,
|
|
64
|
+
sponge_cells,
|
|
65
|
+
donor: Optional[str] = None,
|
|
66
|
+
**options,
|
|
60
67
|
):
|
|
61
|
-
|
|
68
|
+
request = {
|
|
62
69
|
"bbox": _bbox(bbox),
|
|
63
70
|
"start_date": iso_utc(start),
|
|
64
71
|
"duration_hours": int(hours),
|
|
@@ -69,6 +76,22 @@ def _ocean_request(
|
|
|
69
76
|
"sponge_cells": int(sponge_cells),
|
|
70
77
|
**options,
|
|
71
78
|
}
|
|
79
|
+
if donor:
|
|
80
|
+
request["donor"] = str(donor)
|
|
81
|
+
return request
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
def _check_donor(item: Dict[str, Any], donor: Optional[str]) -> Dict[str, Any]:
|
|
85
|
+
"""`item`, unless a named `donor` is not the one that delivered it: a service older than the
|
|
86
|
+
`donor` option ignores it and ranks the donors, and its store must not pass for the named one."""
|
|
87
|
+
delivered = item["properties"].get("forcingkit:donor")
|
|
88
|
+
if donor and delivered != donor:
|
|
89
|
+
raise ForcingkitError(
|
|
90
|
+
f"asked for donor {donor!r} but store {item['id']} is from {delivered!r}; "
|
|
91
|
+
"the service may predate the donor option",
|
|
92
|
+
detail={"requested": donor, "delivered": delivered},
|
|
93
|
+
)
|
|
94
|
+
return item
|
|
72
95
|
|
|
73
96
|
|
|
74
97
|
def _atmosphere_request(bbox, start, hours, resolution_deg, margin_deg, cache_bust):
|
|
@@ -238,6 +261,10 @@ class _Base:
|
|
|
238
261
|
"forcingkit_schema": props.get("forcingkit:schema") or "",
|
|
239
262
|
}
|
|
240
263
|
)
|
|
264
|
+
# Ocean stores say where their vertical velocity comes from ("model", "continuity" or
|
|
265
|
+
# "none"); a service older than the property, and other kinds of store, leave it out.
|
|
266
|
+
if props.get("forcingkit:w_source"):
|
|
267
|
+
ds.attrs["forcingkit_w_source"] = props["forcingkit:w_source"]
|
|
241
268
|
return ds
|
|
242
269
|
|
|
243
270
|
|
|
@@ -287,15 +314,31 @@ class Client(_Base):
|
|
|
287
314
|
vertical_spacing_m: float = 2.0,
|
|
288
315
|
pad_cells: int = 1,
|
|
289
316
|
sponge_cells: int = 0,
|
|
317
|
+
donor: Optional[str] = None,
|
|
290
318
|
**options: Any,
|
|
291
319
|
) -> xr.Dataset:
|
|
292
320
|
"""The parent ocean over `bbox` for `hours` from `start`: `u, v` (m/s), `temp` (deg C),
|
|
293
|
-
`salt`, `zeta` (m) on `(time, z, lat, lon)`, z in
|
|
294
|
-
|
|
321
|
+
`salt`, `zeta` (m) on `(time, z, lat, lon)`, z in meters positive up, plus `w` (m/s,
|
|
322
|
+
upward; its origin in the `forcingkit_w_source` attribute).
|
|
323
|
+
|
|
324
|
+
`donor` names one parent model to use alone ("necofs", "hycom", "doppio", ...): the
|
|
325
|
+
service neither ranks nor falls back, and refuses an unknown id or a box outside that
|
|
326
|
+
model's domain. A store from any other donor (a service too old to know the option) is
|
|
327
|
+
refused here with ForcingkitError. Without it the service picks the donor. Further
|
|
328
|
+
`options` (`include_tides`, `allow_donor_fallback`, `cache_bust`, ...) pass through to
|
|
329
|
+
the service."""
|
|
295
330
|
payload = _ocean_request(
|
|
296
|
-
bbox,
|
|
331
|
+
bbox,
|
|
332
|
+
start,
|
|
333
|
+
hours,
|
|
334
|
+
vertical_spacing_m,
|
|
335
|
+
pad_cells,
|
|
336
|
+
sponge_cells,
|
|
337
|
+
donor=donor,
|
|
338
|
+
**options,
|
|
297
339
|
)
|
|
298
|
-
|
|
340
|
+
item = _check_donor(self._wait("/api/v1/jobs/obc", payload), donor)
|
|
341
|
+
return self.open_item(item)
|
|
299
342
|
|
|
300
343
|
def atmosphere(
|
|
301
344
|
self,
|
|
@@ -428,12 +471,21 @@ class AsyncClient(_Base):
|
|
|
428
471
|
vertical_spacing_m: float = 2.0,
|
|
429
472
|
pad_cells: int = 1,
|
|
430
473
|
sponge_cells: int = 0,
|
|
474
|
+
donor: Optional[str] = None,
|
|
431
475
|
**options: Any,
|
|
432
476
|
) -> xr.Dataset:
|
|
477
|
+
"""`Client.ocean` (including the `donor` option) for an asynchronous client."""
|
|
433
478
|
payload = _ocean_request(
|
|
434
|
-
bbox,
|
|
479
|
+
bbox,
|
|
480
|
+
start,
|
|
481
|
+
hours,
|
|
482
|
+
vertical_spacing_m,
|
|
483
|
+
pad_cells,
|
|
484
|
+
sponge_cells,
|
|
485
|
+
donor=donor,
|
|
486
|
+
**options,
|
|
435
487
|
)
|
|
436
|
-
item = await self._wait("/api/v1/jobs/obc", payload)
|
|
488
|
+
item = _check_donor(await self._wait("/api/v1/jobs/obc", payload), donor)
|
|
437
489
|
return await asyncio.to_thread(self.open_item, item)
|
|
438
490
|
|
|
439
491
|
async def atmosphere(
|
|
@@ -2,9 +2,9 @@
|
|
|
2
2
|
|
|
3
3
|
The service writes two parent-ocean layouts. Schema `z-v3` (streamed donors) is already what the
|
|
4
4
|
client returns: `u, v, temp, salt` on `(time, z, lat, lon)` with `z` in metres, positive up, and
|
|
5
|
-
longitudes in -180..180
|
|
6
|
-
|
|
7
|
-
0..360. `normalise` brings those to the z-v3 conventions and refuses any schema it does not know,
|
|
5
|
+
longitudes in -180..180; NECOFS stores add `w`, the upward velocity, on the same axes. Older
|
|
6
|
+
one-shot donors write `legacy` stores in the donor's own conventions: `depth` positive down,
|
|
7
|
+
`latitude`/`longitude` names, and for HYCOM longitudes in 0..360. `normalise` brings those to the z-v3 conventions and refuses any schema it does not know,
|
|
8
8
|
so a layout change fails here with a clear message instead of later on a variable name.
|
|
9
9
|
The atmosphere (`hrrr-atm-v1`) and waves (`nwps-waves-v1`) stores are written in that convention
|
|
10
10
|
too and pass through unchanged.
|
|
@@ -136,9 +136,14 @@ def interpolate_points(
|
|
|
136
136
|
|
|
137
137
|
|
|
138
138
|
def _provenance(prefix: str, ds: xr.Dataset) -> Dict[str, Any]:
|
|
139
|
+
"""The store's identity as trajectory attributes; an ocean store also names where its w
|
|
140
|
+
comes from (`forcingkit_ocean_w_source`: "model", "continuity" or "none")."""
|
|
141
|
+
keys = ["store_id", "store_version", "donor", "schema"]
|
|
142
|
+
if "forcingkit_w_source" in ds.attrs:
|
|
143
|
+
keys.append("w_source")
|
|
139
144
|
return {
|
|
140
145
|
f"forcingkit_{prefix}_{key}": ds.attrs.get(f"forcingkit_{key}", "")
|
|
141
|
-
for key in
|
|
146
|
+
for key in keys
|
|
142
147
|
}
|
|
143
148
|
|
|
144
149
|
|
|
@@ -17,15 +17,24 @@ STORE_ID = "obc_0123456789ab"
|
|
|
17
17
|
BBOX = (-70.9, 41.3, -70.6, 41.6)
|
|
18
18
|
|
|
19
19
|
|
|
20
|
-
def _item(
|
|
20
|
+
def _item(
|
|
21
|
+
base: str,
|
|
22
|
+
store_id: str = STORE_ID,
|
|
23
|
+
schema: str = "z-v3",
|
|
24
|
+
w_source: str | None = None,
|
|
25
|
+
donor: str = "necofs",
|
|
26
|
+
) -> dict:
|
|
27
|
+
props = {
|
|
28
|
+
"forcingkit:schema": schema,
|
|
29
|
+
"forcingkit:donor": donor,
|
|
30
|
+
"forcingkit:store_version": "1",
|
|
31
|
+
}
|
|
32
|
+
if w_source is not None:
|
|
33
|
+
props["forcingkit:w_source"] = w_source
|
|
21
34
|
return {
|
|
22
35
|
"type": "Feature",
|
|
23
36
|
"id": store_id,
|
|
24
|
-
"properties":
|
|
25
|
-
"forcingkit:schema": schema,
|
|
26
|
-
"forcingkit:donor": "necofs",
|
|
27
|
-
"forcingkit:store_version": "1",
|
|
28
|
-
},
|
|
37
|
+
"properties": props,
|
|
29
38
|
# The service names itself as it saw the request; the client re-roots on its own URL.
|
|
30
39
|
"assets": {
|
|
31
40
|
"zarr": {"href": f"http://elsewhere:9598/api/v1/stores/{store_id}.zarr"}
|
|
@@ -36,8 +45,16 @@ def _item(base: str, store_id: str = STORE_ID, schema: str = "z-v3") -> dict:
|
|
|
36
45
|
class FakeService:
|
|
37
46
|
"""/api/v1/jobs: queued, then running, then `final` ('succeeded' or 'failed')."""
|
|
38
47
|
|
|
39
|
-
def __init__(
|
|
48
|
+
def __init__(
|
|
49
|
+
self,
|
|
50
|
+
base: str,
|
|
51
|
+
final: str = "succeeded",
|
|
52
|
+
schema: str = "z-v3",
|
|
53
|
+
w_source: str | None = None,
|
|
54
|
+
donor: str = "necofs",
|
|
55
|
+
):
|
|
40
56
|
self.base, self.final, self.schema = base, final, schema
|
|
57
|
+
self.w_source, self.donor = w_source, donor
|
|
41
58
|
self.posts: list[dict] = []
|
|
42
59
|
self.polls = 0
|
|
43
60
|
|
|
@@ -48,7 +65,9 @@ class FakeService:
|
|
|
48
65
|
"status": status,
|
|
49
66
|
"error": "All OBC donors failed" if status == "failed" else None,
|
|
50
67
|
"status_url": f"http://elsewhere:9598/api/v1/jobs/{STORE_ID}",
|
|
51
|
-
"result": _item(
|
|
68
|
+
"result": _item(
|
|
69
|
+
self.base, schema=self.schema, w_source=self.w_source, donor=self.donor
|
|
70
|
+
)
|
|
52
71
|
if status == "succeeded"
|
|
53
72
|
else None,
|
|
54
73
|
}
|
|
@@ -219,6 +238,21 @@ def test_interpolate_trajectory_through_client(store_server, tmp_path):
|
|
|
219
238
|
)
|
|
220
239
|
assert ds.attrs["forcingkit_ocean_store_id"] == STORE_ID
|
|
221
240
|
assert ds.attrs["featureType"] == "trajectory"
|
|
241
|
+
# A service older than `forcingkit:w_source` leaves the attribute out.
|
|
242
|
+
assert "forcingkit_ocean_w_source" not in ds.attrs
|
|
243
|
+
|
|
244
|
+
|
|
245
|
+
@pytest.mark.parametrize("w_source", ["model", "continuity"])
|
|
246
|
+
def test_trajectory_names_where_w_comes_from(store_server, tmp_path, w_source):
|
|
247
|
+
root, base, _ = store_server
|
|
248
|
+
_publish(root, z_v3_store())
|
|
249
|
+
fk = _client(base, FakeService(base, w_source=w_source), tmp_path)
|
|
250
|
+
ocean = fk.ocean(BBOX, "2026-06-01T00:00:00Z", 6)
|
|
251
|
+
assert ocean.attrs["forcingkit_w_source"] == w_source
|
|
252
|
+
ds = fk.interpolate_trajectory(
|
|
253
|
+
np.array([-70.85]), np.array([41.35]), T0 + pd.to_timedelta([1.5], unit="h")
|
|
254
|
+
)
|
|
255
|
+
assert ds.attrs["forcingkit_ocean_w_source"] == w_source
|
|
222
256
|
|
|
223
257
|
|
|
224
258
|
def _waves_store() -> xr.Dataset:
|
|
@@ -291,3 +325,48 @@ async def test_async_waves(store_server, tmp_path):
|
|
|
291
325
|
ds = await fk.waves(BBOX, T0, 2)
|
|
292
326
|
assert service.posts[0]["wfo"] == "okx" and service.posts[0]["grid"] == "CG1"
|
|
293
327
|
assert float(ds["dir"].values[0, 1, 1]) == 90.0
|
|
328
|
+
|
|
329
|
+
|
|
330
|
+
def test_ocean_names_a_donor_only_when_asked(store_server, tmp_path):
|
|
331
|
+
root, base, _ = store_server
|
|
332
|
+
_publish(root, z_v3_store())
|
|
333
|
+
service = FakeService(base, donor="doppio")
|
|
334
|
+
fk = _client(base, service, tmp_path)
|
|
335
|
+
ds = fk.ocean(BBOX, "2026-06-01T00:00:00Z", 6, donor="doppio")
|
|
336
|
+
assert service.posts[0]["donor"] == "doppio"
|
|
337
|
+
assert ds.attrs["forcingkit_donor"] == "doppio"
|
|
338
|
+
fk.ocean(BBOX, "2026-06-01T00:00:00Z", 6)
|
|
339
|
+
assert "donor" not in service.posts[1]
|
|
340
|
+
|
|
341
|
+
|
|
342
|
+
def test_ocean_refuses_a_store_from_another_donor(store_server, tmp_path):
|
|
343
|
+
"""A service older than the donor option ignores it and ranks the donors."""
|
|
344
|
+
root, base, _ = store_server
|
|
345
|
+
_publish(root, z_v3_store())
|
|
346
|
+
fk = _client(base, FakeService(base, donor="necofs"), tmp_path)
|
|
347
|
+
with pytest.raises(ForcingkitError, match="asked for donor 'doppio'") as err:
|
|
348
|
+
fk.ocean(BBOX, "2026-06-01T00:00:00Z", 6, donor="doppio")
|
|
349
|
+
assert err.value.detail == {"requested": "doppio", "delivered": "necofs"}
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def test_async_ocean_passes_the_donor(store_server, tmp_path):
|
|
353
|
+
import asyncio
|
|
354
|
+
|
|
355
|
+
root, base, _ = store_server
|
|
356
|
+
_publish(root, z_v3_store())
|
|
357
|
+
service = FakeService(base, donor="doppio")
|
|
358
|
+
|
|
359
|
+
async def run():
|
|
360
|
+
fk = AsyncClient(
|
|
361
|
+
base,
|
|
362
|
+
cache_dir=tmp_path / "cache",
|
|
363
|
+
poll_s=0,
|
|
364
|
+
http=httpx.AsyncClient(transport=httpx.MockTransport(service)),
|
|
365
|
+
)
|
|
366
|
+
return await fk.ocean(BBOX, "2026-06-01T00:00:00Z", 6, donor="doppio")
|
|
367
|
+
|
|
368
|
+
ds = asyncio.run(run())
|
|
369
|
+
assert (
|
|
370
|
+
service.posts[0]["donor"] == "doppio"
|
|
371
|
+
and ds.attrs["forcingkit_donor"] == "doppio"
|
|
372
|
+
)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|