Skip to content

Commit 57b44d6

Browse files
committed
2 parents 45e060b + 32941dc commit 57b44d6

File tree

5 files changed

+64
-10
lines changed

5 files changed

+64
-10
lines changed

CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
* add point value query on right-click to map viewer (@hrodmn, https://github.com/developmentseed/titiler/pull/1100)
1010

11+
### titiler.xarray
12+
13+
* change `get_variable.drop_dim` parameter type from `str` to `List[str]` **breaking change**
14+
1115
## 0.21.1 (2025-01-29)
1216

1317
### titiler.core

src/titiler/xarray/tests/test_dependencies.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,24 @@ def tiles(
4444
response = client.get("/tiles/1/2/3", params={"variable": "yo"})
4545
params = response.json()
4646
assert params == {"variable": "yo"}
47+
48+
response = client.get("/tiles/1/2/3", params={"drop_dim": "yo=yo"})
49+
params = response.json()
50+
assert params == {"drop_dim": ["yo=yo"]}
51+
52+
response = client.get("/tiles/1/2/3", params={"drop_dim": "yo=1.0"})
53+
params = response.json()
54+
assert params == {"drop_dim": ["yo=1.0"]}
55+
56+
response = client.get("/tiles/1/2/3", params={"drop_dim": ["yo=yo", "ye=ye"]})
57+
params = response.json()
58+
assert params == {"drop_dim": ["yo=yo", "ye=ye"]}
59+
60+
response = client.get("/tiles/1/2/3", params={"drop_dim": "yo"})
61+
assert response.status_code == 422
62+
63+
response = client.get("/tiles/1/2/3", params={"drop_dim": "=yo"})
64+
assert response.status_code == 422
65+
66+
response = client.get("/tiles/1/2/3", params={"drop_dim": "yo="})
67+
assert response.status_code == 422

src/titiler/xarray/tests/test_io_tools.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,27 @@ def test_get_variable():
8787
with pytest.raises(AssertionError):
8888
get_variable(ds, "dataset")
8989

90-
da = get_variable(ds, "dataset", drop_dim="universe=somewhere")
90+
da = get_variable(ds, "dataset", drop_dim=["universe=somewhere"])
91+
assert da.rio.crs
92+
assert da.dims == ("z", "y", "x")
93+
94+
# 5D dataset - drop time dim
95+
arr = numpy.arange(0, 33 * 35 * 2).reshape(2, 1, 1, 33, 35)
96+
data = xarray.DataArray(
97+
arr,
98+
dims=("time", "universe", "z", "y", "x"),
99+
coords={
100+
"x": numpy.arange(-170, 180, 10),
101+
"y": numpy.arange(-80, 85, 5),
102+
"z": [0],
103+
"universe": ["somewhere"],
104+
"time": [datetime(2022, 1, 1), datetime(2023, 1, 1)],
105+
},
106+
)
107+
108+
da = get_variable(
109+
ds, "dataset", drop_dim=["universe=somewhere", "time=2022-01-01T00:00:00"]
110+
)
91111
assert da.rio.crs
92112
assert da.dims == ("z", "y", "x")
93113

src/titiler/xarray/titiler/xarray/dependencies.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
"""titiler.xarray dependencies."""
22

33
from dataclasses import dataclass
4-
from typing import Optional, Union
4+
from typing import List, Optional, Union
55

66
import numpy
77
from fastapi import Query
8+
from pydantic.types import StringConstraints
89
from rio_tiler.types import RIOResampling, WarpResampling
910
from typing_extensions import Annotated
1011

@@ -33,15 +34,20 @@ class XarrayIOParams(DefaultDependency):
3334
# cache_client
3435

3536

37+
DropDimStr = Annotated[str, StringConstraints(pattern=r"^[^=]+=[^=]+$")]
38+
39+
3640
@dataclass
3741
class XarrayDsParams(DefaultDependency):
3842
"""Xarray Dataset Options."""
3943

4044
variable: Annotated[str, Query(description="Xarray Variable name")]
4145

4246
drop_dim: Annotated[
43-
Optional[str],
44-
Query(description="Dimension to drop"),
47+
Optional[List[DropDimStr]],
48+
Query(
49+
description="Dimensions to drop in form of `{dimension}={value}`",
50+
),
4551
] = None
4652

4753
datetime: Annotated[
@@ -68,8 +74,10 @@ class CompatXarrayParams(XarrayIOParams):
6874
variable: Annotated[Optional[str], Query(description="Xarray Variable name")] = None
6975

7076
drop_dim: Annotated[
71-
Optional[str],
72-
Query(description="Dimension to drop"),
77+
Optional[List[DropDimStr]],
78+
Query(
79+
description="Dimensions to drop in form of `{dimension}={value}`",
80+
),
7381
] = None
7482

7583
datetime: Annotated[

src/titiler/xarray/titiler/xarray/io.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,15 +139,15 @@ def get_variable(
139139
ds: xarray.Dataset,
140140
variable: str,
141141
datetime: Optional[str] = None,
142-
drop_dim: Optional[str] = None,
142+
drop_dim: Optional[List[str]] = None,
143143
) -> xarray.DataArray:
144144
"""Get Xarray variable as DataArray.
145145
146146
Args:
147147
ds (xarray.Dataset): Xarray Dataset.
148148
variable (str): Variable to extract from the Dataset.
149149
datetime (str, optional): datetime to select from the DataArray.
150-
drop_dim (str, optional): DataArray dimension to drop in form of `{dimension}={value}`.
150+
drop_dim (list of str, optional): DataArray dimension to drop in form of `[{dimension}={value}],`.
151151
152152
Returns:
153153
xarray.DataArray: 2D or 3D DataArray.
@@ -156,8 +156,9 @@ def get_variable(
156156
da = ds[variable]
157157

158158
if drop_dim:
159-
dim_to_drop, dim_val = drop_dim.split("=")
160-
da = da.sel({dim_to_drop: dim_val}).drop_vars(dim_to_drop)
159+
for dim in drop_dim:
160+
dim_to_drop, dim_val = dim.split("=")
161+
da = da.sel({dim_to_drop: dim_val}).drop_vars(dim_to_drop)
161162

162163
da = _arrange_dims(da)
163164

0 commit comments

Comments
 (0)