Skip to content

Fix _get_default_engine_netcdf to check for h5netcdf #10557

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
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
29 changes: 14 additions & 15 deletions xarray/backends/api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from __future__ import annotations

import importlib.util
import os
from collections.abc import (
Callable,
Expand Down Expand Up @@ -122,23 +123,21 @@ def _get_default_engine_gz() -> Literal["scipy"]:
return engine


def _get_default_engine_netcdf() -> Literal["netcdf4", "scipy"]:
engine: Literal["netcdf4", "scipy"]
try:
import netCDF4 # noqa: F401
def _get_default_engine_netcdf() -> Literal["netcdf4", "h5netcdf", "scipy"]:
candidates: list[tuple[str, str]] = [
("netcdf4", "netCDF4"),
("h5netcdf", "h5netcdf"),
("scipy", "scipy.io.netcdf"),
]

engine = "netcdf4"
except ImportError: # pragma: no cover
try:
import scipy.io.netcdf # noqa: F401
for engine, module_name in candidates:
if importlib.util.find_spec(module_name) is not None:
return cast(Literal["netcdf4", "h5netcdf", "scipy"], engine)

engine = "scipy"
except ImportError as err:
raise ValueError(
"cannot read or write netCDF files without "
"netCDF4-python or scipy installed"
) from err
return engine
raise ValueError(
"cannot read or write NetCDF files because none of "
"'netCDF4-python', 'h5netcdf', or 'scipy' are installed"
)


def _get_default_engine(path: str, allow_remote: bool = False) -> T_NetcdfEngine:
Expand Down
15 changes: 14 additions & 1 deletion xarray/tests/test_backends_api.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,18 @@
from __future__ import annotations

import sys
from numbers import Number

import numpy as np
import pytest

import xarray as xr
from xarray.backends.api import _get_default_engine
from xarray.backends.api import _get_default_engine, _get_default_engine_netcdf
from xarray.tests import (
assert_identical,
assert_no_warnings,
requires_dask,
requires_h5netcdf,
requires_netCDF4,
requires_scipy,
)
Expand All @@ -29,6 +31,17 @@ def test__get_default_engine() -> None:
assert engine_default == "netcdf4"


@requires_h5netcdf
def test_default_engine_h5netcdf(monkeypatch):
"""Test the default netcdf engine when h5netcdf is the only importable module."""

monkeypatch.delitem(sys.modules, "netCDF4", raising=False)
monkeypatch.delitem(sys.modules, "scipy", raising=False)
monkeypatch.setattr(sys, "meta_path", [])

assert _get_default_engine_netcdf() == "h5netcdf"


def test_custom_engine() -> None:
expected = xr.Dataset(
dict(a=2 * np.arange(5)), coords=dict(x=("x", np.arange(5), dict(units="s")))
Expand Down
Loading