Skip to content

Commit 54b503f

Browse files
mjrenomjreno
authored andcommitted
data tests, add longname
1 parent 2850490 commit 54b503f

File tree

9 files changed

+110
-44
lines changed

9 files changed

+110
-44
lines changed

autotest/regression/test_model_netcdf.py

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
from flopy.utils.gridutil import get_disv_kwargs
1515

1616

17-
def check_netcdf(path, mesh=None):
17+
def check_netcdf(path, mobj, mesh=None):
1818
"""Check for functional equivalence"""
1919
ds = xr.open_dataset(path, engine="netcdf4")
2020
packages = [
@@ -47,13 +47,39 @@ def check_netcdf(path, mesh=None):
4747
p = varname.rsplit("_", 2)[0]
4848

4949
if p in packages:
50+
l = -1
5051
assert "modflow_input" in da.attrs
5152
if mesh is None:
5253
assert "layer" not in da.attrs
5354
else:
5455
lstr = varname.rsplit("_", 1)[1]
5556
if lstr[0] == "l":
5657
assert "layer" in da.attrs
58+
l = da.attrs["layer"] - 1
59+
60+
tag = da.attrs["modflow_input"].rsplit("/", 1)[1].lower()
61+
pobj = getattr(mobj, p)
62+
d = getattr(pobj, tag)
63+
if p == "ghbg_0":
64+
spd = d.get_data()
65+
for per in spd:
66+
if spd[per] is not None:
67+
istp = sum(mobj.modeltime.nstp[0:per])
68+
if l >= 0:
69+
assert np.allclose(
70+
ds.data_vars[varname][istp].fillna(3.00000000e30).data,
71+
spd[per][l],
72+
)
73+
else:
74+
assert np.allclose(
75+
ds.data_vars[varname][istp].fillna(3.00000000e30).data,
76+
spd[per],
77+
)
78+
else:
79+
if l >= 0:
80+
assert np.allclose(ds.data_vars[varname].values, d.get_data()[l])
81+
else:
82+
assert np.allclose(ds.data_vars[varname].values, d.get_data())
5783

5884

5985
def update_dataset(dataset, pobj):
@@ -120,7 +146,7 @@ def test_uzf01_sim_scope_nomesh(function_tmpdir, example_data_path):
120146
sim.set_sim_path(ws)
121147
sim.write_simulation(netcdf=netcdf)
122148

123-
check_netcdf(ws / fname)
149+
check_netcdf(ws / fname, sim.get_model(sim_name))
124150

125151

126152
@pytest.mark.regression
@@ -139,7 +165,7 @@ def test_uzf01_sim_scope_mesh(function_tmpdir, example_data_path):
139165
sim.set_sim_path(ws)
140166
sim.write_simulation(netcdf=netcdf)
141167

142-
check_netcdf(ws / fname, mesh=netcdf)
168+
check_netcdf(ws / fname, sim.get_model(sim_name), mesh=netcdf)
143169

144170

145171
@pytest.mark.regression
@@ -162,7 +188,7 @@ def test_uzf01_sim_scope_fname(function_tmpdir, example_data_path):
162188
sim.set_sim_path(ws)
163189
sim.write_simulation(netcdf=netcdf)
164190

165-
check_netcdf(ws / fname)
191+
check_netcdf(ws / fname, sim.get_model(sim_name))
166192

167193

168194
@pytest.mark.regression
@@ -181,7 +207,7 @@ def test_uzf02_sim_scope(function_tmpdir, example_data_path):
181207
sim.set_sim_path(ws)
182208
sim.write_simulation(netcdf=netcdf)
183209

184-
check_netcdf(ws / fname, mesh=netcdf)
210+
check_netcdf(ws / fname, sim.get_model(sim_name), mesh=netcdf)
185211

186212

187213
@pytest.mark.regression
@@ -204,7 +230,7 @@ def test_uzf02_sim_scope_fname(function_tmpdir, example_data_path):
204230
sim.set_sim_path(ws)
205231
sim.write_simulation(netcdf=netcdf)
206232

207-
check_netcdf(ws / fname, mesh=netcdf)
233+
check_netcdf(ws / fname, sim.get_model(sim_name), mesh=netcdf)
208234

209235

210236
@pytest.mark.regression
@@ -232,7 +258,7 @@ def test_uzf01_model_scope_nomesh(function_tmpdir, example_data_path):
232258
# write dataset to netcdf
233259
ds.to_netcdf(ws / fname, format="NETCDF4", engine="netcdf4")
234260

235-
check_netcdf(ws / fname)
261+
check_netcdf(ws / fname, sim.get_model(sim_name))
236262

237263

238264
@pytest.mark.regression
@@ -261,7 +287,7 @@ def test_uzf01_model_scope_mesh(function_tmpdir, example_data_path):
261287
# write dataset to netcdf
262288
ds.to_netcdf(ws / fname, format="NETCDF4", engine="netcdf4")
263289

264-
check_netcdf(ws / fname, mesh=mesh)
290+
check_netcdf(ws / fname, sim.get_model(sim_name), mesh=mesh)
265291

266292

267293
@pytest.mark.regression
@@ -290,7 +316,7 @@ def test_uzf02_model_scope(function_tmpdir, example_data_path):
290316
# write dataset to netcdf
291317
ds.to_netcdf(ws / fname, format="NETCDF4", engine="netcdf4")
292318

293-
check_netcdf(ws / fname, mesh=mesh)
319+
check_netcdf(ws / fname, sim.get_model(sim_name), mesh=mesh)
294320

295321

296322
@pytest.mark.regression
@@ -338,7 +364,7 @@ def test_uzf01_pkg_scope(function_tmpdir, example_data_path):
338364
# write dataset to netcdf
339365
ds.to_netcdf(ws / fname, format="NETCDF4", engine="netcdf4")
340366

341-
check_netcdf(ws / fname)
367+
check_netcdf(ws / fname, sim.get_model(sim_name))
342368

343369

344370
@pytest.mark.regression
@@ -417,7 +443,7 @@ def test_uzf01_pkg_scope_modify(function_tmpdir, example_data_path):
417443
# write dataset to netcdf
418444
ds.to_netcdf(ws / fname, format="NETCDF4", engine="netcdf4")
419445

420-
check_netcdf(ws / fname)
446+
check_netcdf(ws / fname, sim.get_model(sim_name))
421447
assert (
422448
ds["npf_k_updated"].attrs["standard_name"]
423449
== "soil_hydraulic_conductivity_at_saturation"
@@ -440,15 +466,14 @@ def test_uzf01_cycle(function_tmpdir, example_data_path):
440466
sim.set_sim_path(ws)
441467
sim.write_simulation(netcdf=netcdf)
442468

443-
check_netcdf(ws / fname)
469+
check_netcdf(ws / fname, sim.get_model(sim_name))
444470

445471
# set simulation path and rewrite base simulation
446472
sim.set_sim_path(ws / "mf6")
447-
# gwf = sim.get_model(sim_name)
448-
# gwf.name_file.nc_filerecord = None
449473
sim.write_simulation()
450474

451475
assert not (ws / "mf6" / fname).exists()
452476

477+
# codegen isn't using latest modflow dev branch
453478
# success, buff = sim.run_simulation(silent=True, report=True)
454479
# assert success, pformat(buff)

flopy/discretization/grid.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1262,15 +1262,16 @@ def write_shapefile(self, filename="grid.shp", crs=None, prjfile=None, **kwargs)
12621262

12631263
def dataset(self, modeltime=None, mesh=None):
12641264
"""
1265-
Method to generate baseline Xarray dataset
1265+
Method to generate baseline xarray dataset
12661266
12671267
Parameters
12681268
----------
1269-
mesh
1269+
modeltime : FloPy ModelTime object
1270+
mesh : mesh type
12701271
12711272
Returns
12721273
-------
1273-
Xarray dataset
1274+
xarray dataset
12741275
"""
12751276
raise NotImplementedError("dataset must be defined in the child class")
12761277

flopy/discretization/structuredgrid.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1676,6 +1676,11 @@ def get_plottable_layer_array(self, a, layer):
16761676
return plotarray
16771677

16781678
def dataset(self, modeltime=None, mesh=None):
1679+
"""
1680+
modeltime : FloPy ModelTime object
1681+
mesh : mesh type
1682+
valid mesh types are "layered" or None
1683+
"""
16791684
from ..utils import import_optional_dependency
16801685

16811686
xr = import_optional_dependency("xarray")
@@ -1696,6 +1701,17 @@ def _layered_mesh_dataset(self, ds, modeltime=None):
16961701
FILLNA_DBL = 9.96920996838687e36
16971702
lenunits = {0: "m", 1: "ft", 2: "m", 3: "m"}
16981703

1704+
# create dataset coordinate vars
1705+
var_d = {
1706+
"time": (["time"], modeltime.totim),
1707+
}
1708+
ds = ds.assign(var_d)
1709+
ds["time"].attrs["calendar"] = "standard"
1710+
ds["time"].attrs["units"] = f"days since {modeltime.start_datetime}"
1711+
ds["time"].attrs["axis"] = "T"
1712+
ds["time"].attrs["standard_name"] = "time"
1713+
ds["time"].attrs["long_name"] = "time"
1714+
16991715
# mesh container variable
17001716
ds = ds.assign({"mesh": ([], np.int32(1))})
17011717
ds["mesh"].attrs["cf_role"] = "mesh_topology"

flopy/discretization/vertexgrid.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -601,6 +601,12 @@ def get_plottable_layer_array(self, a, layer):
601601
return plotarray
602602

603603
def dataset(self, modeltime=None, mesh=None):
604+
"""
605+
modeltime : FloPy ModelTime object
606+
mesh : mesh type
607+
valid mesh types are "layered" or None
608+
VertexGrid objects only support layered mesh
609+
"""
604610
from ..utils import import_optional_dependency
605611

606612
xr = import_optional_dependency("xarray")
@@ -618,6 +624,17 @@ def dataset(self, modeltime=None, mesh=None):
618624
ds = xr.Dataset()
619625
ds.attrs["modflow_grid"] = "VERTEX"
620626

627+
# create dataset coordinate vars
628+
var_d = {
629+
"time": (["time"], modeltime.totim),
630+
}
631+
ds = ds.assign(var_d)
632+
ds["time"].attrs["calendar"] = "standard"
633+
ds["time"].attrs["units"] = f"days since {modeltime.start_datetime}"
634+
ds["time"].attrs["axis"] = "T"
635+
ds["time"].attrs["standard_name"] = "time"
636+
ds["time"].attrs["long_name"] = "time"
637+
621638
# mesh container variable
622639
ds = ds.assign({"mesh": ([], np.int32(1))})
623640
ds["mesh"].attrs["cf_role"] = "mesh_topology"

flopy/mf6/mfmodel.py

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,7 +1324,7 @@ def write(
13241324
with relative paths, leaving files defined by absolute paths fixed.
13251325
netcdf : str
13261326
ASCII package files will be written as configured for NetCDF input.
1327-
'mesh2d' and 'structured' are supported types.
1327+
'layered' and 'structured' are supported types.
13281328
"""
13291329

13301330
write_netcdf = netcdf and (
@@ -2338,7 +2338,6 @@ def update_dataset(self, dataset, netcdf_info=None, mesh=None):
23382338
for v in nc_info:
23392339
name = nc_info[v]["attrs"]["modflow_input"].split("/")[2].lower()
23402340
if mesh == None:
2341-
#name = nc_info[v]["varname"].rsplit("_", 1)[1]
23422341
d = getattr(p, name)
23432342
if d.repeating:
23442343
for per in d.get_data():
@@ -2348,10 +2347,8 @@ def update_dataset(self, dataset, netcdf_info=None, mesh=None):
23482347
dataset[nc_info[v]["varname"]].values = d.get_data()
23492348
elif mesh.upper() == "LAYERED":
23502349
if "layer" in nc_info[v]["attrs"]:
2351-
#name = nc_info[v]["varname"].rsplit("_", 2)[1]
23522350
layer = nc_info[v]["attrs"]["layer"] - 1
23532351
else:
2354-
#name = nc_info[v]["varname"].rsplit("_", 1)[1]
23552352
layer = -1
23562353
d = getattr(p, name)
23572354
if d.repeating:

flopy/mf6/mfpackage.py

Lines changed: 25 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3464,7 +3464,7 @@ def plot(self, **kwargs):
34643464

34653465
@staticmethod
34663466
def _add_netcdf_entries(
3467-
attrs, mname, pname, data_item, auxiliary=None, mesh=None, nlay=1
3467+
entries, mname, pname, data_item, auxiliary=None, mesh=None, nlay=1
34683468
):
34693469
DNODATA = 3.0e30 # MF6 DNODATA constant
34703470
FILLNA_INT32 = np.int32(-2147483647) # netcdf-fortran NF90_FILL_INT
@@ -3478,7 +3478,7 @@ def _add_netcdf_entries(
34783478
def _add_entry(tagname, iaux=None, layer=None):
34793479

34803480
# netcdf variable dictionary
3481-
a = {}
3481+
e = {}
34823482

34833483
# set dict key and netcdf variable name
34843484
key = tagname
@@ -3493,11 +3493,11 @@ def _add_entry(tagname, iaux=None, layer=None):
34933493
name = f"{name}_l{layer}"
34943494

34953495
# add non-attrs to dictionary
3496-
a["varname"] = name.lower()
3496+
e["varname"] = name.lower()
34973497
if (data_item.type) == DatumType.integer:
3498-
a["xarray_type"] = np.int32
3498+
e["xarray_type"] = np.int32
34993499
elif (data_item.type) == DatumType.double_precision:
3500-
a["xarray_type"] = np.float64
3500+
e["xarray_type"] = np.float64
35013501
dims = []
35023502
if data_item.shape[0] == 'nodes':
35033503
if data_item.block_name == "griddata":
@@ -3529,27 +3529,30 @@ def _add_entry(tagname, iaux=None, layer=None):
35293529
elif data_item.shape[0] == 'nrow':
35303530
dims.append("y")
35313531

3532-
a["netcdf_shape"] = dims[::-1]
3532+
e["netcdf_shape"] = dims[::-1]
35333533

35343534
# add variable attributes dictionary
3535-
a["attrs"] = {}
3536-
a["attrs"]["modflow_input"] = (f"{mname}/{pname}/{tagname}").upper()
3535+
e["attrs"] = {}
3536+
e["attrs"]["modflow_input"] = (f"{mname}/{pname}/{tagname}").upper()
35373537
if iaux is not None:
3538-
a["attrs"]["modflow_iaux"] = iaux + 1
3538+
e["attrs"]["modflow_iaux"] = iaux + 1
35393539
if layer is not None:
3540-
a["attrs"]["layer"] = layer
3540+
e["attrs"]["layer"] = layer
35413541
if (data_item.type) == DatumType.integer:
3542-
a["attrs"]["_FillValue"] = FILLNA_INT32
3542+
e["attrs"]["_FillValue"] = FILLNA_INT32
35433543
elif (data_item.type) == DatumType.double_precision:
35443544
if data_item.block_name == "griddata":
3545-
a["attrs"]["_FillValue"] = FILLNA_DBL
3545+
e["attrs"]["_FillValue"] = FILLNA_DBL
35463546
elif data_item.block_name == "period":
3547-
a["attrs"]["_FillValue"] = DNODATA
3547+
e["attrs"]["_FillValue"] = DNODATA
35483548
if data_item.longname is not None:
3549-
a["attrs"]["longname"] = data_item.longname
3549+
if layer is not None:
3550+
e["attrs"]["longname"] = f"{data_item.longname} layer {layer}"
3551+
else:
3552+
e["attrs"]["longname"] = data_item.longname
35503553

35513554
# set dictionary
3552-
attrs[key] = a
3555+
entries[key] = e
35533556

35543557
if data_item.layered and mesh and mesh.upper() == "LAYERED":
35553558
if data_item.name == "aux" or data_item.name == "auxvar":
@@ -3570,7 +3573,7 @@ def _add_entry(tagname, iaux=None, layer=None):
35703573
def netcdf_package(mtype, ptype, auxiliary=None, mesh=None, nlay=1):
35713574
from .data.mfstructure import DfnPackage, MFSimulationStructure
35723575

3573-
attrs = {}
3576+
entries = {}
35743577
sim_struct = MFSimulationStructure()
35753578

35763579
for package in MFPackage.__subclasses__():
@@ -3593,7 +3596,7 @@ def netcdf_package(mtype, ptype, auxiliary=None, mesh=None, nlay=1):
35933596
for d in block.data_structures:
35943597
if block.data_structures[d].netcdf:
35953598
MFPackage._add_netcdf_entries(
3596-
attrs,
3599+
entries,
35973600
f"<{mtype}name>",
35983601
pname,
35993602
block.data_structures[d],
@@ -3602,10 +3605,10 @@ def netcdf_package(mtype, ptype, auxiliary=None, mesh=None, nlay=1):
36023605
nlay,
36033606
)
36043607

3605-
return attrs
3608+
return entries
36063609

36073610
def netcdf_info(self, mesh=None):
3608-
attrs = {}
3611+
entries = {}
36093612

36103613
if self.dimensions.get_aux_variables():
36113614
auxnames = list(self.dimensions.get_aux_variables()[0])
@@ -3624,7 +3627,7 @@ def netcdf_info(self, mesh=None):
36243627
):
36253628
if dataset.structure.netcdf and dataset.has_data():
36263629
MFPackage._add_netcdf_entries(
3627-
attrs,
3630+
entries,
36283631
self.model_name,
36293632
self.package_name,
36303633
dataset.structure,
@@ -3633,7 +3636,7 @@ def netcdf_info(self, mesh=None):
36333636
self.model_or_sim.modelgrid.nlay,
36343637
)
36353638

3636-
return attrs
3639+
return entries
36373640

36383641
def update_dataset(self, dataset, netcdf_info=None, mesh=None):
36393642
from ..discretization.vertexgrid import VertexGrid
@@ -3682,7 +3685,7 @@ def _data_shape(shape):
36823685
return dataset
36833686

36843687
def _set_netcdf_storage(self, reset=False):
3685-
"""Set griddata array dataset storage to netcdf.
3688+
"""set array dataset storage to netcdf.
36863689
36873690
Parameters
36883691
----------

0 commit comments

Comments
 (0)