Skip to content

Commit e516715

Browse files
mjrenomjreno
authored andcommitted
add tests
1 parent 40c4737 commit e516715

File tree

5 files changed

+615
-341
lines changed

5 files changed

+615
-341
lines changed
Lines changed: 78 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
# extension: .py
88
# format_name: light
99
# format_version: '1.5'
10-
# jupytext_version: 1.17.2
10+
# jupytext_version: 1.16.4
1111
# kernelspec:
1212
# display_name: Python 3 (ipykernel)
1313
# language: python
@@ -18,13 +18,15 @@
1818
#
1919
# ## NetCDF tutorial 1: MODFLOW 6 structured input file
2020
#
21-
# This tutorial demonstrates how to generate a MODFLOW 6 NetCDF file from
22-
# an existing FloPy simulation. In the tutorial, candidate array data is
23-
# added to an xarray dataset and annotated so that the generated NetCDF
24-
# file can be read by MODFLOW 6 as model input.
21+
# This tutorial shows how to generate a MODFLOW 6 NetCDF file from
22+
# an existing FloPy simulation. Two methods will be demonstrated that
23+
# generate a simulation with package data stored in a model NetCDF
24+
# file. The first method is non-interactive- FloPy will generate the
25+
# file with a modified `write_simulation()` call. The second method
26+
# is interactive, which provides an oppurtinity to modify the dataset
27+
# before it is written to NetCDF.
2528
#
26-
# This tutorial generates a structured NetCDF variant - for more information
27-
# on supported MODFLOW 6 NetCDF formats see:
29+
# For more information on supported MODFLOW 6 NetCDF formats see:
2830
# [MODFLOW NetCDF Format](https://github.com/MODFLOW-ORG/modflow6/wiki/MODFLOW-NetCDF-Format).
2931
#
3032
# Note that NetCDF is only supported by the Extended version of MODFLOW 6.
@@ -75,13 +77,13 @@
7577
"uzf01.uzf.obs": None,
7678
}
7779

78-
# for fname, fhash in file_names.items():
79-
# pooch.retrieve(
80-
# url=f"https://github.com/modflowpy/flopy/raw/develop/examples/data/{sim_name}/{fname}",
81-
# fname=fname,
82-
# path=data_path / sim_name,
83-
# known_hash=fhash,
84-
# )
80+
for fname, fhash in file_names.items():
81+
pooch.retrieve(
82+
url=f"https://github.com/modflowpy/flopy/raw/develop/examples/data/{sim_name}/{fname}",
83+
fname=fname,
84+
path=data_path / sim_name,
85+
known_hash=fhash,
86+
)
8587

8688
# ## Create simulation workspace
8789

@@ -104,7 +106,44 @@
104106
success, buff = sim.run_simulation(silent=True, report=True)
105107
assert success, pformat(buff)
106108

