Skip to content

Commit 83e5167

Browse files
mjrenomjreno
authored andcommitted
static methods return full info dict
1 parent 05f1728 commit 83e5167

File tree

4 files changed

+43
-22
lines changed

4 files changed

+43
-22
lines changed

.docs/Notebooks/netcdf01_tutorial.py

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
from pathlib import Path
3-
from pprint import pformat
3+
from pprint import pformat, pprint
44
from tempfile import TemporaryDirectory
55

66
import numpy as np
@@ -14,6 +14,8 @@
1414
DNODATA = 3.0e30
1515

1616

17+
# A FloPy simulation ASCII sim that will be updated
18+
# use netcdf inputs
1719
def create_sim(ws):
1820
name = "uzf01"
1921
perlen = [500.0]
@@ -185,6 +187,8 @@ def create_sim(ws):
185187
return sim
186188

187189

190+
# A subroutine that can update an xarray dataset
191+
# with package netcdf information stored in a dict
188192
def add_netcdf_vars(dataset, nc_info, dimmap):
189193
def _data_shape(shape):
190194
dims_l = []
@@ -196,18 +200,19 @@ def _data_shape(shape):
196200
for v in nc_info:
197201
varname = nc_info[v]["varname"]
198202
data = np.full(
199-
_data_shape(nc_info[v]["nc_shape"]),
203+
_data_shape(nc_info[v]["netcdf_shape"]),
200204
nc_info[v]["attrs"]["_FillValue"],
201-
dtype=nc_info[v]["nc_type"],
205+
dtype=nc_info[v]["xarray_type"],
202206
)
203-
var_d = {varname: (nc_info[v]["nc_shape"], data)}
207+
var_d = {varname: (nc_info[v]["netcdf_shape"], data)}
204208
dataset = dataset.assign(var_d)
205209
for a in nc_info[v]["attrs"]:
206210
dataset[varname].attrs[a] = nc_info[v]["attrs"][a]
207211

208212
return dataset
209213

210214

215+
# create temporary directories
211216
temp_dir = TemporaryDirectory()
212217
workspace = Path(temp_dir.name)
213218

@@ -228,6 +233,7 @@ def _data_shape(shape):
228233

229234
# get model netcdf info
230235
nc_info = gwf.netcdf_info()
236+
pprint(nc_info)
231237

232238
# update dataset with required attributes
233239
for a in nc_info["attrs"]:
@@ -254,6 +260,7 @@ def _data_shape(shape):
254260
# dis
255261
dis = gwf.get_package("dis")
256262
nc_info = dis.netcdf_info()
263+
pprint(nc_info)
257264

258265
# create dis dataset variables
259266
ds = add_netcdf_vars(ds, nc_info, dimmap)
@@ -286,6 +293,7 @@ def _data_shape(shape):
286293
# npf
287294
npf = gwf.get_package("npf")
288295
nc_info = npf.netcdf_info()
296+
pprint(nc_info)
289297

290298
# create npf dataset variables
291299
ds = add_netcdf_vars(ds, nc_info, dimmap)
@@ -306,6 +314,7 @@ def _data_shape(shape):
306314
# get ghbg package netcdf info
307315
ghbg = gwf.get_package("ghbg_0")
308316
nc_info = ghbg.netcdf_info()
317+
pprint(nc_info)
309318

310319
# create ghbg dataset variables
311320
ds = add_netcdf_vars(ds, nc_info, dimmap)
@@ -320,6 +329,9 @@ def _data_shape(shape):
320329
istp = sum(gwf.modeltime.nstp[0:p])
321330
ds["ghbg_0_cond"].values[istp] = ghbg.cond.get_data()[p]
322331

