Skip to content

Commit 850d90e

Browse files
mjrenomjreno
authored andcommitted
discretization creates dataset
1 parent 8522e10 commit 850d90e

File tree

3 files changed

+96
-22
lines changed

3 files changed

+96
-22
lines changed

.docs/Notebooks/netcdf01_tutorial.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@
6262
# For the purposes of this tutorial, the specifics of this simulation
6363
# other than it is a candidate for NetCDF input are not a focus. It
6464
# is a NetCDF input candidate because it defines a candidate model type
65-
# (`GWF6`) with packages that support NetCDF input parameters.
65+
# (`GWF6`) with a structured discretization and packages that support
66+
# NetCDF input parameters.
6667
#
6768
# A NetCDF dataset will be created from array data in the `DIS`, `NPF` and
6869
# `GHBG` packages. Data will be copied from the package objects into dataset
@@ -274,10 +275,14 @@ def _data_shape(shape):
274275
return dataset
275276

276277

278+
# ## Create simulation workspace
279+
277280
# create temporary directories
278281
temp_dir = TemporaryDirectory()
279282
workspace = Path(temp_dir.name)
280283

284+
# ## Write and run baseline simulation
285+
281286
# run the non-netcdf simulation
282287
sim = create_sim(ws=workspace)
283288
sim.write_simulation()
@@ -300,9 +305,14 @@ def _data_shape(shape):
300305
print(fh.read())
301306

302307
# ## Create dataset
308+
#
309+
# Create the base xarray dataset from the modelgrid object. This
310+
# will add required dimensions and coordinate variables to the
311+
# dataset according to the grid specification. Modeltime is needed
312+
# to for timeseries support.
303313

304314
# create the dataset
305-
ds = xr.Dataset()
315+
ds = gwf.modelgrid.dataset(modeltime=gwf.modeltime)
306316

307317
# ## Access model NetCDF attributes
308318
#
@@ -322,27 +332,15 @@ def _data_shape(shape):
322332
for a in nc_info["attrs"]:
323333
ds.attrs[a] = nc_info["attrs"][a]
324334

325-
# ## Define dimensions relevant to NetCDF input file
335+
# ## Map dataset dimension names to values
326336

327337
# define dimensional info
328-
dis = gwf.modelgrid
329-
xoff = dis.xoffset
330-
yoff = dis.yoffset
331-
x = xoff + dis.xycenters[0]
332-
y = yoff + dis.xycenters[1]
333-
z = [float(x) for x in range(1, dis.nlay + 1)]
334-
nstp = sum(gwf.modeltime.nstp)
335-
time = gwf.modeltime.tslen
336-
nlay = dis.nlay
337-
nrow = dis.nrow
338-
ncol = dis.ncol
339-
dimmap = {"time": nstp, "z": nlay, "y": nrow, "x": ncol}
340-
341-
# ## Create dataset dimensions
342-
343-
# create dataset coordinate vars
344-
var_d = {"time": (["time"], time), "z": (["z"], z), "y": (["y"], y), "x": (["x"], x)}
345-
ds = ds.assign(var_d)
338+
dimmap = {
339+
"time": sum(gwf.modeltime.nstp),
340+
"z": gwf.modelgrid.nlay,
341+
"y": gwf.modelgrid.nrow,
342+
"x": gwf.modelgrid.ncol,
343+
}
346344

347345
# ## Access package NetCDF attributes
348346
#
@@ -406,7 +404,6 @@ def _data_shape(shape):
406404
with open(workspace / "netcdf" / "uzf01.dis", "r") as fh:
407405
print(fh.read())
408406

409-
410407
# ## Update MODFLOW 6 package input file
411408
#
412409
# Follow the same process as above for the `NPF` package.

flopy/discretization/grid.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1260,6 +1260,20 @@ def write_shapefile(self, filename="grid.shp", crs=None, prjfile=None, **kwargs)
12601260
)
12611261
return
12621262

1263+
def dataset(self, mesh=None):
1264+
"""
1265+
Method to generate baseline Xarray dataset
1266+
1267+
Parameters
1268+
----------
1269+
mesh
1270+
1271+
Returns
1272+
-------
1273+
Xarray dataset
1274+
"""
1275+
raise NotImplementedError("dataset must be defined in the child class")
1276+
12631277
# initialize grid from a grb file
12641278
@classmethod
12651279
def from_binary_grid_file(cls, file_path, verbose=False):

flopy/discretization/structuredgrid.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1675,6 +1675,69 @@ def get_plottable_layer_array(self, a, layer):
16751675
assert plotarray.shape == required_shape, msg
16761676
return plotarray
16771677

1678+
def dataset(self, modeltime=None, mesh=None):
1679+
import xarray as xr
1680+
1681+
lenunits = {0: "m", 1: "ft", 2: "m", 3: "m"}
1682+
1683+
ds = xr.Dataset()
1684+
1685+
x = self.xoffset + self.xycenters[0]
1686+
y = self.yoffset + self.xycenters[1]
1687+
z = [float(x) for x in range(1, self.nlay + 1)]
1688+
1689+
# set coordinate var bounds
1690+
x_bnds = []
1691+
xv = self.xoffset + self.xyedges[0]
1692+
for idx, val in enumerate(xv):
1693+
if idx + 1 < len(xv):
1694+
bnd = []
1695+
bnd.append(xv[idx])
1696+
bnd.append(xv[idx + 1])
1697+
x_bnds.append(bnd)
1698+
1699+
y_bnds = []
1700+
yv = self.yoffset + self.xyedges[1]
1701+
for idx, val in enumerate(yv):
1702+
if idx + 1 < len(yv):
1703+
bnd = []
1704+
bnd.append(yv[idx + 1])
1705+
bnd.append(yv[idx])
1706+
y_bnds.append(bnd)
1707+
1708+
# create dataset coordinate vars
1709+
var_d = {
1710+
"time": (["time"], modeltime.totim),
1711+
"z": (["z"], z),
1712+
"y": (["y"], y),
1713+
"x": (["x"], x),
1714+
}
1715+
ds = ds.assign(var_d)
1716+
1717+
# create bound vars
1718+
var_d = {"x_bnds": (["x", "bnd"], x_bnds), "y_bnds": (["y", "bnd"], y_bnds)}
1719+
ds = ds.assign(var_d)
1720+
1721+
ds["time"].attrs["calendar"] = "standard"
1722+
ds["time"].attrs["units"] = f"days since {modeltime.start_datetime}"
1723+
ds["time"].attrs["axis"] = "T"
1724+
ds["time"].attrs["standard_name"] = "time"
1725+
ds["time"].attrs["long_name"] = "time"
1726+
ds["z"].attrs["units"] = "layer"
1727+
ds["z"].attrs["long_name"] = "layer number"
1728+
ds["y"].attrs["units"] = lenunits[self.lenuni]
1729+
ds["y"].attrs["axis"] = "Y"
1730+
ds["y"].attrs["standard_name"] = "projection_y_coordinate"
1731+
ds["y"].attrs["long_name"] = "Northing"
1732+
ds["y"].attrs["bounds"] = "y_bnds"
1733+
ds["x"].attrs["units"] = lenunits[self.lenuni]
1734+
ds["x"].attrs["axis"] = "X"
1735+
ds["x"].attrs["standard_name"] = "projection_x_coordinate"
1736+
ds["x"].attrs["long_name"] = "Easting"
1737+
ds["x"].attrs["bounds"] = "x_bnds"
1738+
1739+
return ds
1740+
16781741
def _set_structured_iverts(self):
16791742
"""
16801743
Build a list of the vertices that define each model cell and the x, y

0 commit comments

Comments
 (0)