Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

open_data with time_range doesn't work with dataset id cmems_mod_glo_wav_anfc_0.083deg_PT3H-i #22

Open
TejasMorbagal opened this issue Mar 2, 2023 · 1 comment

Comments

@TejasMorbagal
Copy link
Collaborator

The following example doesn't work

ds = cmems_store.open_data('cmems_mod_glo_wav_anfc_0.083deg_PT3H-i','dataset:zarr:cmems',variable_names=['VMDR'],time_range=('2022-01-01','2022-01-02'))
ds
--------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
Cell In[11], line 1
----> 1 ds = cmems_store.open_data('cmems_mod_glo_wav_anfc_0.083deg_PT3H-i','dataset:zarr:cmems',variable_names=['VMDR'],time_range=('2022-01-01','2022-01-02'))
      2 ds

File ~/micromamba/envs/xcube-micromamba/lib/python3.9/site-packages/xcube_cmems/store.py:350, in CmemsDataStore.open_data(self, data_id, opener_id, **open_params)
    348 def open_data(self, data_id: str, opener_id: str = None,
    349               **open_params) -> Any:
--> 350     return self._get_opener(opener_id=opener_id).open_data(data_id,
    351                                                            **open_params)

File ~/micromamba/envs/xcube-micromamba/lib/python3.9/site-packages/xcube_cmems/store.py:250, in CmemsDataOpener.open_data(self, data_id, **open_params)
    248 ds = self.open_dataset(data_id)
    249 if open_params:
--> 250     ds = self.subset_cube_with_open_params(ds, **open_params)
    251 return ds

File ~/micromamba/envs/xcube-micromamba/lib/python3.9/site-packages/xcube_cmems/store.py:214, in CmemsDataOpener.subset_cube_with_open_params(ds, **open_params)
    210 @staticmethod
    211 def subset_cube_with_open_params(ds: xr.Dataset,
    212                                  **open_params) -> xr.Dataset:
    213     if 'time_range' in open_params:
--> 214         ds = ds.sel(time=slice(open_params.get('time_range')[0],
    215                                open_params.get('time_range')[1]))
    216     if 'bbox' in open_params:
    217         x_y_var_name_pairs = [
    218             {'y': 'latitude', 'x': 'longitude'},
    219             {'y': 'lat', 'x': 'lon'},
    220             {'y': 'y', 'x': 'x'}
    221         ]

File ~/micromamba/envs/xcube-micromamba/lib/python3.9/site-packages/xarray/core/dataset.py:2573, in Dataset.sel(self, indexers, method, tolerance, drop, **indexers_kwargs)
   2512 """Returns a new dataset with each array indexed by tick labels
   2513 along the specified dimension(s).
   2514 
   (...)
   2570 DataArray.sel
   2571 """
   2572 indexers = either_dict_or_kwargs(indexers, indexers_kwargs, "sel")
-> 2573 query_results = map_index_queries(
   2574     self, indexers=indexers, method=method, tolerance=tolerance
   2575 )
   2577 if drop:
   2578     no_scalar_variables = {}

File ~/micromamba/envs/xcube-micromamba/lib/python3.9/site-packages/xarray/core/indexing.py:188, in map_index_queries(obj, indexers, method, tolerance, **indexers_kwargs)
    186         results.append(IndexSelResult(labels))
    187     else:
--> 188         results.append(index.sel(labels, **options))
    190 merged = merge_sel_results(results)
    192 # drop dimension coordinates found in dimension indexers
    193 # (also drop multi-index if any)
    194 # (.sel() already ensures alignment)

File ~/micromamba/envs/xcube-micromamba/lib/python3.9/site-packages/xarray/core/indexes.py:446, in PandasIndex.sel(self, labels, method, tolerance)
    443 coord_name, label = next(iter(labels.items()))
    445 if isinstance(label, slice):
--> 446     indexer = _query_slice(self.index, label, coord_name, method, tolerance)
    447 elif is_dict_like(label):
    448     raise ValueError(
    449         "cannot use a dict-like object for selection on "
    450         "a dimension that does not have a MultiIndex"
    451     )

File ~/micromamba/envs/xcube-micromamba/lib/python3.9/site-packages/xarray/core/indexes.py:219, in _query_slice(index, label, coord_name, method, tolerance)
    211 indexer = index.slice_indexer(
    212     _sanitize_slice_element(label.start),
    213     _sanitize_slice_element(label.stop),
    214     _sanitize_slice_element(label.step),
    215 )
    216 if not isinstance(indexer, slice):
    217     # unlike pandas, in xarray we never want to silently convert a
    218     # slice indexer into an array indexer
--> 219     raise KeyError(
    220         "cannot represent labeled-based slice indexer for coordinate "
    221         f"{coord_name!r} with a slice over integer positions; the index is "
    222         "unsorted or non-unique"
    223     )
    224 return indexer

KeyError: "cannot represent labeled-based slice indexer for coordinate 'time' with a slice over integer positions; the index is unsorted or non-unique"

@pont-us
Copy link
Member

pont-us commented Mar 2, 2023

By the way, I also encountered this error while reviewing the CMEMS JNB update PR in xcube a couple of weeks ago (see xcube-dev/xcube#805 (review) ), and found it was because the CMEMS backend had started returning duplicate time-steps for the dataset in question. This seems to me like a bug on their side, but I don't know what the desired behaviour for xcube-cmems should be when it encounters something like this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants