1 | |
---|
2 | """ |
---|
3 | Plot up files from the Hinwood project. |
---|
4 | """ |
---|
5 | from os import sep |
---|
6 | import project |
---|
7 | from copy import deepcopy |
---|
8 | #from scipy import arange |
---|
9 | from csv import writer |
---|
10 | |
---|
11 | from Numeric import arange, array, zeros, Float, where, greater, less, \ |
---|
12 | compress, argmin, choose, searchsorted |
---|
13 | |
---|
14 | from anuga.fit_interpolate.interpolate import interpolate_sww2csv |
---|
15 | from anuga.shallow_water.data_manager import csv2dict |
---|
16 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
17 | |
---|
18 | |
---|
19 | SLOPE_STR = 'stage_slopes' |
---|
20 | TIME_STR = 'times' |
---|
21 | |
---|
22 | TIME_BORDER = 5 |
---|
23 | LOCATION_BORDER = .5 |
---|
24 | |
---|
25 | def load_sensors(quantity_file): |
---|
26 | """ |
---|
27 | Load a csv file, where the first row is the column header and |
---|
28 | the first colum explains the rows. |
---|
29 | """ |
---|
30 | #slope, _ = csv2dict(file_sim) |
---|
31 | |
---|
32 | # Read the depth file |
---|
33 | dfid = open(quantity_file) |
---|
34 | lines = dfid.readlines() |
---|
35 | dfid.close() |
---|
36 | |
---|
37 | title = lines.pop(0) |
---|
38 | n_time = len(lines) |
---|
39 | n_sensors = len(lines[0].split(','))-1 # -1 to remove time |
---|
40 | times = zeros(n_time, Float) #Time |
---|
41 | depths = zeros(n_time, Float) # |
---|
42 | sensors = zeros((n_time,n_sensors), Float) |
---|
43 | quantity_locations = title.split(',') #(',') |
---|
44 | quantity_locations.pop(0) # remove 'time' |
---|
45 | |
---|
46 | # Doing j.split(':')[0] drops the y location |
---|
47 | locations = [float(j.split(':')[0]) for j in quantity_locations] |
---|
48 | |
---|
49 | for i, line in enumerate(lines): |
---|
50 | fields = line.split(',') #(',') |
---|
51 | fields = [float(j) for j in fields] |
---|
52 | times[i] = fields[0] |
---|
53 | sensors[i] = fields[1:] # 1: to remove time |
---|
54 | |
---|
55 | #print "times",times |
---|
56 | #print "locations", locations |
---|
57 | #print "sensors", sensors |
---|
58 | return times, locations, sensors |
---|
59 | |
---|
60 | def load_slopes(stage_file): |
---|
61 | """ |
---|
62 | Finds the slope, wrt distance of a distance, time, quantity csv file. |
---|
63 | |
---|
64 | returns the times and slope_locations vectors and the slopes array. |
---|
65 | """ |
---|
66 | times, locations, sensors = load_sensors(stage_file) |
---|
67 | n_slope_locations = len(locations)-1 |
---|
68 | n_time = len(times) |
---|
69 | slope_locations = zeros(n_slope_locations, Float) # |
---|
70 | slopes = zeros((n_time,n_slope_locations), Float) |
---|
71 | |
---|
72 | # An array of the sensor spacing values |
---|
73 | delta_locations = zeros(n_slope_locations, Float) |
---|
74 | |
---|
75 | for i in arange(n_slope_locations): |
---|
76 | delta_locations[i] = (locations[i+1] - locations[i]) |
---|
77 | slope_locations[i] = locations[i] + 0.5*delta_locations[i] |
---|
78 | |
---|
79 | for j in arange(n_time): |
---|
80 | for i in arange(n_slope_locations): |
---|
81 | slopes[j,i] = (sensors[j,i+1] - sensors[j,i])/delta_locations[i] |
---|
82 | |
---|
83 | return times, slope_locations, slopes |
---|
84 | |
---|
85 | |
---|
86 | def graph_contours(times, x_data, z_data, |
---|
87 | y_label='Time, seconds', |
---|
88 | plot_title="slope", |
---|
89 | x_label='x location, m', |
---|
90 | save_as=None, |
---|
91 | is_interactive=False, |
---|
92 | break_xs=None, |
---|
93 | break_times=None): |
---|
94 | """ |
---|
95 | Currently used to generate stage slope contour graphs. |
---|
96 | |
---|
97 | Has been generalised a bit. |
---|
98 | """ |
---|
99 | # Do not move these imports. Tornado doesn't have pylab |
---|
100 | from pylab import meshgrid, cm, contourf, contour, ion, plot, xlabel, \ |
---|
101 | ylabel, close, legend, savefig, title, figure ,colorbar, show , axis |
---|
102 | |
---|
103 | origin = 'lower' |
---|
104 | |
---|
105 | if is_interactive: |
---|
106 | ion() |
---|
107 | |
---|
108 | # Can't seem to reshape this info once it is in the function |
---|
109 | CS = contourf(x_data, times, z_data, 10, |
---|
110 | cmap=cm.bone, |
---|
111 | origin=origin) |
---|
112 | |
---|
113 | #CS2 = contour(x_data, times, z_data, CS.levels[::1], |
---|
114 | # colors = 'r', |
---|
115 | # origin=origin, |
---|
116 | # hold='on') |
---|
117 | |
---|
118 | title(plot_title) |
---|
119 | xlabel(x_label) |
---|
120 | ylabel(y_label) |
---|
121 | |
---|
122 | if break_times is not None and break_xs is not None: |
---|
123 | plot(break_xs, break_times, 'ro') |
---|
124 | |
---|
125 | |
---|
126 | # Make a colorbar for the ContourSet returned by the contourf call. |
---|
127 | cbar = colorbar(CS) |
---|
128 | |
---|
129 | # Add the contour line levels to the colorbar |
---|
130 | cbar.ax.set_ylabel('stage slope') |
---|
131 | #cbar.add_lines(CS2) |
---|
132 | |
---|
133 | if is_interactive: |
---|
134 | raw_input() # Wait for enter pressed |
---|
135 | |
---|
136 | if save_as is not None: |
---|
137 | savefig(save_as) |
---|
138 | close() #Need to close this plot |
---|
139 | |
---|
140 | def graph_froude(times, x_data, z_data, |
---|
141 | y_label='Time, seconds', |
---|
142 | plot_title="Froude Number", |
---|
143 | x_label='x location, m', |
---|
144 | save_as=None, |
---|
145 | is_interactive=False, |
---|
146 | break_xs=None, |
---|
147 | break_times=None): |
---|
148 | """ |
---|
149 | Used to generate a froude Number contour graphs. |
---|
150 | |
---|
151 | """ |
---|
152 | # Do not move these imports. Tornado doesn't have pylab |
---|
153 | from pylab import meshgrid, cm, contourf, contour, ion, plot, xlabel, \ |
---|
154 | ylabel, close, legend, savefig, title, figure ,colorbar, show , axis |
---|
155 | |
---|
156 | origin = 'lower' |
---|
157 | |
---|
158 | if is_interactive: |
---|
159 | ion() |
---|
160 | |
---|
161 | # Can't seem to reshape this info once it is in the function |
---|
162 | #CS = contourf(x_data, times, z_data, [-1,0.6,0.8,1,2,4], |
---|
163 | # colors = ('black', 'r', 'g', 'b','r'), |
---|
164 | # #cmap=cm.bone, |
---|
165 | # origin=origin) |
---|
166 | CS = contourf(x_data, times, z_data, 10, |
---|
167 | #colors = ('black', 'r', 'g', 'b','r'), |
---|
168 | cmap=cm.bone, |
---|
169 | origin=origin) |
---|
170 | |
---|
171 | #CS2 = contour(x_data, times, z_data, CS.levels[::1], |
---|
172 | # colors = 'r', |
---|
173 | # origin=origin, |
---|
174 | # hold='on') |
---|
175 | |
---|
176 | title(plot_title) |
---|
177 | xlabel(x_label) |
---|
178 | ylabel(y_label) |
---|
179 | |
---|
180 | if break_times is not None and break_xs is not None: |
---|
181 | plot(break_xs, break_times, 'yo') |
---|
182 | |
---|
183 | |
---|
184 | # Make a colorbar for the ContourSet returned by the contourf call. |
---|
185 | cbar = colorbar(CS) |
---|
186 | |
---|
187 | # Add the contour line levels to the colorbar |
---|
188 | cbar.ax.set_ylabel('Froude Number') |
---|
189 | #cbar.add_lines(CS2) |
---|
190 | |
---|
191 | if is_interactive: |
---|
192 | raw_input() # Wait for enter pressed |
---|
193 | |
---|
194 | if save_as is not None: |
---|
195 | savefig(save_as) |
---|
196 | close() #Need to close this plot |
---|
197 | |
---|
198 | def auto_graph_slopes(outputdir_tag, scenarios, is_interactive=False): |
---|
199 | """ |
---|
200 | Used to generate all the stage slope contour graphs of a scenario list |
---|
201 | """ |
---|
202 | plot_type = ".pdf" |
---|
203 | for run_data in scenarios: |
---|
204 | id = run_data['scenario_id'] |
---|
205 | outputdir_name = id + outputdir_tag |
---|
206 | pro_instance = project.Project(['data','flumes','Hinwood_2008'], |
---|
207 | outputdir_name=outputdir_name) |
---|
208 | end = id + ".csv" |
---|
209 | anuga_break_times = [] |
---|
210 | for break_time in run_data['break_times']: |
---|
211 | anuga_break_times.append( \ |
---|
212 | break_time - run_data['ANUGA_start_time']) |
---|
213 | stage_file = pro_instance.outputdir + "fslope_stage_" + end |
---|
214 | plot_title = "Stage slope " + id + "\n file:" + \ |
---|
215 | outputdir_name + "_slope_stage" + plot_type |
---|
216 | print "Creating ", stage_file |
---|
217 | save_as = pro_instance.plots_dir + sep + \ |
---|
218 | outputdir_name + "_slope_stage" + plot_type |
---|
219 | times, locations, slopes = load_slopes(stage_file) |
---|
220 | #times, slopes = get_band(anuga_break_times[0]-TIME_BORDER, |
---|
221 | # 100, times, slopes, 0) |
---|
222 | #locations, slopes = get_band( |
---|
223 | # min(run_data['break_xs'])- 2*LOCATION_BORDER, |
---|
224 | # 100, locations, slopes, -1) |
---|
225 | graph_contours(times, locations, slopes, |
---|
226 | plot_title=plot_title, |
---|
227 | break_xs=run_data['break_xs'], |
---|
228 | break_times=anuga_break_times, |
---|
229 | save_as=save_as, |
---|
230 | is_interactive=is_interactive) |
---|
231 | |
---|
232 | def auto_graph_froudes(outputdir_tag, scenarios, is_interactive=False): |
---|
233 | """ |
---|
234 | Used to generate all the Froude number contour graphs of a scenario list |
---|
235 | """ |
---|
236 | |
---|
237 | plot_type = ".pdf" |
---|
238 | |
---|
239 | for run_data in scenarios: |
---|
240 | id = run_data['scenario_id'] |
---|
241 | outputdir_name = id + outputdir_tag |
---|
242 | pro_instance = project.Project(['data','flumes','Hinwood_2008'], |
---|
243 | outputdir_name=outputdir_name) |
---|
244 | end = id + ".csv" |
---|
245 | anuga_break_times = [] |
---|
246 | for break_time in run_data['break_times']: |
---|
247 | anuga_break_times.append( \ |
---|
248 | break_time - run_data['ANUGA_start_time']) |
---|
249 | plot_title = "Froude Number" + id + "\n file:" + \ |
---|
250 | outputdir_name + "_froude" + plot_type |
---|
251 | froude_file = pro_instance.outputdir + "fslope_froude_" + end |
---|
252 | print "Creating ", froude_file |
---|
253 | save_as = pro_instance.plots_dir + sep + \ |
---|
254 | outputdir_name + "_froude" + plot_type |
---|
255 | dtimes, locations, sensors = load_sensors(froude_file) |
---|
256 | dtimes, sensors = get_band(anuga_break_times[0]-TIME_BORDER, |
---|
257 | 100, dtimes, sensors, 0) |
---|
258 | locations, sensors = get_band( |
---|
259 | min(run_data['break_xs'])-LOCATION_BORDER, |
---|
260 | 100, locations, sensors, -1) |
---|
261 | #print "dtimes", dtimes |
---|
262 | #print "sensors", sensors |
---|
263 | #times, slope_locations, slopes = load_slopes(stage_file) |
---|
264 | graph_froude(dtimes, locations, sensors, |
---|
265 | plot_title=plot_title, |
---|
266 | break_xs=run_data['break_xs'], |
---|
267 | break_times=anuga_break_times, |
---|
268 | save_as=save_as, |
---|
269 | is_interactive=is_interactive) |
---|
270 | |
---|
271 | def find_froude(times_froude, locations_froude, froudes_array, |
---|
272 | times, locations): |
---|
273 | """ |
---|
274 | interpolate across location to find froude number values |
---|
275 | """ |
---|
276 | |
---|
277 | if len(times) == 0: |
---|
278 | return [] |
---|
279 | time_indexes = searchsorted(times_froude, times) |
---|
280 | location_indexes = searchsorted(locations_froude, locations) |
---|
281 | |
---|
282 | |
---|
283 | assert len(time_indexes) == len(location_indexes) |
---|
284 | |
---|
285 | froudes = [] |
---|
286 | for time_i, loc_i, time, location in map(None, time_indexes, |
---|
287 | location_indexes, |
---|
288 | times, locations): |
---|
289 | # the time values should be the same |
---|
290 | assert times_froude[time_i] == time |
---|
291 | |
---|
292 | # The distance value should be half way between the froude locations |
---|
293 | midpoint = locations_froude[loc_i-1] + \ |
---|
294 | (locations_froude[loc_i]-locations_froude[loc_i-1])*0.5 |
---|
295 | #print "location", location |
---|
296 | #print "midpoint", midpoint |
---|
297 | assert location == midpoint |
---|
298 | froude = froudes_array[time_i, loc_i-1] + \ |
---|
299 | (froudes_array[time_i, loc_i]- \ |
---|
300 | froudes_array[time_i, loc_i-1])*0.5 |
---|
301 | froudes.append(froude) |
---|
302 | |
---|
303 | return froudes |
---|
304 | |
---|
305 | def auto_find_min_slopes(slope_tag, outputdir_tag, scenarios): |
---|
306 | """ |
---|
307 | Given stage and froude wrt time and location csv files, |
---|
308 | find the waves and get the froude number and stage slope |
---|
309 | at the wave face. |
---|
310 | |
---|
311 | For each wave write a csv file giving the location, stage slope, time and |
---|
312 | froude number. |
---|
313 | """ |
---|
314 | |
---|
315 | for run_data in scenarios: |
---|
316 | id = run_data['scenario_id'] |
---|
317 | outputdir_name = id + outputdir_tag |
---|
318 | pro_instance = project.Project(['data','flumes','Hinwood_2008'], |
---|
319 | outputdir_name=outputdir_name) |
---|
320 | end = id + ".csv" |
---|
321 | anuga_break_times = [] |
---|
322 | for break_time in run_data['break_times']: |
---|
323 | anuga_break_times.append( \ |
---|
324 | break_time - run_data['ANUGA_start_time']) |
---|
325 | |
---|
326 | stage_file = pro_instance.outputdir + slope_tag + "slope_stage_" + end |
---|
327 | froude_file = pro_instance.outputdir + slope_tag + "slope_froude_" + \ |
---|
328 | end |
---|
329 | |
---|
330 | times, slope_locations, slopes = load_slopes(stage_file) |
---|
331 | #print "slope_locations", slope_locations |
---|
332 | times_froude, locations_froude, froudes_a = load_sensors(froude_file) |
---|
333 | #print "locations_froude", locations_froude |
---|
334 | waves = find_min_slopes(times, slope_locations, slopes, |
---|
335 | anuga_break_times, |
---|
336 | run_data['band_offset']) |
---|
337 | |
---|
338 | # write the wave info here |
---|
339 | # and find the froude values |
---|
340 | for i, wave in enumerate(waves): |
---|
341 | |
---|
342 | id = "wave_" + str(i) |
---|
343 | wave_file = stage_file[:-4] + '_'+ id + ".csv" |
---|
344 | print "wave_file", wave_file |
---|
345 | froudes = find_froude(times_froude, locations_froude, |
---|
346 | froudes_a, wave[TIME_STR], |
---|
347 | slope_locations) |
---|
348 | wave_writer = writer(file(wave_file, "wb")) |
---|
349 | wave_writer.writerow(["x location", "min slope", "Time", "Froude"]) |
---|
350 | wave_writer.writerows(map(None, |
---|
351 | slope_locations, |
---|
352 | wave[SLOPE_STR], |
---|
353 | wave[TIME_STR], |
---|
354 | froudes)) |
---|
355 | |
---|
356 | def calc_wave_file_min_slope_max_froude(slope_tag, outputdir_tag, scenarios): |
---|
357 | """ |
---|
358 | Calc the min slope and max froude number in the wave files |
---|
359 | Used so all graphs have the same axis. |
---|
360 | """ |
---|
361 | min_slope = 0 |
---|
362 | max_froude = 0 |
---|
363 | |
---|
364 | for run_data in scenarios: |
---|
365 | for wave_file, save_as, wave_number in Get_file_name( |
---|
366 | run_data, outputdir_tag, slope_tag): |
---|
367 | simulation, _ = csv2dict(wave_file) |
---|
368 | slope = [float(x) for x in simulation['min slope']] |
---|
369 | froude = [float(x) for x in simulation['Froude']] |
---|
370 | |
---|
371 | min_slope = min(min(slope), min_slope) |
---|
372 | |
---|
373 | max_froude = max(max(froude), max_froude) |
---|
374 | |
---|
375 | |
---|
376 | return min_slope, max_froude |
---|
377 | |
---|
378 | class Get_file_name: |
---|
379 | """ |
---|
380 | Used to make the file names, and workout the wave number. |
---|
381 | """ |
---|
382 | |
---|
383 | def __init__(self, run_data, outputdir_tag, slope_tag): |
---|
384 | |
---|
385 | self.plot_type = ".pdf" |
---|
386 | # The scenario data |
---|
387 | id = run_data['scenario_id'] |
---|
388 | |
---|
389 | self.outputdir_name = id + outputdir_tag |
---|
390 | self.pro_instance = project.Project(['data','flumes','Hinwood_2008'], |
---|
391 | outputdir_name=self.outputdir_name) |
---|
392 | self.wave_number = -1 |
---|
393 | self.max_waves = len(run_data['break_type']) |
---|
394 | self.slope_tag = slope_tag |
---|
395 | self.end = id + ".csv" |
---|
396 | |
---|
397 | def next(self): |
---|
398 | self.wave_number += 1 |
---|
399 | if self.wave_number >= self.max_waves: raise StopIteration |
---|
400 | wave_tag = "wave_" + str(self.wave_number) |
---|
401 | stage_file = self.pro_instance.outputdir + self.slope_tag + \ |
---|
402 | "slope_stage_" + self.end |
---|
403 | wave_file = stage_file[:-4] + '_'+ wave_tag + ".csv" |
---|
404 | save_as = self.pro_instance.plots_dir + sep + \ |
---|
405 | self.outputdir_name + "_" + wave_tag + self.plot_type |
---|
406 | return wave_file, save_as, self.wave_number |
---|
407 | |
---|
408 | def __iter__(self): |
---|
409 | return self |
---|
410 | |
---|
411 | |
---|
412 | |
---|
413 | |
---|
414 | def auto_plot_froude_slopes(slope_tag, outputdir_tag, scenarios): |
---|
415 | """ |
---|
416 | Used to generate all the Froude number, stage slope, time graphs |
---|
417 | of a scenario list |
---|
418 | """ |
---|
419 | |
---|
420 | slope_min, froude_max = calc_wave_file_min_slope_max_froude( |
---|
421 | slope_tag, outputdir_tag, scenarios) |
---|
422 | |
---|
423 | |
---|
424 | for run_data in scenarios: |
---|
425 | assert len(run_data['break_times']) == len(run_data['break_xs']) |
---|
426 | assert len(run_data['break_times']) == len(run_data['break_type']) |
---|
427 | |
---|
428 | anuga_break_times = [] |
---|
429 | for break_time in run_data['break_times']: |
---|
430 | anuga_break_times.append( \ |
---|
431 | break_time - run_data['ANUGA_start_time']) |
---|
432 | |
---|
433 | for wave_file, save_as, wave_number in Get_file_name( |
---|
434 | run_data, outputdir_tag, slope_tag): |
---|
435 | print "wave_file", wave_file |
---|
436 | break_type = run_data['break_type'][wave_number] |
---|
437 | plot_title = run_data['scenario_id'] + \ |
---|
438 | ' Wave: ' + str(wave_number) + \ |
---|
439 | ' Break Type: ' + break_type + '\n' + \ |
---|
440 | 'File: ' + wave_file[34:] # not good! |
---|
441 | plot_foude_slope_stage(wave_file, |
---|
442 | anuga_break_times[wave_number], |
---|
443 | run_data['break_xs'][wave_number], |
---|
444 | plot_title=plot_title, |
---|
445 | break_type=break_type, |
---|
446 | save_as=save_as, |
---|
447 | is_interactive=False, |
---|
448 | froude_min=0, |
---|
449 | froude_max=froude_max, |
---|
450 | slope_min=slope_min, |
---|
451 | slope_max=0) |
---|
452 | |
---|
453 | |
---|
454 | |
---|
455 | def gauges_for_slope(slope_tag, outputdir_tag, scenarios): |
---|
456 | """ |
---|
457 | This is used to create a stage file, using gauges relivent to |
---|
458 | finding a slope. |
---|
459 | |
---|
460 | It also create's a frounde file. |
---|
461 | """ |
---|
462 | dx = 0.005 |
---|
463 | for run_data in scenarios: |
---|
464 | point_x = arange(run_data['start_slope_x'], |
---|
465 | run_data['finish_slope_x'], |
---|
466 | dx).tolist() |
---|
467 | flume_y_middle = 0.0 |
---|
468 | points = [] |
---|
469 | for gauge_x in point_x: |
---|
470 | points.append([gauge_x, flume_y_middle]) |
---|
471 | id = run_data['scenario_id'] |
---|
472 | |
---|
473 | basename = 'zz_' + run_data['scenario_id'] |
---|
474 | outputdir_name = id + outputdir_tag |
---|
475 | pro_instance = project.Project(['data','flumes','Hinwood_2008'], |
---|
476 | outputdir_name=outputdir_name) |
---|
477 | end = id + ".csv" |
---|
478 | interpolate_sww2csv( \ |
---|
479 | pro_instance.outputdir + basename +".sww", |
---|
480 | points, |
---|
481 | pro_instance.outputdir + slope_tag + "slope_depth_" + end, |
---|
482 | pro_instance.outputdir + slope_tag + "slope_velocity_x_" + end, |
---|
483 | pro_instance.outputdir + slope_tag + "slope_velocity_y_" + end, |
---|
484 | pro_instance.outputdir + slope_tag + "slope_stage_" + end, |
---|
485 | pro_instance.outputdir + slope_tag + "slope_froude_" + end, |
---|
486 | time_thinning=1) |
---|
487 | |
---|
488 | def find_min_slopes(times, slope_locations, slopes, |
---|
489 | anuga_break_times, band_offset): |
---|
490 | """ |
---|
491 | |
---|
492 | """ |
---|
493 | bands = break_times2bands(anuga_break_times, band_offset) |
---|
494 | |
---|
495 | waves = [] |
---|
496 | for i,_ in enumerate(bands[0:-1]): |
---|
497 | max_q, max_q_times = get_min_in_band(bands[i], bands[i+1], |
---|
498 | times, slopes) |
---|
499 | waves.append({SLOPE_STR:max_q, TIME_STR:max_q_times}) |
---|
500 | return waves |
---|
501 | |
---|
502 | |
---|
503 | def get_band(min, max, vector, quantity_array, axis): |
---|
504 | """ |
---|
505 | Return a band of vector and quantity, within (not including) the |
---|
506 | min, max. |
---|
507 | |
---|
508 | For a time band, set the axis to 0. |
---|
509 | For a location band, set the axis to -1. |
---|
510 | |
---|
511 | """ |
---|
512 | |
---|
513 | SMALL_MIN = -1e10 # Not that small, but small enough |
---|
514 | vector = ensure_numeric(vector) |
---|
515 | quantity_array = ensure_numeric(quantity_array) |
---|
516 | |
---|
517 | assert min > SMALL_MIN |
---|
518 | no_maxs = where(less(vector,max), vector, SMALL_MIN) |
---|
519 | #print "no_maxs", no_maxs |
---|
520 | band_condition = greater(no_maxs, min) |
---|
521 | band_vector = compress(band_condition, vector, axis=axis) |
---|
522 | #print "band_time", band_time |
---|
523 | #print "quantity_array", quantity_array.shape |
---|
524 | band_quantity = compress(band_condition, quantity_array, axis=axis) |
---|
525 | return band_vector, band_quantity |
---|
526 | |
---|
527 | def get_min_in_band(min_time, max_time, time_vector, quantity_array): |
---|
528 | """ |
---|
529 | given a quantity array, with the 2nd axis being |
---|
530 | time, represented by the time_vector, find the minimum within |
---|
531 | the time band. |
---|
532 | |
---|
533 | Assumes times are positive |
---|
534 | """ |
---|
535 | |
---|
536 | time_vector = ensure_numeric(time_vector) |
---|
537 | quantity_array = ensure_numeric(quantity_array) |
---|
538 | |
---|
539 | band_time, band_quantity = get_band(min_time, max_time, |
---|
540 | time_vector, quantity_array, 0) |
---|
541 | #print "band_quantity",band_quantity |
---|
542 | try: |
---|
543 | max_quantity_indices = argmin(band_quantity, axis=0) |
---|
544 | except: |
---|
545 | #print "time_vector", time_vector |
---|
546 | print "min_time",min_time |
---|
547 | print "max_time", max_time |
---|
548 | return [],[] |
---|
549 | |
---|
550 | #print "max_quantity_indices", max_quantity_indices |
---|
551 | max_quantity_times = choose(max_quantity_indices, band_time) |
---|
552 | #print "max_quantity_times", max_quantity_times |
---|
553 | max_quantities = choose(max_quantity_indices, band_quantity) |
---|
554 | #print "max_quantities", max_quantities |
---|
555 | |
---|
556 | return max_quantities, max_quantity_times |
---|
557 | |
---|
558 | def break_times2bands(break_times, band_offset): |
---|
559 | """ |
---|
560 | Break_times is a list of times, ascending. |
---|
561 | bands is a list of times, being the midpoints of break_times, with a min |
---|
562 | and max band added. |
---|
563 | """ |
---|
564 | assert len(break_times)>2 |
---|
565 | |
---|
566 | bands = [] #deepcopy(break_times) |
---|
567 | bands.append(break_times[0]-0.5*(break_times[1]-break_times[0])) |
---|
568 | |
---|
569 | |
---|
570 | for i,break_x in enumerate(break_times[0:-1]): |
---|
571 | bands.append(break_times[i]+0.5*(break_times[i+1]-break_times[i])) |
---|
572 | |
---|
573 | bands.append(break_times[-1]+0.5*(break_times[-1]-break_times[-2])) |
---|
574 | bands = ensure_numeric(bands) |
---|
575 | bands += band_offset |
---|
576 | return bands |
---|
577 | |
---|
578 | def plot_foude_slope_stage(wave_file, |
---|
579 | break_time, |
---|
580 | break_x, |
---|
581 | save_as=None, |
---|
582 | plot_title="", |
---|
583 | is_interactive=False, |
---|
584 | break_type="", |
---|
585 | froude_min=None, |
---|
586 | froude_max=None, |
---|
587 | slope_min=None, |
---|
588 | slope_max=None): |
---|
589 | """ |
---|
590 | """ |
---|
591 | from pylab import ion, plot, xlabel, ylabel, close, legend, \ |
---|
592 | savefig, title, axis, setp, subplot, grid, axvspan |
---|
593 | from anuga.shallow_water.data_manager import csv2dict |
---|
594 | |
---|
595 | |
---|
596 | |
---|
597 | # Load in the csv files and convert info from strings to floats |
---|
598 | simulation, _ = csv2dict(wave_file) |
---|
599 | location = [float(x) for x in simulation['x location']] |
---|
600 | slope = [float(x) for x in simulation['min slope']] |
---|
601 | time = [float(x) for x in simulation['Time']] |
---|
602 | froude = [float(x) for x in simulation['Froude']] |
---|
603 | |
---|
604 | min_location = min(location) |
---|
605 | max_location = max(location) |
---|
606 | |
---|
607 | if is_interactive: |
---|
608 | ion() |
---|
609 | # The upper subplot |
---|
610 | subplot(311) |
---|
611 | l_froude = plot(location, froude) |
---|
612 | #setp(l_froude, color='r') |
---|
613 | |
---|
614 | # Add axis stuff |
---|
615 | title(plot_title) |
---|
616 | y_label = "Froude Number" |
---|
617 | ylabel(y_label) |
---|
618 | grid(True) |
---|
619 | axvspan(break_x-0.001,break_x+0.001, facecolor='g') |
---|
620 | if froude_min is not None and froude_max is not None: |
---|
621 | axis(ymin=froude_min, ymax=froude_max) |
---|
622 | |
---|
623 | # The slope subplot |
---|
624 | subplot(312) |
---|
625 | l_slope = plot(location, slope) |
---|
626 | setp(l_slope, color='r') |
---|
627 | |
---|
628 | # Add axis stuff and legend |
---|
629 | x_label = "X location, m" |
---|
630 | y_label = "Stage slope" |
---|
631 | #xlabel(x_label) |
---|
632 | ylabel(y_label) |
---|
633 | grid(True) |
---|
634 | axvspan(break_x-0.001,break_x+0.001, facecolor='g') |
---|
635 | if slope_min is not None and slope_max is not None: |
---|
636 | axis(ymin=slope_min, ymax=slope_max ) |
---|
637 | |
---|
638 | # The time, x location subplot |
---|
639 | subplot(313) |
---|
640 | l_time = plot(location, time) |
---|
641 | setp(l_time, color='g') |
---|
642 | #print "break_x", break_x |
---|
643 | #print "break_time", break_time |
---|
644 | plot([break_x], [break_time], 'yo') |
---|
645 | #plot([break_x-1], [], 'yo') |
---|
646 | |
---|
647 | # Add axis stuff and legend |
---|
648 | x_label = "X location, m" |
---|
649 | y_label = "time, sec" |
---|
650 | xlabel(x_label) |
---|
651 | ylabel(y_label) |
---|
652 | grid(True) |
---|
653 | |
---|
654 | |
---|
655 | # The order defines the label |
---|
656 | #legend((legend_exp, legend_sim),'upper left') |
---|
657 | #legend(('Wave front'),'upper left') |
---|
658 | |
---|
659 | if is_interactive: |
---|
660 | # Wait for enter pressed |
---|
661 | raw_input() |
---|
662 | |
---|
663 | if save_as is not None: |
---|
664 | savefig(save_as) |
---|
665 | |
---|
666 | #Need to close this plot |
---|
667 | close() |
---|
668 | |
---|
669 | #------------------------------------------------------------- |
---|
670 | if __name__ == "__main__": |
---|
671 | """ |
---|
672 | """ |
---|
673 | from scenarios import scenarios |
---|
674 | #scenarios = [scenarios[0]] |
---|
675 | outputdir_tag = "_good_tri_area_0.01_limiterD" |
---|
676 | outputdir_tag = "_good_lmts_wdth_0.1_z_0.012_ys_0.01_mta_0.0001_F" |
---|
677 | slope_tag = "" |
---|
678 | #outputdir_tag = "_test_limiterC" |
---|
679 | scenarios = [scenarios[4]] # !!!!!!!!!!!!!!!!!!!!!! |
---|
680 | #scenarios = scenarios[4:] # !!!!!!!!!!!!!!!!!!!!!! |
---|
681 | |
---|
682 | #gauges_for_slope(slope_tag, outputdir_tag, scenarios) |
---|
683 | #auto_graph_slopes(outputdir_tag, scenarios) #, is_interactive=True) |
---|
684 | #auto_find_min_slopes(slope_tag, outputdir_tag, scenarios) |
---|
685 | #auto_graph_froudes(outputdir_tag, scenarios) |
---|
686 | auto_plot_froude_slopes(slope_tag, outputdir_tag, scenarios) |
---|
687 | #g = Get_file_name(scenarios[0], outputdir_tag, slope_tag) |
---|
688 | #for wave_file, save_as, wave_number in Get_file_name( |
---|
689 | # scenarios[0], outputdir_tag, slope_tag): |
---|
690 | # print "**************" |
---|