source: production/onslow_2006/get_timeseries.py @ 2773

Last change on this file since 2773 was 2773, checked in by nick, 19 years ago

changes to onslow

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