Skip to content

Add open_dataarray to BackendEntrypoint? #10562

Description

@weiji14

Is your feature request related to a problem?

The BackendEntrypoint class at

class BackendEntrypoint:
"""
``BackendEntrypoint`` is a class container and it is the main interface
for the backend plugins, see :ref:`RST backend_entrypoint`.
It shall implement:
- ``open_dataset`` method: it shall implement reading from file, variables
decoding and it returns an instance of :py:class:`~xarray.Dataset`.
It shall take in input at least ``filename_or_obj`` argument and
``drop_variables`` keyword argument.
For more details see :ref:`RST open_dataset`.
- ``guess_can_open`` method: it shall return ``True`` if the backend is able to open
``filename_or_obj``, ``False`` otherwise. The implementation of this
method is not mandatory.
- ``open_datatree`` method: it shall implement reading from file, variables
decoding and it returns an instance of :py:class:`~datatree.DataTree`.
It shall take in input at least ``filename_or_obj`` argument. The
implementation of this method is not mandatory. For more details see
<reference to open_datatree documentation>.
Attributes
----------
open_dataset_parameters : tuple, default: None
A list of ``open_dataset`` method parameters.
The setting of this attribute is not mandatory.
description : str, default: ""
A short string describing the engine.
The setting of this attribute is not mandatory.
url : str, default: ""
A string with the URL to the backend's documentation.
The setting of this attribute is not mandatory.
"""
open_dataset_parameters: ClassVar[tuple | None] = None
description: ClassVar[str] = ""
url: ClassVar[str] = ""
def __repr__(self) -> str:
txt = f"<{type(self).__name__}>"
if self.description:
txt += f"\n {self.description}"
if self.url:
txt += f"\n Learn more at {self.url}"
return txt
def open_dataset(
self,
filename_or_obj: str | os.PathLike[Any] | ReadBuffer | AbstractDataStore,
*,
drop_variables: str | Iterable[str] | None = None,
) -> Dataset:
"""
Backend open_dataset method used by Xarray in :py:func:`~xarray.open_dataset`.
"""
raise NotImplementedError()
def guess_can_open(
self,
filename_or_obj: str | os.PathLike[Any] | ReadBuffer | AbstractDataStore,
) -> bool:
"""
Backend open_dataset method used by Xarray in :py:func:`~xarray.open_dataset`.
"""
return False
def open_datatree(
self,
filename_or_obj: str | os.PathLike[Any] | ReadBuffer | AbstractDataStore,
*,
drop_variables: str | Iterable[str] | None = None,
) -> DataTree:
"""
Backend open_datatree method used by Xarray in :py:func:`~xarray.open_datatree`.
"""
raise NotImplementedError()
currently has open_dataset and open_datatree methods, but not open_dataarray. Currently, someone implementing a BackendEntrypoint to do xarray.open_dataarray(..., engine="<something>") would have to implement open_dataset, and behind the scenes, this could result in a roundtrip xr.DataArray -> xr.Dataset -> xr.DataArray conversion, which can lead to some attributes/state getting lost along the way (xref #3268).

Describe the solution you'd like

Enable open_dataarray to be implemented directly, without a roundtrip conversion to/from xr.Dataset

Describe alternatives you've considered

No response

Additional context

GenericMappingTools/pygmt#4008 (comment)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions