FloPy

Reading binary grid file

This notebook demonstrates use of a binary grid (grd) file to plot structured and unstructured models and use of the plot_cvfd() and contour_array_cvfd() mapping methods in flopy.


In [1]:
%matplotlib inline
import os
import sys
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
import flopy

print(sys.version)
print('numpy version: {}'.format(np.__version__))
print('matplotlib version: {}'.format(mpl.__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)]
numpy version: 1.11.3
matplotlib version: 2.0.0
flopy version: 3.2.6

In [2]:
workspace = os.path.join('..', 'data', 'mfgrd_test')

Single layer structured model


In [3]:
fn = os.path.join(workspace, 'nwtp3.dis.grb')
grd = flopy.utils.MfGrdFile(fn, verbose=True)
iverts, verts = grd.get_verts()
sr = grd.get_spatialreference()
extents = sr.get_extent()
vertc = grd.get_centroids()
print(vertc[0, :])


read 12 records from ../data/mfgrd_test/nwtp3.dis.grb
[ 50. -50.]

In [4]:
print(len(iverts))
print(iverts[0])
print(verts.shape)
print(verts[0:5,:])
print(sr.get_extent())


6400
[0, 1, 2, 3, 4]
(32000, 2)
[[   0.    0.]
 [   0. -100.]
 [ 100. -100.]
 [ 100.    0.]
 [   0.    0.]]
(0.0, 8000.0, -8000.0, 0.0)

In [5]:
fn = os.path.join(workspace, 'nwtp3.hds.cmp')
ho = flopy.utils.HeadFile(fn)
times = ho.get_times()
print(times)
h = ho.get_data(totim=times[-1]).reshape(80*80)
print(h.shape)


[365.0]
(6400,)

In [6]:
mm = flopy.plot.ModelMap(sr=sr, layer=0)
ax = plt.gca()
ax.set_xlim(extents[:2])
ax.set_ylim(extents[2:])
mm.plot_cvfd(verts, iverts, a=h)
cs = mm.contour_array_cvfd(vertc, h, colors='white')
plt.clabel(cs, fmt='%.1f', colors='white', fontsize=11);


Multi-layer structured model


In [7]:
fn = os.path.join(workspace, 'uzfp3_lakmvr_v2.dis.grb')
grd = flopy.utils.MfGrdFile(fn, verbose=True)
iverts, verts = grd.get_verts()
sr = grd.get_spatialreference()
extents = sr.get_extent()
vertc = grd.get_centroids()


read 12 records from ../data/mfgrd_test/uzfp3_lakmvr_v2.dis.grb

In [8]:
fn = os.path.join(workspace, 'uzfp3_lakmvr.hds.cmp')
ho = flopy.utils.HeadFile(fn)
times = ho.get_times()
print(times)
h = ho.get_data(totim=times[-1]).reshape(2*15*10)
print(h.shape)
print(vertc.shape)


[2628000.0, 5256000.0, 7884000.0, 10512000.0, 13140000.0, 15768000.0, 18396000.0, 21024000.0, 23652000.0, 26280000.0, 28908000.0, 31536000.0, 34164000.0, 36792000.0, 39420000.0, 42048000.0, 44676000.0, 47304000.0, 49932000.0, 52560000.0, 55188000.0, 57816000.0, 60444000.0, 63072000.0]
(300,)
(300, 2)

In [9]:
mm = flopy.plot.ModelMap(sr=sr, layer=0)
ax = plt.gca()
ax.set_xlim(extents[:2])
ax.set_ylim(extents[2:])
v = mm.plot_cvfd(verts, iverts, a=h, ncpl=150, masked_values=[6999.], cmap='viridis')
cs = mm.contour_array_cvfd(vertc, h, ncpl=150, masked_values=[6999.], 
                           levels=[1024], colors='white')
plt.clabel(cs, fmt='%.0f', colors='white', fontsize=11)
plt.colorbar(v, shrink=0.5)


Out[9]:
<matplotlib.colorbar.Colorbar at 0x10f61a550>

In [10]:
vertc = grd.get_centroids()
mm = flopy.plot.ModelMap(sr=sr, layer=1)
ax = plt.gca()
ax.set_xlim(extents[:2])
ax.set_ylim(extents[2:])
v = mm.plot_cvfd(verts, iverts, a=h, ncpl=150, masked_values=[6999.], cmap='viridis')
cs = mm.contour_array_cvfd(vertc, h, ncpl=150, masked_values=[6999.], 
                           levels=[1020, 1021, 1022, 1023, 1024, 1025, 1026], colors='white')
plt.clabel(cs, fmt='%.0f', colors='white', fontsize=11)
plt.colorbar(v, shrink=0.5)


Out[10]:
<matplotlib.colorbar.Colorbar at 0x111c28908>

Single-layer unstructured model


In [11]:
fn = os.path.join(workspace, 'flow.disv.grb')
grd = flopy.utils.MfGrdFile(fn, verbose=True)
iverts, verts = grd.get_verts()
vertc = grd.get_centroids()
xc = vertc[:, 0]
yc = vertc[:, 1]
sr = flopy.utils.reference.SpatialReferenceUnstructured(xc, yc, verts, iverts, [xc.shape[0]])


