Skip to content

Commit 2c7e5f3

Browse files
committed
abstract the lines for merging the ext_quantities and the mdu_quantities
1 parent d9ae51d commit 2c7e5f3

File tree

1 file changed

+38
-18
lines changed

1 file changed

+38
-18
lines changed

hydrolib/tools/ext_old_to_new/converters.py

Lines changed: 38 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,38 @@ def get_time_series_data(tim_model: TimModel) -> Dict[str, List[float]]:
281281

282282
return data
283283

284+
@staticmethod
285+
def merge_mdu_and_ext_file_quantities(
286+
mdu_quantities: Dict[str, bool], temp_salinity_from_ext: Dict[str, int]
287+
) -> List[str]:
288+
"""Merge the temperature and salinity from the mdu file with the temperature and salinity from the external file.
289+
290+
Args:
291+
mdu_quantities (Dict[str, bool]): A dictionary containing the temperature and salinity details from the
292+
mdu file, with bool values indecating if the temperature/salinity is activated in the mdu file.
293+
temp_salinity_from_ext (Dict[str,int]): A dictionary containing the temperature and salinity details from
294+
the external file.
295+
296+
Returns:
297+
List[str]: A list of quantities that will be used in the tim file.
298+
"""
299+
if mdu_quantities:
300+
mdu_file_quantity_list = [key for key, val in mdu_quantities.items() if val]
301+
temp_salinity_from_mdu = find_temperature_salinity_in_quantities(
302+
mdu_file_quantity_list
303+
)
304+
final_temp_salinity = temp_salinity_from_ext | temp_salinity_from_mdu
305+
# the kwargs will be provided only from the source and sink converter
306+
# Ensure 'temperature' comes before 'salinity'
307+
keys = list(final_temp_salinity.keys())
308+
if "temperaturedelta" in keys and "salinitydelta" in keys:
309+
keys.remove("salinitydelta")
310+
keys.insert(keys.index("temperaturedelta"), "salinitydelta")
311+
else:
312+
keys = list(temp_salinity_from_ext.keys())
313+
314+
return keys
315+
284316
def parse_tim_model(
285317
self, tim_file: Path, ext_file_quantity_list: List[str], **mdu_quantities
286318
) -> Dict[str, List[float]]:
@@ -382,24 +414,12 @@ def parse_tim_model(
382414
ext_file_quantity_list
383415
)
384416

385-
# test if the temperature and salinity in the ext file conforms with the mdu file
386-
# compare the temperature and salinity from mdu with the temperature and salinity from the external file
387-
if mdu_quantities:
388-
mdu_file_quantity_list = [key for key, val in mdu_quantities.items() if val]
389-
temp_salinity_from_mdu = find_temperature_salinity_in_quantities(
390-
mdu_file_quantity_list
391-
)
392-
final_temp_salinity = temp_salinity_from_ext | temp_salinity_from_mdu
393-
# the kwargs will be provided only from the source and sink converter
394-
# Ensure 'temperature' comes before 'salinity'
395-
keys = list(final_temp_salinity.keys())
396-
if "temperaturedelta" in keys and "salinitydelta" in keys:
397-
keys.remove("salinitydelta")
398-
keys.insert(keys.index("temperaturedelta"), "salinitydelta")
399-
else:
400-
keys = list(temp_salinity_from_ext.keys())
401-
402-
final_quantities_list = ["discharge"] + keys + required_quantities_from_ext
417+
final_temp_salinity = self.merge_mdu_and_ext_file_quantities(
418+
mdu_quantities, temp_salinity_from_ext
419+
)
420+
final_quantities_list = (
421+
["discharge"] + final_temp_salinity + required_quantities_from_ext
422+
)
403423

404424
if len(time_series) != len(final_quantities_list):
405425
raise ValueError(

0 commit comments

Comments
 (0)