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