Skip to content

Commit

Permalink
Merge pull request #611 from DHI/pfs-improvements
Browse files Browse the repository at this point in the history
Pfs improvements
  • Loading branch information
ecomodeller authored Mar 13, 2024
2 parents c125cb8 + c12e966 commit a6800c6
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 24 deletions.
12 changes: 6 additions & 6 deletions mikeio/pfs/_pfsdocument.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ def __init__(
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")
Expand Down Expand Up @@ -137,6 +136,12 @@ def items(self):
"""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 = []
Expand Down Expand Up @@ -312,11 +317,6 @@ def _parse_line(self, line: str, level: int = 0) -> Tuple[str, int]:
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}"

Expand Down
2 changes: 1 addition & 1 deletion mikeio/pfs/_pfssection.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ def to_dict(self) -> dict:
"""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

Expand Down
21 changes: 4 additions & 17 deletions tests/test_pfs.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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]
Expand All @@ -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():
Expand All @@ -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()
Expand All @@ -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]
Expand All @@ -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
Expand Down Expand Up @@ -557,7 +554,6 @@ def test_mdf():


def test_read_in_memory_string():

text = """
[ENGINE]
option = foo,bar
Expand All @@ -569,7 +565,6 @@ def test_read_in_memory_string():


def test_read_mixed_array():

text = """
[ENGINE]
advanced= false
Expand All @@ -586,7 +581,6 @@ def test_read_mixed_array():


def test_read_mixed_array2():

text = """
[ENGINE]
fill_list = 'dsd', 0, 0.0, false
Expand All @@ -602,7 +596,6 @@ def test_read_mixed_array2():


def test_read_mixed_array3():

text = """
[ENGINE]
fill_list = 'dsd', 0, 0.0, "str2", false, 'str3'
Expand All @@ -620,7 +613,6 @@ def test_read_mixed_array3():


def test_read_array():

text = """
[ENGINE]
fill_list = 1, 2
Expand All @@ -635,7 +627,6 @@ def test_read_array():


def test_empty(tmp_path):

text = """
[ENGINE]
A =
Expand All @@ -662,7 +653,6 @@ def test_empty(tmp_path):


def test_difficult_chars_in_str(tmp_path):

text = """
[ENGINE]
A = 'str,s/d\sd.dfs0'
Expand Down Expand Up @@ -700,7 +690,6 @@ def test_difficult_chars_in_str(tmp_path):


def test_difficult_chars_in_str2(tmp_path):

text = """
[ENGINE]
A = 'str,s/d\sd.dfs0'
Expand Down Expand Up @@ -737,7 +726,6 @@ def test_end_of_stream():


def test_read_string_array():

text = """
[ENGINE]
fill_list = 'foo', 'bar', 'baz'
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit a6800c6

Please sign in to comment.