Skip to content

Commit e5dbff8

Browse files
mjrenomjreno
authored andcommitted
add static methods
1 parent d3a8746 commit e5dbff8

File tree

2 files changed

+161
-92
lines changed

2 files changed

+161
-92
lines changed

flopy/mf6/mfmodel.py

Lines changed: 46 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -808,32 +808,6 @@ def export(self, f, **kwargs):
808808

809809
return utils.model_export(f, self, **kwargs)
810810

811-
def netcdf_attrs(self, mesh=None):
812-
"""Return dictionary of dataset (model) scoped attributes
813-
Parameters
814-
----------
815-
mesh : str
816-
mesh type if dataset is ugrid compliant
817-
"""
818-
attrs = {
819-
"modflow_grid": "",
820-
"modflow_model": "",
821-
}
822-
if self.get_grid_type() == DiscretizationType.DIS:
823-
attrs["modflow_grid"] = "STRUCTURED"
824-
elif self.get_grid_type() == DiscretizationType.DISV:
825-
attrs["modflow_grid"] = "VERTEX"
826-
827-
attrs["modflow_model"] = (
828-
f"{self.name.upper()}: MODFLOW 6 {self.model_type.upper()[0:3]} model"
829-
)
830-
831-
# supported => LAYERED
832-
if mesh:
833-
attrs["mesh"] = mesh
834-
835-
return attrs
836-
837811
@property
838812
def verbose(self):
839813
"""Verbose setting for model operations (True/False)"""
@@ -2229,3 +2203,49 @@ def _resolve_idomain(idomain, botm):
22292203
else:
22302204
return np.ones_like(botm)
22312205
return idomain
2206+
2207+
@staticmethod
2208+
def netcdf_attrs(mname, mtype, grid_type, mesh=None):
2209+
"""Return dictionary of dataset (model) scoped attributes
2210+
Parameters
2211+
----------
2212+
mname : str
2213+
model name
2214+
mtype : str
2215+
model type
2216+
grid_type:
2217+
DiscretizationType
2218+
mesh : str
2219+
mesh type if dataset is ugrid compliant
2220+
"""
2221+
attrs = {
2222+
"modflow_grid": "",
2223+
"modflow_model": "",
2224+
}
2225+
if grid_type == DiscretizationType.DIS:
2226+
attrs["modflow_grid"] = "STRUCTURED"
2227+
elif grid_type == DiscretizationType.DISV:
2228+
attrs["modflow_grid"] = "VERTEX"
2229+
2230+
attrs["modflow_model"] = f"{mname.upper()}: MODFLOW 6 {mtype.upper()} model"
2231+
2232+
# supported => LAYERED
2233+
if mesh:
2234+
attrs["mesh"] = mesh
2235+
2236+
return attrs
2237+
2238+
def netcdf_info(self, mesh=None):
2239+
"""Return dictionary of dataset (model) scoped attributes
2240+
Parameters
2241+
----------
2242+
mesh : str
2243+
mesh type if dataset is ugrid compliant
2244+
"""
2245+
attrs = MFModel.netcdf_attrs(
2246+
self.name, self.model_type, self.get_grid_type(), mesh
2247+
)
2248+
2249+
res_d = {}
2250+
res_d["attrs"] = attrs
2251+
return res_d

flopy/mf6/mfpackage.py

Lines changed: 115 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,72 +2086,6 @@ def package_filename_dict(self):
20862086
)
20872087
return self._package_container.package_filename_dict
20882088

