This page was generated from export_tutorial.py. It's also available as a notebook.

Exporting to netCDF and shapefile

[1]:
import os
import sys
from tempfile import TemporaryDirectory

import flopy

print(sys.version)
print(f"flopy version: {flopy.__version__}")
3.9.19 (main, Mar 20 2024, 16:40:02)
[GCC 11.4.0]
flopy version: 3.7.0.dev0

Load our old friend…the Freyberg model

[2]:
nam_file = "freyberg.nam"
model_ws = os.path.join(
    "..", "..", "examples", "data", "freyberg_multilayer_transient"
)
ml = flopy.modflow.Modflow.load(nam_file, model_ws=model_ws, check=False)

We can see the Modelgrid instance has generic entries, as does start_datetime

[3]:
ml.modelgrid
[3]:
xll:622241.1904510253; yll:3343617.741737109; rotation:15.0; crs:EPSG:32614; units:meters; lenuni:2
[4]:
ml.modeltime.start_datetime
[4]:
'1/1/2015'

Setting the attributes of the ml.modelgrid is easy:

[5]:
ml.modelgrid.set_coord_info(
    xoff=123456.7, yoff=765432.1, angrot=15.0, crs=3070
)
ml.dis.start_datetime = "7/4/1776"
[6]:
ml.modeltime.start_datetime
[6]:
'7/4/1776'

Basic netCDF export capabilities

[7]:
# temporary directory
temp_dir = TemporaryDirectory()
pth = temp_dir.name
[8]:
# export the whole model (inputs and outputs)
fnc = ml.export(os.path.join(pth, f"{ml.name}.in.nc"))
initialize_geometry::
model crs: EPSG:3070
initialize_geometry::nc_crs = EPSG:4326
transforming coordinates using = proj=pipeline step inv proj=tmerc lat_0=0 lon_0=-90 k=0.9996 x_0=520000 y_0=-4480000 ellps=GRS80 step proj=unitconvert xy_in=rad xy_out=deg
[9]:
# export outputs using spatial reference info
hds = flopy.utils.HeadFile(os.path.join(model_ws, "freyberg.hds"))
flopy.export.utils.output_helper(
    os.path.join(pth, f"{ml.name}.out.nc"), ml, {"hds": hds}
)
initialize_geometry::
model crs: EPSG:3070
initialize_geometry::nc_crs = EPSG:4326
transforming coordinates using = proj=pipeline step inv proj=tmerc lat_0=0 lon_0=-90 k=0.9996 x_0=520000 y_0=-4480000 ellps=GRS80 step proj=unitconvert xy_in=rad xy_out=deg
[9]:
<flopy.export.netcdf.NetCdf at 0x7fe954aeea30>

Export an array to netCDF or shapefile

[10]:
# export a 2d array
ml.dis.top.export(os.path.join(pth, "top.nc"))
ml.dis.top.export(os.path.join(pth, "top.shp"))
initialize_geometry::
model crs: EPSG:3070
initialize_geometry::nc_crs = EPSG:4326
transforming coordinates using = proj=pipeline step inv proj=tmerc lat_0=0 lon_0=-90 k=0.9996 x_0=520000 y_0=-4480000 ellps=GRS80 step proj=unitconvert xy_in=rad xy_out=deg

sparse export of stress period data for a boundary condition package

  • excludes cells that aren’t in the package (aren’t in package.stress_period_data)

  • by default, stress periods with duplicate parameter values (e.g., stage, conductance, etc.) are omitted (squeeze=True); only stress periods with different values are exported

  • argue squeeze=False to export all stress periods

[11]:
ml.drn.stress_period_data.export(os.path.join(pth, "drn.shp"), sparse=True)

Export a 3d array

[12]:
# export a 3d array
ml.upw.hk.export(os.path.join(pth, "hk.nc"))
ml.upw.hk.export(os.path.join(pth, "hk.shp"))
initialize_geometry::
model crs: EPSG:3070
initialize_geometry::nc_crs = EPSG:4326
transforming coordinates using = proj=pipeline step inv proj=tmerc lat_0=0 lon_0=-90 k=0.9996 x_0=520000 y_0=-4480000 ellps=GRS80 step proj=unitconvert xy_in=rad xy_out=deg

Export a number of things to the same netCDF file

[13]:
# export lots of things to the same nc file
fnc = ml.dis.botm.export(os.path.join(pth, "test.nc"))
ml.upw.hk.export(fnc)
ml.dis.top.export(fnc)

# export transient 2d
ml.rch.rech.export(fnc)
initialize_geometry::
model crs: EPSG:3070
initialize_geometry::nc_crs = EPSG:4326
transforming coordinates using = proj=pipeline step inv proj=tmerc lat_0=0 lon_0=-90 k=0.9996 x_0=520000 y_0=-4480000 ellps=GRS80 step proj=unitconvert xy_in=rad xy_out=deg
[13]:
<flopy.export.netcdf.NetCdf at 0x7fe925828880>

Export a package to netCDF

[14]:
# export mflist
fnc = ml.wel.export(os.path.join(pth, "packages.nc"))
ml.upw.export(fnc)
initialize_geometry::
model crs: EPSG:3070
initialize_geometry::nc_crs = EPSG:4326
transforming coordinates using = proj=pipeline step inv proj=tmerc lat_0=0 lon_0=-90 k=0.9996 x_0=520000 y_0=-4480000 ellps=GRS80 step proj=unitconvert xy_in=rad xy_out=deg
[14]:
<flopy.export.netcdf.NetCdf at 0x7fe925828a60>

Export an entire model to netCDF

[15]:
fnc = ml.export(os.path.join(pth, "model.nc"))
initialize_geometry::
model crs: EPSG:3070
initialize_geometry::nc_crs = EPSG:4326
transforming coordinates using = proj=pipeline step inv proj=tmerc lat_0=0 lon_0=-90 k=0.9996 x_0=520000 y_0=-4480000 ellps=GRS80 step proj=unitconvert xy_in=rad xy_out=deg

Export model outputs to netCDF

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.

Let’s use output from the Freyberg model as an example of these functions

[16]:
# load binary head and cell budget files
fhead = os.path.join(model_ws, "freyberg.hds")
fcbc = os.path.join(model_ws, "freyberg.cbc")

hds = flopy.utils.HeadFile(fhead)
cbc = flopy.utils.CellBudgetFile(fcbc)

export_dict = {"hds": hds, "cbc": cbc}

# export head and cell budget outputs to netcdf
fnc = flopy.export.utils.output_helper(
    os.path.join(pth, "output.nc"), ml, export_dict
)
initialize_geometry::
model crs: EPSG:3070
initialize_geometry::nc_crs = EPSG:4326
transforming coordinates using = proj=pipeline step inv proj=tmerc lat_0=0 lon_0=-90 k=0.9996 x_0=520000 y_0=-4480000 ellps=GRS80 step proj=unitconvert xy_in=rad xy_out=deg
error getting data for cell_by_cell_flowstorage at time 1.0:list index out of range
error getting data for cell_by_cell_flowstorage at time 1097.0:list index out of range
[17]:
try:
    # ignore PermissionError on Windows
    temp_dir.cleanup()
except:
    pass