flopy/examples/Notebooks/flopy3_export.ipynb

1324 lines
44 KiB
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# FloPy\n",
"\n",
"### Demo of netCDF and shapefile export capabilities within the flopy export module. "
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"3.8.6 | packaged by conda-forge | (default, Oct 7 2020, 18:42:56) \n",
"[Clang 10.0.1 ]\n",
"flopy version: 3.3.3\n"
]
}
],
"source": [
"import os\n",
"import sys\n",
"import datetime\n",
"\n",
"# run installed version of flopy or add local path\n",
"try:\n",
" import flopy\n",
"except:\n",
" fpth = os.path.abspath(os.path.join('..', '..'))\n",
" sys.path.append(fpth)\n",
" import flopy\n",
"\n",
"print(sys.version)\n",
"print('flopy version: {}'.format(flopy.__version__))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Load our old friend...the Freyberg model"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"nam_file = \"freyberg.nam\"\n",
"model_ws = os.path.join(\"..\", \"data\", \"freyberg_multilayer_transient\")\n",
"ml = flopy.modflow.Modflow.load(nam_file, model_ws=model_ws, check=False)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can see the ``Modelgrid`` instance has generic entries, as does ``start_datetime``"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"xll:622241.1904510253; yll:3343617.741737109; rotation:15.0; proj4_str:+proj=utm +zone=14 +ellps=WGS84 +datum=WGS84 +units=m +no_defs; units:meters; lenuni:2"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ml.modelgrid"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1/1/2015'"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ml.modeltime.start_datetime"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Setting the attributes of the ``ml.modelgrid`` is easy:"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"proj4_str = \"+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\"\n",
"ml.modelgrid.set_coord_info(xoff=123456.7, yoff=765432.1, angrot=15.0, proj4=proj4_str)\n",
"ml.dis.start_datetime = '7/4/1776'"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'7/4/1776'"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"ml.modeltime.start_datetime"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Some netCDF export capabilities:\n",
"\n",
"#### Export the whole model (inputs and outputs)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# make directory\n",
"pth = os.path.join('data', 'netCDF_export')\n",
"if not os.path.exists(pth):\n",
" os.makedirs(pth)"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"initialize_geometry::proj4_str = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n",
"initialize_geometry::self.grid_crs = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +type=crs\n",
"initialize_geometry::nc_crs = epsg:4326\n",
"transforming coordinates using = proj=noop ellps=GRS80\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"initialize_geometry::proj4_str = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n",
"initialize_geometry::self.grid_crs = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +type=crs\n",
"initialize_geometry::nc_crs = epsg:4326\n",
"transforming coordinates using = proj=noop ellps=GRS80\n"
]
},
{
"data": {
"text/plain": [
"<flopy.export.netcdf.NetCdf at 0x10fec13d0>"
]
},
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fnc = ml.export(os.path.join(pth, ml.name+'.in.nc'))\n",
"hds = flopy.utils.HeadFile(os.path.join(model_ws,\"freyberg.hds\"))\n",
"flopy.export.utils.output_helper(os.path.join(pth, ml.name+'.out.nc'), ml, {\"hds\":hds})"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### export a single array to netcdf or shapefile"
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"initialize_geometry::proj4_str = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n",
"initialize_geometry::self.grid_crs = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +type=crs\n",
"initialize_geometry::nc_crs = epsg:4326\n",
"transforming coordinates using = proj=noop ellps=GRS80\n",
"wrote data/netCDF_export/top.shp\n"
]
}
],
"source": [
"# export a 2d array\n",
"ml.dis.top.export(os.path.join(pth, 'top.nc'))\n",
"ml.dis.top.export(os.path.join(pth, 'top.shp'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### sparse export of stress period data for a boundary condition package \n",
"* excludes cells that aren't in the package (aren't in `package.stress_period_data`) \n",
"* by default, stress periods with duplicate parameter values (e.g., stage, conductance, etc.) are omitted\n",
"(`squeeze=True`); only stress periods with different values are exported \n",
"* argue `squeeze=False` to export all stress periods"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"wrote data/netCDF_export/drn.shp\n"
]
}
],
"source": [
"ml.drn.stress_period_data.export(os.path.join(pth, 'drn.shp'), sparse=True)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Export a 3d array"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"initialize_geometry::proj4_str = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n",
"initialize_geometry::self.grid_crs = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +type=crs\n",
"initialize_geometry::nc_crs = epsg:4326\n",
"transforming coordinates using = proj=noop ellps=GRS80\n",
"wrote data/netCDF_export/hk.shp\n"
]
}
],
"source": [
"#export a 3d array\n",
"ml.upw.hk.export(os.path.join(pth, 'hk.nc'))\n",
"ml.upw.hk.export(os.path.join(pth, 'hk.shp'))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"#### Export a number of things to the same netCDF file"
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"initialize_geometry::proj4_str = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n",
"initialize_geometry::self.grid_crs = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +type=crs\n",
"initialize_geometry::nc_crs = epsg:4326\n",
"transforming coordinates using = proj=noop ellps=GRS80\n"
]
},
{
"data": {
"text/plain": [
"<flopy.export.netcdf.NetCdf at 0x12f50f850>"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# export lots of things to the same nc file\n",
"fnc = ml.dis.botm.export(os.path.join(pth, 'test.nc'))\n",
"ml.upw.hk.export(fnc)\n",
"ml.dis.top.export(fnc)\n",
"\n",
"# export transient 2d\n",
"ml.rch.rech.export(fnc)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Export whole packages to a netCDF file"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"initialize_geometry::proj4_str = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n",
"initialize_geometry::self.grid_crs = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +type=crs\n",
"initialize_geometry::nc_crs = epsg:4326\n",
"transforming coordinates using = proj=noop ellps=GRS80\n"
]
},
{
"data": {
"text/plain": [
"<class 'netCDF4._netCDF4.Dataset'>\n",
"root group (NETCDF4 data model, file format HDF5):\n",
" Conventions: CF-1.6, ACDD-1.3, flopy 3.3.3\n",
" date_created: 2021-02-18T17:31:00Z\n",
" geospatial_vertical_positive: up\n",
" geospatial_vertical_min: -25.0\n",
" geospatial_vertical_max: 4.832500457763672\n",
" geospatial_vertical_resolution: variable\n",
" featureType: Grid\n",
" namefile: freyberg.nam\n",
" model_ws: ../data/freyberg_multilayer_transient\n",
" exe_name: mf2005.exe\n",
" modflow_version: mfnwt\n",
" create_hostname: IGSAAAHMLT40179\n",
" create_platform: Darwin\n",
" create_directory: /Users/jdhughes/Documents/Development/flopy_git/flopy_fork/examples/Notebooks\n",
" solver_head_tolerance: -999\n",
" solver_flux_tolerance: -999\n",
" flopy_sr_xll: 123456.7\n",
" flopy_sr_yll: 765432.1\n",
" flopy_sr_rotation: 15.0\n",
" flopy_sr_proj4_str: +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n",
" start_datetime: 7/4/1776\n",
" dimensions(sizes): time(1097), layer(3), y(40), x(20)\n",
" variables(dimensions): int32 crs(), float64 time(time), float64 elevation(layer,y,x), float64 longitude(y,x), float64 latitude(y,x), float64 x_proj(y,x), float64 y_proj(y,x), float32 layer(layer), float32 delc(y), float32 delr(x), |S1 VerticalTransform(), float32 wel_flux(time,layer,y,x), float32 hani(layer,y,x), float32 hk(layer,y,x), float32 ss(layer,y,x), float32 sy(layer,y,x), float32 vka(layer,y,x), float32 vkcb(layer,y,x)\n",
" groups: "
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# export mflist\n",
"fnc = ml.wel.export(os.path.join(pth, 'packages.nc'))\n",
"ml.upw.export(fnc)\n",
"fnc.nc"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Export the whole model to a netCDF"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"initialize_geometry::proj4_str = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n",
"initialize_geometry::self.grid_crs = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +type=crs\n",
"initialize_geometry::nc_crs = epsg:4326\n",
"transforming coordinates using = proj=noop ellps=GRS80\n"
]
},
{
"data": {
"text/plain": [
"<class 'netCDF4._netCDF4.Dataset'>\n",
"root group (NETCDF4 data model, file format HDF5):\n",
" Conventions: CF-1.6, ACDD-1.3, flopy 3.3.3\n",
" date_created: 2021-02-18T17:31:00Z\n",
" geospatial_vertical_positive: up\n",
" geospatial_vertical_min: -25.0\n",
" geospatial_vertical_max: 4.832500457763672\n",
" geospatial_vertical_resolution: variable\n",
" featureType: Grid\n",
" namefile: freyberg.nam\n",
" model_ws: ../data/freyberg_multilayer_transient\n",
" exe_name: mf2005.exe\n",
" modflow_version: mfnwt\n",
" create_hostname: IGSAAAHMLT40179\n",
" create_platform: Darwin\n",
" create_directory: /Users/jdhughes/Documents/Development/flopy_git/flopy_fork/examples/Notebooks\n",
" solver_head_tolerance: -999\n",
" solver_flux_tolerance: -999\n",
" flopy_sr_xll: 123456.7\n",
" flopy_sr_yll: 765432.1\n",
" flopy_sr_rotation: 15.0\n",
" flopy_sr_proj4_str: +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n",
" start_datetime: 7/4/1776\n",
" dimensions(sizes): time(1097), layer(3), y(40), x(20)\n",
" variables(dimensions): int32 crs(), float64 time(time), float64 elevation(layer,y,x), float64 longitude(y,x), float64 latitude(y,x), float64 x_proj(y,x), float64 y_proj(y,x), float32 layer(layer), float32 delc(y), float32 delr(x), |S1 VerticalTransform(), float32 botm(layer,y,x), float32 thickness(layer,y,x), float32 model_top(y,x), int32 ibound(layer,y,x), float32 strt(layer,y,x), float32 rech(time,layer,y,x), float32 wel_flux(time,layer,y,x), float32 hani(layer,y,x), float32 hk(layer,y,x), float32 ss(layer,y,x), float32 sy(layer,y,x), float32 vka(layer,y,x), float32 vkcb(layer,y,x), float32 drn_elev(time,layer,y,x), float32 drn_cond(time,layer,y,x)\n",
" groups: "
]
},
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"fnc = ml.export(os.path.join(pth, 'model.nc'))\n",
"fnc.nc"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Export output to netcdf\n",
"\n",
"FloPy has utilities to export model outputs to a netcdf file. Valid output types for export are MODFLOW binary head files, formatted head files, cell budget files, seawat concentration files, and zonebudget output.\n",
"\n",
"Let's use output from the Freyberg model as an example of these functions"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"initialize_geometry::proj4_str = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n",
"initialize_geometry::self.grid_crs = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +type=crs\n",
"initialize_geometry::nc_crs = epsg:4326\n",
"transforming coordinates using = proj=noop ellps=GRS80\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"error getting data for cell_by_cell_flowstorage at time 1.0:list index out of range\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"error getting data for cell_by_cell_flowstorage at time 1097.0:list index out of range\n"
]
},
{
"data": {
"text/plain": [
"<class 'netCDF4._netCDF4.Dataset'>\n",
"root group (NETCDF4 data model, file format HDF5):\n",
" Conventions: CF-1.6, ACDD-1.3, flopy 3.3.3\n",
" date_created: 2021-02-18T17:31:00Z\n",
" geospatial_vertical_positive: up\n",
" geospatial_vertical_min: -25.0\n",
" geospatial_vertical_max: 4.832500457763672\n",
" geospatial_vertical_resolution: variable\n",
" featureType: Grid\n",
" namefile: freyberg.nam\n",
" model_ws: ../data/freyberg_multilayer_transient\n",
" exe_name: mf2005.exe\n",
" modflow_version: mfnwt\n",
" create_hostname: IGSAAAHMLT40179\n",
" create_platform: Darwin\n",
" create_directory: /Users/jdhughes/Documents/Development/flopy_git/flopy_fork/examples/Notebooks\n",
" solver_head_tolerance: -999\n",
" solver_flux_tolerance: -999\n",
" flopy_sr_xll: 123456.7\n",
" flopy_sr_yll: 765432.1\n",
" flopy_sr_rotation: 15.0\n",
" flopy_sr_proj4_str: +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n",
" start_datetime: 7/4/1776\n",
" dimensions(sizes): time(1097), layer(3), y(40), x(20)\n",
" variables(dimensions): int32 crs(), float64 time(time), float64 elevation(layer,y,x), float64 longitude(y,x), float64 latitude(y,x), float64 x_proj(y,x), float64 y_proj(y,x), float32 layer(layer), float32 delc(y), float32 delr(x), |S1 VerticalTransform(), float32 head(time,layer,y,x), float32 constant_head(time,layer,y,x), float32 flow_right_face(time,layer,y,x), float32 flow_front_face(time,layer,y,x), float32 flow_lower_face(time,layer,y,x), float32 storage(time,layer,y,x)\n",
" groups: "
]
},
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# load binary head and cell budget files\n",
"fhead = os.path.join(model_ws, 'freyberg.hds')\n",
"fcbc = os.path.join(model_ws, 'freyberg.cbc')\n",
"\n",
"hds = flopy.utils.HeadFile(fhead)\n",
"cbc = flopy.utils.CellBudgetFile(fcbc)\n",
"\n",
"export_dict = {\"hds\": hds,\n",
" \"cbc\": cbc}\n",
"\n",
"# export head and cell budget outputs to netcdf\n",
"fnc = flopy.export.utils.output_helper(os.path.join(pth, \"output.nc\"), ml, export_dict)\n",
"fnc.nc"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Exporting zonebudget output\n",
"\n",
"zonebudget output can be exported with other modflow outputs, and is placed in a seperate group which allows the user to post-process the zonebudget output before exporting.\n",
"\n",
"Here are two examples on how to export zonebudget output with a binary head and cell budget file\n",
"\n",
"__Example 1__: No postprocessing of the zonebudget output"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"ZoneBudgetOutput Class\n",
"----------------------\n",
"\n",
"Number of zones: 4\n",
"Unique zones: 0, 1, 2, 3\n",
"Number of buget records: 3291"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# load the zonebudget output file\n",
"zonbud_ws = os.path.join(\"..\", \"data\", \"zonbud_examples\")\n",
"fzonbud = os.path.join(zonbud_ws, \"freyberg_mlt.2.csv\")\n",
"zon_arrays = flopy.utils.zonbud.read_zbarray(os.path.join(zonbud_ws, \"zonef_mlt.zbr\"))\n",
"\n",
"zbout = flopy.utils.ZoneBudgetOutput(fzonbud, ml.dis, zon_arrays)\n",
"zbout"
]
},
{
"cell_type": "code",
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"initialize_geometry::proj4_str = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n",
"initialize_geometry::self.grid_crs = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +type=crs\n",
"initialize_geometry::nc_crs = epsg:4326\n",
"transforming coordinates using = proj=noop ellps=GRS80\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"error getting data for cell_by_cell_flowstorage at time 1.0:list index out of range\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"error getting data for cell_by_cell_flowstorage at time 1097.0:list index out of range\n"
]
},
{
"data": {
"text/plain": [
"<class 'netCDF4._netCDF4.Dataset'>\n",
"root group (NETCDF4 data model, file format HDF5):\n",
" Conventions: CF-1.6, ACDD-1.3, flopy 3.3.3\n",
" date_created: 2021-02-18T17:31:00Z\n",
" geospatial_vertical_positive: up\n",
" geospatial_vertical_min: -25.0\n",
" geospatial_vertical_max: 4.832500457763672\n",
" geospatial_vertical_resolution: variable\n",
" featureType: Grid\n",
" namefile: freyberg.nam\n",
" model_ws: ../data/freyberg_multilayer_transient\n",
" exe_name: mf2005.exe\n",
" modflow_version: mfnwt\n",
" create_hostname: IGSAAAHMLT40179\n",
" create_platform: Darwin\n",
" create_directory: /Users/jdhughes/Documents/Development/flopy_git/flopy_fork/examples/Notebooks\n",
" solver_head_tolerance: -999\n",
" solver_flux_tolerance: -999\n",
" flopy_sr_xll: 123456.7\n",
" flopy_sr_yll: 765432.1\n",
" flopy_sr_rotation: 15.0\n",
" flopy_sr_proj4_str: +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n",
" start_datetime: 7/4/1776\n",
" dimensions(sizes): time(1097), layer(3), y(40), x(20)\n",
" variables(dimensions): int32 crs(), float64 time(time), float64 elevation(layer,y,x), float64 longitude(y,x), float64 latitude(y,x), float64 x_proj(y,x), float64 y_proj(y,x), float32 layer(layer), float32 delc(y), float32 delr(x), |S1 VerticalTransform(), float32 head(time,layer,y,x), float32 constant_head(time,layer,y,x), float32 flow_right_face(time,layer,y,x), float32 flow_front_face(time,layer,y,x), float32 flow_lower_face(time,layer,y,x), float32 storage(time,layer,y,x), float32 budget_zones(time,layer,y,x)\n",
" groups: zonebudget"
]
},
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"export_dict = {'hds': hds,\n",
" 'cbc': cbc}\n",
"\n",
"fnc = flopy.export.utils.output_helper(os.path.join(pth, \"output_with_zonebudget.nc\"),\n",
" ml, export_dict)\n",
"\n",
"fnc = zbout.export(fnc, ml)\n",
"fnc.nc"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"A budget_zones variable has been added to the root group and a new zonebudget group has been added to the netcdf file which hosts all of the budget data"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"__Example 2__: postprocessing zonebudget output then exporting"
]
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>kper</th>\n",
" <th>kstp</th>\n",
" <th>zone</th>\n",
" <th>storage</th>\n",
" <th>constant head</th>\n",
" <th>other zones</th>\n",
" <th>total</th>\n",
" <th>zone 0</th>\n",
" <th>zone 1</th>\n",
" <th>zone 2</th>\n",
" <th>zone 3</th>\n",
" <th>tslen</th>\n",
" <th>totim</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0.000000</td>\n",
" <td>-821.281900</td>\n",
" <td>-1570.821</td>\n",
" <td>-2392.103</td>\n",
" <td>0.0</td>\n",
" <td>0.0000</td>\n",
" <td>-1530.422</td>\n",
" <td>-40.3993</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>0.000000</td>\n",
" <td>-648.804700</td>\n",
" <td>630.730</td>\n",
" <td>-18.075</td>\n",
" <td>0.0</td>\n",
" <td>1530.4220</td>\n",
" <td>0.000</td>\n",
" <td>-899.6920</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>0</td>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>0.000000</td>\n",
" <td>-976.232200</td>\n",
" <td>940.092</td>\n",
" <td>-36.140</td>\n",
" <td>0.0</td>\n",
" <td>40.3993</td>\n",
" <td>899.692</td>\n",
" <td>0.0000</td>\n",
" <td>1.0</td>\n",
" <td>1.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>218.568500</td>\n",
" <td>-816.347300</td>\n",
" <td>-1173.221</td>\n",
" <td>-1770.999</td>\n",
" <td>0.0</td>\n",
" <td>0.0000</td>\n",
" <td>-1134.937</td>\n",
" <td>-38.2835</td>\n",
" <td>1.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>191.816342</td>\n",
" <td>-643.938700</td>\n",
" <td>433.628</td>\n",
" <td>-18.493</td>\n",
" <td>0.0</td>\n",
" <td>1134.9370</td>\n",
" <td>0.000</td>\n",
" <td>-701.3090</td>\n",
" <td>1.0</td>\n",
" <td>2.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>...</th>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" <td>...</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3286</th>\n",
" <td>1095</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>-626.408120</td>\n",
" <td>-505.116270</td>\n",
" <td>1113.766</td>\n",
" <td>-17.758</td>\n",
" <td>0.0</td>\n",
" <td>2489.4040</td>\n",
" <td>0.000</td>\n",
" <td>-1375.6380</td>\n",
" <td>1.0</td>\n",
" <td>1096.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3287</th>\n",
" <td>1095</td>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>-627.235750</td>\n",
" <td>-801.732376</td>\n",
" <td>1393.454</td>\n",
" <td>-35.514</td>\n",
" <td>0.0</td>\n",
" <td>17.8163</td>\n",
" <td>1375.638</td>\n",
" <td>0.0000</td>\n",
" <td>1.0</td>\n",
" <td>1096.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3288</th>\n",
" <td>1096</td>\n",
" <td>0</td>\n",
" <td>1</td>\n",
" <td>0.000000</td>\n",
" <td>-230.548300</td>\n",
" <td>-152.236</td>\n",
" <td>-382.784</td>\n",
" <td>0.0</td>\n",
" <td>0.0000</td>\n",
" <td>-205.822</td>\n",
" <td>53.5856</td>\n",
" <td>1.0</td>\n",
" <td>1097.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3289</th>\n",
" <td>1096</td>\n",
" <td>0</td>\n",
" <td>2</td>\n",
" <td>0.000000</td>\n",
" <td>15.864900</td>\n",
" <td>-30.796</td>\n",
" <td>-14.931</td>\n",
" <td>0.0</td>\n",
" <td>205.8220</td>\n",
" <td>0.000</td>\n",
" <td>-236.6170</td>\n",
" <td>1.0</td>\n",
" <td>1097.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3290</th>\n",
" <td>1096</td>\n",
" <td>0</td>\n",
" <td>3</td>\n",
" <td>0.000000</td>\n",
" <td>-212.896600</td>\n",
" <td>183.031</td>\n",
" <td>-29.865</td>\n",
" <td>0.0</td>\n",
" <td>-53.5856</td>\n",
" <td>236.617</td>\n",
" <td>0.0000</td>\n",
" <td>1.0</td>\n",
" <td>1097.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"<p>3291 rows × 13 columns</p>\n",
"</div>"
],
"text/plain": [
" kper kstp zone storage constant head other zones total \\\n",
"0 0 0 1 0.000000 -821.281900 -1570.821 -2392.103 \n",
"1 0 0 2 0.000000 -648.804700 630.730 -18.075 \n",
"2 0 0 3 0.000000 -976.232200 940.092 -36.140 \n",
"3 1 0 1 218.568500 -816.347300 -1173.221 -1770.999 \n",
"4 1 0 2 191.816342 -643.938700 433.628 -18.493 \n",
"... ... ... ... ... ... ... ... \n",
"3286 1095 0 2 -626.408120 -505.116270 1113.766 -17.758 \n",
"3287 1095 0 3 -627.235750 -801.732376 1393.454 -35.514 \n",
"3288 1096 0 1 0.000000 -230.548300 -152.236 -382.784 \n",
"3289 1096 0 2 0.000000 15.864900 -30.796 -14.931 \n",
"3290 1096 0 3 0.000000 -212.896600 183.031 -29.865 \n",
"\n",
" zone 0 zone 1 zone 2 zone 3 tslen totim \n",
"0 0.0 0.0000 -1530.422 -40.3993 1.0 1.0 \n",
"1 0.0 1530.4220 0.000 -899.6920 1.0 1.0 \n",
"2 0.0 40.3993 899.692 0.0000 1.0 1.0 \n",
"3 0.0 0.0000 -1134.937 -38.2835 1.0 2.0 \n",
"4 0.0 1134.9370 0.000 -701.3090 1.0 2.0 \n",
"... ... ... ... ... ... ... \n",
"3286 0.0 2489.4040 0.000 -1375.6380 1.0 1096.0 \n",
"3287 0.0 17.8163 1375.638 0.0000 1.0 1096.0 \n",
"3288 0.0 0.0000 -205.822 53.5856 1.0 1097.0 \n",
"3289 0.0 205.8220 0.000 -236.6170 1.0 1097.0 \n",
"3290 0.0 -53.5856 236.617 0.0000 1.0 1097.0 \n",
"\n",
"[3291 rows x 13 columns]"
]
},
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# load the zonebudget output and get the budget information\n",
"zbout = flopy.utils.ZoneBudgetOutput(fzonbud, ml.dis, zon_arrays)\n",
"df = zbout.dataframe\n",
"df"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's calculate a yearly volumetric budget from the zonebudget data"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/html": [
"<div>\n",
"<style scoped>\n",
" .dataframe tbody tr th:only-of-type {\n",
" vertical-align: middle;\n",
" }\n",
"\n",
" .dataframe tbody tr th {\n",
" vertical-align: top;\n",
" }\n",
"\n",
" .dataframe thead th {\n",
" text-align: right;\n",
" }\n",
"</style>\n",
"<table border=\"1\" class=\"dataframe\">\n",
" <thead>\n",
" <tr style=\"text-align: right;\">\n",
" <th></th>\n",
" <th>year</th>\n",
" <th>zone</th>\n",
" <th>storage</th>\n",
" <th>constant head</th>\n",
" <th>other zones</th>\n",
" <th>zone 1</th>\n",
" <th>zone 2</th>\n",
" <th>zone 3</th>\n",
" <th>totim</th>\n",
" </tr>\n",
" </thead>\n",
" <tbody>\n",
" <tr>\n",
" <th>0</th>\n",
" <td>1776</td>\n",
" <td>1</td>\n",
" <td>81203.267170</td>\n",
" <td>-134930.451200</td>\n",
" <td>-176631.7910</td>\n",
" <td>0.0000</td>\n",
" <td>-172310.4031</td>\n",
" <td>-4321.3803</td>\n",
" <td>181.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>1</th>\n",
" <td>1776</td>\n",
" <td>2</td>\n",
" <td>37268.485533</td>\n",
" <td>-102758.917473</td>\n",
" <td>62223.7540</td>\n",
" <td>172310.4031</td>\n",
" <td>0.0000</td>\n",
" <td>-110086.6556</td>\n",
" <td>181.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>2</th>\n",
" <td>1776</td>\n",
" <td>3</td>\n",
" <td>37296.183058</td>\n",
" <td>-158237.438437</td>\n",
" <td>114408.0385</td>\n",
" <td>4321.3803</td>\n",
" <td>110086.6556</td>\n",
" <td>0.0000</td>\n",
" <td>181.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>3</th>\n",
" <td>1777</td>\n",
" <td>1</td>\n",
" <td>91777.777222</td>\n",
" <td>-265545.355300</td>\n",
" <td>-407281.7640</td>\n",
" <td>0.0000</td>\n",
" <td>-398769.3120</td>\n",
" <td>-8512.4548</td>\n",
" <td>546.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>4</th>\n",
" <td>1777</td>\n",
" <td>2</td>\n",
" <td>42820.852999</td>\n",
" <td>-200142.961123</td>\n",
" <td>150829.2320</td>\n",
" <td>398769.3120</td>\n",
" <td>0.0000</td>\n",
" <td>-247940.0845</td>\n",
" <td>546.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>5</th>\n",
" <td>1777</td>\n",
" <td>3</td>\n",
" <td>42818.153555</td>\n",
" <td>-312276.958409</td>\n",
" <td>256452.5519</td>\n",
" <td>8512.4548</td>\n",
" <td>247940.0845</td>\n",
" <td>0.0000</td>\n",
" <td>546.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>6</th>\n",
" <td>1778</td>\n",
" <td>1</td>\n",
" <td>108226.799462</td>\n",
" <td>-255186.105500</td>\n",
" <td>-378440.1290</td>\n",
" <td>0.0000</td>\n",
" <td>-371720.4820</td>\n",
" <td>-6719.6672</td>\n",
" <td>911.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>7</th>\n",
" <td>1778</td>\n",
" <td>2</td>\n",
" <td>44930.564942</td>\n",
" <td>-188836.557151</td>\n",
" <td>137315.0100</td>\n",
" <td>371720.4820</td>\n",
" <td>0.0000</td>\n",
" <td>-234405.4742</td>\n",
" <td>911.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>8</th>\n",
" <td>1778</td>\n",
" <td>3</td>\n",
" <td>44936.970500</td>\n",
" <td>-299267.807443</td>\n",
" <td>241125.1428</td>\n",
" <td>6719.6672</td>\n",
" <td>234405.4742</td>\n",
" <td>0.0000</td>\n",
" <td>911.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>9</th>\n",
" <td>1779</td>\n",
" <td>1</td>\n",
" <td>5348.709896</td>\n",
" <td>-129448.085100</td>\n",
" <td>-231291.1680</td>\n",
" <td>0.0000</td>\n",
" <td>-227734.8190</td>\n",
" <td>-3556.3570</td>\n",
" <td>1097.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>10</th>\n",
" <td>1779</td>\n",
" <td>2</td>\n",
" <td>3241.895778</td>\n",
" <td>-95653.391040</td>\n",
" <td>88972.3010</td>\n",
" <td>227734.8190</td>\n",
" <td>0.0000</td>\n",
" <td>-138762.5241</td>\n",
" <td>1097.0</td>\n",
" </tr>\n",
" <tr>\n",
" <th>11</th>\n",
" <td>1779</td>\n",
" <td>3</td>\n",
" <td>3233.272516</td>\n",
" <td>-152427.866886</td>\n",
" <td>142318.8767</td>\n",
" <td>3556.3570</td>\n",
" <td>138762.5241</td>\n",
" <td>0.0000</td>\n",
" <td>1097.0</td>\n",
" </tr>\n",
" </tbody>\n",
"</table>\n",
"</div>"
],
"text/plain": [
" year zone storage constant head other zones zone 1 \\\n",
"0 1776 1 81203.267170 -134930.451200 -176631.7910 0.0000 \n",
"1 1776 2 37268.485533 -102758.917473 62223.7540 172310.4031 \n",
"2 1776 3 37296.183058 -158237.438437 114408.0385 4321.3803 \n",
"3 1777 1 91777.777222 -265545.355300 -407281.7640 0.0000 \n",
"4 1777 2 42820.852999 -200142.961123 150829.2320 398769.3120 \n",
"5 1777 3 42818.153555 -312276.958409 256452.5519 8512.4548 \n",
"6 1778 1 108226.799462 -255186.105500 -378440.1290 0.0000 \n",
"7 1778 2 44930.564942 -188836.557151 137315.0100 371720.4820 \n",
"8 1778 3 44936.970500 -299267.807443 241125.1428 6719.6672 \n",
"9 1779 1 5348.709896 -129448.085100 -231291.1680 0.0000 \n",
"10 1779 2 3241.895778 -95653.391040 88972.3010 227734.8190 \n",
"11 1779 3 3233.272516 -152427.866886 142318.8767 3556.3570 \n",
"\n",
" zone 2 zone 3 totim \n",
"0 -172310.4031 -4321.3803 181.0 \n",
"1 0.0000 -110086.6556 181.0 \n",
"2 110086.6556 0.0000 181.0 \n",
"3 -398769.3120 -8512.4548 546.0 \n",
"4 0.0000 -247940.0845 546.0 \n",
"5 247940.0845 0.0000 546.0 \n",
"6 -371720.4820 -6719.6672 911.0 \n",
"7 0.0000 -234405.4742 911.0 \n",
"8 234405.4742 0.0000 911.0 \n",
"9 -227734.8190 -3556.3570 1097.0 \n",
"10 0.0000 -138762.5241 1097.0 \n",
"11 138762.5241 0.0000 1097.0 "
]
},
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# get a dataframe of volumetric budget information\n",
"vol_df = zbout.volumetric_flux()\n",
"\n",
"# add a year field to the dataframe using datetime\n",
"start_date = ml.modeltime.start_datetime\n",
"start_date = datetime.datetime.strptime(start_date, \"%m/%d/%Y\")\n",
"nzones = len(zbout.zones) - 1\n",
"\n",
"year = [start_date.year] * nzones\n",
"for totim in vol_df.totim.values[:-nzones]:\n",
" t = start_date + datetime.timedelta(days=totim)\n",
" year.append(t.year)\n",
"\n",
"vol_df['year'] = year\n",
" \n",
"# calculate yearly volumetric change using pandas\n",
"totim_df = vol_df.groupby(['year', 'zone'], as_index=False)['totim'].max()\n",
"yearly = vol_df.groupby(['year', 'zone'], as_index=False)[['storage', 'constant head', 'other zones',\n",
" 'zone 1', 'zone 2', 'zone 3']].sum()\n",
"yearly['totim'] = totim_df['totim']\n",
"yearly"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"And finally, export the pandas dataframe to netcdf"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"initialize_geometry::proj4_str = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n",
"initialize_geometry::self.grid_crs = +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs +type=crs\n",
"initialize_geometry::nc_crs = epsg:4326\n",
"transforming coordinates using = proj=noop ellps=GRS80\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"error getting data for cell_by_cell_flowstorage at time 1.0:list index out of range\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"error getting data for cell_by_cell_flowstorage at time 1097.0:list index out of range\n"
]
},
{
"data": {
"text/plain": [
"<class 'netCDF4._netCDF4.Dataset'>\n",
"root group (NETCDF4 data model, file format HDF5):\n",
" Conventions: CF-1.6, ACDD-1.3, flopy 3.3.3\n",
" date_created: 2021-02-18T17:31:00Z\n",
" geospatial_vertical_positive: up\n",
" geospatial_vertical_min: -25.0\n",
" geospatial_vertical_max: 4.832500457763672\n",
" geospatial_vertical_resolution: variable\n",
" featureType: Grid\n",
" namefile: freyberg.nam\n",
" model_ws: ../data/freyberg_multilayer_transient\n",
" exe_name: mf2005.exe\n",
" modflow_version: mfnwt\n",
" create_hostname: IGSAAAHMLT40179\n",
" create_platform: Darwin\n",
" create_directory: /Users/jdhughes/Documents/Development/flopy_git/flopy_fork/examples/Notebooks\n",
" solver_head_tolerance: -999\n",
" solver_flux_tolerance: -999\n",
" flopy_sr_xll: 123456.7\n",
" flopy_sr_yll: 765432.1\n",
" flopy_sr_rotation: 15.0\n",
" flopy_sr_proj4_str: +proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs\n",
" start_datetime: 7/4/1776\n",
" dimensions(sizes): time(1097), layer(3), y(40), x(20)\n",
" variables(dimensions): int32 crs(), float64 time(time), float64 elevation(layer,y,x), float64 longitude(y,x), float64 latitude(y,x), float64 x_proj(y,x), float64 y_proj(y,x), float32 layer(layer), float32 delc(y), float32 delr(x), |S1 VerticalTransform(), float32 head(time,layer,y,x), float32 constant_head(time,layer,y,x), float32 flow_right_face(time,layer,y,x), float32 flow_front_face(time,layer,y,x), float32 flow_lower_face(time,layer,y,x), float32 storage(time,layer,y,x), float32 budget_zones(time,layer,y,x)\n",
" groups: zonebudget"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# process the new dataframe into a format that is compatible with netcdf exporting\n",
"zbncf = zbout.dataframe_to_netcdf_fmt(yearly, flux=False)\n",
"\n",
"# export to netcdf\n",
"export_dict = {\"hds\": hds,\n",
" \"cbc\": cbc,\n",
" \"zbud\": zbncf}\n",
"\n",
"fnc = flopy.export.utils.output_helper(os.path.join(pth, \"output_with_zonebudget.2.nc\"),\n",
" ml, export_dict)\n",
"fnc.nc"
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.6"
}
},
"nbformat": 4,
"nbformat_minor": 1
}