|
7 | 7 | # extension: .py |
8 | 8 | # format_name: light |
9 | 9 | # format_version: '1.5' |
10 | | -# jupytext_version: 1.17.2 |
| 10 | +# jupytext_version: 1.16.4 |
11 | 11 | # kernelspec: |
12 | 12 | # display_name: Python 3 (ipykernel) |
13 | 13 | # language: python |
|
18 | 18 | # |
19 | 19 | # ## NetCDF tutorial 1: MODFLOW 6 structured input file |
20 | 20 | # |
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. |
25 | 28 | # |
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: |
28 | 30 | # [MODFLOW NetCDF Format](https://github.com/MODFLOW-ORG/modflow6/wiki/MODFLOW-NetCDF-Format). |
29 | 31 | # |
30 | 32 | # Note that NetCDF is only supported by the Extended version of MODFLOW 6. |
|
75 | 77 | "uzf01.uzf.obs": None, |
76 | 78 | } |
77 | 79 |
|
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 | + ) |
85 | 87 |
|
86 | 88 | # ## Create simulation workspace |
87 | 89 |
|
|
104 | 106 | success, buff = sim.run_simulation(silent=True, report=True) |
105 | 107 | assert success, pformat(buff) |
106 | 108 |
|
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 |
108 | 147 | # |
109 | 148 | # Reset the simulation path and set the `GWF` name file `nc_filerecord` |
110 | 149 | # attribute to the name of the intended input NetCDF file. Display |
|
116 | 155 | # and `GHBG` packages. Data will be copied from the package objects into |
117 | 156 | # dataset arrays. |
118 | 157 | # |
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. |
121 | 162 |
|
122 | 163 | # create directory for netcdf sim |
123 | | -sim.set_sim_path(workspace / "netcdf") |
| 164 | +sim.set_sim_path(workspace / "netcdf3") |
124 | 165 | # set model name file nc_filerecord attribute to export name |
125 | 166 | gwf = sim.get_model("uzf01") |
126 | 167 | gwf.name_file.nc_filerecord = "uzf01.structured.nc" |
127 | 168 | # 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 | + |
129 | 174 | # 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: |
131 | 176 | print(fh.read()) |
| 177 | + |
| 178 | +# ## Show example package file with NetCDF keywords |
| 179 | + |
132 | 180 | # 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: |
134 | 182 | print(fh.read()) |
135 | 183 |
|
136 | 184 | # ## Create dataset |
|
184 | 232 | # existing simulation objects and update the dataset. |
185 | 233 | # |
186 | 234 | # 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. |
188 | 238 |
|
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() |
195 | 244 |
|
196 | 245 | # ## Access `NPF` package NetCDF attributes |
197 | 246 | # |
|
277 | 326 |
|
278 | 327 | # write dataset to netcdf |
279 | 328 | ds.to_netcdf( |
280 | | - workspace / "netcdf/uzf01.structured.nc", format="NETCDF4", engine="netcdf4" |
| 329 | + workspace / "netcdf3" / "uzf01.structured.nc", format="NETCDF4", engine="netcdf4" |
281 | 330 | ) |
282 | 331 |
|
283 | 332 | # ## Run MODFLOW 6 simulation with NetCDF input |
|
0 commit comments