1 | """ Utilities for reading data / plotting of channel/floodplain case |
---|
2 | """ |
---|
3 | |
---|
4 | class get_output: |
---|
5 | """Read in data from an .sww file in a convenient form |
---|
6 | e.g. |
---|
7 | p = util.get_output('channel3.sww', minimum_allowed_height=0.01) |
---|
8 | |
---|
9 | p then contains most relevant information as e.g., p.stage, p.elev, p.xmom, etc |
---|
10 | """ |
---|
11 | def __init__(self, filename, minimum_allowed_height=1.0e-03): |
---|
12 | self.x, self.y, self.time, self.vols, self.stage, \ |
---|
13 | self.elev, self.xmom, self.ymom, \ |
---|
14 | self.xvel, self.yvel, self.vel, self.minimum_allowed_height = \ |
---|
15 | read_output(filename, minimum_allowed_height) |
---|
16 | |
---|
17 | |
---|
18 | def read_output(filename, minimum_allowed_height): |
---|
19 | # Input: The name of an .sww file to read data from, |
---|
20 | # e.g. read_sww('channel3.sww') |
---|
21 | # |
---|
22 | # Purpose: To read the sww file, and output a number of variables as arrays that |
---|
23 | # we can then manipulate (e.g. plot, interrogate) |
---|
24 | # |
---|
25 | # Output: x, y, time, stage, elev, xmom, ymom, xvel, yvel, vel |
---|
26 | |
---|
27 | # Import modules |
---|
28 | |
---|
29 | from Scientific.IO.NetCDF import NetCDFFile |
---|
30 | |
---|
31 | |
---|
32 | # Open ncdf connection |
---|
33 | fid=NetCDFFile(filename) |
---|
34 | |
---|
35 | |
---|
36 | # Read variables |
---|
37 | x=fid.variables['x'] |
---|
38 | x=x.getValue() |
---|
39 | y=fid.variables['y'] |
---|
40 | y=y.getValue() |
---|
41 | |
---|
42 | stage=fid.variables['stage'] |
---|
43 | stage=stage.getValue() |
---|
44 | |
---|
45 | elev=fid.variables['elevation'] |
---|
46 | elev=elev.getValue() |
---|
47 | |
---|
48 | xmom=fid.variables['xmomentum'] |
---|
49 | xmom=xmom.getValue() |
---|
50 | |
---|
51 | ymom=fid.variables['ymomentum'] |
---|
52 | ymom=ymom.getValue() |
---|
53 | |
---|
54 | time=fid.variables['time'] |
---|
55 | time=time.getValue() |
---|
56 | |
---|
57 | vols=fid.variables['volumes'] |
---|
58 | vols=vols.getValue() |
---|
59 | |
---|
60 | |
---|
61 | # Calculate velocity = momentum/depth |
---|
62 | xvel=xmom*0.0 |
---|
63 | yvel=ymom*0.0 |
---|
64 | |
---|
65 | for i in range(xmom.shape[0]): |
---|
66 | xvel[i,:]=xmom[i,:]/(stage[i,:]-elev+1.0e-06)*(stage[i,:]> elev+minimum_allowed_height) |
---|
67 | yvel[i,:]=ymom[i,:]/(stage[i,:]-elev+1.0e-06)*(stage[i,:]> elev+minimum_allowed_height) |
---|
68 | |
---|
69 | vel = (xvel**2+yvel**2)**0.5 |
---|
70 | |
---|
71 | return x, y, time, vols, stage, elev, xmom, ymom, xvel, yvel, vel, minimum_allowed_height |
---|
72 | |
---|
73 | ############## |
---|
74 | |
---|
75 | def animate_1D(time, var, x, ylab=' '): #, x=range(var.shape[1]), vmin=var.min(), vmax=var.max()): |
---|
76 | # Input: time = one-dimensional time vector; |
---|
77 | # var = array with first dimension = len(time) ; |
---|
78 | # x = (optional) vector width dimension equal to var.shape[1]; |
---|
79 | |
---|
80 | import pylab |
---|
81 | import numpy |
---|
82 | |
---|
83 | |
---|
84 | |
---|
85 | pylab.close() |
---|
86 | pylab.ion() |
---|
87 | |
---|
88 | # Initial plot |
---|
89 | vmin=var.min() |
---|
90 | vmax=var.max() |
---|
91 | line, = pylab.plot( (x.min(), x.max()), (vmin, vmax), 'o') |
---|
92 | |
---|
93 | # Lots of plots |
---|
94 | for i in range(len(time)): |
---|
95 | line.set_xdata(x) |
---|
96 | line.set_ydata(var[i,:]) |
---|
97 | pylab.draw() |
---|
98 | pylab.xlabel('x') |
---|
99 | pylab.ylabel(ylab) |
---|
100 | pylab.title('time = ' + str(time[i])) |
---|
101 | |
---|
102 | return |
---|
103 | |
---|
104 | |
---|
105 | class get_centroids: |
---|
106 | """ |
---|
107 | Extract centroid values from the output of get_output. |
---|
108 | e.g. |
---|
109 | p = util.get_output('my_sww.sww', minimum_allowed_height=0.01) # vertex values |
---|
110 | pc=util.get_centroids(p, velocity_extrapolation=True) # centroid values |
---|
111 | """ |
---|
112 | def __init__(self,p, velocity_extrapolation=False): |
---|
113 | self.time, self.x, self.y, self.stage, self.xmom,\ |
---|
114 | self.ymom, self.elev, self.xvel, \ |
---|
115 | self.yvel, self.vel= \ |
---|
116 | get_centroid_values(p, velocity_extrapolation) |
---|
117 | |
---|
118 | |
---|
119 | def get_centroid_values(p, velocity_extrapolation): |
---|
120 | # Input: p is the result of e.g. p=util.get_output('mysww.sww'). See the get_output class defined above |
---|
121 | # Output: Values of x, y, Stage, xmom, ymom, elev, xvel, yvel, vel at centroids |
---|
122 | import numpy |
---|
123 | |
---|
124 | # Make 3 arrays, each containing one index of a vertex of every triangle. |
---|
125 | l=len(p.vols) |
---|
126 | vols0=numpy.zeros(l, dtype='int') |
---|
127 | vols1=numpy.zeros(l, dtype='int') |
---|
128 | vols2=numpy.zeros(l, dtype='int') |
---|
129 | |
---|
130 | # FIXME: 22/2/12/ - I think this loop is slow, should be able to do this |
---|
131 | # another way |
---|
132 | for i in range(l): |
---|
133 | vols0[i]=p.vols[i][0] |
---|
134 | vols1[i]=p.vols[i][1] |
---|
135 | vols2[i]=p.vols[i][2] |
---|
136 | |
---|
137 | # Then use these to compute centroid averages |
---|
138 | x_cent=(p.x[vols0]+p.x[vols1]+p.x[vols2])/3.0 |
---|
139 | y_cent=(p.y[vols0]+p.y[vols1]+p.y[vols2])/3.0 |
---|
140 | |
---|
141 | stage_cent=(p.stage[:,vols0]+p.stage[:,vols1]+p.stage[:,vols2])/3.0 |
---|
142 | elev_cent=(p.elev[vols0]+p.elev[vols1]+p.elev[vols2])/3.0 |
---|
143 | |
---|
144 | # Here, we need to treat differently the case of momentum extrapolation or |
---|
145 | # velocity extrapolation |
---|
146 | if velocity_extrapolation: |
---|
147 | xvel_cent=(p.xvel[:,vols0]+p.xvel[:,vols1]+p.xvel[:,vols2])/3.0 |
---|
148 | yvel_cent=(p.yvel[:,vols0]+p.yvel[:,vols1]+p.yvel[:,vols2])/3.0 |
---|
149 | |
---|
150 | # Now compute momenta |
---|
151 | xmom_cent=stage_cent*0.0 |
---|
152 | ymom_cent=stage_cent*0.0 |
---|
153 | |
---|
154 | t=len(p.time) |
---|
155 | |
---|
156 | for i in range(t): |
---|
157 | xmom_cent[i,:]=xvel_cent[i,:]*(stage_cent[i,:]-elev_cent+1.0e-06)*\ |
---|
158 | (stage_cent[i,:]>elev_cent+p.minimum_allowed_height) |
---|
159 | ymom_cent[i,:]=yvel_cent[i,:]*(stage_cent[i,:]-elev_cent+1.0e-06)*\ |
---|
160 | (stage_cent[i,:]>elev_cent+p.minimum_allowed_height) |
---|
161 | |
---|
162 | else: |
---|
163 | xmom_cent=(p.xmom[:,vols0]+p.xmom[:,vols1]+p.xmom[:,vols2])/3.0 |
---|
164 | ymom_cent=(p.ymom[:,vols0]+p.ymom[:,vols1]+p.ymom[:,vols2])/3.0 |
---|
165 | |
---|
166 | # Now compute velocities |
---|
167 | xvel_cent=stage_cent*0.0 |
---|
168 | yvel_cent=stage_cent*0.0 |
---|
169 | |
---|
170 | t=len(p.time) |
---|
171 | |
---|
172 | for i in range(t): |
---|
173 | xvel_cent[i,:]=xmom_cent[i,:]/(stage_cent[i,:]-elev_cent+1.0e-06)*(stage_cent[i,:]>elev_cent+p.minimum_allowed_height) |
---|
174 | yvel_cent[i,:]=ymom_cent[i,:]/(stage_cent[i,:]-elev_cent+1.0e-06)*(stage_cent[i,:]>elev_cent+p.minimum_allowed_height) |
---|
175 | |
---|
176 | |
---|
177 | # Compute velocity |
---|
178 | vel_cent=(xvel_cent**2 + yvel_cent**2)**0.5 |
---|
179 | |
---|
180 | return p.time, x_cent, y_cent, stage_cent, xmom_cent,\ |
---|
181 | ymom_cent, elev_cent, xvel_cent, yvel_cent, vel_cent |
---|
182 | |
---|
183 | # Make plot of stage over time. |
---|
184 | #pylab.close() |
---|
185 | #pylab.ion() |
---|
186 | #pylab.plot(time, stage[:,1]) |
---|
187 | #for i in range(201): |
---|
188 | # pylab.plot(time,stage[:,i]) |
---|
189 | |
---|
190 | # Momentum should be 0. |
---|
191 | #print 'Momentum max/min is', xmom.max() , xmom.min() |
---|
192 | |
---|
193 | #pylab.gca().set_aspect('equal') |
---|
194 | #pylab.scatter(x,y,c=elev,edgecolors='none') |
---|
195 | #pylab.colorbar() |
---|
196 | # |
---|
197 | #n=xvel.shape[0]-1 |
---|
198 | #pylab.quiver(x,y,xvel[n,:],yvel[n,:]) |
---|
199 | #pylab.savefig('Plot1.png') |
---|