source: inundation/examples/island_timeseries.py @ 2632

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

Work on creep demo

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