107-
# ## Create NetCDF based simulation
109+
# ## Create NetCDF based simulation method 1
110+
#
111+
# This is the most straightforward way to create a NetCDF simulation
112+
# from the loaded ascii input simulation. Simply define the `netcdf`
113+
# argument to `write_simulation()` to be either `structured` or
114+
# `layered`, depending on the desired format of the generated NetCDF
115+
# file.
116+
#
117+
# The name of the created file can be specified by first setting the
118+
# model `name_file.nc_filerecord` attribute to the desired name. If
119+
# this step is not taken, the default name of `{model_name}.input.nc`
120+
# is used.
121+
122+
# create directory for netcdf sim
123+
sim.set_sim_path(workspace / "netcdf1")
124+
# set model name file nc_filerecord attribute to export name
125+
gwf = sim.get_model("uzf01")
126+
gwf.name_file.nc_filerecord = "uzf01.structured.nc"
127+
# write simulation with structured NetCDF file
128+
sim.write_simulation(netcdf="structured")
129+
130+
# success, buff = sim.run_simulation(silent=True, report=True)
131+
# assert success, pformat(buff)
132+
133+
# ## Repeat method 1 with layered mesh NetCDF format
134+
135+
# create directory for netcdf sim
136+
sim.set_sim_path(workspace / "netcdf2")
137+
# set model name file nc_filerecord attribute to export name
138+
gwf = sim.get_model("uzf01")
139+
gwf.name_file.nc_filerecord = "uzf01.layered.nc"
140+
# write simulation with with layered mesh NetCDF file
141+
sim.write_simulation(netcdf="layered")
142+
143+
# success, buff = sim.run_simulation(silent=True, report=True)
144+
# assert success, pformat(buff)
145+
146+
# ## Create NetCDF based simulation method 2
108147
#
109148
# Reset the simulation path and set the `GWF` name file `nc_filerecord`
110149
# attribute to the name of the intended input NetCDF file. Display
@@ -116,21 +155,30 @@
116155
# and `GHBG` packages. Data will be copied from the package objects into
117156
# dataset arrays.
118157
#
119-
# Flopy does not currently generate the NetCDF input file. This tutorial
120-
# shows one way that can be accomplished.
158+
# Flopy will not generate the NetCDF input file when the `netcdf` argument
159+
# to `write_simulation()` is set to `nofile`. This step is needed, however,
160+
# to update ascii input with the keywords required to support the model
161+
# NetCDF file that we will generate.
121162

122163
# create directory for netcdf sim
123-
sim.set_sim_path(workspace / "netcdf")
164+
sim.set_sim_path(workspace / "netcdf3")
124165
# set model name file nc_filerecord attribute to export name
125166
gwf = sim.get_model("uzf01")
126167
gwf.name_file.nc_filerecord = "uzf01.structured.nc"
127168
# write simulation with ASCII inputs tagged for NetCDF
128-
sim.write_simulation(netcdf=True)
169+
# but do not create NetCDF file
170+
sim.write_simulation(netcdf="nofile")
171+
172+
# ## Show name file with NetCDF input configured
173+
129174
# show name file with NetCDF input configured
130-
with open(workspace / "netcdf" / "uzf01.nam", "r") as fh:
175+
with open(workspace / "netcdf3" / "uzf01.nam", "r") as fh:
131176
print(fh.read())
177+
178+
# ## Show example package file with NetCDF keywords
179+
132180
# show example package file with NetCDF input configured
133-
with open(workspace / "netcdf" / "uzf01.ic", "r") as fh:
181+
with open(workspace / "netcdf3" / "uzf01.ic", "r") as fh:
134182
print(fh.read())
135183

136184
# ## Create dataset
@@ -184,14 +232,15 @@
184232
# existing simulation objects and update the dataset.
185233
#
186234
# Default dataset variable names are defined in the package `netcdf_info()`
187-
# dictionary.
235+
# dictionary. Here we will use the info dictionary to programmatically update
236+
# the dataset- for remaining packages we will hardcode the variable names
237+
# being updated for maximum clarity.
188238

189-
# update dataset from dis arrays
190-
ds["dis_delr"].values = dis.delr.get_data()
191-
ds["dis_delc"].values = dis.delc.get_data()
192-
ds["dis_top"].values = dis.top.get_data()
193-
ds["dis_botm"].values = dis.botm.get_data()
194-
ds["dis_idomain"].values = dis.idomain.get_data()
239+
nc_info = dis.netcdf_info()
240+
for v in nc_info:
241+
name = nc_info[v]["attrs"]["modflow_input"].rsplit("/", 1)[1].lower()
242+
d = getattr(dis, name)
243+
ds[nc_info[v]["varname"]].values = d.get_data()
195244

196245
# ## Access `NPF` package NetCDF attributes
197246
#
@@ -277,7 +326,7 @@
277326

278327
# write dataset to netcdf
279328
ds.to_netcdf(
280-
workspace / "netcdf/uzf01.structured.nc", format="NETCDF4", engine="netcdf4"
329+
workspace / "netcdf3" / "uzf01.structured.nc", format="NETCDF4", engine="netcdf4"
281330
)
282331

283332
# ## Run MODFLOW 6 simulation with NetCDF input

0 commit comments

Comments
 (0)