Skip to content

Commit fb1d893

Browse files
authored
various (#267)
* trial an approach for records containing keywords, including converter * dicts should map to record field type, not list * add minimal npf writing test
1 parent b6998fd commit fb1d893

File tree

6 files changed

+49
-23
lines changed

6 files changed

+49
-23
lines changed

flopy4/mf6/codec/filters.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ def field_type(value: Any) -> FieldType:
2020
return "double"
2121
if isinstance(value, str):
2222
return "string"
23-
if isinstance(value, tuple):
23+
if isinstance(value, (dict, tuple)):
2424
return "record"
2525
if isinstance(value, xr.DataArray):
2626
if value.dtype == "object":
2727
return "list"
2828
return "array"
29-
if isinstance(value, (list, dict, xr.Dataset)):
29+
if isinstance(value, (list, xr.Dataset)):
3030
return "list"
3131
raise ValueError(f"Unsupported field type: {type(value)}")

flopy4/mf6/converter/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from flopy4.mf6.component import Component
1010
from flopy4.mf6.context import Context
11-
from flopy4.mf6.converter.structure import structure_array
11+
from flopy4.mf6.converter.structure import structure_array, structure_keyword
1212
from flopy4.mf6.converter.unstructure import (
1313
unstructure_component,
1414
)
@@ -19,6 +19,7 @@
1919
"unstructure",
2020
"structure_array",
2121
"unstructure_array",
22+
"structure_keyword",
2223
"COMPONENT_CONVERTER",
2324
]
2425

flopy4/mf6/converter/structure.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
from flopy4.mf6.constants import FILL_DNODATA
1111

1212

13+
def structure_keyword(value, field) -> str | None:
14+
return field.name if value else None
15+
16+
1317
def structure_array(value, self_, field) -> NDArray:
1418
"""
1519
Convert a sparse dictionary representation of an array to a

flopy4/mf6/gwf/npf.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
from pathlib import Path
2-
from typing import Optional
2+
from typing import Literal, Optional
33

4+
import attrs
45
import numpy as np
56
from attrs import Converter, define
67
from numpy.typing import NDArray
78
from xattree import xattree
89

9-
from flopy4.mf6.converter import structure_array
10+
from flopy4.mf6.converter import structure_array, structure_keyword
1011
from flopy4.mf6.package import Package
1112
from flopy4.mf6.spec import array, field, path
1213
from flopy4.utils import to_path
@@ -16,8 +17,12 @@
1617
class Npf(Package):
1718
@define(slots=False)
1819
class CvOptions:
19-
variablecv: bool = field(default=False)
20-
dewatered: bool = field(default=False)
20+
variablecv: Literal["variablecv"] = attrs.field(init=False, default="variablecv")
21+
dewatered: Literal["dewatered"] | None = attrs.field(
22+
default=None,
23+
# TODO: adopt this pattern for all record types in all components?
24+
converter=Converter(structure_keyword, takes_field=True), # type: ignore
25+
)
2126

2227
@define(slots=False)
2328
class RewetRecord:

0 commit comments

Comments
 (0)