From 1fdc33db3cc755f6707a592c85b15596e636946e Mon Sep 17 00:00:00 2001 From: "F.N. Claessen" Date: Wed, 5 Mar 2025 10:03:39 +0100 Subject: [PATCH] feat: support for shared speed units Signed-off-by: F.N. Claessen --- flexmeasures/utils/unit_utils.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/flexmeasures/utils/unit_utils.py b/flexmeasures/utils/unit_utils.py index 7d61400e4..e16399cff 100644 --- a/flexmeasures/utils/unit_utils.py +++ b/flexmeasures/utils/unit_utils.py @@ -251,6 +251,22 @@ def is_energy_price_unit(unit: str) -> bool: return False +def is_speed_unit(unit: str) -> bool: + """For example: + >>> is_speed_unit("m/s") + True + >>> is_speed_unit("km/h") + True + >>> is_speed_unit("W") + False + >>> is_speed_unit("m/s²") + False + """ + if not is_valid_unit(unit): + return False + return ur.Quantity(unit).dimensionality == ur.Quantity("m/s").dimensionality + + def get_unit_dimension(unit: str) -> str: """For example: >>> get_unit_dimension("kW") @@ -271,6 +287,8 @@ def get_unit_dimension(unit: str) -> str: 'length' >>> get_unit_dimension("h") 'time' + >>> get_unit_dimension("m/s") + 'speed' """ if is_power_unit(unit): return "power" @@ -282,6 +300,8 @@ def get_unit_dimension(unit: str) -> str: return "price" if is_currency_unit(unit): return "currency" + if is_speed_unit(unit): + return "speed" if unit == "%": return "percentage" dimensions = ur.Quantity(unit).dimensionality