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 importlib.resources deprecations for py 3.9 #1485

Merged
merged 9 commits into from
Oct 16, 2023
20 changes: 8 additions & 12 deletions xclim/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Climate indices computation package based on Xarray."""
from __future__ import annotations

from importlib.resources import contents, path
from importlib.resources import files as _files

from xclim.core import units # noqa
from xclim.core.indicator import build_indicator_module_from_yaml
Expand All @@ -14,19 +14,15 @@
__version__ = "0.45.6-beta"


_module_data = _files("xclim.data")

# Load official locales
for filename in contents("xclim.data"):
for filename in _module_data.glob("??.json"):
# Only select <locale>.json and not <module>.<locale>.json
if filename.endswith(".json") and filename.count(".") == 1:
locale = filename.split(".")[0]
with path("xclim.data", filename) as f:
load_locale(f, locale)
load_locale(filename, filename.stem)


# Virtual modules creation:
with path("xclim.data", "icclim.yml") as f:
build_indicator_module_from_yaml(f.with_suffix(""), mode="raise")
with path("xclim.data", "anuclim.yml") as f:
build_indicator_module_from_yaml(f.with_suffix(""), mode="raise")
with path("xclim.data", "cf.yml") as f:
build_indicator_module_from_yaml(f.with_suffix(""), mode="raise")
build_indicator_module_from_yaml(_module_data / "icclim", mode="raise")
build_indicator_module_from_yaml(_module_data / "anuclim", mode="raise")
build_indicator_module_from_yaml(_module_data / "cf", mode="raise")
7 changes: 4 additions & 3 deletions xclim/core/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@
import logging
import re
import warnings
from importlib.resources import open_text
from importlib.resources import files
from inspect import _empty, signature # noqa
from typing import Any, Callable, Tuple
from typing import Any, Callable

import numpy as np
import pint
Expand Down Expand Up @@ -112,7 +112,8 @@
units.add_context(hydro)


CF_CONVERSIONS = safe_load(open_text("xclim.data", "variables.yml"))["conversions"]
with (files("xclim.data") / "variables.yml").open() as f:
CF_CONVERSIONS = safe_load(f)["conversions"]
_CONVERSIONS = {}


Expand Down
9 changes: 5 additions & 4 deletions xclim/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@
from collections import defaultdict
from enum import IntEnum
from functools import partial
from importlib.resources import open_text
from importlib.resources import files
from inspect import Parameter, _empty # noqa
from io import StringIO
from pathlib import Path
from typing import Callable, Mapping, NewType, Sequence, TypeVar
from typing import Callable, NewType, Sequence, TypeVar

import numpy as np
import xarray as xr
Expand All @@ -37,8 +37,9 @@
#: Type annotation for thresholds and other not-exactly-a-variable quantities
Quantified = TypeVar("Quantified", xr.DataArray, str, Quantity)

VARIABLES = safe_load(open_text("xclim.data", "variables.yml"))["variables"]
"""Official variables definitions.
with (files("xclim.data") / "variables.yml").open() as f:
VARIABLES = safe_load(f)["variables"]
"""Official variables definitions.

A mapping from variable name to a dict with the following keys:

Expand Down
9 changes: 4 additions & 5 deletions xclim/testing/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

import os
import warnings
from importlib.resources import open_text
from pathlib import Path

import numpy as np
Expand All @@ -12,6 +11,7 @@
from yaml import safe_load

from xclim.core import calendar
from xclim.core.utils import VARIABLES
from xclim.indices import (
longwave_upwelling_radiation_from_net_downwelling,
shortwave_upwelling_radiation_from_net_downwelling,
Expand Down Expand Up @@ -232,13 +232,12 @@ def test_timeseries(
else:
coords = pd.date_range(start, periods=len(values), freq=freq)

data_on_var = safe_load(open_text("xclim.data", "variables.yml"))["variables"]
if variable in data_on_var:
if variable in VARIABLES:
attrs = {
a: data_on_var[variable].get(a, "")
a: VARIABLES[variable].get(a, "")
for a in ["description", "standard_name", "cell_methods"]
}
attrs["units"] = data_on_var[variable]["canonical_units"]
attrs["units"] = VARIABLES[variable]["canonical_units"]

else:
warnings.warn(f"Variable {variable} not recognised. Attrs will not be filled.")
Expand Down
Loading