Skip to content

Commit 8c0d8d9

Browse files
committed
Support boolean-valued include_attrs in get_data_ids in accordance with API update in xcube 1.8.0
1 parent 71563c7 commit 8c0d8d9

File tree

1 file changed

+21
-10
lines changed

1 file changed

+21
-10
lines changed

xcube_cmems/store.py

Lines changed: 21 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2020
# SOFTWARE.
2121

22-
from typing import Any, List, Tuple, Container, Union, Iterator, Dict
22+
from typing import Any, List, Tuple, Container, Iterator, Dict, Optional
2323

2424
import logging
2525
import xarray as xr
@@ -87,19 +87,18 @@ def _get_var_descriptors(
8787
return var_descriptors
8888

8989
@staticmethod
90-
def _determine_time_period(data: xr.Dataset):
90+
def _determine_time_period(data: xr.Dataset) -> Optional[str]:
9191
if "time" in data and len(data["time"].values) > 1:
92-
time_diff = (
93-
data["time"].diff(dim=data["time"].dims[0]).values.astype(np.float64)
94-
)
92+
time_diff = data["time"].diff(dim=data["time"].dims[0]).values
9593
time_res = time_diff[0]
96-
time_regular = np.allclose(time_res, time_diff, 1e-8)
94+
time_regular = np.allclose(time_diff, time_res, rtol=1e-8, atol=0)
9795
if time_regular:
9896
time_period = pd.to_timedelta(time_res).isoformat()
9997
# remove leading P
10098
time_period = time_period[1:]
10199
# removing sub-day precision
102100
return time_period.split("T")[0]
101+
return None
103102

104103
def describe_data(self, data_id: str) -> DatasetDescriptor:
105104
xr_ds = self.cmems.open_dataset(data_id)
@@ -228,11 +227,23 @@ def _get_opener(
228227
return self._dataset_opener
229228

230229
def get_data_ids(
231-
self, data_type: DataTypeLike = None, include_attrs: Container[str] = None
232-
) -> Union[Iterator[str], Iterator[Tuple[str, Dict[str, Any]]]]:
230+
self,
231+
data_type: DataTypeLike = None,
232+
include_attrs: Container[str] | bool = False,
233+
) -> Iterator[str] | Iterator[Tuple[str, Dict[str, Any]]]:
234+
233235
dataset_ids_with_titles = self._dataset_opener.cmems.get_datasets_with_titles()
234-
return_tuples = include_attrs is not None
235-
include_titles = return_tuples and "title" in include_attrs
236+
237+
if isinstance(include_attrs, bool):
238+
return_tuples = include_attrs
239+
include_titles = include_attrs
240+
elif isinstance(include_attrs, Container):
241+
return_tuples = True
242+
include_titles = "title" in include_attrs
243+
else:
244+
raise ValueError(
245+
f"Invalid type {type(include_attrs)} for include_attrs"
246+
)
236247

237248
for dataset in dataset_ids_with_titles:
238249
data_id = dataset["dataset_id"]

0 commit comments

Comments
 (0)