In [2]:
import os
import pandas
import python_subgrid
import netCDF4
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.lines

In [3]:
ds = netCDF4.Dataset('../../models/1d-democase/subgrid_map.nc')

In [4]:
u1 = ds.variables['unorm'][0,:] # called u1 in model
t = ds.variables['time'][:]

In [5]:
# We have 1d, 2d links followed by 1d and 2d boundaries
print ds.nFlowLink1d, ds.nFlowLink2d, ds.nFlowLink1dBounds, ds.nFlowLink2dBounds
# This variable should be of shape time * the sum of the above
print ds.variables['unorm'].shape


68 180 2 0
(201, 250)

In [6]:
# We now that we can split the variables in  1d and 2d links and elements
xn2d, xn1d,_ = np.split(ds.variables['FlowElem_xcc'][:], np.cumsum([getattr(ds,'nFlowElem2d'),getattr(ds,'nFlowElem1d')]))
yn2d, yn1d,_ = np.split(ds.variables['FlowElem_ycc'][:], np.cumsum([getattr(ds,'nFlowElem2d'),getattr(ds,'nFlowElem1d')]))
xl2d, xl1d,_ = np.split(ds.variables['FlowLink_xu'][:], np.cumsum([getattr(ds,'nFlowLink2d'),getattr(ds,'nFlowLink1d')]))
yl2d, yl1d,_ = np.split(ds.variables['FlowLink_yu'][:], np.cumsum([getattr(ds,'nFlowLink2d'),getattr(ds,'nFlowLink1d')]))
u12d, u11d,_ = np.split(u1, np.cumsum([getattr(ds,'nFlowLink2d'),getattr(ds,'nFlowLink1d')]))

# We read everything we need from the file, let's close it
ds.close()

In [7]:
# Let's read our network
lines = open('../../models/1d-democase/one_d/netwerk.inp').readlines()
rows = [map(int, line.split()) for line in lines]
# I'm not writing a parser here, the first 6 rows are 1d nodes
nodes = pandas.DataFrame(rows[:6], columns=('idx', 'a', 'x','y','b', 'c'))
nodes = nodes.set_index('idx')

# Starting from row 10 we have the branches 
branches = pandas.DataFrame(rows[9:], columns=('idx', 'a', 'node0_idx', 'node1_idx', 'b', 'c', 'd'))
branches = branches.set_index('idx')

In [8]:
# Let's plot the network
fig, ax = plt.subplots()
ax.set_aspect('equal')
ax.plot(nodes.x, nodes.y, 'bs', label='netn', alpha=0.3, markersize=5)

for i, row in branches.iterrows():
    node0 = nodes.ix[row['node0_idx']]
    node1 = nodes.ix[row['node1_idx']]
    ax.text(node0['x']+1, node0['y']+2, node0.name)
    ax.text(node1['x']+1, node1['y']+2, node1.name)
    ax.text((node1['x']+node0['x'])/2.0+1, (node0['y']+node1['y'])/2.0+2, row.name, color='red')
    line = matplotlib.lines.Line2D(xdata=[node0['x'], node1['x']], ydata=[node0['y'], node1['y']])
    
    ax.add_line(line)
ax.set_title('1D Network')
ax.set_xlim(auto=True)
ax.set_ylim(456000.0-10, 456100.0+10)


Out[8]:
(455990.0, 456110.0)

In [9]:
fig, ax = plt.subplots()
# We can also add all the computational links 
l = ax.plot(xn1d, yn1d, 'go', label='1dn')
l = ax.plot(xn2d, yn2d, 'k.', alpha=0.3)
l = ax.plot(xl1d, yl1d, 'gx', label='1dl')
l = ax.plot(xl2d, yl2d, 'k+', alpha=0.3)
l = ax.quiver(xl1d, yl1d, np.zeros(u11d.shape), u11d, label='u1 1d', alpha=0.3)
l = ax.quiver(xl2d, yl2d, np.zeros(u12d.shape), u12d, label='u1 2d', alpha=0.3)

ax.legend()
ax.set_title('Computational links')


Out[9]:
<matplotlib.text.Text at 0x4840b10>

In [20]:
a = np.array([[2,3],[1,2]])

In [23]:
np.array(a.tolist())


Out[23]:
array([[2, 3],
       [1, 2]])

In [ ]: