Skip to content

Commit

Permalink
wip: fix _read_string_threshold for period_per and doy_per
Browse files Browse the repository at this point in the history
  • Loading branch information
Abel Aoun committed Feb 26, 2024
1 parent 10f2a1b commit 6b492cb
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 16 deletions.
31 changes: 17 additions & 14 deletions src/icclim/generic_indices/thresholds/factory.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from __future__ import annotations

import re
from collections.abc import Sequence
from typing import TYPE_CHECKING

Expand Down Expand Up @@ -169,18 +168,23 @@ def build_threshold(
raise NotImplementedError(msg)


def _get_operator(query: str) -> tuple[Operator | None, str]:
prefix = query[0:3]
ops = OperatorRegistry.values()
for op in ops:
for alias in op.aliases:
if alias in prefix:
return op, query.replace(alias, "", 1)
return None, query


def _read_string_threshold(query: str) -> tuple[str, str, float]:
value = re.findall(r"-?\d+\.?\d*", query)
if len(value) == 0:
msg = f"Cannot build threshold from '{query}'"
raise InvalidIcclimArgumentError(msg)
value = value[0]
value_index = query.find(value)
operator = query[0:value_index].strip()
if operator == "":
operator = None
unit = None if query.endswith(value) else query[value_index + len(value) :].strip()
return operator, unit, float(value)
# find op, remove op from query, if doy_per and period_per not in query, parse the rest with pint
op, no_op_query = _get_operator(query)
q = xc_units.Quantity(no_op_query)
if op is not None:
return op.operand, str(q.units), q.m
return "", str(q.units), q.m


def _build_quantity(
Expand All @@ -202,8 +206,7 @@ def _build_quantity(
if operator is not None and operator != "":
msg = (
f"Cannot parse quantity '{quantity}'"
f" The operator {operator} is not needed here"
f" operator for this parameter."
f" The operator {operator} should be removed."
)
raise InvalidIcclimArgumentError(msg)
return xc_units.Quantity(value=value, units=unit)
Expand Down
2 changes: 1 addition & 1 deletion src/icclim/models/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,4 @@ def catalog(cls) -> dict[str, T]:

@classmethod
def values(cls) -> list[T]:
return [v for k, v in cls.__dict__.items() if isinstance(v, cls._item_class)]
return [v for v in cls.__dict__.values() if isinstance(v, cls._item_class)]
2 changes: 1 addition & 1 deletion tests/test_generic_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,4 @@ def test_percentile():
resample_freq=FrequencyRegistry.MONTH,
is_compared_to_reference=False,
)
assert result[1] == -5
assert result[0] == -5

0 comments on commit 6b492cb

Please sign in to comment.