Skip to content

Commit 66ca90d

Browse files
authored
fix: defer get_variable to xarray.io.Reader (#107)
1 parent 5e3f67e commit 66ca90d

File tree

9 files changed

+39
-29
lines changed

9 files changed

+39
-29
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
## unreleased
44

5-
* nothing
5+
* fix: actually use `sel_method` when loading the variable by @hrodmn ([#107](https://github.com/developmentseed/titiler-multidim/pull/107))
66

77
# 0.7.0
88

pyproject.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,3 +153,4 @@ excludes = ["tests/", "**/.mypy_cache", "**/.DS_Store"]
153153
[tool.pytest.ini_options]
154154
addopts = "--cov=titiler.multidim --cov-report term-missing -s -vv"
155155
pythonpath = ["src", "tests"]
156+
norecursedirs = ["infrastructure"]

src/titiler/multidim/reader.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,17 @@
11
"""XarrayReader"""
22

3-
import pickle
43
import os
5-
from typing import Any, List, Optional, Dict
4+
import pickle
5+
from typing import Any, Dict, List, Optional
66
from urllib.parse import urlparse
77

8-
98
import attr
109
import xarray as xr
1110
from pydantic_settings import BaseSettings
11+
from titiler.xarray.io import Reader, xarray_open_dataset
1212

1313
from titiler.multidim.redis_pool import get_redis
1414
from titiler.multidim.settings import ApiSettings
15-
from titiler.xarray.io import Reader, get_variable, xarray_open_dataset
1615

1716
try:
1817
import icechunk
@@ -231,11 +230,6 @@ def __attrs_post_init__(self):
231230
print(f"Adding dataset in Cache: {cache_key}")
232231
cache_client.set(cache_key, data_bytes, ex=300)
233232

234-
self.input = get_variable(
235-
self.ds,
236-
self.variable,
237-
sel=self.sel,
238-
)
239233
super().__attrs_post_init__()
240234

241235
@classmethod

tests/fixtures/generate_test_netcdf.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
"""Create NetCDF fixture."""
22

33
import numpy as np
4-
from netCDF4 import Dataset
4+
from netCDF4 import Dataset, date2num
5+
from datetime import datetime, timedelta
56

67
# File name
78
filename = "testfile.nc"
@@ -35,7 +36,16 @@
3536
)
3637

3738
# Fill variables with data
38-
times[:] = np.arange(5)
39+
# Create actual datetime values starting from 2020-01-01 with daily intervals
40+
start_date = datetime(2020, 1, 1)
41+
time_values = [start_date + timedelta(days=i) for i in range(5)]
42+
units = "days since 1970-01-01 00:00:00"
43+
calendar = "gregorian"
44+
times[:] = date2num(time_values, units=units, calendar=calendar)
45+
times.units = units
46+
times.calendar = calendar
47+
times.standard_name = "time"
48+
3949
lats[:] = np.linspace(lat_min + res / 2, lat_max - res / 2, lat_n)
4050
lons[:] = np.linspace(lon_min + res / 2, lon_max - res / 2, lon_n)
4151
data[:, :, :] = np.random.randint(-128, 127, size=(5, lat_n, lon_n), dtype=np.int8)
Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,42 @@
11
[
22
{
33
"bucket": [-128.0, -102.5999984741211],
4-
"value": 6598
4+
"value": 6653
55
},
66
{
77
"bucket": [-102.5999984741211, -77.19999694824219],
8-
"value": 6254
8+
"value": 6327
99
},
1010
{
1111
"bucket": [-77.19999694824219, -51.80000305175781],
12-
"value": 6562
12+
"value": 6644
1313
},
1414
{
1515
"bucket": [-51.80000305175781, -26.400001525878906],
16-
"value": 6446
16+
"value": 6312
1717
},
1818
{
1919
"bucket": [-26.400001525878906, -1.0],
20-
"value": 6235
20+
"value": 6369
2121
},
2222
{
2323
"bucket": [-1.0, 24.399993896484375],
24-
"value": 6658
24+
"value": 6508
2525
},
2626
{
2727
"bucket": [24.399993896484375, 49.80000305175781],
28-
"value": 6428
28+
"value": 6411
2929
},
3030
{
3131
"bucket": [49.80000305175781, 75.19999694824219],
32-
"value": 6670
32+
"value": 6749
3333
},
3434
{
3535
"bucket": [75.19999694824219, 100.59999084472656],
36-
"value": 6257
36+
"value": 6320
3737
},
3838
{
3939
"bucket": [100.59999084472656, 126.0],
40-
"value": 6692
40+
"value": 6507
4141
}
4242
]

tests/fixtures/responses/testfile_nc_info.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"bounds": [-180.0, -90.0, 180.0, 90.0],
33
"crs": "http://www.opengis.net/def/crs/EPSG/0/4326",
44
"band_metadata": [["b1", {}]],
5-
"band_descriptions": [["b1", "0.0"]],
5+
"band_descriptions": [["b1", "2020-01-01T00:00:00.000000000"]],
66
"dtype": "float32",
77
"nodata_type": "None",
88
"name": "data",

tests/fixtures/responses/testfile_nc_tilejson.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
"version": "1.0.0",
44
"scheme": "xyz",
55
"tiles": [
6-
"http://testserver/tiles/WebMercatorQuad/{z}/{x}/{y}@1x?url=tests%2Ffixtures%2Ftestfile.nc&variable=data&decode_times=false&sel=time%3D0"
6+
"http://testserver/tiles/WebMercatorQuad/{z}/{x}/{y}@1x?url=tests%2Ffixtures%2Ftestfile.nc&variable=data&decode_times=true&sel=time%3D2020-01-01"
77
],
88
"minzoom": 0,
99
"maxzoom": 0,
1010
"bounds": [-180.0, -90.0, 180.0, 90.0],
1111
"center": [0.0, 0.0, 0]
1212
}
13-

tests/fixtures/testfile.nc

-153 Bytes
Binary file not shown.

tests/test_app.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import json
22
import os
3-
import pytest
43

4+
import pytest
55
from helpers import find_string_in_stream
66

7-
87
DATA_DIR = "tests/fixtures"
98
test_zarr_store_v2 = os.path.join(DATA_DIR, "zarr_store_v2.zarr")
109
test_zarr_store_v3 = os.path.join(DATA_DIR, "zarr_store_v3.zarr")
@@ -71,8 +70,8 @@
7170
"params": {
7271
"url": test_netcdf_store,
7372
"variable": "data",
74-
"decode_times": False,
75-
"sel": "time=0",
73+
"decode_times": True,
74+
"sel": "time=2020-01-01",
7675
},
7776
"variables": ["data"],
7877
}
@@ -233,3 +232,10 @@ def test_map_with_params(store_params, app):
233232
assert response.status_code == 200
234233
assert response.headers["Content-Type"] == "text/html; charset=utf-8"
235234
assert find_string_in_stream(response, '<div id="map"></div>')
235+
236+
237+
def test_sel_nearest_netcdf(app):
238+
params = store_params["netcdf_store"]["params"].copy()
239+
params.update({"sel": "time=2020-01-06", "sel_method": "nearest"})
240+
response = app.get("/info", params=params)
241+
assert response.status_code == 200

0 commit comments

Comments
 (0)