|
19 | 19 | # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
20 | 20 | # SOFTWARE. |
21 | 21 |
|
22 | | -from typing import Any, List, Tuple, Container, Union, Iterator, Dict |
| 22 | +from typing import Any, List, Tuple, Container, Iterator, Dict, Optional |
23 | 23 |
|
24 | 24 | import logging |
25 | 25 | import xarray as xr |
@@ -87,19 +87,18 @@ def _get_var_descriptors( |
87 | 87 | return var_descriptors |
88 | 88 |
|
89 | 89 | @staticmethod |
90 | | - def _determine_time_period(data: xr.Dataset): |
| 90 | + def _determine_time_period(data: xr.Dataset) -> Optional[str]: |
91 | 91 | 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 |
95 | 93 | 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) |
97 | 95 | if time_regular: |
98 | 96 | time_period = pd.to_timedelta(time_res).isoformat() |
99 | 97 | # remove leading P |
100 | 98 | time_period = time_period[1:] |
101 | 99 | # removing sub-day precision |
102 | 100 | return time_period.split("T")[0] |
| 101 | + return None |
103 | 102 |
|
104 | 103 | def describe_data(self, data_id: str) -> DatasetDescriptor: |
105 | 104 | xr_ds = self.cmems.open_dataset(data_id) |
@@ -228,11 +227,23 @@ def _get_opener( |
228 | 227 | return self._dataset_opener |
229 | 228 |
|
230 | 229 | 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 | + |
233 | 235 | 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 | + ) |
236 | 247 |
|
237 | 248 | for dataset in dataset_ids_with_titles: |
238 | 249 | data_id = dataset["dataset_id"] |
|
0 commit comments