read 13 records from ../data/mfgrd_test/flow.disv.grb
returning vertices for ../data/mfgrd_test/flow.disv.grb

In [12]:
fn = os.path.join(workspace, 'flow.hds.cmp')
ho = flopy.utils.HeadFile(fn)
times = ho.get_times()
print(times)
h = ho.get_data(totim=times[-1]).reshape(218)
print(h.shape)


[1.0]
(218,)

In [13]:
mm = flopy.plot.ModelMap(sr=sr)
ax = plt.gca()
ax.set_xlim(0,700)
ax.set_ylim(0,700)
v = mm.plot_cvfd(verts, iverts, edgecolor='black', a=h)
cs = mm.contour_array_cvfd(vertc, h, colors='white')
plt.clabel(cs, fmt='%.1f', colors='white', fontsize=11)
plt.colorbar(v, shrink=0.5)


Out[13]:
<matplotlib.colorbar.Colorbar at 0x112cf9dd8>

Single-layer unstructured model with xt3d


In [14]:
fn = os.path.join(workspace, 'flowxt3d.disv.grb')
grd = flopy.utils.MfGrdFile(fn, verbose=True)
iverts, verts = grd.get_verts()
vertc = grd.get_centroids()
xc = vertc[:, 0]
yc = vertc[:, 1]
sr = flopy.utils.reference.SpatialReferenceUnstructured(xc, yc, verts, iverts, [xc.shape[0]])


read 13 records from ../data/mfgrd_test/flowxt3d.disv.grb
returning vertices for ../data/mfgrd_test/flowxt3d.disv.grb

In [15]:
fn = os.path.join(workspace, 'flowxt3d.hds.cmp')
ho = flopy.utils.HeadFile(fn, precision='double')
times = ho.get_times()
print(times)
h = ho.get_data(totim=times[-1]).reshape(218)
print(h.shape)


[1.0]
(218,)

In [16]:
mm = flopy.plot.ModelMap(sr=sr)
ax = plt.gca()
ax.set_xlim(0,700)
ax.set_ylim(0,700)
v = mm.plot_cvfd(verts, iverts, edgecolor='black', a=h)
cs = mm.contour_array_cvfd(vertc, h, colors='white')
plt.clabel(cs, fmt='%.1f', colors='white', fontsize=11)
plt.colorbar(v, shrink=0.5)


Out[16]:
<matplotlib.colorbar.Colorbar at 0x112d47a20>

Single-layer unstructured model with a well


In [17]:
fn = os.path.join(workspace, 'flowwel.disv.grb')
grd = flopy.utils.MfGrdFile(fn, verbose=True)
iverts, verts = grd.get_verts()
vertc = grd.get_centroids()
xc = vertc[:, 0]
yc = vertc[:, 1]
sr = flopy.utils.reference.SpatialReferenceUnstructured(xc, yc, verts, iverts, [xc.shape[0]])


read 13 records from ../data/mfgrd_test/flowwel.disv.grb
returning vertices for ../data/mfgrd_test/flowwel.disv.grb

In [18]:
fn = os.path.join(workspace, 'flowwel.hds.cmp')
ho = flopy.utils.HeadFile(fn, precision='double')
times = ho.get_times()
print(times)
h = ho.get_data(totim=times[-1]).reshape(218)
print(h.shape)


[1.0]
(218,)

In [19]:
mm = flopy.plot.ModelMap(sr=sr)
ax = plt.gca()
ax.set_xlim(0,700)
ax.set_ylim(0,700)
v = mm.plot_cvfd(verts, iverts, edgecolor='black', a=h)
cs = mm.contour_array_cvfd(vertc, h, colors='white')
plt.clabel(cs, fmt='%.1f', colors='white', fontsize=11)
plt.colorbar(v, shrink=0.5)


Out[19]:
<matplotlib.colorbar.Colorbar at 0x1126d02e8>

Single-layer unstructured model with a well and xt3d


In [20]:
fn = os.path.join(workspace, 'flowwelxt3d.disv.grb')
grd = flopy.utils.MfGrdFile(fn, verbose=True)
iverts, verts = grd.get_verts()
vertc = grd.get_centroids()
xc = vertc[:, 0]
yc = vertc[:, 1]
sr = flopy.utils.reference.SpatialReferenceUnstructured(xc, yc, verts, iverts, [xc.shape[0]])


read 13 records from ../data/mfgrd_test/flowwelxt3d.disv.grb
returning vertices for ../data/mfgrd_test/flowwelxt3d.disv.grb

In [21]:
fn = os.path.join(workspace, 'flowwelxt3d.hds.cmp')
ho = flopy.utils.HeadFile(fn, precision='double')
times = ho.get_times()
print(times)
h2 = ho.get_data(totim=times[-1]).reshape(218)
print(h2.shape)


[1.0]
(218,)

