Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
pre-commit-ci[bot] committed Jun 28, 2022
1 parent fdc0b24 commit 5b6c7a8
Show file tree
Hide file tree
Showing 6 changed files with 117 additions and 108 deletions.
16 changes: 8 additions & 8 deletions icclim/ecad/ecad_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
def gd4(config: IndexConfig) -> DataArray:
return _compute_threshold_index(
da=config.tas.study_da,
threshold=4.0 ,
threshold=4.0,
freq=config.frequency,
xclim_index_fun=atmos.growing_degree_days,
)
Expand All @@ -36,7 +36,7 @@ def gd4(config: IndexConfig) -> DataArray:
def cfd(config: IndexConfig) -> DataArray:
return _compute_threshold_index(
da=config.tasmin.study_da,
threshold=0.0 ,
threshold=0.0,
freq=config.frequency,
xclim_index_fun=atmos.consecutive_frost_days,
)
Expand All @@ -45,7 +45,7 @@ def cfd(config: IndexConfig) -> DataArray:
def fd(config: IndexConfig) -> DataArray:
return _compute_threshold_index(
da=config.tasmin.study_da,
threshold=0.0 ,
threshold=0.0,
freq=config.frequency,
xclim_index_fun=atmos.frost_days,
)
Expand All @@ -63,7 +63,7 @@ def hd17(config: IndexConfig) -> DataArray:
def id(config: IndexConfig) -> DataArray:
return _compute_threshold_index(
da=config.tasmax.study_da,
threshold=0.0 ,
threshold=0.0,
freq=config.frequency,
xclim_index_fun=atmos.ice_days,
)
Expand Down Expand Up @@ -320,7 +320,7 @@ def r75p(config: IndexConfig) -> tuple[DataArray, DataArray | None]:
return _compute_rxxp(
pr=config.pr,
freq=config.frequency,
pr_per_thresh=75.,
pr_per_thresh=75.0,
per_interpolation=config.interpolation,
save_percentile=config.save_percentile,
is_percent=config.is_percent,
Expand All @@ -331,7 +331,7 @@ def r75ptot(config: IndexConfig) -> tuple[DataArray, DataArray | None]:
return _compute_rxxptot(
pr=config.pr,
freq=config.frequency,
pr_per_thresh=75.,
pr_per_thresh=75.0,
per_interpolation=config.interpolation,
save_percentile=config.save_percentile,
)
Expand All @@ -341,7 +341,7 @@ def r95p(config: IndexConfig) -> tuple[DataArray, DataArray | None]:
return _compute_rxxp(
pr=config.pr,
freq=config.frequency,
pr_per_thresh=95.,
pr_per_thresh=95.0,
per_interpolation=config.interpolation,
save_percentile=config.save_percentile,
is_percent=config.is_percent,
Expand All @@ -352,7 +352,7 @@ def r95ptot(config: IndexConfig) -> tuple[DataArray, DataArray | None]:
return _compute_rxxptot(
pr=config.pr,
freq=config.frequency,
pr_per_thresh=95.,
pr_per_thresh=95.0,
per_interpolation=config.interpolation,
save_percentile=config.save_percentile,
)
Expand Down
105 changes: 53 additions & 52 deletions icclim/generic_indices/generic_index_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,23 @@
from icclim.models.index_config import IndexConfig
from icclim.models.quantile_interpolation import QuantileInterpolation


def _can_run_bootstrap(cf_var: CfVariable) -> bool:
"""Avoid bootstrapping if there is one single year overlapping
or no year overlapping or all year overlapping.
"""
study_years = np.unique(cf_var.study_da.indexes.get("time").year)
overlapping_years = np.unique(
cf_var.study_da.sel(time=_get_ref_period_slice(cf_var.reference_da))
.indexes.get("time")
.year
.indexes.get("time")
.year
)
return 1 < len(overlapping_years) < len(study_years)


def _get_ref_period_slice(da: DataArray) -> slice:
time_length = len(da.time)
return slice(*da.time[0:: time_length - 1].dt.strftime("%Y-%m-%d").values)
return slice(*da.time[0 :: time_length - 1].dt.strftime("%Y-%m-%d").values)


def _to_percent(da: DataArray, sampling_freq: Frequency) -> DataArray:
Expand Down Expand Up @@ -93,11 +94,11 @@ def _add_bootstrap_meta(result: DataArray, per: DataArray) -> DataArray:


def _compute_percentile_doy(
cf_var: CfVariable,
percentile: float,
window: int = 5,
interpolation=QuantileInterpolation.MEDIAN_UNBIASED,
callback: Callable = None,
cf_var: CfVariable,
percentile: float,
window: int = 5,
interpolation=QuantileInterpolation.MEDIAN_UNBIASED,
callback: Callable = None,
) -> (DataArray, bool):
if PercentileDataArray.is_compatible(cf_var.reference_da):
per = cf_var.reference_da
Expand All @@ -117,7 +118,7 @@ def _compute_percentile_doy(


def _compute_precip_percentile_over_period(
cf_var: CfVariable, interpolation: QuantileInterpolation, percentiles: float
cf_var: CfVariable, interpolation: QuantileInterpolation, percentiles: float
) -> DataArray:
if PercentileDataArray.is_compatible(cf_var.reference_da):
return cf_var.reference_da
Expand Down Expand Up @@ -146,26 +147,26 @@ def _filter_in_wet_days(da: DataArray, dry_day_value: float):


def _compute_threshold_index(
da: DataArray,
threshold: float,
freq: Frequency,
xclim_index_fun: Callable,
da: DataArray,
threshold: float,
freq: Frequency,
xclim_index_fun: Callable,
) -> DataArray:
return xclim_index_fun(
da, thresh=f"{threshold} °C", **freq.build_frequency_kwargs()
)


def _compute_spell_duration(
cf_var: CfVariable,
freq: Frequency,
per_window: int,
per_thresh: float,
per_interpolation: QuantileInterpolation,
min_spell_duration: int,
save_percentile: bool,
callback: Callable,
xclim_index_fun: Callable,
cf_var: CfVariable,
freq: Frequency,
per_window: int,
per_thresh: float,
per_interpolation: QuantileInterpolation,
min_spell_duration: int,
save_percentile: bool,
callback: Callable,
xclim_index_fun: Callable,
) -> tuple[DataArray, DataArray | None]:
per, run_bootstrap = _compute_percentile_doy(
cf_var,
Expand Down Expand Up @@ -195,16 +196,16 @@ def _compute_spell_duration(


def compute_compound_index(
tas: CfVariable,
pr: CfVariable,
freq: Frequency,
tas_per_thresh: int,
pr_per_thresh: int,
per_window: int,
per_interpolation: QuantileInterpolation,
save_percentile: bool,
callback: Callable,
xclim_index_fun: Callable,
tas: CfVariable,
pr: CfVariable,
freq: Frequency,
tas_per_thresh: int,
pr_per_thresh: int,
per_window: int,
per_interpolation: QuantileInterpolation,
save_percentile: bool,
callback: Callable,
xclim_index_fun: Callable,
) -> DataArray:
"""CD, CW, WD, WW
Expand Down Expand Up @@ -260,11 +261,11 @@ def compute_compound_index(


def _compute_rxxptot(
pr: CfVariable,
freq: Frequency,
pr_per_thresh: float,
per_interpolation: QuantileInterpolation,
save_percentile: bool,
pr: CfVariable,
freq: Frequency,
pr_per_thresh: float,
per_interpolation: QuantileInterpolation,
save_percentile: bool,
) -> tuple[DataArray, DataArray | None]:
per = _compute_precip_percentile_over_period(pr, per_interpolation, pr_per_thresh)
result = atmos.fraction_over_precip_thresh(
Expand All @@ -282,12 +283,12 @@ def _compute_rxxptot(


def _compute_rxxp(
pr: CfVariable,
freq: Frequency,
pr_per_thresh: float,
per_interpolation: QuantileInterpolation,
save_percentile: bool,
is_percent: bool,
pr: CfVariable,
freq: Frequency,
pr_per_thresh: float,
per_interpolation: QuantileInterpolation,
save_percentile: bool,
is_percent: bool,
) -> tuple[DataArray, DataArray | None]:
per = _compute_precip_percentile_over_period(pr, per_interpolation, pr_per_thresh)
result = atmos.days_over_precip_thresh(
Expand All @@ -306,15 +307,15 @@ def _compute_rxxp(


def _compute_temperature_percentile_index(
cf_var: CfVariable,
freq: Frequency,
tas_per_thresh: int,
per_window: int,
per_interpolation: QuantileInterpolation,
save_percentile: bool,
is_percent: bool,
callback: Callable,
xclim_index_fun: Callable,
cf_var: CfVariable,
freq: Frequency,
tas_per_thresh: int,
per_window: int,
per_interpolation: QuantileInterpolation,
save_percentile: bool,
is_percent: bool,
callback: Callable,
xclim_index_fun: Callable,
) -> tuple[DataArray, DataArray | None]:
per, run_bootstrap = _compute_percentile_doy(
cf_var,
Expand Down
6 changes: 2 additions & 4 deletions icclim/generic_indices/generic_indices.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ def __init__(self, climate_index: ClimateIndex):
)



def tx_above_scalar(c)->DataArray:
def tx_above_scalar(c) -> DataArray:
pass


# -- COUNT DAYS ABOVE
# su
days_above_scalar
Expand Down Expand Up @@ -128,5 +128,3 @@ def tx_above_scalar(c)->DataArray:
# - rxxpTOT
# - prcptot
# - handle units


4 changes: 2 additions & 2 deletions icclim/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
import xclim_catalog
from xarray.core.dataarray import DataArray
from xarray.core.dataset import Dataset

from xclim_catalog import XclimIndices

from icclim.ecad.ecad_functions import IndexConfig
from icclim.ecad.ecad_indices import EcadIndex, get_season_excluded_indices
from icclim.icclim_exceptions import InvalidIcclimArgumentError
Expand Down Expand Up @@ -149,7 +149,7 @@ def index(
user_indice: UserIndexDict = None,
transfer_limit_Mbytes: float = None,
# additional kwargs
**kwargs: dict
**kwargs: dict,
) -> Dataset:
"""
Main entry point for icclim to compute climate indices.
Expand Down
26 changes: 13 additions & 13 deletions icclim/models/index_config.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
from __future__ import annotations

from typing import Callable, Any
from typing import Any, Callable

from icclim.models.cf_variable import CfVariable
from icclim.models.climate_index import ClimateIndex
from icclim.models.constants import PR, TAS, TAS_MAX, TAS_MIN, SFC_WIND
from icclim.models.constants import PR, SFC_WIND, TAS, TAS_MAX, TAS_MIN
from icclim.models.frequency import Frequency
from icclim.models.netcdf_version import NetcdfVersion
from icclim.models.quantile_interpolation import QuantileInterpolation
Expand Down Expand Up @@ -55,17 +55,17 @@ class IndexConfig:

def __init__(
self,
frequency: Frequency,
netcdf_version: str | NetcdfVersion,
index: ClimateIndex | None,
cf_variables: list[CfVariable],
save_percentile: bool = False,
window_width: int | None = 5,
threshold: list[float] | float | None = None,
out_unit: str | None = None,
interpolation=QuantileInterpolation.MEDIAN_UNBIASED,
callback: Callable[[int], None] | None = None,
xclim_kwargs: dict[str, Any] | None = None,
frequency: Frequency,
netcdf_version: str | NetcdfVersion,
index: ClimateIndex | None,
cf_variables: list[CfVariable],
save_percentile: bool = False,
window_width: int | None = 5,
threshold: list[float] | float | None = None,
out_unit: str | None = None,
interpolation=QuantileInterpolation.MEDIAN_UNBIASED,
callback: Callable[[int], None] | None = None,
xclim_kwargs: dict[str, Any] | None = None,
):
self.frequency = frequency
self.cf_variables = cf_variables
Expand Down
Loading

0 comments on commit 5b6c7a8

Please sign in to comment.