Skip to content

Commit f836921

Browse files
committed
structure dfn attr (still broken)
1 parent 43d8967 commit f836921

File tree

11 files changed

+513
-541
lines changed

11 files changed

+513
-541
lines changed

flopy/mf6/data/mfdatautil.py

Lines changed: 0 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -1054,89 +1054,3 @@ def empty(
10541054
return template
10551055
else:
10561056
return rec_array
1057-
1058-
1059-
class MFDocString:
1060-
"""
1061-
Helps build a python class doc string
1062-
1063-
Parameters
1064-
----------
1065-
description : string
1066-
description of the class
1067-
1068-
Attributes
1069-
----------
1070-
indent: string
1071-
indent to use in doc string
1072-
description : string
1073-
description of the class
1074-
parameter_header : string
1075-
header for parameter section of doc string
1076-
parameters : list
1077-
list of docstrings for class parameters
1078-
1079-
Methods
1080-
-------
1081-
add_parameter : (param_descr : string, beginning_of_list : bool)
1082-
adds doc string for a parameter with description 'param_descr' to the
1083-
end of the list unless beginning_of_list is True
1084-
get_doc_string : () : string
1085-
builds and returns the docstring for the class
1086-
"""
1087-
1088-
def __init__(self, description):
1089-
self.indent = " "
1090-
self.description = description
1091-
self.parameter_header = (
1092-
f"{self.indent}Parameters\n{self.indent}----------"
1093-
)
1094-
self.parameters = []
1095-
self.model_parameters = []
1096-
1097-
def add_parameter(
1098-
self, param_descr, beginning_of_list=False, model_parameter=False
1099-
):
1100-
if beginning_of_list:
1101-
self.parameters.insert(0, param_descr)
1102-
if model_parameter:
1103-
self.model_parameters.insert(0, param_descr)
1104-
else:
1105-
self.parameters.append(param_descr)
1106-
if model_parameter:
1107-
self.model_parameters.append(param_descr)
1108-
1109-
def get_doc_string(self, model_doc_string=False, sim_doc_string=False):
1110-
doc_string = '{}"""\n{}{}\n\n{}\n'.format(
1111-
self.indent, self.indent, self.description, self.parameter_header
1112-
)
1113-
if model_doc_string:
1114-
param_list = self.model_parameters
1115-
doc_string = (
1116-
"{} modelname : string\n name of the "
1117-
"model\n model_nam_file : string\n"
1118-
" relative path to the model name file from "
1119-
"model working folder\n version : string\n"
1120-
" version of modflow\n exe_name : string\n"
1121-
" model executable name\n"
1122-
" model_ws : string\n"
1123-
" model working folder path"
1124-
"\n".format(doc_string)
1125-
)
1126-
else:
1127-
param_list = self.parameters
1128-
for parameter in param_list:
1129-
if sim_doc_string:
1130-
pclean = parameter.strip()
1131-
if (
1132-
pclean.startswith("simulation")
1133-
or pclean.startswith("loading_package")
1134-
or pclean.startswith("filename")
1135-
or pclean.startswith("pname")
1136-
or pclean.startswith("parent_file")
1137-
):
1138-
continue
1139-
doc_string += f"{parameter}\n"
1140-
if not (model_doc_string or sim_doc_string):
1141-
doc_string += f'\n{self.indent}"""'
1142-
return doc_string

flopy/mf6/data/mfstructure.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1781,7 +1781,6 @@ class MFInputFileStructure:
17811781

17821782
def __init__(self, dfn_file, path, common, model_file):
17831783
# initialize
1784-
self.valid = True
17851784
self.file_type = dfn_file.package_type
17861785
self.file_prefix = dfn_file.package_prefix
17871786
self.dfn_type = dfn_file.dfn_type

flopy/mf6/mfsimbase.py

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -483,15 +483,6 @@ def __init__(
483483
self.simulation_data.lazy_io = True
484484
self._package_container = PackageContainer(self.simulation_data)
485485

486-
# verify metadata
487-
fpdata = mfstructure.MFStructure()
488-
if not fpdata.valid:
489-
excpt_str = (
490-
"Invalid package metadata. Unable to load MODFLOW "
491-
"file structure metadata."
492-
)
493-
raise FlopyException(excpt_str)
494-
495486
# initialize
496487
self.dimensions = None
497488
self.type = "Simulation"
@@ -503,7 +494,7 @@ def __init__(
503494
self._exchange_files = {}
504495
self._solution_files = {}
505496
self._other_files = {}
506-
self.structure = fpdata.sim_struct
497+
self.structure = mfstructure.MFStructure().sim_struct
507498
self.model_type = None
508499

509500
self._exg_file_num = {}

flopy/mf6/utils/codegen/__init__.py

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,16 @@
22
from os import PathLike
33
from pathlib import Path
44

5-
from flopy.utils import import_optional_dependency
6-
75
__all__ = ["make_init", "make_targets", "make_all"]
86

97

108
def _get_template_env():
119
# import here instead of module so we don't
1210
# expect optional deps at module init time
13-
jinja = import_optional_dependency("jinja2")
14-
loader = jinja.PackageLoader("flopy", "mf6/utils/codegen/templates/")
15-
env = jinja.Environment(
11+
import jinja2
12+
13+
loader = jinja2.PackageLoader("flopy", "mf6/utils/codegen/templates/")
14+
env = jinja2.Environment(
1615
loader=loader,
1716
trim_blocks=True,
1817
lstrip_blocks=True,
@@ -22,22 +21,24 @@ def _get_template_env():
2221

2322
from flopy.mf6.utils.codegen.filters import Filters
2423

25-
env.filters["base"] = Filters.Cls.base
26-
env.filters["title"] = Filters.Cls.title
27-
env.filters["description"] = Filters.Cls.description
28-
env.filters["prefix"] = Filters.Cls.prefix
29-
env.filters["parent"] = Filters.Cls.parent
30-
env.filters["skip_init"] = Filters.Cls.skip_init
31-
env.filters["package_abbr"] = Filters.Cls.package_abbr
32-
env.filters["attrs"] = Filters.Vars.attrs
33-
env.filters["init"] = Filters.Vars.init
34-
env.filters["untag"] = Filters.Var.untag
35-
env.filters["type"] = Filters.Var.type
24+
env.filters["base"] = Filters.base
25+
env.filters["title"] = Filters.title
26+
env.filters["description"] = Filters.description
27+
env.filters["prefix"] = Filters.prefix
28+
env.filters["parent"] = Filters.parent
29+
env.filters["skip_init"] = Filters.skip_init
30+
env.filters["package_abbr"] = Filters.package_abbr
31+
env.filters["attrs"] = Filters.attrs
32+
env.filters["init"] = Filters.init
33+
env.filters["untag"] = Filters.untag
34+
env.filters["type"] = Filters.type
3635
env.filters["safe_name"] = Filters.safe_name
3736
env.filters["escape_trailing_underscore"] = (
3837
Filters.escape_trailing_underscore
3938
)
4039
env.filters["value"] = Filters.value
40+
env.filters["math"] = Filters.math
41+
env.filters["clean"] = Filters.clean
4142

4243
return env
4344

@@ -73,7 +74,7 @@ def make_targets(dfn, outdir: PathLike, verbose: bool = False):
7374

7475
def _get_template_name(ctx_name) -> str:
7576
"""The template file to use."""
76-
base = Filters.Cls.base(ctx_name)
77+
base = Filters.base(ctx_name)
7778
if base == "MFSimulationBase":
7879
return "simulation.py.jinja"
7980
elif base == "MFModel":
@@ -87,7 +88,7 @@ def _get_template_name(ctx_name) -> str:
8788

8889
for context in Context.from_dfn(dfn):
8990
name = context["name"]
90-
target_path = outdir / f"mf{Filters.Cls.title(name)}.py"
91+
target_path = outdir / f"mf{Filters.title(name)}.py"
9192
template = env.get_template(_get_template_name(name))
9293
with open(target_path, "w") as f:
9394
f.write(template.render(**context))

flopy/mf6/utils/codegen/context.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
TypedDict,
77
)
88

9-
from modflow_devtools.dfn import Dfn, Vars
10-
119

1210
class Context(TypedDict):
1311
"""
@@ -37,12 +35,15 @@ class Name(NamedTuple):
3735
r: Optional[str]
3836

3937
@staticmethod
40-
def from_dfn(dfn: Dfn) -> List["Context.Name"]:
38+
def from_dfn(dfn: dict) -> List["Context.Name"]:
4139
"""
4240
Returns a list of context names this definition produces.
4341
An input definition may produce one or more input contexts.
4442
"""
45-
name = dfn["name"].split("-")
43+
name = dfn.get("name", None)
44+
if not name:
45+
raise ValueError(f"DFN must have a 'name' entry")
46+
name = name.split("-")
4647
if name[1] == "nam":
4748
if name[0] == "sim":
4849
return [
@@ -67,22 +68,24 @@ def from_dfn(dfn: Dfn) -> List["Context.Name"]:
6768
return [Context.Name(*name)]
6869

6970
name: Name
70-
vars: Vars
7171

7272
@staticmethod
73-
def from_dfn(dfn: Dfn) -> Iterator["Context"]:
73+
def from_dfn(dfn: dict) -> Iterator["Context"]:
7474
"""
75-
Extract context class descriptor(s) from an input definition.
76-
These are structured representations of input context classes.
77-
78-
Each input definition yields one or more input contexts.
75+
Extract context(s) from an input definition.
76+
Each definition yields one or more contexts.
7977
"""
8078

81-
def _ctx(name, _dfn):
82-
_dfn = _dfn.copy()
83-
_dfn.pop("name", None)
84-
_vars = _dfn.pop("vars", dict())
85-
return Context(name=name, vars=_vars, **_dfn)
79+
def get_vars(dfn_):
80+
vars_ = dict()
81+
blocks = dfn_.get("blocks", dict())
82+
for block in blocks.values():
83+
for name, var in block.items():
84+
vars_[name] = var
85+
return vars_
8686

8787
for name in Context.Name.from_dfn(dfn):
88-
yield _ctx(name, dfn)
88+
dfn_ = dfn.copy()
89+
dfn_.pop("name", None)
90+
vars_ = get_vars(dfn_)
91+
yield Context(name=name, vars=vars_, **dfn_)

0 commit comments

Comments
 (0)