Skip to content

Commit 176894d

Browse files
committed
even more cleanup
1 parent 66a59b2 commit 176894d

File tree

3 files changed

+77
-75
lines changed

3 files changed

+77
-75
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ def _get_template_env():
2020
env.filters["keystring_children"] = filters.keystring_children
2121
env.filters["group_period_fields"] = filters.group_period_fields
2222
env.filters["get_recarray_name"] = filters.get_recarray_name
23-
env.filters["get_recarray_columns"] = filters.get_recarray_columns
2423
env.filters["get_all_grouped_field_names"] = filters.get_all_grouped_field_names
2524
return env
2625

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

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -77,39 +77,6 @@ def get_recarray_name(block_name: str) -> str:
7777
return f"{block_name}data"
7878

7979

80-
def get_recarray_columns(
81-
field_names: list[str], block_fields: Mapping[str, FieldV2]
82-
) -> list[tuple[str, bool]]:
83-
"""
84-
Get column names for a recarray with optionality info.
85-
86-
Returns list of (column_name, is_optional) tuples like:
87-
[('cellid', False), ('q', False), ('aux', True), ('boundname', True)]
88-
"""
89-
columns = []
90-
91-
# Check if any field has spatial dimensions (indicates cellid is needed)
92-
has_spatial = False
93-
for name in field_names:
94-
field = block_fields[name]
95-
if field.shape and any(
96-
dim in field.shape for dim in ["nnodes", "ncells", "nlay", "nrow", "ncol"]
97-
):
98-
has_spatial = True
99-
break
100-
101-
if has_spatial:
102-
columns.append(("cellid", False)) # cellid is always required
103-
104-
# Add the field names as columns with their optionality
105-
for name in field_names:
106-
field = block_fields[name]
107-
is_optional = getattr(field, "optional", False)
108-
columns.append((name, is_optional))
109-
110-
return columns
111-
112-
11380
def get_all_grouped_field_names(blocks: Mapping[str, Mapping[str, FieldV2]]) -> set[str]:
11481
"""
11582
Get all field names that are grouped into recarrays across all blocks.

flopy4/mf6/codec/reader/grammar/templates/component.lark.jinja

Lines changed: 77 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,50 @@
1+
{# Macros for field generation #}
2+
{% macro render_record_field(field_name, field) -%}
3+
{{ field_name }}: {% for child_name, child in field.children.items() -%}
4+
{%- set child_type = child|record_child_type -%}
5+
{%- if child.type == 'keyword' -%}
6+
"{{ child.name }}"i
7+
{%- elif child.type == 'union' -%}
8+
{{ child_name }}
9+
{%- else -%}
10+
{{ child_type }}
11+
{%- endif -%}
12+
{%- if not loop.last %} {% endif -%}
13+
{%- endfor %}
14+
{%- endmacro %}
15+
16+
{% macro render_union_field(field_name, field) -%}
17+
{{ field_name }}: {% for child_name, child in field.children.items() -%}
18+
{{ field_name }}_{{ child_name }}
19+
{%- if not loop.last %} | {% endif -%}
20+
{%- endfor %}
21+
{% for child_name, child in field.children.items() %}
22+
{{ field_name }}_{{ child_name }}: {% if child.type == 'keyword' -%}
23+
"{{ child.name }}"i
24+
{%- else -%}
25+
"{{ child.name }}"i {{ child.type }}
26+
{%- endif %}
27+
{% endfor -%}
28+
{%- endmacro %}
29+
30+
{% macro render_simple_field(field_name, field, field_type) -%}
31+
{{ field_name }}: "{{ field.name }}"i {{ field_type }}
32+
{%- endmacro %}
33+
34+
{% macro render_nested_union(child_name, child) -%}
35+
{{ child_name }}: {% for opt_name, opt in child.children.items() -%}
36+
{{ child_name }}_{{ opt_name }}
37+
{%- if not loop.last %} | {% endif -%}
38+
{%- endfor %}
39+
{% for opt_name, opt in child.children.items() %}
40+
{{ child_name }}_{{ opt_name }}: {% if opt.type == 'keyword' -%}
41+
"{{ opt.name }}"i
42+
{%- else -%}
43+
"{{ opt.name }}"i {{ opt.type }}
44+
{%- endif %}
45+
{% endfor -%}
46+
{%- endmacro %}
47+
148
// Auto-generated grammar for MF6 {{ name|upper }}
249
%import common.WS
350
%import common.SH_COMMENT
@@ -6,6 +53,20 @@
653
%ignore WS
754
%ignore SH_COMMENT
855

56+
{# Pre-compute block metadata to avoid multiple iterations #}
57+
{% set block_metadata = {} %}
58+
{% for block_name, block_fields in blocks.items() %}
59+
{% set period_groups = block_fields|group_period_fields %}
60+
{% set _ = block_metadata.update({
61+
block_name: {
62+
'fields': block_fields,
63+
'period_groups': period_groups,
64+
'recarray_name': (block_name|get_recarray_name) if period_groups else none,
65+
'has_index': block_name == 'period'
66+
}
67+
}) %}
68+
{% endfor %}
69+
970
// Top-level structure
1071
start: block*
1172
block: {% for block_name in blocks.keys() %}
@@ -14,36 +75,34 @@ block: {% for block_name in blocks.keys() %}
1475
{%- endfor %}
1576

1677
// Block definitions
17-
{% for block_name, block_fields in blocks.items() %}
18-
{% if block_name == 'period' %}
78+
{% for block_name, meta in block_metadata.items() %}
79+
{% if meta.has_index %}
1980
{{ block_name }}_block: "begin"i "{{ block_name }}"i block_index {{ block_name }}_fields "end"i "{{ block_name }}"i block_index
2081
{% else %}
2182
{{ block_name }}_block: "begin"i "{{ block_name }}"i {{ block_name }}_fields "end"i "{{ block_name }}"i
2283
{% endif %}
2384
{% endfor %}
24-
{% if 'period' in blocks %}
85+
{% if block_metadata.values()|selectattr('has_index')|list %}
2586
block_index: integer
2687
{% endif %}
2788
// Block field lists
28-
{% for block_name, block_fields in blocks.items() %}
29-
{% set period_groups = block_fields|group_period_fields %}
30-
{% if period_groups %}
89+
{% for block_name, meta in block_metadata.items() %}
90+
{% if meta.period_groups %}
3191
{# This block has period data (e.g., stress_period_data), split fields accordingly #}
32-
{% set recarray_name = block_name|get_recarray_name %}
33-
{% set grouped_field_names = period_groups.values()|first %}
92+
{% set grouped_field_names = meta.period_groups.values()|first %}
3493
{% set non_grouped_fields = [] %}
35-
{% for field_name in block_fields.keys() %}
94+
{% for field_name in meta.fields.keys() %}
3695
{%- if field_name not in grouped_field_names %}
3796
{%- set _ = non_grouped_fields.append(field_name) %}
3897
{%- endif %}
3998
{%- endfor %}
4099
{{ block_name }}_fields: (
41100
{%- for field_name in non_grouped_fields %}{{ field_name }} | {% endfor -%}
42-
{{ recarray_name }})*
101+
{{ meta.recarray_name }})*
43102
{% else %}
44103
{# Regular block - just list all fields #}
45104
{{ block_name }}_fields: (
46-
{%- for field_name in block_fields.keys() %}
105+
{%- for field_name in meta.fields.keys() %}
47106
{{- field_name }}{% if not loop.last %} | {% endif %}
48107
{%- endfor -%}
49108
)*
@@ -55,21 +114,11 @@ block_index: integer
55114
{% if field_name not in grouped_fields %}
56115
{% set field_type = field|field_type %}
57116
{% if field.type == 'record' and field.children is not none %}
58-
{{ field_name }}: {% for child_name, child in field.children.items() %}{% set child_type = child|record_child_type
59-
%}{% if child.type == 'keyword' %}"{{ child.name }}"i{% elif child.type == 'union' %}{{ child_name }}{%
60-
else %}{{ child_type }}{% endif %}{% if not loop.last %} {% endif %}{% endfor %}
61-
117+
{{ render_record_field(field_name, field) }}
62118
{% elif field.type == 'union' and field.children is not none %}
63-
{{ field_name }}: {% for child_name, child in field.children.items() %}{{ field_name }}_{{ child_name }}{% if not
64-
loop.last %} | {% endif %}{% endfor %}
65-
{% for child_name, child in field.children.items() %}
66-
67-
{{ field_name }}_{{ child_name }}: {% if child.type == 'keyword' %}"{{ child.name }}"i{% else %}"{{ child.name }}"i {{
68-
child.type }}{% endif %}
69-
70-
{% endfor %}
119+
{{ render_union_field(field_name, field) }}
71120
{% else %}
72-
{{ field_name }}: "{{ field.name }}"i {{ field_type }}
121+
{{ render_simple_field(field_name, field, field_type) }}
73122
{% endif %}
74123
{% endif %}
75124
{% endfor %}
@@ -79,29 +128,16 @@ child.type }}{% endif %}
79128
{% if field.type == 'record' and field.children is not none %}
80129
{% for child_name, child in field.children.items() %}
81130
{% if child.type == 'union' and child_name not in unions_generated %}
82-
{{ child_name }}: {% for opt_name, opt in child.children.items() %}{{ child_name }}_{{ opt_name }}{% if not loop.last
83-
%} | {% endif %}
84-
{% endfor %}
85-
86-
{% for opt_name, opt in child.children.items() %}
87-
{{ child_name }}_{{ opt_name }}: {% if opt.type == 'keyword' %}"{{ opt.name }}"i{% else %}"{{ opt.name }}"i {{
88-
opt.type }}{% endif %}
89-
90-
{% endfor %}
91-
131+
{{ render_nested_union(child_name, child) }}
92132
{% set _ = unions_generated.append(child_name) %}
93133
{% endif %}
94134
{% endfor %}
95135
{% endif %}
96136
{% endfor %}
97137
// Recarray rules for period data (stress_period_data, etc.)
98-
{% for block_name, block_fields in blocks.items() %}
99-
{% set period_groups = block_fields|group_period_fields %}
100-
{% if period_groups %}
101-
{% set recarray_name = block_name|get_recarray_name %}
102-
{% set field_names = period_groups.values()|first %}
103-
{% set columns = field_names|get_recarray_columns(block_fields) %}
104-
{{ recarray_name}}: (number | simple_string)+ NEWLINE
138+
{% for block_name, meta in block_metadata.items() %}
139+
{% if meta.recarray_name %}
140+
{{ meta.recarray_name }}: (number | simple_string)+ NEWLINE
105141
{% endif %}
106142
{% endfor %}
107143

0 commit comments

Comments
 (0)