Skip to content

Commit b7705cc

Browse files
committed
wip
1 parent 52bffaf commit b7705cc

File tree

6 files changed

+46
-38
lines changed

6 files changed

+46
-38
lines changed

flopy4/mf6/__init__.py

Lines changed: 5 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,10 @@
1-
from pathlib import Path
2-
31
from flopy4.mf6.codec import dump, load
42
from flopy4.mf6.component import Component
53
from flopy4.uio import DEFAULT_REGISTRY
64

5+
# register io methods for ascii format input files
6+
# TODO: call this "mf6" or something? since it might include binary files
7+
DEFAULT_REGISTRY.register_loader(Component, "ascii", lambda c: load(c.path))
8+
DEFAULT_REGISTRY.register_writer(Component, "ascii", lambda c: dump(c, c.path))
79

8-
def _default_filename(component: Component) -> str:
9-
"""Default path for a component, based on its name."""
10-
if filename := component.filename:
11-
return filename
12-
name = component.name # type: ignore
13-
cls_name = component.__class__.__name__.lower()
14-
return f"{name}.{cls_name}"
15-
16-
17-
def _path(component: Component) -> str:
18-
"""Default path for a component, based on its name."""
19-
if hasattr(component, "path") and component.path is not None:
20-
path = Path(component.path).expanduser().resolve()
21-
if path.is_dir():
22-
return str(path / _default_filename(component))
23-
return str(path)
24-
return _default_filename(component)
25-
26-
27-
DEFAULT_REGISTRY.register_loader(Component, "ascii", lambda component: load(_path(component)))
28-
DEFAULT_REGISTRY.register_writer(
29-
Component, "ascii", lambda component: dump(component, _path(component))
30-
)
10+
# TODO: explore what a netcdf format for the whole simulation would look like?

flopy4/mf6/component.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from abc import ABC
22
from collections.abc import MutableMapping
3+
from pathlib import Path
34

45
from modflow_devtools.dfn import Dfn, Field
56
from xattree import xattree
@@ -32,11 +33,24 @@ class Component(ABC, MutableMapping):
3233

3334
filename: str = field(default=None)
3435

36+
@property
37+
def path(self) -> Path:
38+
return Path.cwd() / self.filename
39+
40+
def _default_filename(self) -> str:
41+
name = self.name # type: ignore
42+
cls_name = self.__class__.__name__.lower()
43+
return f"{name}.{cls_name}"
44+
3545
@classmethod
3646
def __attrs_init_subclass__(cls):
3747
COMPONENTS[cls.__name__.lower()] = cls
3848
cls.dfn = cls.get_dfn()
3949

50+
def __attrs_post_init__(self):
51+
if not self.filename:
52+
self.filename = self._default_filename()
53+
4054
def __getitem__(self, key):
4155
return self.children[key] # type: ignore
4256

flopy4/mf6/context.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from abc import ABC
2+
from pathlib import Path
3+
4+
from xattree import xattree
5+
6+
from flopy4.mf6.component import Component
7+
from flopy4.mf6.spec import field
8+
9+
10+
@xattree
11+
class Context(Component, ABC):
12+
workspace: Path = field(default=None)
13+
14+
def __attrs_post_init__(self):
15+
super().__attrs_post_init__()
16+
if self.workspace is None:
17+
self.workspace = Path.cwd()
18+
19+
@property
20+
def path(self) -> Path:
21+
return self.workspace / self.filename

flopy4/mf6/interface/flopy3.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import re
22
from typing import Optional
3+
from warnings import warn
34

45
import numpy as np
56
from flopy.datbase import DataInterface, DataListInterface, DataType
@@ -332,7 +333,7 @@ def data_type(self):
332333
return DataType.array3d
333334
# TODO: boundname, auxvar arrays of strings?
334335
case _:
335-
raise Exception(f"UNMATCHED data_type {self._name}: {self._spec.type.__name__}")
336+
warn(f"UNMATCHED data_type {self._name}: {self._spec.type.__name__}", UserWarning)
336337

337338
@property
338339
def dtype(self):

flopy4/mf6/simulation.py

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
from os import PathLike
2-
from pathlib import Path
32
from warnings import warn
43

54
from flopy.discretization.modeltime import ModelTime
65
from modflow_devtools.misc import cd, run_cmd
76
from xattree import xattree
87

9-
from flopy4.mf6.component import Component
8+
from flopy4.mf6.context import Context
109
from flopy4.mf6.exchange import Exchange
1110
from flopy4.mf6.model import Model
1211
from flopy4.mf6.solution import Solution
@@ -23,13 +22,12 @@ def convert_time(value):
2322

2423

2524
@xattree
26-
class Simulation(Component):
25+
class Simulation(Context):
2726
models: dict[str, Model] = field()
2827
exchanges: dict[str, Exchange] = field()
2928
solutions: dict[str, Solution] = field()
3029
tdis: Tdis = field(converter=convert_time)
31-
workspace: Path = field(default=None)
32-
filename: str = field(default="mfsim.nam")
30+
filename: str = field(default="mfsim.nam", init=False)
3331

3432
def __attrs_post_init__(self):
3533
super().__attrs_post_init__()
@@ -40,13 +38,6 @@ def __attrs_post_init__(self):
4038
)
4139
self.filename = "mfsim.nam"
4240

43-
@property
44-
def path(self) -> Path:
45-
"""Return the path to the simulation namefile."""
46-
if self.workspace is None:
47-
raise ValueError("Simulation has no workspace path.")
48-
return Path(self.workspace).expanduser().resolve() / self.filename
49-
5041
@property
5142
def time(self) -> ModelTime:
5243
"""Return the simulation time discretization."""

test/test_interface.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ def test_flopy3_package():
212212

213213
# package data
214214
data_list = [
215+
"filename",
215216
"nogrb",
216217
"xorigin",
217218
"yorigin",

0 commit comments

Comments
 (0)