1 | """Read in sww file, interpolate at specified locations and plot time series |
---|
2 | |
---|
3 | """ |
---|
4 | |
---|
5 | import project |
---|
6 | from pyvolution.util import file_function |
---|
7 | #from coordinate_transforms.redfearn import degminsec2decimal_degrees, redfearn |
---|
8 | from pylab import * |
---|
9 | from matplotlib.ticker import MultipleLocator, FormatStrFormatter |
---|
10 | |
---|
11 | swwfile = project.outputname + '.sww' |
---|
12 | #swwfile = project.outputdir + timestampdir + sep + project.outputname + '.sww' |
---|
13 | # place to store figures |
---|
14 | #graphloc = project.outputdir + timestampdir + sep |
---|
15 | graphloc = project.outputdir |
---|
16 | |
---|
17 | #Time interval to plot |
---|
18 | tmin = 2*60 |
---|
19 | tmax = 10*60 |
---|
20 | |
---|
21 | def get_gauges_from_file(filename): |
---|
22 | fid = open(filename) |
---|
23 | lines = fid.readlines() |
---|
24 | fid.close() |
---|
25 | |
---|
26 | gauges = [] |
---|
27 | gaugelocation = [] |
---|
28 | for line in lines[1:]: |
---|
29 | fields = line.split(',') |
---|
30 | # my gauge file set up as locationname, easting, northing |
---|
31 | location = fields[0] |
---|
32 | easting = float(fields[1]) |
---|
33 | northing = float(fields[2]) |
---|
34 | #z, easting, northing = redfearn(lat, lon) |
---|
35 | gauges.append([easting, northing]) |
---|
36 | gaugelocation.append(location) |
---|
37 | |
---|
38 | return gauges, lines, gaugelocation |
---|
39 | |
---|
40 | #gauges, buildings = get_gauges_from_file(project.gauge_filename) |
---|
41 | gauges, lines, locations = get_gauges_from_file(project.gauge_filename) |
---|
42 | |
---|
43 | print 'number of gauges: ', len(gauges) |
---|
44 | |
---|
45 | #Read model output |
---|
46 | quantities = ['stage', 'elevation', 'xmomentum', 'ymomentum'] |
---|
47 | f = file_function(swwfile, |
---|
48 | quantities = quantities, |
---|
49 | interpolation_points = gauges, |
---|
50 | verbose = True, |
---|
51 | use_cache = True) |
---|
52 | |
---|
53 | print 'size f', size(f.quantities['stage'],axis=0), size(f.quantities['stage'],axis=1) |
---|
54 | |
---|
55 | from math import sqrt, atan, degrees |
---|
56 | from Numeric import ones |
---|
57 | N = len(gauges) |
---|
58 | for k, g in enumerate(gauges): |
---|
59 | if k%((N+10)/10)==0: # diagnostics - print 10 lines |
---|
60 | print 'Doing row %d of %d' %(k, N) |
---|
61 | |
---|
62 | model_time = [] |
---|
63 | stages = [] |
---|
64 | elevations = [] |
---|
65 | momenta = [] |
---|
66 | velocity = [] |
---|
67 | xmom = [] |
---|
68 | ymom = [] |
---|
69 | bearings = [] |
---|
70 | depths = [] |
---|
71 | |
---|
72 | max_depth = 0 |
---|
73 | max_momentum = 0 |
---|
74 | max_velocity = 0 |
---|
75 | |
---|
76 | T = f.get_time() |
---|
77 | due_east = 90.0*ones([len(T)]) |
---|
78 | due_west = 270.0*ones([len(T)]) |
---|
79 | maxT = max(T) |
---|
80 | tstep = maxT/(len(T)-1) |
---|
81 | |
---|
82 | for i, t in enumerate(T): # T is a list of times |
---|
83 | #if tmin < t < tmax: |
---|
84 | w = f(t, point_id = k)[0] |
---|
85 | z = f(t, point_id = k)[1] |
---|
86 | uh = f(t, point_id = k)[2] |
---|
87 | vh = f(t, point_id = k)[3] |
---|
88 | gaugeloc = locations[k] |
---|
89 | depth = w-z |
---|
90 | |
---|
91 | m = sqrt(uh*uh + vh*vh) #Absolute momentum |
---|
92 | vel = sqrt(uh*uh + vh*vh) / (w-z + 1.e-30) #Absolute velocity |
---|
93 | angle = degrees(atan(vh/(uh+1.e-15))) |
---|
94 | if (0 < angle < 90.0): |
---|
95 | if vh > 0: |
---|
96 | bearing = 90.0 - abs(angle) |
---|
97 | if vh < 0: |
---|
98 | bearing = 270.0 - abs(angle) |
---|
99 | if (-90 < angle < 0): |
---|
100 | if vh < 0: |
---|
101 | bearing = 90.0 - abs(angle) |
---|
102 | if vh > 0: |
---|
103 | bearing = 270.0 - abs(angle) |
---|
104 | if angle == 0: |
---|
105 | bearing = 0.0 |
---|
106 | |
---|
107 | model_time.append(t) |
---|
108 | stages.append(w) |
---|
109 | elevations.append(z) #Should be constant over time |
---|
110 | momenta.append(m) |
---|
111 | velocity.append(vel) |
---|
112 | xmom.append(uh) |
---|
113 | ymom.append(vh) |
---|
114 | bearings.append(bearing) |
---|
115 | depths.append(depth) |
---|
116 | |
---|
117 | if w-z > max_depth: |
---|
118 | max_depth = w-z |
---|
119 | if m > max_momentum: |
---|
120 | max_momentum = m |
---|
121 | if vel > max_velocity: |
---|
122 | max_velocity = vel |
---|
123 | |
---|
124 | #Plot only those gauges that have been inundated by more than a threshold |
---|
125 | #if max_depth < 0.2: |
---|
126 | # print 'Skipping gauge %d' %k |
---|
127 | # continue |
---|
128 | |
---|
129 | ion() |
---|
130 | hold(False) |
---|
131 | |
---|
132 | if elevations[0] < -10: |
---|
133 | plot(model_time, stages, '-b') |
---|
134 | else: |
---|
135 | plot(model_time, stages, '-b', |
---|
136 | model_time, elevations, '-k') |
---|
137 | #name = 'Gauge_%d: (%.1f, %.1f)' %(k, g[0], g[1]) |
---|
138 | name = 'Gauge_%d: (%.1f, %.1f) Location: %s' %(k, g[0], g[1], gaugeloc) |
---|
139 | title(name) |
---|
140 | |
---|
141 | title('%s (stage)' %name) |
---|
142 | xlabel('time [s]') |
---|
143 | ylabel('elevation [m]') |
---|
144 | legend(('Stage', 'Bed = %.1f' %elevations[0]), |
---|
145 | shadow=True, |
---|
146 | loc='upper right') |
---|
147 | #('Gauge_%d_stage' %k) |
---|
148 | savefig('%sGauge_%s_stage' %(graphloc, gaugeloc)) |
---|
149 | #savefig('Gauge_%s_stage.eps' %gaugeloc) |
---|
150 | |
---|
151 | #Momentum plot |
---|
152 | ion() |
---|
153 | hold(False) |
---|
154 | plot(model_time, momenta, '-r') |
---|
155 | title(name) |
---|
156 | |
---|
157 | title('%s (momentum)' %name) |
---|
158 | xlabel('time [s]') |
---|
159 | ylabel('sqrt( uh^2 + vh^2 ) [m^2/s]') |
---|
160 | #savefig('Gauge_%d_momentum' %k) |
---|
161 | savefig('%sGauge_%s_momentum' %(graphloc, gaugeloc)) |
---|
162 | |
---|
163 | #Bearing plot |
---|
164 | ion() |
---|
165 | hold(False) |
---|
166 | ax = plot(model_time, bearings, '-b', model_time, due_west, '-.b', |
---|
167 | model_time, due_east, '-.b') |
---|
168 | title(name) |
---|
169 | ax = axis([0, maxT, 0, 360]) |
---|
170 | text(maxT+tstep, 90, 'East') |
---|
171 | text(maxT+tstep, 270, 'West') |
---|
172 | #majorLocator = MultipleLocator(3600) |
---|
173 | #print 'major', majorLocator[1] |
---|
174 | #ax.xaxis.set_major_locator(majorLocator) #'yticklabels', range(30,390,30)) |
---|
175 | # set(labels,color='g',rotation=45) |
---|
176 | |
---|
177 | title('%s (bearing)' %name) |
---|
178 | xlabel('time [s]') |
---|
179 | ylabel(' atan(vh/uh) [degrees from North]') |
---|
180 | #savefig('Gauge_%d_bearing' %k) |
---|
181 | savefig('%sGauge_%s_bearing' %(graphloc, gaugeloc)) |
---|
182 | |
---|
183 | #Speed plot |
---|
184 | ion() |
---|
185 | hold(False) |
---|
186 | plot(model_time, velocity, '-r') |
---|
187 | title(name) |
---|
188 | |
---|
189 | title('%s (velocity)' %name) |
---|
190 | xlabel('time [s]') |
---|
191 | ylabel('sqrt( uh^2 + vh^2 ) / depth [m/s]') |
---|
192 | #savefig('Gauge_%d_speed' %k) |
---|
193 | savefig('%sGauge_%s_speed' %(graphloc, gaugeloc)) |
---|
194 | |
---|
195 | whichone = '_%s' %gaugeloc |
---|
196 | thisfile = project.gaugetimeseries+whichone+'.csv' |
---|
197 | fid = open(thisfile, 'w') |
---|
198 | s = 'Time, Depth, Momentum, Velocity \n' |
---|
199 | fid.write(s) |
---|
200 | for i_t, i_d, i_m, i_vel in zip(model_time, depths, momenta, velocity): |
---|
201 | s = '%.2f, %.2f, %.2f, %.2f\n' %(i_t, i_d, i_m, i_vel) |
---|
202 | fid.write(s) |
---|
203 | |
---|
204 | show() |
---|
205 | |
---|
206 | |
---|