Skip to content

Commit 7d41fcc

Browse files
author
wpbonelli
committed
path -> record conversion
1 parent 7c8c04b commit 7d41fcc

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

flopy4/mf6/codec/writer/templates/macros.jinja

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,13 @@
2323
{% endmacro %}
2424

2525
{% macro record(name, value) %}
26-
{% for itemitem in value.values() -%}
27-
{% if item.tagged %}{{ item.name.upper() }} {% endif %}{{ field(item) }}
26+
{% if value is mapping %}
27+
{% for item in value.values() -%}
28+
{{ item.name.upper() }} {{ field(item) }}
2829
{%- endfor %}
30+
{% else %}
31+
{{ value|join(" ") }}
32+
{% endif %}
2933
{% endmacro %}
3034

3135
{% macro recarray(name, value, how="internal") %}

flopy4/mf6/converter.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from pathlib import Path
12
from typing import Any
23

34
import xattree
@@ -7,14 +8,31 @@
78
from flopy4.mf6.spec import get_blocks
89

910

11+
def _transform_path_to_record(field_name: str, path_value: Path) -> tuple:
12+
"""Transform a Path field to its corresponding MF6 record format."""
13+
# Infer record structure from field name
14+
if field_name.endswith("_file"):
15+
base_name = field_name.replace("_file", "").upper()
16+
return (base_name, "FILEOUT", str(path_value))
17+
18+
# Default fallback
19+
return (field_name.upper(), "FILEOUT", str(path_value))
20+
21+
1022
def unstructure_component(value: Component) -> dict[str, Any]:
1123
data = xattree.asdict(value)
1224
blockspec = get_blocks(value.dfn)
1325
blocks = {}
1426
for block_name, block in blockspec.items():
1527
blocks[block_name] = {}
1628
for field_name in block.keys():
17-
blocks[block_name][field_name] = data[field_name]
29+
field_value = data[field_name]
30+
31+
# Transform Path fields to record format
32+
if isinstance(field_value, Path) and field_value is not None:
33+
field_value = _transform_path_to_record(field_name, field_value)
34+
35+
blocks[block_name][field_name] = field_value
1836
return blocks
1937

2038

0 commit comments

Comments
 (0)