FloPy

Demo of netCDF and shapefile export capabilities within the flopy export module.


In [1]:
from __future__ import print_function
import os
import sys
import flopy

print(sys.version)
print('flopy version: {}'.format(flopy.__version__))


3.6.0 |Anaconda 4.3.0 (x86_64)| (default, Dec 23 2016, 13:19:00) 
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
flopy version: 3.2.6

Load our old friend...the Freyberg model


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

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


In [3]:
ml.dis.sr


Out[3]:
xul:0; yul:10000; rotation:0; proj4_str:+init=EPSG:4326; units:meters; lenuni:2; length_multiplier:1.0

In [4]:
ml.dis.start_datetime


Out[4]:
'1/1/1970'

Setting the attributes of the ml.dis.sr is easy:


In [5]:
ml.dis.sr.xul = 123456.7
ml.dis.sr.yul = 765432.1
rotation = 15.0
proj4_str = "+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs"
ml.dis.start_datetime = '7/4/1776'
ml.dis.sr


Out[5]:
xul:123456.7; yul:765432.1; rotation:0; proj4_str:+init=EPSG:4326; units:meters; lenuni:2; length_multiplier:1.0

In [6]:
ml.dis.start_datetime


Out[6]:
'7/4/1776'

Some netCDF export capabilities:

Export the whole model (inputs and outputs)


In [7]:
# make directory
pth = os.path.join('data', 'netCDF_export')
if not os.path.exists(pth):
    os.makedirs(pth)

In [8]:
fnc = ml.export(os.path.join(pth, ml.name+'.in.nc'))
hds = flopy.utils.HeadFile(os.path.join(model_ws,"freyberg.hds"))
flopy.export.utils.output_helper(os.path.join(pth, ml.name+'.out.nc'), ml, {"hds":hds})


Out[8]:
<flopy.export.netcdf.NetCdf at 0x113ff1cf8>

export a single array to netcdf or shapefile


In [9]:
# export a 2d array
ml.dis.top.export(os.path.join(pth, 'top.nc'))
ml.dis.top.export(os.path.join(pth, 'top.shp'))

Export a 3d array


In [10]:
#export a 3d array
ml.upw.hk.export(os.path.join(pth, 'hk.nc'))
ml.upw.hk.export(os.path.join(pth, 'hk.shp'))

Export a number of things to the same netCDF file


In [11]:
# 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)


Out[11]:
<flopy.export.netcdf.NetCdf at 0x118c29940>

Export whole packages to a netCDF file


In [12]:
# export mflist
fnc = ml.wel.export(os.path.join(pth, 'packages.nc'))
ml.upw.export(fnc)
fnc.nc


Out[12]:
<class 'netCDF4._netCDF4.Dataset'>
root group (NETCDF4 data model, file format HDF5):
    Conventions: CF-1.6
    date_created: 2017-03-18T23:27:00Z
    geospatial_vertical_positive: up
    geospatial_vertical_min: -4.83250045776
    geospatial_vertical_max: 25.0
    geospatial_vertical_resolution: variable
    featureType: Grid
    origin_x: 123456.7
    origin_y: 765432.1
    origin_crs: +init=EPSG:4326
    grid_rotation_from_origin: 0.0
    namefile: freyberg.nam
    model_ws: ../data/freyberg_multilayer_transient
    exe_name: mf2005.exe
    modflow_version: mfnwt
    create_hostname: JDHughes-MBP.home
    create_platform: Darwin
    create_directory: /Users/JosephHughes/Development/flopy-git/examples/Notebooks
    solver_head_tolerance: 0.01
    solver_flux_tolerance: 500.0
    flopy_sr_xul: 123456.7
    flopy_sr_yul: 765432.1
    flopy_sr_rotation: 0.0
    flopy_sr_proj4_str: +init=EPSG:4326
    start_datetime: 7/4/1776
    dimensions(sizes): time(1097), layer(3), y(40), x(20)
    variables(dimensions): int32 crs(), float64 time(time), float64 elevation(layer,y,x), float64 longitude(y,x), float64 latitude(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)
    groups: 

Export the whole model to a netCDF


In [13]:
fnc = ml.export(os.path.join(pth, 'model.nc'))
fnc.nc


Out[13]:
<class 'netCDF4._netCDF4.Dataset'>
root group (NETCDF4 data model, file format HDF5):
    Conventions: CF-1.6
    date_created: 2017-03-18T23:27:00Z
    geospatial_vertical_positive: up
    geospatial_vertical_min: -4.83250045776
    geospatial_vertical_max: 25.0
    geospatial_vertical_resolution: variable
    featureType: Grid
    origin_x: 123456.7
    origin_y: 765432.1
    origin_crs: +init=EPSG:4326
    grid_rotation_from_origin: 0.0
    namefile: freyberg.nam
    model_ws: ../data/freyberg_multilayer_transient
    exe_name: mf2005.exe
    modflow_version: mfnwt
    create_hostname: JDHughes-MBP.home
    create_platform: Darwin
    create_directory: /Users/JosephHughes/Development/flopy-git/examples/Notebooks
    solver_head_tolerance: 0.01
    solver_flux_tolerance: 500.0
    flopy_sr_xul: 123456.7
    flopy_sr_yul: 765432.1
    flopy_sr_rotation: 0.0
    flopy_sr_proj4_str: +init=EPSG:4326
    start_datetime: 7/4/1776
    dimensions(sizes): time(1097), layer(3), y(40), x(20)
    variables(dimensions): int32 crs(), float64 time(time), float64 elevation(layer,y,x), float64 longitude(y,x), float64 latitude(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)
    groups: 

In [ ]: