Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions xarray/backends/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -375,10 +375,11 @@ def open_dataset(
scipy.io.netcdf (only netCDF3 supported). Byte-strings or file-like
objects are opened by scipy.io.netcdf (netCDF3) or h5py (netCDF4/HDF).
engine : {"netcdf4", "scipy", "pydap", "h5netcdf", "pynio", "cfgrib", \
"pseudonetcdf", "zarr"}, optional
"pseudonetcdf", "zarr"} or a custom backend class, optional
Comment thread
Illviljan marked this conversation as resolved.
Outdated
Engine to use when reading files. If not provided, the default engine
is chosen based on available dependencies, with a preference for
"netcdf4".
"netcdf4". A custom backend class can also be used, it requires some
specific methods to work properly.
Comment thread
Illviljan marked this conversation as resolved.
Outdated
chunks : int or dict, optional
If chunks is provided, it is used to load the new dataset into dask
arrays. ``chunks=-1`` loads the dataset with dask using a single
Expand Down
17 changes: 11 additions & 6 deletions xarray/backends/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,14 @@ def guess_engine(store_spec):

def get_backend(engine):
"""Select open_dataset method based on current engine"""
engines = list_engines()
if engine not in engines:
raise ValueError(
f"unrecognized engine {engine} must be one of: {list(engines)}"
)
return engines[engine]
if hasattr(engine, "open_dataset"):
Comment thread
Illviljan marked this conversation as resolved.
Outdated
backend = engine
else:
engines = list_engines()
if engine not in engines:
raise ValueError(
f"unrecognized engine {engine} must be one of: {list(engines)}"
)
backend = engines[engine]
Comment thread
Illviljan marked this conversation as resolved.
Outdated

return backend
27 changes: 26 additions & 1 deletion xarray/tests/test_backends_api.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import numpy as np

import xarray as xr
from xarray.backends.api import _get_default_engine

from . import requires_netCDF4, requires_scipy
from . import assert_identical, requires_netCDF4, requires_scipy


@requires_netCDF4
Expand All @@ -14,3 +17,25 @@ def test__get_default_engine():

engine_default = _get_default_engine("/example")
assert engine_default == "netcdf4"


def test_custom_engine():
expected = xr.Dataset(
dict(a=2 * np.arange(5)), coords=dict(x=("x", np.arange(5), dict(units="s")))
)

class CustomBackend:
open_dataset_parameters = None

def open_dataset(
filename_or_obj,
drop_variables=None,
**kwargs,
):
return expected.copy(deep=True)

def guess_can_open(filename_or_obj):
return False

actual = xr.open_dataset("fake_filename", engine=CustomBackend)
assert_identical(expected, actual)