-
Notifications
You must be signed in to change notification settings - Fork 44
Add xcube support #2917
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
Draft
bouweandela
wants to merge
5
commits into
main
Choose a base branch
from
add-xcube-support
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Add xcube support #2917
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,3 +21,4 @@ Submodules | |
| esmvalcore.io.intake_esgf | ||
| esmvalcore.io.local | ||
| esmvalcore.io.protocol | ||
| esmvalcore.io.xcube | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| esmvalcore.io.xcube | ||
| =================== | ||
|
|
||
| .. automodule:: esmvalcore.io.xcube | ||
| :no-inherited-members: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| # Read data from the ESA Climate Data Centre (ESA CCI) using xcube. | ||
| # More information available at | ||
| # https://xcube.readthedocs.io/en/latest/dataaccess.html#esa-climate-data-centre-esa-cci-cciodp-ccizarr-esa-cci-kc. | ||
| projects: | ||
| external: | ||
| data: | ||
| ccizarr: | ||
| type: "esmvalcore.io.xcube.XCubeDataSource" | ||
| data_store_id: "ccizarr" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,224 @@ | ||
| """Access data using `xcube <https://xcube.readthedocs.io>`_. | ||
|
|
||
| Run the command ``esmvaltool config copy data-xcube-ccizarr.yml`` to update | ||
| your :ref:`configuration <config-data-sources>` to use this module. This will | ||
| create a file with the following content in your configuration directory: | ||
|
|
||
| .. literalinclude:: ../configurations/data-xcube-ccizarr.yml | ||
| :language: yaml | ||
| :caption: Contents of ``data-xcube-ccizarr.yml`` | ||
|
|
||
| """ | ||
|
|
||
| from __future__ import annotations | ||
|
|
||
| import copy | ||
| import fnmatch | ||
| from dataclasses import dataclass, field | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| import iris.cube | ||
| import iris.std_names | ||
| import xcube.core.store | ||
|
|
||
| import esmvalcore.io.protocol | ||
| from esmvalcore.iris_helpers import dataset_to_iris | ||
|
|
||
| if TYPE_CHECKING: | ||
| from esmvalcore.typing import Facets, FacetValue | ||
|
|
||
|
|
||
| @dataclass | ||
| class XCubeDataset(esmvalcore.io.protocol.DataElement): | ||
| """A dataset that can be used to load data found using xcube_.""" | ||
|
|
||
| name: str | ||
| """A unique name identifying the data.""" | ||
|
|
||
| facets: Facets = field(repr=False) | ||
| """Facets are key-value pairs that were used to find this data.""" | ||
|
|
||
| store: xcube.core.store.store.DataStore = field(repr=False) | ||
| """The store containing the data.""" | ||
|
|
||
| open_params: dict[str, Any] = field(default_factory=dict, repr=False) | ||
| """Parameters to use when opening the data.""" | ||
|
|
||
| _attributes: dict[str, Any] | None = field( | ||
| init=False, | ||
| repr=False, | ||
| default=None, | ||
| ) | ||
|
|
||
| def __hash__(self) -> int: | ||
| """Return a number uniquely representing the data element.""" | ||
| return hash((self.name, self.facets.get("version"))) | ||
|
|
||
| def prepare(self) -> None: | ||
| """Prepare the data for access.""" | ||
| self.store.preload_data(self.name) | ||
|
|
||
| @property | ||
| def attributes(self) -> dict[str, Any]: | ||
| """Attributes are key-value pairs describing the data.""" | ||
| if self._attributes is None: | ||
| msg = ( | ||
| "Attributes have not been read yet. Call the `to_iris` method " | ||
| "first to read the attributes from the file." | ||
| ) | ||
| raise ValueError(msg) | ||
| return self._attributes | ||
|
|
||
| @attributes.setter | ||
| def attributes(self, value: dict[str, Any]) -> None: | ||
| self._attributes = value | ||
|
|
||
| def to_iris(self) -> iris.cube.CubeList: | ||
| """Load the data as Iris cubes. | ||
|
|
||
| Returns | ||
| ------- | ||
| : | ||
| The loaded data. | ||
| """ | ||
| dataset = self.store.open_data(self.name, **self.open_params) | ||
| # Keep only variables matching the "short_name" facet. | ||
| short_names = self.facets.get("short_name", []) | ||
| if isinstance(short_names, str | int): | ||
| short_names = [str(short_names)] | ||
| if short_names: | ||
| dataset = dataset[short_names] | ||
|
|
||
| # Drop invalid standard_names. | ||
| # TODO: move this to a standalone fixes package. | ||
| for data_var in dataset.data_vars.values(): | ||
| if ( | ||
| "standard_name" in data_var.attrs | ||
| and data_var.attrs["standard_name"] | ||
| not in iris.std_names.STD_NAMES | ||
| ): | ||
| data_var.attrs.pop("standard_name") | ||
|
|
||
| # Cache the attributes. | ||
| self.attributes = copy.deepcopy(dataset.attrs) | ||
| return dataset_to_iris(dataset) | ||
|
|
||
|
|
||
| @dataclass | ||
| class XCubeDataSource(esmvalcore.io.protocol.DataSource): | ||
| """Data source for finding files on a local filesystem.""" | ||
|
|
||
| name: str | ||
| """A name identifying the data source.""" | ||
|
|
||
| project: str | ||
| """The project that the data source provides data for.""" | ||
|
|
||
| priority: int | ||
| """The priority of the data source. Lower values have priority.""" | ||
|
|
||
| debug_info: str = field(init=False, repr=False, default="") | ||
| """A string containing debug information when no data is found.""" | ||
|
|
||
| data_store_id: str | ||
| """Name of the data store. | ||
|
|
||
| A list of available data stores can be found in the `xcube documentation | ||
| <https://xcube.readthedocs.io/en/latest/dataaccess.html#available-data-stores>`__. | ||
| """ | ||
|
|
||
| data_store_params: dict[str, Any] = field(default_factory=dict, repr=False) | ||
| """Parameters to use when creating the data store.""" | ||
|
|
||
| open_params: dict[str, Any] = field(default_factory=dict, repr=False) | ||
| """Parameters to use when opening the data.""" | ||
|
|
||
| def find_data(self, **facets: FacetValue) -> list[XCubeDataset]: # noqa: C901 | ||
| # TODO: fix complexity | ||
| """Find data. | ||
|
|
||
| Parameters | ||
| ---------- | ||
| **facets : | ||
| Find data matching these facets. | ||
|
|
||
| Returns | ||
| ------- | ||
| : | ||
| A list of data elements that have been found. | ||
| """ | ||
| store = xcube.core.store.new_data_store( | ||
| self.data_store_id, | ||
| **self.data_store_params, | ||
| ) | ||
| result = [] | ||
| requested_short_names = facets.get("short_name", "*") | ||
| if isinstance(requested_short_names, str | int): | ||
| requested_short_names = [str(requested_short_names)] | ||
| requested_datasets = facets.get("dataset", "*") | ||
| if isinstance(requested_datasets, str | int): | ||
| requested_datasets = [str(requested_datasets)] | ||
| available_datasets = store.list_data_ids() | ||
| for data_id in available_datasets: | ||
| for dataset_pattern in requested_datasets: | ||
| if fnmatch.fnmatchcase(data_id, dataset_pattern): | ||
| description = store.describe_data(data_id) | ||
| available_short_names = list(description.data_vars) | ||
| short_names = [ | ||
| short_name | ||
| for short_name in available_short_names | ||
| for short_name_pattern in requested_short_names | ||
| if fnmatch.fnmatchcase(short_name, short_name_pattern) | ||
| ] | ||
| # TODO: Maybe this is too complicated and we should only | ||
| # decide which variables to keep/drop after load and conversion | ||
| # to iris cube. | ||
| open_params = copy.deepcopy(self.open_params) | ||
| open_params_schema = store.get_open_data_params_schema() | ||
| if "variable_names" in open_params_schema.properties: | ||
| open_params["variable_names"] = short_names | ||
| elif "drop_variables" in open_params_schema.properties: | ||
| drop_variables = { | ||
| short_name | ||
| for short_name in available_short_names | ||
| if short_name not in short_names | ||
| } | ||
| for coord in description.coords.values(): | ||
| if bound_var := coord.attrs.get("bounds"): | ||
| drop_variables.remove(bound_var) | ||
| for data_var in description.data_vars.values(): | ||
| # TODO: keep cell measures | ||
| for ancillary_var in data_var.attrs.get( | ||
| "ancillary_variables", | ||
| "", | ||
| ).split(): | ||
| drop_variables.remove(ancillary_var) | ||
| open_params["drop_variables"] = sorted(drop_variables) | ||
| timerange = f"{description.time_range[0]}/{description.time_range[1]}".replace( | ||
| "-", | ||
| "", | ||
| ) | ||
| frequencies = { | ||
| "P1M": "mon", | ||
| } | ||
| frequency = frequencies[ | ||
| description.attrs["time_coverage_resolution"] | ||
| ] | ||
| dataset = XCubeDataset( | ||
| name=data_id, | ||
| facets={ | ||
| "dataset": data_id, | ||
| "short_name": short_names | ||
| if len(short_names) > 1 | ||
| else short_names[0], | ||
| "frequency": frequency, | ||
| "timerange": timerange, | ||
| }, | ||
| store=store, | ||
| open_params=open_params, | ||
| ) | ||
| dataset.attributes = description.attrs | ||
|
|
||
| result.append(dataset) | ||
|
|
||
| return result |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
zarr3 is perfectly able to read zarr2 datasets, bud
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xcube requires zarr==2
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well that's a bummer - that means it can't read Zarr3 spec?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also Zarr2 is borderline archaic - good luck to us trying to maintain such an evironment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it's on the TODO list: xcube-dev/xcube#1182
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
good, otherwise without a pixi env this would be diabolically hard to maintain π