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

Fix xarray.backends imports #409

Open
wants to merge 1 commit into
base: master
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
5 changes: 3 additions & 2 deletions cfgrib/xarray_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@
raise ImportError("xarray_plugin module needs xarray version >= 0.18+")

from xarray.backends.common import AbstractDataStore, BackendArray, BackendEntrypoint
from xarray.backends.locks import SerializableLock, ensure_lock

from . import abc, dataset, messages

# FIXME: Add a dedicated lock, even if ecCodes is supposed to be thread-safe
# in most circumstances. See:
# https://confluence.ecmwf.int/display/ECC/Frequently+Asked+Questions
ECCODES_LOCK = xr.backends.locks.SerializableLock() # type: ignore
ECCODES_LOCK = SerializableLock() # type: ignore


class CfGribDataStore(AbstractDataStore):
Expand All @@ -32,7 +33,7 @@ def __init__(
):
if lock is None:
lock = ECCODES_LOCK
self.lock = xr.backends.locks.ensure_lock(lock) # type: ignore
self.lock = ensure_lock(lock) # type: ignore
if isinstance(filename, (str, pathlib.PurePath)):
opener = dataset.open_file
else:
Expand Down
7 changes: 4 additions & 3 deletions cfgrib/xarray_to_grib.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import numpy as np
import xarray as xr
from xarray.backends import api as backends_api

from . import cfmessage, dataset, messages

Expand Down Expand Up @@ -252,12 +253,12 @@ def canonical_dataset_to_grib(dataset, path, mode="wb", no_warn=False, grib_keys
warnings.warn("GRIB write support is experimental, DO NOT RELY ON IT!", FutureWarning)

# validate Dataset keys, DataArray names, and attr keys/values
xr.backends.api._validate_dataset_names(dataset) # type: ignore
backends_api._validate_dataset_names(dataset) # type: ignore
# _validate_attrs takes the engine name as its 2nd arg from xarray 2024.09.0
try:
xr.backends.api._validate_attrs(dataset) # type: ignore
backends_api._validate_attrs(dataset) # type: ignore
except TypeError:
xr.backends.api._validate_attrs(dataset, "cfgrib") # type: ignore
backends_api._validate_attrs(dataset, "cfgrib") # type: ignore

real_grib_keys = {str(k)[5:]: v for k, v in dataset.attrs.items() if str(k)[:5] == "GRIB_"}
real_grib_keys.update(grib_keys)
Expand Down
Loading