Skip to content

Commit a7b577d

Browse files
committed
use v2 dfns
1 parent aafab8a commit a7b577d

File tree

134 files changed

+534
-1016
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

134 files changed

+534
-1016
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ venv/
99
*.egg-info
1010
temp/
1111
.coverage
12+
flopy4/mf6/codec/reader/grammar/generated/*.lark

flopy4/mf6/codec/filters.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Filters shared by both reader and writer."""
2+
3+
from typing import Any
4+
5+
import xarray as xr
6+
from modflow_devtools.dfn.schema.field import Field
7+
from modflow_devtools.dfn.schema.v2 import FieldType
8+
9+
10+
def field_type(value: Any) -> FieldType:
11+
"""Get a value's type according to the MF6 specification."""
12+
13+
if isinstance(value, Field):
14+
return value.type
15+
if isinstance(value, bool):
16+
return "keyword"
17+
if isinstance(value, int):
18+
return "integer"
19+
if isinstance(value, float):
20+
return "double"
21+
if isinstance(value, str):
22+
return "string"
23+
if isinstance(value, tuple):
24+
return "record"
25+
if isinstance(value, xr.DataArray):
26+
if value.dtype == "object":
27+
return "list"
28+
return "array"
29+
if isinstance(value, (list, dict, xr.Dataset)):
30+
return "list"
31+
raise ValueError(f"Unsupported field type: {type(value)}")

flopy4/mf6/codec/reader/dfn2lark.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from os import PathLike
55
from pathlib import Path
66

7-
from modflow_devtools.dfn import Dfn
7+
from modflow_devtools.dfn import load_flat, map
88

99
from flopy4.mf6.codec.reader.grammar import make_all_grammars
1010

@@ -17,11 +17,13 @@ def generate(dfndir: PathLike, outdir: PathLike):
1717
dfndir = Path(dfndir).expanduser().absolute()
1818
outdir = Path(outdir).expanduser().absolute()
1919
outdir.mkdir(exist_ok=True, parents=True)
20-
dfns = Dfn.load_all(dfndir, version=2)
21-
make_all_grammars(dfns, outdir)
20+
dfns_v1 = load_flat(dfndir)
21+
dfns_v2 = {name: map(dfn, schema_version=2) for name, dfn in dfns_v1.items()}
22+
# TODO fix devtools v1 -> v2 map, it's not converting the field type
23+
make_all_grammars(dfns_v2, outdir)
2224

2325

24-
if __name__ == "__main__":
26+
def main():
2527
parser = argparse.ArgumentParser(description="Generate lark grammars from DFNs.")
2628
parser.add_argument(
2729
"--dfndir",
@@ -37,3 +39,7 @@ def generate(dfndir: PathLike, outdir: PathLike):
3739
)
3840
args = parser.parse_args()
3941
generate(args.dfndir, args.outdir)
42+
43+
44+
if __name__ == "__main__":
45+
main()

flopy4/mf6/codec/reader/grammar/__init__.py

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,9 @@
22
from pathlib import Path
33

44
import jinja2
5-
from modflow_devtools.dfn import Dfn, get_blocks, get_fields
5+
from modflow_devtools.dfn import Dfn
66

7-
from flopy4.mf6.codec.reader.grammar.filters import (
8-
field_type,
9-
keystring_children,
10-
record_child_type,
11-
)
7+
from flopy4.mf6.codec.reader.grammar import filters
128

139

1410
def _get_template_env():
@@ -19,9 +15,9 @@ def _get_template_env():
1915
lstrip_blocks=True,
2016
keep_trailing_newline=True,
2117
)
22-
env.filters["field_type"] = field_type
23-
env.filters["record_child_type"] = record_child_type
24-
env.filters["keystring_children"] = keystring_children
18+
env.filters["field_type"] = filters.field_type
19+
env.filters["record_child_type"] = filters.record_child_type
20+
env.filters["keystring_children"] = filters.keystring_children
2521
return env
2622

2723

@@ -30,12 +26,10 @@ def make_grammar(dfn: Dfn, outdir: PathLike):
3026
outdir = Path(outdir).expanduser().resolve().absolute()
3127
env = _get_template_env()
3228
template = env.get_template("component.lark.jinja")
33-
target_path = outdir / f"{dfn['name']}.lark"
29+
target_path = outdir / f"{dfn.name}.lark"
3430
with open(target_path, "w") as f:
35-
name = dfn["name"]
36-
blocks = get_blocks(dfn)
37-
fields = get_fields(dfn)
38-
f.write(template.render(name=name, blocks=blocks, fields=fields))
31+
name = dfn.name
32+
f.write(template.render(name=name, blocks=dfn.blocks, fields=dfn.fields))
3933

4034

4135
def make_all_grammars(dfns: dict[str, Dfn], outdir: PathLike):
Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,33 @@
1-
from modflow_devtools.dfn import Field
1+
from modflow_devtools.dfn.schema.v2 import FieldV2
22

33

4-
def field_type(field: Field) -> str:
5-
match field["type"]:
6-
case t if t in ["string", "integer", "double precision"] and "shape" in field:
7-
if "period" in field["block"]:
4+
def field_type(field: FieldV2) -> str:
5+
match field.type:
6+
case t if t in ["string", "integer", "double"] and field.shape:
7+
if "period" in field.block:
88
return "list"
99
return "array"
1010
case "keyword":
1111
return ""
12-
case "keystring":
12+
case "union":
1313
return "" # keystrings generate their own union rules
1414
case _:
15-
return field["type"]
15+
return field.type
1616

1717

18-
def record_child_type(field: Field) -> str:
18+
def record_child_type(field: FieldV2) -> str:
1919
"""Get the grammar type for a field within a record context."""
20-
match field["type"]:
21-
case "string":
22-
return "string"
23-
case "integer":
24-
return "integer"
25-
case "double precision":
26-
return "double"
20+
match field.type:
21+
case t if t in ["string", "double", "integer"]:
22+
return t
2723
case "keyword":
2824
return ""
29-
case "keystring":
25+
case "union":
3026
return "" # keystrings generate their own union rules
3127
case _:
32-
return field["type"]
28+
return field.type
3329

3430

35-
def keystring_children(field: Field) -> dict:
31+
def keystring_children(field: FieldV2) -> dict:
3632
"""Get the children of a keystring field for union generation."""
37-
if field["type"] != "keystring":
38-
return {}
39-
return field.get("children", {})
33+
return {} if field.type != "union" else field.children

flopy4/mf6/codec/reader/grammar/generated/chf-cdb.lark

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ boundnames: "boundnames"i
1414
print_input: "print_input"i
1515
print_flows: "print_flows"i
1616
save_flows: "save_flows"i
17-
obs_filerecord: "obs6"i "filein"i string
17+
obs_filerecord: "filein"i "obs6"i string
1818
maxbound: "maxbound"i integer
1919
idcxs: "idcxs"i list
20-
width: "width"i list
21-
aux: "aux"i list
20+
width: "width"i double precision
21+
aux: "aux"i double precision
2222
boundname: "boundname"i list

flopy4/mf6/codec/reader/grammar/generated/chf-chd.lark

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ print_input: "print_input"i
1616
print_flows: "print_flows"i
1717
save_flows: "save_flows"i
1818
ts_filerecord: "ts6"i "filein"i string
19-
obs_filerecord: "obs6"i "filein"i string
19+
obs_filerecord: "filein"i "obs6"i string
2020
maxbound: "maxbound"i integer
21-
head: "head"i list
22-
aux: "aux"i list
21+
head: "head"i double precision
22+
aux: "aux"i double precision
2323
boundname: "boundname"i list

flopy4/mf6/codec/reader/grammar/generated/chf-dfw.lark

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ save_velocity: "save_velocity"i
1616
obs_filerecord: "obs6"i "filein"i string
1717
export_array_ascii: "export_array_ascii"i
1818
dev_swr_conductance: "dev_swr_conductance"i
19-
manningsn: "manningsn"i array
19+
manningsn: "manningsn"i double precision
2020
idcxs: "idcxs"i array

flopy4/mf6/codec/reader/grammar/generated/chf-disv1d.lark

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ export_array_ascii: "export_array_ascii"i
2323
crs: "crs"i array
2424
nodes: "nodes"i integer
2525
nvert: "nvert"i integer
26-
width: "width"i array
27-
bottom: "bottom"i array
26+
width: "width"i double precision
27+
bottom: "bottom"i double precision
2828
idomain: "idomain"i array
2929
vertices: "vertices"i recarray
3030
cell1d: "cell1d"i recarray

flopy4/mf6/codec/reader/grammar/generated/chf-evp.lark

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@ print_input: "print_input"i
1616
print_flows: "print_flows"i
1717
save_flows: "save_flows"i
1818
ts_filerecord: "ts6"i "filein"i string
19-
obs_filerecord: "obs6"i "filein"i string
19+
obs_filerecord: "filein"i "obs6"i string
2020
maxbound: "maxbound"i integer
21-
evaporation: "evaporation"i list
22-
aux: "aux"i list
21+
evaporation: "evaporation"i double precision
22+
aux: "aux"i double precision
2323
boundname: "boundname"i list

0 commit comments

Comments
 (0)