1
1
"""Generic functions for working with all types of dfs files."""
2
2
3
3
from __future__ import annotations
4
+ from dataclasses import dataclass
4
5
import math
5
6
import os
6
7
import pathlib
@@ -597,10 +598,7 @@ def extract(
597
598
598
599
is_layered_dfsu = dfs_i .ItemInfo [0 ].Name == "Z coordinate"
599
600
600
- file_start_new , start_step , start_sec , end_step , end_sec = _parse_start_end (
601
- dfs_i .FileInfo .TimeAxis , start , end
602
- )
603
- timestep = _parse_step (dfs_i .FileInfo .TimeAxis , step )
601
+ time = _parse_time (dfs_i .FileInfo .TimeAxis , start , end , step )
604
602
item_numbers = _valid_item_numbers (
605
603
dfs_i .ItemInfo , items , ignore_first = is_layered_dfsu
606
604
)
@@ -612,28 +610,28 @@ def extract(
612
610
dfs_o = _clone (
613
611
str (infilename ),
614
612
str (outfilename ),
615
- start_time = file_start_new ,
616
- timestep = timestep ,
613
+ start_time = time . file_start_new ,
614
+ timestep = time . timestep ,
617
615
items = item_numbers ,
618
616
)
619
617
620
618
file_start_shift = 0
621
- if file_start_new is not None :
619
+ if time . file_start_new is not None :
622
620
file_start_orig = dfs_i .FileInfo .TimeAxis .StartDateTime
623
- file_start_shift = (file_start_new - file_start_orig ).total_seconds ()
621
+ file_start_shift = (time . file_start_new - file_start_orig ).total_seconds ()
624
622
625
623
timestep_out = - 1
626
- for timestep in range (start_step , end_step , step ):
624
+ for timestep in range (time . start_step , time . end_step , step ):
627
625
for item_out , item in enumerate (item_numbers ):
628
626
itemdata = dfs_i .ReadItemTimeStep ((item + 1 ), timestep )
629
627
time_sec = itemdata .Time
630
628
631
- if time_sec > end_sec :
629
+ if time_sec > time . end_sec :
632
630
dfs_i .Close ()
633
631
dfs_o .Close ()
634
632
return
635
633
636
- if time_sec >= start_sec :
634
+ if time_sec >= time . start_sec :
637
635
if item == item_numbers [0 ]:
638
636
timestep_out = timestep_out + 1
639
637
time_sec_out = time_sec - file_start_shift
@@ -647,11 +645,24 @@ def extract(
647
645
dfs_o .Close ()
648
646
649
647
650
- def _parse_start_end (
648
+ @dataclass
649
+ class TimeInfo :
650
+ "Parsed time information."
651
+
652
+ file_start_new : datetime | None
653
+ start_step : int
654
+ start_sec : float
655
+ end_step : int
656
+ end_sec : float
657
+ timestep : float | None
658
+
659
+
660
+ def _parse_time (
651
661
time_axis : TimeAxis ,
652
662
start : int | float | str | datetime ,
653
663
end : int | float | str | datetime ,
654
- ) -> tuple [datetime | None , int , float , int , float ]: # TODO better return type
664
+ step : int ,
665
+ ) -> TimeInfo :
655
666
"""Helper function for parsing start and end arguments."""
656
667
n_time_steps = time_axis .NumberOfTimeSteps
657
668
file_start_datetime = time_axis .StartDateTime
@@ -727,7 +738,9 @@ def _parse_start_end(
727
738
if start_sec > file_start_sec :
728
739
file_start_new = file_start_datetime + timedelta (seconds = start_sec )
729
740
730
- return file_start_new , start_step , start_sec , end_step , end_sec
741
+ timestep = _parse_step (time_axis , step )
742
+
743
+ return TimeInfo (file_start_new , start_step , start_sec , end_step , end_sec , timestep )
731
744
732
745
733
746
def _parse_step (time_axis : TimeAxis , step : int ) -> float | None :
0 commit comments