Skip to content

Commit a591a27

Browse files
wpbonellimjreno
andauthored
factor out function for structured grid dims workaround (#215)
in lieu of an xarray index for structured grids to provide layer/row/column indexing on arrays even when they are defined in terms of nodes, we reshape flat arrays to structured grid dims in the converter for now. move this out into a function so it's more obvious it's temporary. Co-authored-by: mjreno <renomik@gmail.com>
1 parent 5c4ba44 commit a591a27

File tree

1 file changed

+41
-25
lines changed

1 file changed

+41
-25
lines changed

flopy4/mf6/converter.py

Lines changed: 41 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,43 @@ def get_binding_blocks(value: Component) -> dict[str, dict[str, list[tuple[str,
6060
return blocks
6161

6262

63+
def _hack_structured_grid_dims(
64+
value: xr.DataArray, structured_grid_dims: Mapping[str, int]
65+
) -> xr.DataArray:
66+
"""
67+
Temporary hack to convert flat nodes dimension to 3d structured dims.
68+
long term solution for this is to use a custom xarray index. filters
69+
should then have access to all dimensions needed.
70+
"""
71+
72+
if "nodes" not in value.dims:
73+
return value
74+
75+
shape = [
76+
structured_grid_dims["nlay"],
77+
structured_grid_dims["nrow"],
78+
structured_grid_dims["ncol"],
79+
]
80+
dims = ["nlay", "nrow", "ncol"]
81+
coords = {
82+
"nlay": range(structured_grid_dims["nlay"]),
83+
"nrow": range(structured_grid_dims["nrow"]),
84+
"ncol": range(structured_grid_dims["ncol"]),
85+
}
86+
87+
if "nper" in value.dims:
88+
shape.insert(0, value.sizes["nper"])
89+
dims.insert(0, "nper")
90+
coords = {"nper": value.coords["nper"], **coords}
91+
92+
return xr.DataArray(
93+
value.data.reshape(shape),
94+
dims=dims,
95+
coords=coords,
96+
name=value.name,
97+
)
98+
99+
63100
def unstructure_component(value: Component) -> dict[str, Any]:
64101
blockspec = dict(sorted(value.dfn.blocks.items(), key=block_sort_key)) # type: ignore
65102
blocks: dict[str, dict[str, Any]] = {}
@@ -109,31 +146,10 @@ def unstructure_component(value: Component) -> dict[str, Any]:
109146
dim in field_value.dims for dim in ["nlay", "nrow", "ncol", "nodes"]
110147
)
111148
if has_spatial_dims:
112-
# terrible hack to convert flat nodes dimension to 3d structured dims.
113-
# long term solution for this is to use a custom xarray index. filters
114-
# should then have access to all dimensions needed.
115-
dims_ = set(field_value.dims).copy()
116-
dims_.remove("nper")
117-
if dims_ == {"nodes"}:
118-
parent = value.parent # type: ignore
119-
field_value = xr.DataArray(
120-
field_value.data.reshape(
121-
(
122-
field_value.sizes["nper"],
123-
parent.dims["nlay"],
124-
parent.dims["nrow"],
125-
parent.dims["ncol"],
126-
)
127-
),
128-
dims=("nper", "nlay", "nrow", "ncol"),
129-
coords={
130-
"nper": field_value.coords["nper"],
131-
"nlay": range(parent.dims["nlay"]),
132-
"nrow": range(parent.dims["nrow"]),
133-
"ncol": range(parent.dims["ncol"]),
134-
},
135-
name=field_value.name,
136-
)
149+
field_value = _hack_structured_grid_dims(
150+
field_value,
151+
structured_grid_dims=value.parent.data.dims, # type: ignore
152+
)
137153

138154
period_data[field_name] = {
139155
kper: field_value.isel(nper=kper)

0 commit comments

Comments
 (0)