Skip to content

Commit db5bfa6

Browse files
mjrenomjreno
authored andcommitted
netcdf attr dicts for package and model
1 parent 94b304c commit db5bfa6

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed

flopy/mf6/data/mfstructure.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,7 @@ def __init__(self):
594594
self.parameter_name = None
595595
self.one_per_pkg = False
596596
self.jagged_array = None
597+
self.netcdf = False
597598

598599
def set_value(self, line, common):
599600
arr_line = line.strip().split()
@@ -771,6 +772,8 @@ def set_value(self, line, common):
771772
self.one_per_pkg = bool(arr_line[1])
772773
elif arr_line[0] == "jagged_array":
773774
self.jagged_array = arr_line[1]
775+
elif arr_line[0] == "netcdf":
776+
self.netcdf = arr_line[1]
774777

775778
def get_type_string(self):
776779
return f"[{self.type_string}]"
@@ -1089,6 +1092,7 @@ def __init__(self, data_item, model_data, package_type, dfn_list):
10891092
or "nodes" in data_item.shape
10901093
or len(data_item.layer_dims) > 1
10911094
)
1095+
self.netcdf = data_item.netcdf
10921096
self.num_data_items = len(data_item.data_items)
10931097
self.record_within_record = False
10941098
self.file_data = False

flopy/mf6/mfmodel.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,32 @@ 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 complient
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+
811837
@property
812838
def verbose(self):
813839
"""Verbose setting for model operations (True/False)"""

flopy/mf6/mfpackage.py

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2086,6 +2086,72 @@ 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+
20892155
def get_package(self, name=None, type_only=False, name_only=False):
20902156
"""
20912157
Finds a package by package name, package key, package type, or partial

0 commit comments

Comments
 (0)