Skip to content

Commit a2d5c9b

Browse files
committed
Also print the values in the scalars
1 parent 53dd0d8 commit a2d5c9b

File tree

5 files changed

+36
-26
lines changed

5 files changed

+36
-26
lines changed

flopy4/mf6/codec.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,11 @@
1313
trim_blocks=True,
1414
lstrip_blocks=True,
1515
)
16-
JINJA_ENV.filters["fieldkind"] = filters.fieldkind
16+
JINJA_ENV.filters["field_kind"] = filters.field_kind
1717
JINJA_ENV.filters["fieldvalue"] = filters.fieldvalue
1818
JINJA_ENV.filters["arraydelayed"] = filters.arraydelayed
1919
JINJA_ENV.filters["array2string"] = filters.array2string
20+
JINJA_ENV.filters["is_dict"] = filters.is_dict
2021
JINJA_TEMPLATE_NAME = "blocks.jinja"
2122

2223

flopy4/mf6/filters.py

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
1+
from typing import Any
2+
13
import numpy as np
24
import xarray as xr
35
from attrs import Attribute
46
from jinja2 import pass_context
57
from numpy.typing import NDArray
68

79

8-
def fieldkind(field: Attribute) -> str:
10+
def field_kind(field: Attribute) -> str:
911
"""
1012
Get a field's `xattree` kind. Kind is either:
1113
@@ -21,13 +23,13 @@ def fieldkind(field: Attribute) -> str:
2123
raise TypeError(f"Field {field.name} has no xattree metadata")
2224
if "kind" not in xatmeta:
2325
raise TypeError(f"Field {field.name} has no kind")
24-
return xatmeta.get("kind", "attr")
26+
return xatmeta.get("kind") or "attr"
2527

2628

2729
@pass_context
2830
def fieldvalue(ctx, field: Attribute):
2931
"""Get a field's value from the data tree via the template context."""
30-
return ctx["data"][field.name]
32+
return ctx["data"].attrs.get(field.name) or ctx["data"].get(field.name)
3133

3234

3335
def arraydelayed(value: xr.DataArray):
@@ -41,3 +43,8 @@ def arraydelayed(value: xr.DataArray):
4143
def array2string(value: NDArray) -> str:
4244
"""Convert an array to a string."""
4345
return np.array2string(value, separator=" ")[1:-1] # remove brackets
46+
47+
48+
def is_dict(value: Any) -> bool:
49+
"""Check if the value is a dictionary."""
50+
return isinstance(value, dict)

flopy4/mf6/templates/blocks.jinja

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
{% import 'macros.jinja' as macros with context %}
2-
32
{% for block_name, block_ in blocks.items() %}
43
BEGIN {{ block_name }}
54
{% for field in block_.values() %}
65
{{ macros.field(field) }}
76
{% endfor %}
87
END {{ block_name }}
9-
108
{% endfor %}

flopy4/mf6/templates/macros.jinja

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{% macro field(field) %}
2-
{% set kind = field|fieldkind %}
2+
{% set kind = field|field_kind %}
33
{% if kind == 'attr' %}
4-
{% if field.type|isdict %}
4+
{% if field.type|is_dict %}
55
{{ record(field) }}
66
{% else %}
77
{{ scalar(field) }}
@@ -13,50 +13,52 @@
1313
{% elif kind == 'child' %}
1414
{# TODO #}
1515
{% endif %}
16-
{% endmacro %}
16+
{%- endmacro %}
1717

1818
{% macro scalar(field) %}
19-
{{ field.name }} {{ field.value }}
20-
{% endmacro %}
19+
{% set value = field|fieldvalue %}
20+
{% if value is not none %}
21+
{{ field.name }} {{ value }}
22+
{% endif %}
23+
{%- endmacro %}
2124

2225
{% macro keystring(field) %} {# union #}
23-
{% for item in field.value.items() %}
26+
{% for item in (field|fieldvalue).items() %}
2427
{{ field(item) }}
2528
{% endfor %}
26-
{% endmacro %}
29+
{%- endmacro %}
2730

2831
{% macro record(field) %}
29-
{% for item in field.values() %}{% if item.tagged %}{{ item.name }} {% endif %}{{ field(field) }}{%
30-
endfor
31-
%}
32-
{% endmacro %}
32+
{% for item in field|fieldvalue %}
33+
{% if item.tagged %}{{ item.name }} {% endif %}{{ field(field) }}
34+
{% endfor %}
35+
{%- endmacro %}
3336

3437
{% macro array(field, how="internal") %}
3538
{% if how == "layered constant" %}
3639
{{ field.name }} LAYERED
37-
{% for val in field.value %}
40+
{% for val in field|fieldvalue %}
3841
CONSTANT
3942
{% endfor %}
4043
{% elif how == "constant" %}
41-
{{ field.name }} CONSTANT {{ field.value }}
44+
{{ field.name }} CONSTANT {{ field|fieldvalue }}
4245
{% elif how == "layered" %}
4346
{% if layered %}
44-
{{ field.name }}
45-
{% for val in field.value %}{{ val }}{% endfor %}
47+
{{ field.name }}{% for val in field|fieldvalue %} {{ val }}{% endfor %}
4648
{% endif %}
4749
{% elif how == "internal" %}
4850
{{ field.name }} {{ internal_array(field) }}
4951
{% elif how == "external" %}
50-
{{ field.name}} OPEN/CLOSE {{ field.value }}
52+
{{ field.name}} OPEN/CLOSE {{ field|fieldvalue }}
5153
{% endif %}
52-
{% endmacro %}
54+
{%- endmacro %}
5355

5456
{% macro internal_array(field) %}
5557
{% for chunk in field|fieldvalue|arraydelayed %}
5658
{{ chunk|array2string }}
5759
{% endfor %}
58-
{% endmacro %}
60+
{%- endmacro %}
5961

6062
{% macro list(field) %}
6163
{# TODO #}
62-
{% endmacro %}
64+
{%- endmacro %}

test/test_jinja.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,14 @@ def test_simulation_to_jinja():
3838
trim_blocks=True,
3939
lstrip_blocks=True,
4040
)
41-
env.filters["fieldkind"] = filters.fieldkind
41+
env.filters["field_kind"] = filters.field_kind
4242
env.filters["fieldvalue"] = filters.fieldvalue
4343
env.filters["arraydelayed"] = filters.arraydelayed
4444
env.filters["array2string"] = filters.array2string
45+
env.filters["is_dict"] = filters.is_dict
4546

4647
fields = fields_dict(Oc)
4748
blocks = blocks_dict(Oc)
4849
result = env.get_template("blocks.jinja").render(fields=fields, blocks=blocks, data=oc.data)
50+
print(result)
4951
assert result != ""

0 commit comments

Comments
 (0)