Skip to content

extend flopy3 adapters #141

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 31 additions & 15 deletions flopy4/mf6/interface/flopy3.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
from typing import Optional

import numpy as np
from flopy import export
from flopy.datbase import DataInterface, DataListInterface, DataType
from flopy.discretization import StructuredGrid
from flopy.discretization.grid import Grid
from flopy.discretization.modeltime import ModelTime
from flopy.export.utils import model_export, package_export
from flopy.mbase import ModelInterface
from flopy.pakbase import PackageInterface
from flopy.plot.plotutil import PlotUtilities
from xattree import Xattribute, get_xatspec
from xattree import Xattribute, _get_xatspec

from flopy4.mf6.model import Model
from flopy4.mf6.package import Package
Expand All @@ -23,7 +23,7 @@ def __init__(
modelgrid: Optional[Grid] = None,
modeltime: Optional[ModelTime] = None,
ims: Optional[Package] = None,
crs: Optional[int] = None,
**kwargs,
):
self._model = model
self._grid = modelgrid
Expand Down Expand Up @@ -57,8 +57,8 @@ def __init__(
botm=model.dis.botm.data,
idomain=model.dis.idomain.data,
lenuni=lenuni,
crs=crs,
prjfile=None,
crs=kwargs.get("crs", None),
prjfile=kwargs.get("prjfile", None),
xoff=xoff,
yoff=yoff,
angrot=angrot,
Expand Down Expand Up @@ -98,7 +98,7 @@ def model_ws(self):
return ""

@property
def exe_name(self):
def exename(self):
return ""

@property
Expand Down Expand Up @@ -166,7 +166,7 @@ def plot(self, packages: Optional[list] = None, **kwargs):
return PlotUtilities._plot_model_helper(self, SelPackList=packages, **kwargs)

def export(self, f, **kwargs):
return model_export(f, self, **kwargs)
return export.utils.model_export(f, self, **kwargs)


class Flopy3Package(PackageInterface):
Expand All @@ -182,7 +182,7 @@ def __init__(
self._data = package.data
else:
raise Exception("Input package has no data")
self._spec = get_xatspec(type(package))
self._spec = _get_xatspec(type(package))
if modelgrid:
self._grid = modelgrid
elif model:
Expand All @@ -197,12 +197,12 @@ def __init__(
continue
if (
self._data.attrs[a] is not None
and a in self._spec
and self._spec[a].type is not None
and a in self._spec.flat
and self._spec.flat[a].type is not None
):
d_fp3 = Flopy3Data(
data=self._data.attrs[a],
spec=self._spec[a],
spec=self._spec.flat[a],
name=a,
modelname=self.parent,
modelgrid=self._grid,
Expand All @@ -214,12 +214,12 @@ def __init__(
for v in self._data.data_vars:
if (
self._data.data_vars[v] is not None
and v in self._spec
and self._spec[v].type is not None
and v in self._spec.flat
and self._spec.flat[v].type is not None
):
d_fp3 = Flopy3Data(
data=self._data.data_vars[v],
spec=self._spec[v],
spec=self._spec.flat[v],
name=v,
modelname=self.parent,
modelgrid=self._grid,
Expand Down Expand Up @@ -274,7 +274,7 @@ def plot(self, **kwargs):
return PlotUtilities._plot_package_helper(self, **kwargs)

def export(self, f, **kwargs):
return package_export(f, self, **kwargs)
return export.utils.package_export(f, self, **kwargs)


class Flopy3Data(DataInterface):
Expand Down Expand Up @@ -380,6 +380,22 @@ def plottable(self):
return False
return True

def export(self, f, **kwargs):
if self._spec.type.__name__ == "ndarray":
if (
self.data_type == DataType.array2d
and len(self.array.shape) == 2
and self.array.shape[1] > 0
):
return export.utils.array2d_export(f, self, **kwargs)
elif self.data_type == DataType.array3d:
return export.utils.array3d_export(f, self, **kwargs)
elif self.data_type == DataType.transient2d:
return export.utils.transient2d_export(f, self, **kwargs)
elif self.data_type == DataType.transientlist:
return export.utils.mflist_export(f, self, **kwargs)
return export.utils.transient2d_export(f, self, **kwargs)


class Flopy3ListData(DataListInterface):
@property
Expand Down
11 changes: 9 additions & 2 deletions test/test_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ def test_flopy3_export():
Path("output/flopy3_package/shape").mkdir(parents=True, exist_ok=True)
Path("output/flopy3_model/netcdf").mkdir(parents=True, exist_ok=True)
Path("output/flopy3_package/netcdf").mkdir(parents=True, exist_ok=True)
Path("output/flopy3_package/vtk").mkdir(parents=True, exist_ok=True)

time = ModelTime(perlen=[1.0], nstp=[1], tsmult=[1.0])

Expand All @@ -332,14 +333,20 @@ def test_flopy3_export():

# model netcdf export
nc_mpth = Path("output/flopy3_model/netcdf/flopy3_model.nc")
# TODO: needs flopy3 fix
# TODO: needs flopy3 #2513
# gwf3.export(f=nc_mpth)

# package netcdf export
nc_ppth = Path("output/flopy3_package/netcdf/flopy3_package.nc")
# TODO: needs flopy3 fix
# TODO: needs flopy3 #2513
# dis3.export(f=nc_ppth)

for d in dis3.data_list:
print(d.name)
if d.array is not None:
vtk_pth = Path(f"output/flopy3_package/vtk/{d.name}.vtk")
# d.export(vtk_pth, name=d.name, fmt="vtk", binary=False)


@pytest.mark.xfail(
reason=("demonstrate why wrapping array values with DataArray is necessary on set")
Expand Down
Loading