Skip to content

Commit 80a4cec

Browse files
mjrenomjreno
authored andcommitted
edit netcdf tutorial
1 parent d4aeff6 commit 80a4cec

File tree

1 file changed

+77
-54
lines changed

1 file changed

+77
-54
lines changed

.docs/Notebooks/mf6_netcdf01_tutorial.py

Lines changed: 77 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,27 @@
77
# extension: .py
88
# format_name: light
99
# format_version: '1.5'
10-
# jupytext_version: 1.16.4
10+
# jupytext_version: 1.17.2
1111
# kernelspec:
1212
# display_name: Python 3 (ipykernel)
1313
# language: python
1414
# name: python3
1515
# ---
1616

17-
# # MODFLOW 6: Generate MODFLOW 6 NetCDF input from existing FloPy sim
17+
# # MODFLOW 6: MODFLOW 6 NetCDF inputs from FloPy simulation
1818
#
19-
# ## NetCDF tutorial 1: MODFLOW 6 structured input file
19+
# ## Write MODFLOW 6 NetCDF simulation
2020
#
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.
21+
# This tutorial demonstrates how to generate a MODFLOW 6 NetCDF file
22+
# from an existing FloPy simulation. Two variations will be shown.
23+
# In the first, FloPy will generate the file with a modified
24+
# `write_simulation()` call. The second method is more interactive,
25+
# providing an opputinity to modify the dataset before it is written
26+
# to NetCDF.
27+
#
28+
# Support for generating a MODFLOW 6 NetCDF input without a defined
29+
# FloPy mf6 model or package instances is briefly discussed at the
30+
# end of the tutorial.
2831
#
2932
# For more information on supported MODFLOW 6 NetCDF formats see:
3033
# [MODFLOW NetCDF Format](https://github.com/MODFLOW-ORG/modflow6/wiki/MODFLOW-NetCDF-Format).
@@ -93,11 +96,15 @@
9396

9497
# ## Load and run baseline simulation
9598
#
96-
# For the purposes of this tutorial, the specifics of this simulation
99+
# For the purposes of this tutorial, the specifics of the simulation
97100
# other than it is a candidate for NetCDF input are not a focus. It
98101
# is a NetCDF input candidate because it defines a supported model type
99102
# (`GWF6`) with a structured discretization and packages that support
100103
# NetCDF input parameters.
104+
#
105+
# More information about package NetCDF support in MODFLOW 6 can be
106+
# found in the `MODFLOW 6 - Description of Input and Output` (`mf6io.pdf`),
107+
# also available at the nightly build repository linked above.
101108

102109
# load and run the non-netcdf simulation
103110
sim = flopy.mf6.MFSimulation.load(sim_ws=data_path / sim_name)
@@ -108,11 +115,11 @@
108115

109116
# ## Create NetCDF based simulation method 1
110117
#
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.
118+
# The most straightforward way to create a NetCDF simulation
119+
# from the loaded simulation is to provide a `netcdf` argument
120+
# to `write_simulation()` and define it to be either `structured`
121+
# or `layered`, depending on the desired format of the generated
122+
# NetCDF file.
116123
#
117124
# The name of the created file can be specified by first setting the
118125
# model `name_file.nc_filerecord` attribute to the desired name. If
@@ -129,9 +136,9 @@
129136

130137
# ## Run MODFLOW 6 simulation with NetCDF input
131138
#
132-
# The simulation generated by this tutorial should be runnable by
133-
# Extended MODFLOW 6, available from the nightly-build repository
134-
# (linked above).
139+
# The simulation generated by this tutorial should run with the
140+
# Extended MODFLOW 6 executable, available from the nightly-build
141+
# repository (linked above).
135142

136143
# success, buff = sim.run_simulation(silent=True, report=True)
137144
# assert success, pformat(buff)
@@ -148,29 +155,31 @@
148155

149156
# ## Run MODFLOW 6 simulation with NetCDF input
150157
#
151-
# The simulation generated by this tutorial should be runnable by
152-
# Extended MODFLOW 6, available from the nightly-build repository
153-
# (linked above).
158+
# The simulation generated by this tutorial should run with the
159+
# Extended MODFLOW 6 executable, available from the nightly-build
160+
# repository (linked above).
154161

155162
# success, buff = sim.run_simulation(silent=True, report=True)
156163
# assert success, pformat(buff)
157164

158165
# ## Create NetCDF based simulation method 2
159166
#
167+
# In this method we will set the FloPy `netcdf` argument to `nofile`
168+
# when `write_simulation()` is called. As such, FloPy will not generate
169+
# the NetCDF file automatically. We will manage the NetCDF file
170+
# generation ourselves in method 2.
171+
#
160172
# Reset the simulation path and set the `GWF` name file `nc_filerecord`
161173
# attribute to the name of the intended input NetCDF file. Display
162174
# the resultant name file changes.
163175
#
164176
# When we write the updated simulation, all packages that support NetCDF
165-
# input parameters will be converted. We will therefore need to create a
177+
# input parameters will be written such that NetCDF parameters expect the
178+
# data source to be a NetCDF file. We will therefore need to create a
166179
# NetCDF input file containing arrays for the `DIS`, `NPF`, `IC`, `STO`,
167-
# and `GHBG` packages. Data will be copied from the package objects into
168-
# dataset arrays.
169-
#
170-
# Flopy will not generate the NetCDF input file when the `netcdf` argument
171-
# to `write_simulation()` is set to `nofile`. This step is needed, however,
172-
# to update ascii input with the keywords required to support the model
173-
# NetCDF file that we will generate.
180+
# and `GHBG` packages. We will still use FloPy package objects to set the
181+
# parameter data in the dataset; however an `update_data=False` argument
182+
# could be passed to the `update_dataset()` call if this was not desired.
174183

