source: inundation/examples/island_timeseries.py @ 2620

Last change on this file since 2620 was 2620, checked in by ole, 18 years ago

Worked on island.py example and it's gauges.
Renamed mesh_file to mesh_filename

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