Skip to content

Commit 784cb7c

Browse files
committed
Small usability improvements
1 parent 1696b5a commit 784cb7c

1 file changed

Lines changed: 35 additions & 8 deletions

File tree

esmvalcore/io/xcube.py

Lines changed: 35 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import copy
1616
import fnmatch
17+
import logging
1718
from dataclasses import dataclass, field
1819
from typing import TYPE_CHECKING, Any
1920

@@ -28,6 +29,14 @@
2829
from esmvalcore.typing import Facets, FacetValue
2930

3031

32+
logger = logging.getLogger(__name__)
33+
34+
FREQUENCIES = {
35+
"P1M": "mon",
36+
"P1D": "day",
37+
}
38+
39+
3140
@dataclass
3241
class XCubeDataset(esmvalcore.io.protocol.DataElement):
3342
"""A dataset that can be used to load data found using xcube_."""
@@ -133,7 +142,7 @@ class XCubeDataSource(esmvalcore.io.protocol.DataSource):
133142
open_params: dict[str, Any] = field(default_factory=dict, repr=False)
134143
"""Parameters to use when opening the data."""
135144

136-
def find_data(self, **facets: FacetValue) -> list[XCubeDataset]: # noqa: C901
145+
def find_data(self, **facets: FacetValue) -> list[XCubeDataset]: # noqa: C901,PLR0912
137146
# TODO: fix complexity
138147
"""Find data.
139148
@@ -153,12 +162,13 @@ def find_data(self, **facets: FacetValue) -> list[XCubeDataset]: # noqa: C901
153162
)
154163
result = []
155164
requested_short_names = facets.get("short_name", "*")
156-
if isinstance(requested_short_names, str | int):
165+
if isinstance(requested_short_names, str | int | float):
157166
requested_short_names = [str(requested_short_names)]
158167
requested_datasets = facets.get("dataset", "*")
159-
if isinstance(requested_datasets, str | int):
168+
if isinstance(requested_datasets, str | int | float):
160169
requested_datasets = [str(requested_datasets)]
161170
available_datasets = store.list_data_ids()
171+
162172
for data_id in available_datasets:
163173
for dataset_pattern in requested_datasets:
164174
if fnmatch.fnmatchcase(data_id, dataset_pattern):
@@ -170,6 +180,13 @@ def find_data(self, **facets: FacetValue) -> list[XCubeDataset]: # noqa: C901
170180
for short_name_pattern in requested_short_names
171181
if fnmatch.fnmatchcase(short_name, short_name_pattern)
172182
]
183+
if not short_names:
184+
logger.debug(
185+
"No variable matching % found in %s. Available variables are: %s",
186+
requested_short_names,
187+
data_id,
188+
available_short_names,
189+
)
173190
# TODO: Maybe this is too complicated and we should only
174191
# decide which variables to keep/drop after load and conversion
175192
# to iris cube.
@@ -198,12 +215,14 @@ def find_data(self, **facets: FacetValue) -> list[XCubeDataset]: # noqa: C901
198215
"-",
199216
"",
200217
)
201-
frequencies = {
202-
"P1M": "mon",
203-
}
204-
frequency = frequencies[
205-
description.attrs["time_coverage_resolution"]
218+
native_frequency = description.attrs[
219+
"time_coverage_resolution"
206220
]
221+
frequency = FREQUENCIES.get(
222+
native_frequency,
223+
native_frequency,
224+
)
225+
207226
dataset = XCubeDataset(
208227
name=data_id,
209228
facets={
@@ -221,4 +240,12 @@ def find_data(self, **facets: FacetValue) -> list[XCubeDataset]: # noqa: C901
221240

222241
result.append(dataset)
223242

243+
if not result:
244+
logger.debug(
245+
"No datasets matching %s found in %s. Available datasets are: %s",
246+
requested_datasets,
247+
self.data_store_id,
248+
available_datasets,
249+
)
250+
224251
return result

0 commit comments

Comments
 (0)