175184
# create directory for netcdf sim
176185
sim.set_sim_path(workspace / "netcdf3")
@@ -205,17 +214,28 @@
205214

206215
# ## Access model NetCDF attributes
207216
#
208-
# Access model scoped NetCDF details by storing the dictionary
209-
# returned from `netcdf_info()`. In particular, we need to set dataset
210-
# scoped attributes that are stored in the model netcdf info dict.
217+
# Internally, FloPy generates and uses NetCDF metadata dictionaries
218+
# to update datasets. Both model and package objects can generate
219+
# the dictionaries and they contain related but distinct NetCDF
220+
# details. Model object dictionaries contain file scoped attribute
221+
# information while package dictionaries are organized by NetCDF
222+
# variable and contain variable scoped attribute information and
223+
# details related to creating the variable, including dimensions,
224+
# shape and data type.
211225
#
212-
# First, retrieve and store the netcdf info dictionary and display
226+
# The dictionaries are available via the `netcdf_info()` call. Their
227+
# content also varies depending on the desired type of dataset (i.e.
228+
# `structured` or `layered`). In this step we will access the model
229+
# NetCDF metadata and use it to update dataset scoped attributes.
230+
#
231+
# First, retrieve and store the netcdf metadata dictionary and display
213232
# its contents. Then, in the following step, update the dataset with
214233
# the model scoped attributes defined in the dictionary.
215234
#
216-
# These 2 operations can also be accomplished by calling `update_dataset()`
217-
# on the model object. Analogous functions for the package are shown
218-
# below.
235+
# These operations can also both be accomplished by calling
236+
# `update_dataset()` on the model object, albeit without the
237+
# opportunity to modify the intermediate metadata dictionary.
238+
# Examples of this approach (with package objects) are shown below.
219239

220240
# get model netcdf info
221241
nc_info = gwf.netcdf_info()
@@ -227,11 +247,11 @@
227247

228248
# ## Update the dataset with supported `DIS` arrays
229249
#
230-
# Add NetCDF supported data arrays in package to dataset. Internally, this call
231-
# uses a `netcdf_info()` package dictionary to determine candidate variables
232-
# and relevant information about them. Alternatively, this dictionary can
233-
# be directly accessed, updated, and passed to the `update_dataset()` function.
234-
# That workflow will be demonstrated in the `NPF` package update which follows.
250+
# First, we will show how package NetCDF parameters can be added to the
251+
# dataset without using the NetCDF metadata dictionary. We will use the
252+
# metadata dictionary when updating the dataset with NPF parameter data.
253+
#
254+
# Add NetCDF supported data arrays in package to dataset.
235255

236256
# update dataset with `DIS` arrays
237257
dis = gwf.get_package("dis")
@@ -240,9 +260,7 @@
240260
# ## Access `NPF` package NetCDF attributes
241261
#
242262
# Access package scoped NetCDF details by storing the dictionary returned
243-
# from `netcdf_info()`. We need to set package variable attributes that are
244-
# stored in the package netcdf info dict, but we also need other information
245-
# that is relevant to creating the variables themselves.
263+
# from `netcdf_info()`.
246264
#
247265
# The contents of the info dictionary are shown and then, in the following
248266
# step, the dictionary and the dataset are passed to a helper routine that
@@ -253,12 +271,17 @@
253271
nc_info = npf.netcdf_info()
254272
pprint(nc_info)
255273

256-
# ## Update package `netcdf_info` dictionary and dataset
274+
# ## Update package NetCDF metadata dictionary and dataset
275+
#
276+
# Here we update the metadata dictionary and then pass it directly to the
277+
# `update_dataset()` function which uses it when adding variables to the
278+
# dataset.
257279
#
258-
# Here we replace the default name for the `NPF K` input parameter and add
259-
# the `standard_name` attribute to it's attribute dictionary. The dictionary
260-
# is then passed to the `update_dataset()` function. Note the updated name
261-
# is used in the subsequent block when updating the array values.
280+
# We replace the default name for the `NPF K` input parameter and add
281+
# the `standard_name` attribute to it's attribute dictionary. The
282+
# dictionary is then passed to the `update_dataset()` function. Note the
283+
# updated name is used in the subsequent block when updating the array
284+
# values.
262285

263286
# update dataset with `NPF` arrays
264287
nc_info["k"]["varname"] = "npf_k_updated"
@@ -302,14 +325,14 @@
302325

303326
# ## Run MODFLOW 6 simulation with NetCDF input
304327
#
305-
# The simulation generated by this tutorial should be runnable by
306-
# Extended MODFLOW 6, available from the nightly-build repository
307-
# (linked above).
328+
# The simulation generated by this tutorial should run with the
329+
# Extended MODFLOW 6 executable, available from the nightly-build
330+
# repository (linked above).
308331

309332
# success, buff = sim.run_simulation(silent=True, report=True)
310333
# assert success, pformat(buff)
311334

312-
# ## Method 4: DIY with xarray
335+
# ## Method 3: DIY with xarray
313336
#
314337
# The above method still uses FloPy objects to update the dataset arrays
315338
# to values consistent with the state of the objects. The `netcdf_info`

0 commit comments

Comments
 (0)