|
| 1 | +""" |
| 2 | +Validator for parameters provided to Convert Units Processor. |
| 3 | +""" |
| 4 | + |
| 5 | +from __future__ import annotations |
| 6 | + |
| 7 | +import warnings |
| 8 | +from typing import Any, Iterable |
| 9 | + |
| 10 | +from climakitae.core.constants import UNIT_OPTIONS, UNSET |
| 11 | +from climakitae.new_core.param_validation.abc_param_validation import ( |
| 12 | + register_processor_validator, |
| 13 | +) |
| 14 | + |
| 15 | +# All supported units (flattened from UNIT_OPTIONS) |
| 16 | +ALL_SUPPORTED_UNITS = set() |
| 17 | +for unit_list in UNIT_OPTIONS.values(): |
| 18 | + ALL_SUPPORTED_UNITS.update(unit_list) |
| 19 | + |
| 20 | + |
| 21 | +@register_processor_validator("convert_units") |
| 22 | +def validate_convert_units_param( |
| 23 | + value: str | Iterable[str], |
| 24 | + **kwargs: Any, |
| 25 | +) -> bool: |
| 26 | + """ |
| 27 | + Validate the parameters provided to the Convert Units Processor. |
| 28 | +
|
| 29 | + This function checks the value provided to the Convert Units Processor and ensures that it |
| 30 | + meets the expected criteria. Will raise a user warning and return false if the value |
| 31 | + is not valid. |
| 32 | +
|
| 33 | + Parameters |
| 34 | + ---------- |
| 35 | + value : str | Iterable[str] |
| 36 | + The unit(s) to convert to. Can be a single unit string or an iterable of unit strings. |
| 37 | + Valid units include temperature units (K, degC, degF), pressure units (Pa, hPa, mb, inHg), |
| 38 | + wind units (m/s, m s-1, mph, knots), precipitation units (mm, mm/d, mm/h, inches, inches/d, inches/h), |
| 39 | + moisture units (kg/kg, kg kg-1, g/kg, g kg-1), flux units (kg m-2 s-1), and relative humidity |
| 40 | + units ([0 to 100], fraction). |
| 41 | +
|
| 42 | + Returns |
| 43 | + ------- |
| 44 | + bool |
| 45 | + True if all parameters are valid, False otherwise |
| 46 | + """ |
| 47 | + # kwargs unused but required for signature compatibility |
| 48 | + del kwargs |
| 49 | + |
| 50 | + if value is UNSET: |
| 51 | + # UNSET is valid - processor will not perform any conversion |
| 52 | + return True |
| 53 | + |
| 54 | + if not _check_input_types(value): |
| 55 | + return False |
| 56 | + |
| 57 | + if not _check_unit_validity(value): |
| 58 | + return False |
| 59 | + |
| 60 | + return True |
| 61 | + |
| 62 | + |
| 63 | +def _check_input_types(value: str | Iterable[str]) -> bool: |
| 64 | + """ |
| 65 | + Check if the input value has the correct type. |
| 66 | +
|
| 67 | + Parameters |
| 68 | + ---------- |
| 69 | + value : str | Iterable[str] |
| 70 | + The value to check. |
| 71 | +
|
| 72 | + Returns |
| 73 | + ------- |
| 74 | + bool |
| 75 | + True if the input type is valid, False otherwise. |
| 76 | + """ |
| 77 | + if isinstance(value, str): |
| 78 | + return True |
| 79 | + |
| 80 | + warnings.warn( |
| 81 | + "\n\nConvert Units Processor expects a string. " |
| 82 | + f"\nReceived type: {type(value)}" |
| 83 | + ) |
| 84 | + return False |
| 85 | + |
| 86 | + |
| 87 | +def _check_unit_validity(value: str | Iterable[str]) -> bool: |
| 88 | + """ |
| 89 | + Check if the provided unit(s) are supported. |
| 90 | +
|
| 91 | + Parameters |
| 92 | + ---------- |
| 93 | + value : str | Iterable[str] |
| 94 | + The unit(s) to validate. |
| 95 | +
|
| 96 | + Returns |
| 97 | + ------- |
| 98 | + bool |
| 99 | + True if all units are supported, False otherwise. |
| 100 | + """ |
| 101 | + units_to_check = [value] if isinstance(value, str) else list(value) |
| 102 | + |
| 103 | + for unit in units_to_check: |
| 104 | + if unit not in ALL_SUPPORTED_UNITS: |
| 105 | + supported_units_str = ", ".join(sorted(ALL_SUPPORTED_UNITS)) |
| 106 | + warnings.warn( |
| 107 | + f"\n\nUnsupported unit: '{unit}'. " |
| 108 | + f"\nSupported units are: {supported_units_str}" |
| 109 | + ) |
| 110 | + return False |
| 111 | + |
| 112 | + return True |
0 commit comments