[2313] | 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 pyvolution.coordinate_transforms.redfearn import degminsec2decimal_degrees, redfearn |
---|
| 8 | from pylab import * |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | |
---|
| 12 | #swwfile = project.newoutputname + '.sww' |
---|
| 13 | swwfile = project.outputname + '.sww' |
---|
| 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, linesfs |
---|
| 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 | #Read model output |
---|
| 44 | quantities = ['stage', 'elevation', 'xmomentum', 'ymomentum'] |
---|
| 45 | f = file_function(swwfile, |
---|
| 46 | quantities = quantities, |
---|
| 47 | interpolation_points = gauges, |
---|
| 48 | verbose = True, |
---|
| 49 | use_cache = True) |
---|
| 50 | |
---|
| 51 | |
---|
| 52 | |
---|
| 53 | from math import sqrt |
---|
| 54 | N = len(gauges) |
---|
| 55 | for k, g in enumerate(gauges): |
---|
| 56 | if k%((N+10)/10)==0: # diagnostics - print 10 lines |
---|
| 57 | print 'Doing row %d of %d' %(k, N) |
---|
| 58 | |
---|
| 59 | model_time = [] |
---|
| 60 | stages = [] |
---|
| 61 | elevations = [] |
---|
| 62 | momenta = [] |
---|
| 63 | |
---|
| 64 | max_depth = 0 |
---|
| 65 | max_momentum = 0 |
---|
| 66 | for i, t in enumerate(f.T): # T is a list of times |
---|
| 67 | #if tmin < t < tmax: |
---|
| 68 | w = f(t, point_id = k)[0] |
---|
| 69 | z = f(t, point_id = k)[1] |
---|
| 70 | uh = f(t, point_id = k)[2] |
---|
| 71 | vh = f(t, point_id = k)[3] |
---|
| 72 | #myloc = locations[k] |
---|
| 73 | |
---|
| 74 | m = sqrt(uh*uh + vh*vh) #Absolute momentum |
---|
| 75 | |
---|
| 76 | model_time.append(t) |
---|
| 77 | stages.append(w) |
---|
| 78 | elevations.append(z) #Should be constant over time |
---|
| 79 | momenta.append(m) |
---|
| 80 | |
---|
| 81 | if w-z > max_depth: |
---|
| 82 | max_depth = w-z |
---|
| 83 | if m > max_momentum: |
---|
| 84 | max_momentum = m |
---|
| 85 | |
---|
| 86 | |
---|
| 87 | |
---|
| 88 | #Plot only those gauges that have been inundated by more than a threshold |
---|
| 89 | #if max_depth < 0.2: |
---|
| 90 | # print 'Skipping gauge %d' %k |
---|
| 91 | # continue |
---|
| 92 | |
---|
| 93 | ion() |
---|
| 94 | hold(False) |
---|
| 95 | |
---|
| 96 | if elevations[0] < -10: |
---|
| 97 | plot(model_time, stages, '-b') |
---|
| 98 | else: |
---|
| 99 | plot(model_time, stages, '-b', |
---|
| 100 | model_time, elevations, '-k') |
---|
| 101 | name = 'Gauge_%d: (%.1f, %.1f)' %(k, g[0], g[1]) |
---|
| 102 | #name = 'Gauge_%d: (%.1f, %.1f) Location: %s' %(k, g[0], g[1], myloc) |
---|
| 103 | title(name) |
---|
| 104 | |
---|
| 105 | title('%s (stage)' %name) |
---|
| 106 | xlabel('time [s]') |
---|
| 107 | ylabel('elevation [m]') |
---|
| 108 | legend(('Stage', 'Bed = %.1f' %elevations[0]), |
---|
| 109 | shadow=True, |
---|
| 110 | loc='upper right') |
---|
| 111 | savefig('Gauge_%d_stage' %k) # savefig('Gauge_%s_stage' %myloc) |
---|
| 112 | |
---|
| 113 | raw_input('Next') |
---|
| 114 | |
---|
| 115 | |
---|
| 116 | #Momentum plot |
---|
| 117 | ion() |
---|
| 118 | hold(False) |
---|
| 119 | plot(model_time, momenta, '-r') |
---|
| 120 | title(name) |
---|
| 121 | |
---|
| 122 | title('%s (momentum)' %name) |
---|
| 123 | xlabel('time [s]') |
---|
| 124 | ylabel('sqrt( uh^2 + vh^2 ) [m^2/s]') |
---|
| 125 | savefig('Gauge_%d_momentum' %k) |
---|
| 126 | #savefig('Gauge_%s_momentum' %myloc) |
---|
| 127 | |
---|
| 128 | raw_input('Next') |
---|
| 129 | |
---|
| 130 | |
---|
| 131 | show() |
---|
| 132 | |
---|