Skip to content

Commit 40a0cb5

Browse files
committed
fix
1 parent 52bffaf commit 40a0cb5

File tree

6 files changed

+45
-45
lines changed

6 files changed

+45
-45
lines changed

flopy4/mf6/__init__.py

Lines changed: 4 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,8 @@
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

7-
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-
)
5+
# register io methods
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))

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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
if self.workspace is None:
16+
self.workspace = Path.cwd()
17+
18+
@property
19+
def path(self) -> Path:
20+
return self.workspace / self.filename

flopy4/mf6/gwf/dis.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,6 @@ class Dis(Package):
7575
)
7676

7777
def __attrs_post_init__(self):
78-
super().__attrs_post_init__()
7978
self.nnodes = self.ncol * self.nrow * self.nlay
8079

8180
def to_grid(self) -> StructuredGrid:

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: 5 additions & 17 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,30 +22,21 @@ 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):
35-
super().__attrs_post_init__()
3633
if self.filename != "mfsim.nam":
3734
warn(
3835
"Simulation filename must be 'mfsim.nam'.",
3936
UserWarning,
4037
)
4138
self.filename = "mfsim.nam"
4239

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-
5040
@property
5141
def time(self) -> ModelTime:
5242
"""Return the simulation time discretization."""
@@ -66,10 +56,8 @@ def run(self, exe: str | PathLike = "mf6", verbose: bool = False) -> None:
6656

6757
def load(self, format="ascii"):
6858
"""Load the simulation in the specified format."""
69-
with cd(self.workspace):
70-
super().load(format)
59+
super().load(format)
7160

7261
def write(self, format="ascii"):
7362
"""Write the simulation in the specified format."""
74-
with cd(self.workspace):
75-
super().write(format)
63+
super().write(format)

0 commit comments

Comments
 (0)