2089-
def netcdf_attrs(self, mesh=None):
2090-
attrs = {}
2091-
2092-
def attr_d(tagname, iaux=None, layer=None):
2093-
tag = tagname
2094-
name = f"{self.package_name}"
2095-
if iaux:
2096-
auxvar = self.dimensions.get_aux_variables()[0]
2097-
tag = f"{tag}/{iaux}"
2098-
name = f"{name}_{auxvar[iaux]}"
2099-
else:
2100-
name = f"{name}_{tagname}"
2101-
if layer:
2102-
tag = f"{tag}/layer{layer}"
2103-
name = f"{name}_l{layer}"
2104-
2105-
a = {}
2106-
a["varname"] = name
2107-
a["attrs"] = {}
2108-
a["attrs"]["modflow_input"] = (
2109-
f"{self.model_name}/{self.package_name}/{tagname}"
2110-
).upper()
2111-
if iaux:
2112-
a["attrs"]["modflow_iaux"] = iaux
2113-
if layer:
2114-
a["attrs"]["layer"] = layer
2115-
return tag, a
2116-
2117-
for key, block in self.blocks.items():
2118-
if key != "griddata" and key != "period":
2119-
continue
2120-
for dataset in block.datasets.values():
2121-
if isinstance(dataset, mfdataarray.MFArray):
2122-
for index, data_item in enumerate(
2123-
dataset.structure.data_item_structures
2124-
):
2125-
if not (dataset.structure.netcdf and dataset.has_data()):
2126-
continue
2127-
if dataset.structure.layered and mesh == "LAYERED":
2128-
if data_item.name == "aux" or data_item.name == "auxvar":
2129-
for n, auxname in enumerate(
2130-
self.dimensions.get_aux_variables()[0]
2131-
):
2132-
if auxname == "auxiliary" and n == 0:
2133-
continue
2134-
for l in range(self.model_or_sim.modelgrid.nlay):
2135-
key, a = attr_d(data_item.name, n, l + 1)
2136-
attrs[key] = a
2137-
else:
2138-
for l in range(self.model_or_sim.modelgrid.nlay):
2139-
key, a = attr_d(data_item.name, layer=l + 1)
2140-
attrs[key] = a
2141-
else:
2142-
if data_item.name == "aux" or data_item.name == "auxvar":
2143-
for n, auxname in enumerate(
2144-
self.dimensions.get_aux_variables()[0]
2145-
):
2146-
if auxname == "auxiliary" and n == 0:
2147-
continue
2148-
key, a = attr_d(data_item.name, iaux=n)
2149-
attrs[key] = a
2150-
else:
2151-
key, a = attr_d(data_item.name)
2152-
attrs[key] = a
2153-
return attrs
2154-
21552089
def get_package(self, name=None, type_only=False, name_only=False):
21562090
"""
21572091
Finds a package by package name, package key, package type, or partial
@@ -3488,6 +3422,121 @@ def plot(self, **kwargs):
34883422
axes = PlotUtilities._plot_package_helper(self, **kwargs)
34893423
return axes
34903424

3425+
@staticmethod
3426+
def _add_netcdf_entries(
3427+
attrs, mname, pname, data_item, auxiliary=None, mesh=None, nlay=1
3428+
):
3429+
if auxiliary:
3430+
auxnames = auxiliary
3431+
else:
3432+
auxnames = []
3433+
3434+
def add_entry(tagname, iaux=None, layer=None):
3435+
key = tagname
3436+
name = f"{pname}"
3437+
if iaux is not None:
3438+
key = f"{key}/{iaux}"
3439+
name = f"{name}_{auxiliary[iaux]}"
3440+
else:
3441+
name = f"{name}_{tagname}"
3442+
if layer is not None:
3443+
key = f"{key}/layer{layer}"
3444+
name = f"{name}_l{layer}"
3445+
3446+
a = {}
3447+
a["varname"] = name.lower()
3448+
a["attrs"] = {}
3449+
a["attrs"]["modflow_input"] = (f"{mname}/{pname}/{tagname}").upper()
3450+
if iaux is not None:
3451+
a["attrs"]["modflow_iaux"] = iaux + 1
3452+
if layer is not None:
3453+
a["attrs"]["layer"] = layer
3454+
attrs[key] = a
3455+
3456+
if data_item.layered and mesh == "LAYERED":
3457+
if data_item.name == "aux" or data_item.name == "auxvar":
3458+
for n, auxname in enumerate(auxnames):
3459+
for l in range(nlay):
3460+
add_entry(data_item.name, n, l + 1)
3461+
else:
3462+
for l in range(nlay):
3463+
add_entry(data_item.name, layer=l + 1)
3464+
else:
3465+
if data_item.name == "aux" or data_item.name == "auxvar":
3466+
for n, auxname in enumerate(auxnames):
3467+
add_entry(data_item.name, iaux=n)
3468+
else:
3469+
add_entry(data_item.name)
3470+
3471+
@staticmethod
3472+
def netcdf_attrs(mtype, ptype, auxiliary=None, mesh=None, nlay=1):
3473+
from .data.mfstructure import DfnPackage, MFSimulationStructure
3474+
3475+
attrs = {}
3476+
sim_struct = MFSimulationStructure()
3477+
3478+
for package in MFPackage.__subclasses__():
3479+
sim_struct.process_dfn(DfnPackage(package))
3480+
p = DfnPackage(package)
3481+
c, sc = p.dfn_file_name.split(".")[0].split("-")
3482+
if c == mtype.lower() and sc == ptype.lower():
3483+
sim_struct.add_package(p, model_file=False)
3484+
exit
3485+
3486+
if ptype.lower() in sim_struct.package_struct_objs:
3487+
pso = sim_struct.package_struct_objs[ptype.lower()]
3488+
for key, block in pso.blocks.items():
3489+
if key != "griddata" and key != "period":
3490+
continue
3491+
for d in block.data_structures:
3492+
if block.data_structures[d].netcdf:
3493+
MFPackage._add_netcdf_entries(
3494+
attrs,
3495+
mtype,
3496+
ptype,
3497+
block.data_structures[d],
3498+
auxiliary,
3499+
mesh,
3500+
nlay,
3501+
)
3502+
3503+
res_d = {}
3504+
for k in list(attrs):
3505+
res_d[k] = attrs[k]["attrs"]
3506+
3507+
return res_d
3508+
3509+
def netcdf_info(self, mesh=None):
3510+
attrs = {}
3511+
3512+
if self.dimensions.get_aux_variables():
3513+
auxnames = list(self.dimensions.get_aux_variables()[0])
3514+
if len(auxnames) and auxnames[0] == "auxiliary":
3515+
auxnames.pop(0)
3516+
else:
3517+
auxnames = []
3518+
3519+
for key, block in self.blocks.items():
3520+
if key != "griddata" and key != "period":
3521+
continue
3522+
for dataset in block.datasets.values():
3523+
if isinstance(dataset, mfdataarray.MFArray):
3524+
for index, data_item in enumerate(
3525+
dataset.structure.data_item_structures
3526+
):
3527+
if dataset.structure.netcdf and dataset.has_data():
3528+
MFPackage._add_netcdf_entries(
3529+
attrs,
3530+
self.model_name,
3531+
self.package_name,
3532+
dataset.structure,
3533+
auxnames,
3534+
mesh,
3535+
self.model_or_sim.modelgrid.nlay,
3536+
)
3537+
3538+
return attrs
3539+
34913540

34923541
class MFChildPackages:
34933542
"""

0 commit comments

Comments
 (0)