In [22]:
#f = plt.gcf()
#f.set_size_inches(15, 15)
mm = flopy.plot.ModelMap(sr=sr)
ax = plt.gca()
ax.set_xlim(0,700)
ax.set_ylim(0,700)
v = mm.plot_cvfd(verts, iverts, edgecolor='black', a=h2, cmap='jet_r')
cs = mm.contour_array_cvfd(vertc, h2, colors='white', linewidths=2)
cs = mm.contour_array_cvfd(vertc, h, linestyles='dashed', colors='cyan', linewidths=2)
plt.clabel(cs, fmt='%.1f', colors='white', fontsize=11)
plt.colorbar(v, shrink=0.5)


Out[22]:
<matplotlib.colorbar.Colorbar at 0x1125f84a8>

In [23]:
d = h - h2
#f = plt.gcf()
#f.set_size_inches(15, 15)
mm = flopy.plot.ModelMap(sr=sr)
ax = plt.gca()
ax.set_xlim(0,700)
ax.set_ylim(0,700)
v = mm.plot_cvfd(verts, iverts, edgecolor='black', a=d, cmap='jet_r')
#cs = mm.contour_array_cvfd(vertc, d, colors='white')
#plt.clabel(cs, fmt='%.1f', colors='white', fontsize=11)
plt.colorbar(v, shrink=0.5)


Out[23]:
<matplotlib.colorbar.Colorbar at 0x1126a55c0>

Single-layer nested model with a well


In [24]:
fn = os.path.join(workspace, 'flowquadwel.disv.grb')
grd = flopy.utils.MfGrdFile(fn, verbose=True)
iverts, verts = grd.get_verts()
vertc = grd.get_centroids()
xc = vertc[:, 0]
yc = vertc[:, 1]
sr = flopy.utils.reference.SpatialReferenceUnstructured(xc, yc, verts, iverts, [xc.shape[0]])


read 13 records from ../data/mfgrd_test/flowquadwel.disv.grb
returning vertices for ../data/mfgrd_test/flowquadwel.disv.grb

In [25]:
fn = os.path.join(workspace, 'flowquadwel.hds.cmp')
ho = flopy.utils.HeadFile(fn, precision='double')
times = ho.get_times()
print(times)
h = ho.get_data(totim=times[-1]).flatten()
print(h.shape)


[1.0]
(121,)

In [26]:
mm = flopy.plot.ModelMap(sr=sr)
ax = plt.gca()
ax.set_xlim(0,700)
ax.set_ylim(0,700)
v = mm.plot_cvfd(verts, iverts, edgecolor='black', a=h)
cs = mm.contour_array_cvfd(vertc, h, colors='white')
plt.clabel(cs, fmt='%.1f', colors='white', fontsize=11)
plt.colorbar(v, shrink=0.5)


Out[26]:
<matplotlib.colorbar.Colorbar at 0x10f2d10b8>

Single-layer nested model with a well and xt3d


In [27]:
fn = os.path.join(workspace, 'flowquadwelxt3d.disv.grb')
grd = flopy.utils.MfGrdFile(fn, verbose=True)
iverts, verts = grd.get_verts()
vertc = grd.get_centroids()
xc = vertc[:, 0]
yc = vertc[:, 1]
sr = flopy.utils.reference.SpatialReferenceUnstructured(xc, yc, verts, iverts, [xc.shape[0]])


read 13 records from ../data/mfgrd_test/flowquadwelxt3d.disv.grb
returning vertices for ../data/mfgrd_test/flowquadwelxt3d.disv.grb

In [28]:
fn = os.path.join(workspace, 'flowquadwelxt3d.hds.cmp')
ho = flopy.utils.HeadFile(fn, precision='double')
times = ho.get_times()
print(times)
h2 = ho.get_data(totim=times[-1]).flatten()
print(h2.shape)


[1.0]
(121,)

In [29]:
#f = plt.gcf()
#f.set_size_inches(10,10)
mm = flopy.plot.ModelMap(sr=sr)
ax = plt.gca()
ax.set_xlim(0,700)
ax.set_ylim(0,700)
v = mm.plot_cvfd(verts, iverts, edgecolor='black', a=h2)
cs = mm.contour_array_cvfd(vertc, h2, colors='white')
plt.clabel(cs, fmt='%.1f', colors='white', fontsize=11)
cs = mm.contour_array_cvfd(vertc, h, linestyles='dashed', colors='cyan', linewidths=2)
plt.colorbar(v, shrink=0.5)


Out[29]:
<matplotlib.colorbar.Colorbar at 0x1122182e8>

In [30]:
d = h - h2
#f = plt.gcf()
#f.set_size_inches(15, 15)
mm = flopy.plot.ModelMap(sr=sr)
ax = plt.gca()
ax.set_xlim(0,700)
ax.set_ylim(0,700)
v = mm.plot_cvfd(verts, iverts, edgecolor='black', a=d, cmap='jet_r')
#cs = mm.contour_array_cvfd(vertc, d, colors='white')
#plt.clabel(cs, fmt='%.1f', colors='white', fontsize=11)
plt.colorbar(v, shrink=0.5)


Out[30]:
<matplotlib.colorbar.Colorbar at 0x111e85d68>

In [ ]: