source: production/sydney_2006/get_timeseries.py @ 3171

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

Updated production codes in regard to replacing f.T with f.get_time()

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