Skip to content

Commit

Permalink
add utility function for locate the temperature and salinity in the q…
Browse files Browse the repository at this point in the history
…uantities list
  • Loading branch information
MAfarrag committed Dec 19, 2024
1 parent 304cd39 commit 33a467e
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
43 changes: 42 additions & 1 deletion hydrolib/tools/ext_old_to_new/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from collections import OrderedDict
from pathlib import Path
from typing import Any, Dict, Type, Union
from typing import Any, Dict, List, Type, Union

from hydrolib.core.basemodel import DiskOnlyFileModel, FileModel, PathOrStr
from hydrolib.core.dflowfm.ext.models import (
Expand Down Expand Up @@ -218,3 +219,43 @@ def convert_initial_cond_param_dict(forcing: ExtOldForcing) -> Dict[str, str]:
)

return block_data


def find_temperature_salinity_in_quantities(strings: List[str]) -> Dict[str, int]:
"""
Searches for keywords "temperature" and "salinity" in a list of strings
and returns a dictionary with associated values.
Args:
strings (List[str]): A list of strings to search.
Returns:
Dict[str, int]: A dictionary with keys as "temperature" or "salinity"
and values 3 and 4 respectively.
Examples:
>>> find_temperature_salinity_in_quantities(["temperature", "Salinity"])
OrderedDict({"temperature": 3, "salinity": 4})
>>> find_temperature_salinity_in_quantities(["Temperature"])
OrderedDict({"temperature": 3})
>>> find_temperature_salinity_in_quantities(["Salinity"])
OrderedDict({"salinity": 3})
>>> find_temperature_salinity_in_quantities(["tracers"])
OrderedDict()
>>> find_temperature_salinity_in_quantities([])
OrderedDict()
"""
result = OrderedDict()

if any("temperature" in string.lower() for string in strings):
result["temperature"] = 3
if any("salinity" in string.lower() for string in strings):
result["salinity"] = (
result.get("temperature", 2) + 1
) # Default temperature value is 2

return result
18 changes: 18 additions & 0 deletions tests/tools/test_tools_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import unittest
from pathlib import Path

import pytest
Expand All @@ -9,6 +10,7 @@
from hydrolib.tools.ext_old_to_new.utils import (
construct_filemodel_new_or_existing,
convert_interpolation_data,
find_temperature_salinity_in_quantities,
)


Expand All @@ -35,3 +37,19 @@ def test_convert_interpolation_data():
assert data["averagingtype"] == "mean"
assert data["averagingrelsize"] is None
assert data["averagingpercentile"] is None


@pytest.mark.parametrize(
"strings, expected",
[
(["temperature", "Salinity"], {"temperature": 3, "salinity": 4}),
(["Temperature"], {"temperature": 3}),
(["Salinity"], {"salinity": 3}),
(["tracers"], {}),
(["TEMPERATURE", "salInity"], {"temperature": 3, "salinity": 4}),
([], {}),
(["No relevant data here.", "Nothing to match."], {}),
],
)
def test_find_keywords_with_values(strings, expected):
assert find_temperature_salinity_in_quantities(strings) == expected

0 comments on commit 33a467e

Please sign in to comment.