In [1]:
import python_subgrid.wrapper
import python_subgrid.plotting
import matplotlib.cm
import matplotlib.colors
import matplotlib.pyplot as plt
import numpy as np
import logging
import shapely.ops
import scipy.spatial
import scipy.interpolate


/home/fedor/.virtualenvs/main/local/lib/python2.7/site-packages/pkg_resources.py:991: UserWarning: /home/fedor/.python-eggs is writable by group/others and vulnerable to attack when used with get_resource_filename. Consider a more secure location (set with .set_extraction_path or the PYTHON_EGG_CACHE environment variable).
  warnings.warn(msg, UserWarning)

In [2]:
subgrid = python_subgrid.wrapper.SubgridWrapper(mdu="/home/fedor/Checkouts/models/duifp/Duifp_duifpolder.mdu")
python_subgrid.wrapper.logger.setLevel(logging.WARN)

In [3]:
subgrid.start()
subgrid.initmodel()
subgrid.update(-1)


WARNING:python_subgrid.wrapper:Progress stopped but no current progress message
WARNING:python_subgrid.wrapper:Progress stopped but no current progress message
WARNING:python_subgrid.wrapper: Could not apply infiltration for 1D node number         5554
WARNING:python_subgrid.wrapper: Could not apply infiltration for 1D node number         5562
WARNING:python_subgrid.wrapper: Could not apply infiltration for 1D node number         5563
WARNING:python_subgrid.wrapper: Could not apply infiltration for 1D node number         5564
WARNING:python_subgrid.wrapper: Could not apply infiltration for 1D node number         5565
WARNING:python_subgrid.wrapper: Could not apply infiltration for 1D node number         5566
WARNING:python_subgrid.wrapper: Could not apply infiltration for 1D node number         5569
WARNING:python_subgrid.wrapper: Could not apply infiltration for 1D node number         5570
WARNING:python_subgrid.wrapper: Could not apply infiltration for 1D node number         5571
WARNING:python_subgrid.wrapper: Could not apply infiltration for 1D node number         5573
WARNING:python_subgrid.wrapper: Could not apply infiltration for 1D node number         5585
WARNING:python_subgrid.wrapper: Could not apply infiltration for 1D node number         5589
WARNING:python_subgrid.wrapper: Could not apply infiltration for 1D node number         5591
WARNING:python_subgrid.wrapper: Could not apply infiltration for 1D node number         5595
WARNING:python_subgrid.wrapper: Could not apply infiltration for 1D node number         5613
Out[3]:
0

In [4]:
names = {'maxinterception', 'infiltrationrate', 'dsnop', 'x0p', 'y0p', 'x1p', 'y1p', 'dxp', 's1'}
vars = {var:subgrid.get_nd(var) for var in names}

In [5]:
mi_ma = np.ma.masked_equal(vars['maxinterception'][1:-1,1:-1],  vars['dsnop'])

In [6]:
# scenario
# infiltration ratio to 1000
# interception lower area to 1.0 
# constant rain 100

In [7]:
pts = np.array([
                [80300, 439600], 
                [80900, 439950],
                [80800, 440120],
                [80200, 439770],
                [80300, 439600]
                ])

In [8]:
plt.figure(figsize=(10,10))
extent = (vars['x0p'], vars['x1p'], vars['y0p'], vars['y1p'])
plt.imshow(mi_ma, cmap='Greens', extent=extent, origin='lower')
plt.fill(pts[:,0], pts[:,1], 'r-', linewidth=3)
plt.colorbar()


Out[8]:
<matplotlib.colorbar.Colorbar instance at 0x756ce18>

In [9]:
Y, X = np.mgrid[vars['y0p']:vars['y1p']:vars['dxp'],
                vars['x0p']:vars['x1p']:vars['dxp']]

In [10]:
import shapely.geometry
poly = shapely.geometry.Polygon(shell=pts)

In [11]:
coords = np.c_[X.ravel(), Y.ravel()]

In [12]:
inpoly = lambda x: poly.contains(shapely.geometry.Point(x))

In [13]:
import matplotlib.path
path = matplotlib.path.Path(pts, closed=True)
mask = path.contains_points(coords).reshape(mi_ma.shape)

In [14]:
mask_ma = np.ma.masked_array(mask, mask=mi_ma.mask)

In [15]:
plt.figure(figsize=(10,10))
extent = (vars['x0p'], vars['x1p'], vars['y0p'], vars['y1p'])
plt.imshow(mi_ma, cmap='Greens', extent=extent, origin='lower')
plt.imshow(mask_ma, cmap='Reds', extent=extent, origin='lower', alpha=0.5)

plt.colorbar()


Out[15]:
<matplotlib.colorbar.Colorbar instance at 0x76a0128>

In [16]:
# Add a meter of interception
vars['maxinterception'][1:-1,1:-1][mask_ma] = 1
# Slurp
vars['infiltrationrate'][1:-1,1:-1][mask_ma] = 1000.0

In [17]:
plt.figure(figsize=(10,10))
extent = (vars['x0p'], vars['x1p'], vars['y0p'], vars['y1p'])
mi_ma = np.ma.masked_equal(vars['maxinterception'][1:-1,1:-1],  vars['dsnop'])
plt.imshow(mi_ma, cmap='Greens', extent=extent, origin='lower')


Out[17]:
<matplotlib.image.AxesImage at 0x7792150>

In [18]:
x0, y0, x1, y1 = np.array(poly.exterior.bounds)
subgrid.update_rect('maxinterception', x0, x1, y0, y1)
subgrid.update_rect('infiltration', x0, x1, y0, y1)


WARNING:python_subgrid.wrapper:updating interception tables not supported yet.
Out[18]:
-1

In [19]:
import python_subgrid.plotting

In [20]:
quad_grid = python_subgrid.plotting.make_quad_grid(subgrid)

In [21]:
plt.imshow(np.ma.masked_array(vars['s1'][1:][quad_grid.filled(0)-1], quad_grid.mask), origin='lower', cmap='Blues', extent=extent)


Out[21]:
<matplotlib.image.AxesImage at 0x8223410>

In [ ]: