Skip to content

Commit ebf53b9

Browse files
committed
progress. period data looking better
1 parent 4c39153 commit ebf53b9

Some content is hidden

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

93 files changed

+838
-427
lines changed

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ def _get_template_env():
1818
env.filters["field_type"] = filters.field_type
1919
env.filters["record_child_type"] = filters.record_child_type
2020
env.filters["keystring_children"] = filters.keystring_children
21+
env.filters["group_period_fields"] = filters.group_period_fields
22+
env.filters["get_recarray_name"] = filters.get_recarray_name
23+
env.filters["get_recarray_columns"] = filters.get_recarray_columns
24+
env.filters["get_all_grouped_field_names"] = filters.get_all_grouped_field_names
2125
return env
2226

2327

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

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from collections.abc import Mapping
2+
13
from modflow_devtools.dfn.schema.v2 import FieldV2
24

35

@@ -31,3 +33,86 @@ def record_child_type(field: FieldV2) -> str:
3133
def keystring_children(field: FieldV2) -> dict:
3234
"""Get the children of a keystring field for union generation."""
3335
return {} if field.type != "union" else field.children
36+
37+
38+
def is_period_list_field(field: FieldV2) -> bool:
39+
"""Check if a field is part of a period block list/recarray."""
40+
if not field.shape or not field.block:
41+
return False
42+
return (
43+
"period" in field.block
44+
and field.type in ["string", "integer", "double"]
45+
and field.shape is not None
46+
)
47+
48+
49+
def group_period_fields(block_fields: Mapping[str, FieldV2]) -> dict[str, list[str]]:
50+
"""
51+
Group period block fields that should be combined into a single list.
52+
53+
Returns a dict mapping the first field name to a list of all field names
54+
in the group. Fields are grouped if they share similar shapes (same base
55+
dimensions like nper, nnodes).
56+
"""
57+
period_fields = {
58+
name: field for name, field in block_fields.items() if is_period_list_field(field)
59+
}
60+
61+
if not period_fields:
62+
return {}
63+
64+
# All period fields in the same block should be combined into one recarray
65+
# Return a single group with all field names
66+
field_names = list(period_fields.keys())
67+
if field_names:
68+
return {field_names[0]: field_names}
69+
return {}
70+
71+
72+
def get_recarray_name(block_name: str) -> str:
73+
"""Get the name for a recarray representing period data in a block."""
74+
# Use similar naming to V1: stress_period_data, perioddata, etc.
75+
if block_name == "period":
76+
return "stress_period_data"
77+
return f"{block_name}data"
78+
79+
80+
def get_recarray_columns(field_names: list[str], block_fields: Mapping[str, FieldV2]) -> list[str]:
81+
"""
82+
Get column names for a recarray, similar to V1 format.
83+
84+
Returns column names in format like: ['cellid', 'q', 'aux', 'boundname']
85+
"""
86+
columns = []
87+
88+
# Check if any field has spatial dimensions (indicates cellid is needed)
89+
has_spatial = False
90+
for name in field_names:
91+
field = block_fields[name]
92+
if field.shape and any(
93+
dim in field.shape for dim in ["nnodes", "ncells", "nlay", "nrow", "ncol"]
94+
):
95+
has_spatial = True
96+
break
97+
98+
if has_spatial:
99+
columns.append("cellid")
100+
101+
# Add the field names as columns
102+
columns.extend(field_names)
103+
104+
return columns
105+
106+
107+
def get_all_grouped_field_names(blocks: Mapping[str, Mapping[str, FieldV2]]) -> set[str]:
108+
"""
109+
Get all field names that are grouped into recarrays across all blocks.
110+
111+
Returns a set of field names that should not have individual rules generated.
112+
"""
113+
grouped = set()
114+
for block_fields in blocks.values():
115+
period_groups = group_period_fields(block_fields)
116+
for field_list in period_groups.values():
117+
grouped.update(field_list)
118+
return grouped

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@ dimensions_block: "begin"i "dimensions"i dimensions_fields "end"i "dimensions"i
88
period_block: "begin"i "period"i period_fields "end"i "period"i
99
options_fields: (auxiliary | boundnames | print_input | print_flows | save_flows | obs_filerecord)*
1010
dimensions_fields: (maxbound)*
11-
period_fields: (idcxs | width | aux | boundname)*
11+
period_fields: (stress_period_data)*
1212
auxiliary: "auxiliary"i array
1313
boundnames: "boundnames"i
1414
print_input: "print_input"i
1515
print_flows: "print_flows"i
1616
save_flows: "save_flows"i
1717
obs_filerecord: "filein"i "obs6"i string
1818
maxbound: "maxbound"i integer
19-
idcxs: "idcxs"i list
20-
width: "width"i double precision
21-
aux: "aux"i double precision
22-
boundname: "boundname"i list
19+
stress_period_data: cellid idcxs width aux boundname
20+
cellid: integer+
21+
idcxs: integer
22+
width: double
23+
aux: double
24+
boundname: string

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dimensions_block: "begin"i "dimensions"i dimensions_fields "end"i "dimensions"i
88
period_block: "begin"i "period"i period_fields "end"i "period"i
99
options_fields: (auxiliary | auxmultname | boundnames | print_input | print_flows | save_flows | ts_filerecord | obs_filerecord)*
1010
dimensions_fields: (maxbound)*
11-
period_fields: (head | aux | boundname)*
11+
period_fields: (stress_period_data)*
1212
auxiliary: "auxiliary"i array
1313
auxmultname: "auxmultname"i string
1414
boundnames: "boundnames"i
@@ -18,6 +18,8 @@ save_flows: "save_flows"i
1818
ts_filerecord: "ts6"i "filein"i string
1919
obs_filerecord: "filein"i "obs6"i string
2020
maxbound: "maxbound"i integer
21-
head: "head"i double precision
22-
aux: "aux"i double precision
23-
boundname: "boundname"i list
21+
stress_period_data: cellid head aux boundname
22+
cellid: integer+
23+
head: double
24+
aux: double
25+
boundname: string

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ griddata_block: "begin"i "griddata"i griddata_fields "end"i "griddata"i
88
options_fields: (central_in_space | length_conversion | time_conversion | save_flows | print_flows | save_velocity | obs_filerecord | export_array_ascii | dev_swr_conductance)*
99
griddata_fields: (manningsn | idcxs)*
1010
central_in_space: "central_in_space"i
11-
length_conversion: "length_conversion"i double precision
12-
time_conversion: "time_conversion"i double precision
11+
length_conversion: "length_conversion"i double
12+
time_conversion: "time_conversion"i double
1313
save_flows: "save_flows"i
1414
print_flows: "print_flows"i
1515
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 double precision
19+
manningsn: "manningsn"i array
2020
idcxs: "idcxs"i array

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,15 @@ cell1d_fields: (cell1d)*
1616
length_units: "length_units"i string
1717
nogrb: "nogrb"i
1818
grb_filerecord: "grb6"i "fileout"i string
19-
xorigin: "xorigin"i double precision
20-
yorigin: "yorigin"i double precision
21-
angrot: "angrot"i double precision
19+
xorigin: "xorigin"i double
20+
yorigin: "yorigin"i double
21+
angrot: "angrot"i double
2222
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 double precision
27-
bottom: "bottom"i double precision
26+
width: "width"i array
27+
bottom: "bottom"i array
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: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dimensions_block: "begin"i "dimensions"i dimensions_fields "end"i "dimensions"i
88
period_block: "begin"i "period"i period_fields "end"i "period"i
99
options_fields: (auxiliary | auxmultname | boundnames | print_input | print_flows | save_flows | ts_filerecord | obs_filerecord)*
1010
dimensions_fields: (maxbound)*
11-
period_fields: (evaporation | aux | boundname)*
11+
period_fields: (stress_period_data)*
1212
auxiliary: "auxiliary"i array
1313
auxmultname: "auxmultname"i string
1414
boundnames: "boundnames"i
@@ -18,6 +18,8 @@ save_flows: "save_flows"i
1818
ts_filerecord: "ts6"i "filein"i string
1919
obs_filerecord: "filein"i "obs6"i string
2020
maxbound: "maxbound"i integer
21-
evaporation: "evaporation"i double precision
22-
aux: "aux"i double precision
23-
boundname: "boundname"i list
21+
stress_period_data: cellid evaporation aux boundname
22+
cellid: integer+
23+
evaporation: double
24+
aux: double
25+
boundname: string

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dimensions_block: "begin"i "dimensions"i dimensions_fields "end"i "dimensions"i
88
period_block: "begin"i "period"i period_fields "end"i "period"i
99
options_fields: (auxiliary | auxmultname | boundnames | print_input | print_flows | save_flows | ts_filerecord | obs_filerecord)*
1010
dimensions_fields: (maxbound)*
11-
period_fields: (q | aux | boundname)*
11+
period_fields: (stress_period_data)*
1212
auxiliary: "auxiliary"i array
1313
auxmultname: "auxmultname"i string
1414
boundnames: "boundnames"i
@@ -18,6 +18,8 @@ save_flows: "save_flows"i
1818
ts_filerecord: "ts6"i "filein"i string
1919
obs_filerecord: "filein"i "obs6"i string
2020
maxbound: "maxbound"i integer
21-
q: "q"i double precision
22-
aux: "aux"i double precision
23-
boundname: "boundname"i list
21+
stress_period_data: cellid q aux boundname
22+
cellid: integer+
23+
q: double
24+
aux: double
25+
boundname: string

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ griddata_block: "begin"i "griddata"i griddata_fields "end"i "griddata"i
88
options_fields: (export_array_ascii)*
99
griddata_fields: (strt)*
1010
export_array_ascii: "export_array_ascii"i
11-
strt: "strt"i double precision
11+
strt: "strt"i array

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ dimensions_block: "begin"i "dimensions"i dimensions_fields "end"i "dimensions"i
88
period_block: "begin"i "period"i period_fields "end"i "period"i
99
options_fields: (auxiliary | auxmultname | boundnames | print_input | print_flows | save_flows | ts_filerecord | obs_filerecord)*
1010
dimensions_fields: (maxbound)*
11-
period_fields: (precipitation | aux | boundname)*
11+
period_fields: (stress_period_data)*
1212
auxiliary: "auxiliary"i array
1313
auxmultname: "auxmultname"i string
1414
boundnames: "boundnames"i
@@ -18,6 +18,8 @@ save_flows: "save_flows"i
1818
ts_filerecord: "ts6"i "filein"i string
1919
obs_filerecord: "filein"i "obs6"i string
2020
maxbound: "maxbound"i integer
21-
precipitation: "precipitation"i double precision
22-
aux: "aux"i double precision
23-
boundname: "boundname"i list
21+
stress_period_data: cellid precipitation aux boundname
22+
cellid: integer+
23+
precipitation: double
24+
aux: double
25+
boundname: string

0 commit comments

Comments
 (0)