Skip to content

Commit 693f01a

Browse files
authored
fix(array3d_export): fix exporting of array3d to shp (#2310)
Fix the issue raised in discussion #2308 - fix(MultiList): increment index per layer in build_list - fix(array3d_export): fix case for disu grid - add tests
1 parent a0e9219 commit 693f01a

File tree

3 files changed

+78
-11
lines changed

3 files changed

+78
-11
lines changed

autotest/test_export.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -646,6 +646,65 @@ def test_export_array2(function_tmpdir):
646646
assert os.path.isfile(filename), "did not create array shapefile"
647647

648648

649+
@pytest.mark.mf6
650+
@requires_pkg("pyshp", name_map={"pyshp": "shapefile"})
651+
def test_array3d_export_structured(function_tmpdir):
652+
from shapefile import Reader
653+
654+
xll, yll = 468970, 3478635
655+
xur, yur = 681010, 3716462
656+
spacing = 20000
657+
ncol = int((xur - xll) / spacing)
658+
nrow = int((yur - yll) / spacing)
659+
sim = flopy.mf6.MFSimulation("sim", sim_ws=function_tmpdir)
660+
gwf = flopy.mf6.ModflowGwf(
661+
sim,
662+
modelname="array3d_export_unstructured",
663+
)
664+
flopy.mf6.ModflowGwfdis(
665+
gwf,
666+
nlay=3,
667+
top=5,
668+
botm=[4, 3, 2],
669+
delr=spacing,
670+
delc=spacing,
671+
nrow=nrow,
672+
ncol=ncol,
673+
)
674+
675+
shp_file = os.path.join(function_tmpdir, "dis_botm.shp")
676+
gwf.dis.botm.export(shp_file)
677+
678+
with Reader(shp_file) as shp:
679+
assert list(shp.shapeRecord(-1).record) == [
680+
110, # node
681+
11, # row
682+
10, # column
683+
4.0, # botm_1
684+
3.0, # botm_2
685+
2.0, # botm_3
686+
]
687+
688+
689+
@requires_pkg("pyshp", name_map={"pyshp": "shapefile"})
690+
def test_array3d_export_unstructured(function_tmpdir):
691+
from shapefile import Reader
692+
693+
name = "array3d_export_unstructured"
694+
sim = disu_sim(name, function_tmpdir)
695+
gwf = sim.get_model(name)
696+
697+
shp_file = function_tmpdir / "disu_bot.shp"
698+
gwf.disu.bot.export(shp_file)
699+
700+
with Reader(shp_file) as shp:
701+
assert list(shp.shapeRecord(-1).record) == [
702+
1770, # node
703+
3, # layer
704+
0.0, # bot
705+
]
706+
707+
649708
@requires_pkg("pyshp", "shapely", name_map={"pyshp": "shapefile"})
650709
def test_export_array_contours_structured(function_tmpdir):
651710
nrow = 7

flopy/export/utils.py

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1207,16 +1207,22 @@ def array3d_export(f: Union[str, os.PathLike], u3d, fmt=None, **kwargs):
12071207
f
12081208
).suffix.lower() == ".shp":
12091209
array_dict = {}
1210-
for ilay in range(modelgrid.nlay):
1211-
u2d = u3d[ilay]
1212-
if isinstance(u2d, np.ndarray):
1213-
dname = u3d.name
1214-
array = u2d
1215-
else:
1216-
dname = u2d.name
1217-
array = u2d.array
1218-
name = f"{shapefile_utils.shape_attr_name(dname)}_{ilay + 1}"
1219-
array_dict[name] = array
1210+
array_shape = u3d.array.shape
1211+
1212+
if len(array_shape) == 1:
1213+
name = shapefile_utils.shape_attr_name(u3d.name)
1214+
array_dict[name] = u3d.array
1215+
else:
1216+
for ilay in range(array_shape[0]):
1217+
u2d = u3d[ilay]
1218+
if isinstance(u2d, np.ndarray):
1219+
dname = u3d.name
1220+
array = u2d
1221+
else:
1222+
dname = u2d.name
1223+
array = u2d.array
1224+
name = f"{shapefile_utils.shape_attr_name(dname)}_{ilay + 1}"
1225+
array_dict[name] = array
12201226
shapefile_utils.write_grid_shapefile(f, modelgrid, array_dict)
12211227

12221228
elif isinstance(f, NetCdf) or isinstance(f, dict):

flopy/utils/datautil.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,9 @@ def build_list(self, callback):
606606
(entry_point[0][-1], new_location)
607607
)
608608
else:
609-
entry_point[0].append(callback(entry_point[1]))
609+
entry_point[0].append(
610+
callback(tuple(i + val for i in entry_point[1]))
611+
)
610612
entry_points = new_entry_points
611613

612614
def first_item(self):

0 commit comments

Comments
 (0)