Repository: DHI/mikeio
Pull request #611 (2023-10-18 14:09:32)

Pfs improvements
Minor tweaks and fixes to PFS module


Patch:
diff --git a/mikeio/pfs/_pfsdocument.py b/mikeio/pfs/_pfsdocument.py
index 0b55a998..b3770a99 100644
--- a/mikeio/pfs/_pfsdocument.py
+++ b/mikeio/pfs/_pfsdocument.py
@@ -89,7 +89,6 @@ class PfsDocument(PfsSection):
         names=None,
         unique_keywords=False,
     ):
-
         if isinstance(data, (str, Path)) or hasattr(data, "read"):
             if names is not None:
                 raise ValueError("names cannot be given as argument if input is a file")
@@ -137,6 +136,12 @@ class PfsDocument(PfsSection):
         """Return a new view of the PfsDocument's items ((key, value) pairs)"""
         return [(k, v) for k, v in self.__dict__.items() if k not in self._ALIAS_LIST]
 
+    def to_dict(self) -> dict:
+        """Convert to (nested) dict (as a copy)"""
+        d = super().to_dict()
+        _ = d.pop("_ALIAS_LIST")
+        return d
+
     @staticmethod
     def _unravel_items(items: Callable) -> Tuple[List, List]:
         rkeys = []
@@ -312,11 +317,6 @@ class PfsDocument(PfsSection):
                 key = s[0:idx]
                 key = key.strip()
                 value = s[(idx + 1) :].strip()
-
-                if key == "start_time":
-                    value = datetime.strptime(value, "%Y, %m, %d, %H, %M, %S").strftime(
-                        "%Y-%m-%d %H:%M:%S"
-                    )
                 value = self._parse_param(value)
                 s = f"{key}: {value}"
 
diff --git a/mikeio/pfs/_pfssection.py b/mikeio/pfs/_pfssection.py
index 4e4da08c..aa5aa236 100644
--- a/mikeio/pfs/_pfssection.py
+++ b/mikeio/pfs/_pfssection.py
@@ -376,7 +376,7 @@ class PfsSection(SimpleNamespace, MutableMapping):
         """Convert to (nested) dict (as a copy)"""
         d = self.__dict__.copy()
         for key, value in d.items():
-            if isinstance(value, self.__class__):
+            if isinstance(value, PfsSection):
                 d[key] = value.to_dict()
         return d
 


Test patch:
diff --git a/tests/test_pfs.py b/tests/test_pfs.py
index ea92a7fa..f74ae350 100644
--- a/tests/test_pfs.py
+++ b/tests/test_pfs.py
@@ -273,7 +273,6 @@ def test_str_is_scientific_float(d1):
 
 
 def test_basic():
-
     pfs = mikeio.PfsDocument("tests/testdata/pfs/simple.pfs")
 
     data = pfs.targets[0]
@@ -392,7 +391,6 @@ def test_read_write_filenames_modified(tmp_path):
 
 
 def test_sw():
-
     pfs = mikeio.PfsDocument("tests/testdata/pfs/lake.sw")
     assert pfs.targets[0] == pfs.FemEngineSW
     root = pfs.targets[0]
@@ -406,8 +404,10 @@ def test_sw():
 
     assert root.TIME.number_of_time_steps == 450
 
-    assert root.TIME.start_time.year == 2002
-    assert root.TIME.start_time.month == 1
+    # we're no longer parsing datetimes in Pfs module
+    start_time = datetime(*root.TIME.start_time)
+    assert start_time.year == 2002
+    assert start_time.month == 1
 
 
 def test_pfssection_to_dataframe():
@@ -418,7 +418,6 @@ def test_pfssection_to_dataframe():
 
 
 def test_hd_outputs():
-
     with pytest.warns(match="defined multiple times"):
         pfs = mikeio.PfsDocument("tests/testdata/pfs/lake.m21fm", unique_keywords=True)
     df = pfs.HD.OUTPUTS.to_dataframe()
@@ -428,7 +427,6 @@ def test_hd_outputs():
 
 
 def test_included_outputs():
-
     pfs = mikeio.PfsDocument("tests/testdata/pfs/lake.sw")
     df = pfs.SW.OUTPUTS.to_dataframe(prefix="OUTPUT_")
     df = df[df.include == 1]
@@ -438,7 +436,6 @@ def test_included_outputs():
 
 
 def test_output_by_id():
-
     pfs = mikeio.PfsDocument("tests/testdata/pfs/lake.sw")
     df = pfs.SW.OUTPUTS.to_dataframe()
     # .loc refers to output_id irrespective of included or not
@@ -557,7 +554,6 @@ def test_mdf():
 
 
 def test_read_in_memory_string():
-
     text = """
 [ENGINE]
   option = foo,bar
@@ -569,7 +565,6 @@ EndSect // ENGINE
 
 
 def test_read_mixed_array():
-
     text = """
 [ENGINE]
   advanced= false
@@ -586,7 +581,6 @@ EndSect // ENGINE
 
 
 def test_read_mixed_array2():
-
     text = """
 [ENGINE]
   fill_list = 'dsd', 0, 0.0, false
@@ -602,7 +596,6 @@ EndSect // ENGINE
 
 
 def test_read_mixed_array3():
-
     text = """
 [ENGINE]
   fill_list = 'dsd', 0, 0.0, "str2", false, 'str3'
@@ -620,7 +613,6 @@ EndSect // ENGINE
 
 
 def test_read_array():
-
     text = """
 [ENGINE]
   fill_list = 1, 2
@@ -635,7 +627,6 @@ EndSect // ENGINE
 
 
 def test_empty(tmp_path):
-
     text = """
 [ENGINE]
   A = 
@@ -662,7 +653,6 @@ EndSect // ENGINE
 
 
 def test_difficult_chars_in_str(tmp_path):
-
     text = """
 [ENGINE]
   A = 'str,s/d\sd.dfs0'
@@ -700,7 +690,6 @@ EndSect // ENGINE
 
 
 def test_difficult_chars_in_str2(tmp_path):
-
     text = """
 [ENGINE]
    A = 'str,s/d\sd.dfs0'
@@ -737,7 +726,6 @@ EndSect  // Results
 
 
 def test_read_string_array():
-
     text = """
 [ENGINE]
   fill_list = 'foo', 'bar', 'baz'
@@ -1056,7 +1044,6 @@ def test_nonunique_mixed_keywords_sections2(tmp_path):
 
 
 def test_parse_mike_she_pfs():
-
     pfs = mikeio.PfsDocument("tests/testdata/pfs/Karup_basic.she")
 
     assert pfs.n_targets == 2