332+
# show the dataset
333+
print(ds)
334+
323335
# write the netcdf
324336
ds.to_netcdf(
325337
workspace / "netcdf/uzf01.structured.nc", format="NETCDF4", engine="netcdf4"

.docs/Notebooks/netcdf02_tutorial.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import sys
22
from pathlib import Path
3-
from pprint import pformat
3+
from pprint import pformat, pprint
44
from tempfile import TemporaryDirectory
55

66
import numpy as np
@@ -15,6 +15,8 @@
1515
DNODATA = 3.0e30
1616

1717

18+
# A FloPy simulation ASCII sim that will be updated
19+
# use netcdf inputs
1820
def create_sim(ws):
1921
name = "flow"
2022
gwfname = name
@@ -186,6 +188,8 @@ def create_sim(ws):
186188
return sim
187189

188190

191+
# A subroutine that can update an xarray dataset
192+
# with package netcdf information stored in a dict
189193
def add_netcdf_vars(dataset, nc_info, dimmap):
190194
def _data_shape(shape):
191195
dims_l = []
@@ -197,18 +201,19 @@ def _data_shape(shape):
197201
for v in nc_info:
198202
varname = nc_info[v]["varname"]
199203
data = np.full(
200-
_data_shape(nc_info[v]["nc_shape"]),
204+
_data_shape(nc_info[v]["netcdf_shape"]),
201205
nc_info[v]["attrs"]["_FillValue"],
202-
dtype=nc_info[v]["nc_type"],
206+
dtype=nc_info[v]["xarray_type"],
203207
)
204-
var_d = {varname: (nc_info[v]["nc_shape"], data)}
208+
var_d = {varname: (nc_info[v]["netcdf_shape"], data)}
205209
dataset = dataset.assign(var_d)
206210
for a in nc_info[v]["attrs"]:
207211
dataset[varname].attrs[a] = nc_info[v]["attrs"][a]
208212

209213
return dataset
210214

211215

216+
# create temporary directories
212217
temp_dir = TemporaryDirectory()
213218
workspace = Path(temp_dir.name)
214219

@@ -229,6 +234,7 @@ def _data_shape(shape):
229234

230235
# get model netcdf info
231236
nc_info = gwf.netcdf_info()
237+
pprint(nc_info)
232238

233239
# update dataset with required attributes
234240
for a in nc_info["attrs"]:
@@ -258,6 +264,7 @@ def _data_shape(shape):
258264
# update for welg
259265
welg = gwf.get_package("wel-1")
260266
nc_info = welg.netcdf_info()
267+
pprint(nc_info)
261268

262269
# create welg dataset variables
263270
ds = add_netcdf_vars(ds, nc_info, dimmap)
@@ -292,6 +299,7 @@ def _data_shape(shape):
292299
# get ghbg package netcdf info
293300
ghbg = gwf.get_package(f"ghb-{ip}")
294301
nc_info = ghbg.netcdf_info()
302+
pprint(nc_info)
295303

296304
# create ghbg dataset variables
297305
ds = add_netcdf_vars(ds, nc_info, dimmap)
@@ -334,6 +342,7 @@ def _data_shape(shape):
334342
# get rivg package netcdf info
335343
rivg = gwf.get_package(f"riv-{ip}")
336344
nc_info = rivg.netcdf_info()
345+
pprint(nc_info)
337346

338347
# create rivg dataset variables
339348
ds = add_netcdf_vars(ds, nc_info, dimmap)
@@ -382,6 +391,7 @@ def _data_shape(shape):
382391
# get drng package netcdf info
383392
drng = gwf.get_package(f"drn-{ip}")
384393
nc_info = drng.netcdf_info()
394+
pprint(nc_info)
385395

386396
# create drng dataset variables
387397
ds = add_netcdf_vars(ds, nc_info, dimmap)
@@ -416,6 +426,9 @@ def _data_shape(shape):
416426
f.write(" concentration NETCDF\n")
417427
f.write("END period 1\n\n")
418428

429+
# show the dataset
430+
print(ds)
431+
419432
# write the netcdf
420433
ds.to_netcdf(
421434
workspace / "netcdf/flow.structured.nc", format="NETCDF4", engine="netcdf4"

flopy/mf6/mfmodel.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2205,14 +2205,14 @@ def _resolve_idomain(idomain, botm):
22052205
return idomain
22062206

22072207
@staticmethod
2208-
def netcdf_attrs(mname, mtype, grid_type, mesh=None):
2208+
def netcdf_model(mname, mtype, grid_type, mesh=None):
22092209
"""Return dictionary of dataset (model) scoped attributes
22102210
Parameters
22112211
----------
22122212
mname : str
22132213
model name
22142214
mtype : str
2215-
model type
2215+
model type, e.g. GWF6
22162216
grid_type:
22172217
DiscretizationType
22182218
mesh : str
@@ -2227,7 +2227,7 @@ def netcdf_attrs(mname, mtype, grid_type, mesh=None):
22272227
elif grid_type == DiscretizationType.DISV:
22282228
attrs["modflow_grid"] = "VERTEX"
22292229

2230-
attrs["modflow_model"] = f"{mname.upper()}: MODFLOW 6 {mtype.upper()} model"
2230+
attrs["modflow_model"] = f"{mtype.upper()}: {mname.upper()}"
22312231

22322232
# supported => LAYERED
22332233
if mesh:
@@ -2242,7 +2242,7 @@ def netcdf_info(self, mesh=None):
22422242
mesh : str
22432243
mesh type if dataset is ugrid compliant
22442244
"""
2245-
attrs = MFModel.netcdf_attrs(
2245+
attrs = MFModel.netcdf_model(
22462246
self.name, self.model_type, self.get_grid_type(), mesh
22472247
)
22482248

flopy/mf6/mfpackage.py

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3455,9 +3455,9 @@ def _add_entry(tagname, iaux=None, layer=None):
34553455
# add non-attrs to dictionary
34563456
a["varname"] = name.lower()
34573457
if (data_item.type) == DatumType.integer:
3458-
a["nc_type"] = np.int32
3458+
a["xarray_type"] = np.int32
34593459
elif (data_item.type) == DatumType.double_precision:
3460-
a["nc_type"] = np.float64
3460+
a["xarray_type"] = np.float64
34613461
dims = []
34623462
if data_item.shape[0] == 'nodes':
34633463
if data_item.block_name == "griddata":
@@ -3470,7 +3470,7 @@ def _add_entry(tagname, iaux=None, layer=None):
34703470
for k, v in dimmap.items():
34713471
s = s.replace(k, v)
34723472
dims.append(s)
3473-
a["nc_shape"] = dims[::-1]
3473+
a["netcdf_shape"] = dims[::-1]
34743474

34753475
# add variable attributes dictionary
34763476
a["attrs"] = {}
@@ -3490,7 +3490,7 @@ def _add_entry(tagname, iaux=None, layer=None):
34903490
# set dictionary
34913491
attrs[key] = a
34923492

3493-
if data_item.layered and mesh == "LAYERED":
3493+
if data_item.layered and mesh and mesh.upper() == "LAYERED":
34943494
if data_item.name == "aux" or data_item.name == "auxvar":
34953495
for n, auxname in enumerate(auxnames):
34963496
for l in range(nlay):
@@ -3506,7 +3506,7 @@ def _add_entry(tagname, iaux=None, layer=None):
35063506
_add_entry(data_item.name)
35073507

35083508
@staticmethod
3509-
def netcdf_attrs(mtype, ptype, auxiliary=None, mesh=None, nlay=1):
3509+
def netcdf_package(mtype, ptype, auxiliary=None, mesh=None, nlay=1):
35103510
from .data.mfstructure import DfnPackage, MFSimulationStructure
35113511

35123512
attrs = {}
@@ -3541,11 +3541,7 @@ def netcdf_attrs(mtype, ptype, auxiliary=None, mesh=None, nlay=1):
35413541
nlay,
35423542
)
35433543

3544-
res_d = {}
3545-
for k in list(attrs):
3546-
res_d[k] = attrs[k]["attrs"]
3547-
3548-
return res_d
3544+
return attrs
35493545

35503546
def netcdf_info(self, mesh=None):
35513547
attrs = {}

0 commit comments

Comments
 (0)