1 | """Read in tms file, interpolate at specified locations (ch 5,7,9) and compare |
---|
2 | """ |
---|
3 | |
---|
4 | import sys |
---|
5 | from os import sep |
---|
6 | from caching import cache |
---|
7 | import project |
---|
8 | |
---|
9 | sys.path.append('..'+sep+'..'+sep) |
---|
10 | |
---|
11 | |
---|
12 | gauges = [[0.000, 1.696]] #Boundary |
---|
13 | gauges += [[4.521, 1.196], [4.521, 1.696], [4.521, 2.196]] #Ch 5-7-9 |
---|
14 | |
---|
15 | gauge_names = ['Boundary', 'ch5', 'ch7', 'ch9'] |
---|
16 | |
---|
17 | |
---|
18 | finaltime = 22.5 |
---|
19 | timestep = 0.05 |
---|
20 | |
---|
21 | #Read reference data |
---|
22 | |
---|
23 | #Input wave |
---|
24 | filename = 'input_wave.tms' |
---|
25 | print 'Reading', filename |
---|
26 | from Scientific.IO.NetCDF import NetCDFFile |
---|
27 | from Numeric import allclose |
---|
28 | fid = NetCDFFile(filename, 'r')# |
---|
29 | |
---|
30 | input_time = fid.variables['time'][:] |
---|
31 | input_stage = fid.variables['stage'][:] |
---|
32 | |
---|
33 | |
---|
34 | #gauges |
---|
35 | reference_time = [] |
---|
36 | ch5 = [] |
---|
37 | ch7 = [] |
---|
38 | ch9 = [] |
---|
39 | filename = 'output_ch5-7-9.txt' |
---|
40 | fid = open(filename) |
---|
41 | lines = fid.readlines() |
---|
42 | fid.close() |
---|
43 | for i, line in enumerate(lines[1:]): |
---|
44 | if i == len(input_time): break |
---|
45 | |
---|
46 | fields = line.split() |
---|
47 | |
---|
48 | reference_time.append(float(fields[0])) |
---|
49 | ch5.append(float(fields[1])/100) #cm2m |
---|
50 | ch7.append(float(fields[2])/100) #cm2m |
---|
51 | ch9.append(float(fields[3])/100) #cm2m |
---|
52 | |
---|
53 | |
---|
54 | from pyvolution.util import file_function |
---|
55 | from utilities.numerical_tools import ensure_numeric |
---|
56 | gauge_values = [ensure_numeric(input_stage), |
---|
57 | ensure_numeric(ch5), |
---|
58 | ensure_numeric(ch7), |
---|
59 | ensure_numeric(ch9)] #Reference values |
---|
60 | |
---|
61 | |
---|
62 | |
---|
63 | #Read model output |
---|
64 | filename = project.basename + '.sww' |
---|
65 | |
---|
66 | f = cache(file_function, filename, |
---|
67 | {'quantities': 'stage', |
---|
68 | 'interpolation_points': gauges, |
---|
69 | 'verbose': True}, |
---|
70 | #evaluate = True, |
---|
71 | dependencies = [filename], |
---|
72 | verbose = True) |
---|
73 | |
---|
74 | |
---|
75 | |
---|
76 | |
---|
77 | #Checks |
---|
78 | #print reference_time |
---|
79 | #print input_time |
---|
80 | assert reference_time[0] == 0.0 |
---|
81 | assert reference_time[-1] == finaltime |
---|
82 | assert allclose(reference_time, input_time) |
---|
83 | |
---|
84 | |
---|
85 | |
---|
86 | #Validation |
---|
87 | |
---|
88 | |
---|
89 | for k, name in enumerate(gauge_names): |
---|
90 | sqsum = 0 |
---|
91 | denom = 0 |
---|
92 | model = [] |
---|
93 | print 'Validating ' + name |
---|
94 | for i, t in enumerate(reference_time): |
---|
95 | ref = gauge_values[k][i] |
---|
96 | val = f(t, point_id = k)[0] |
---|
97 | model.append(val) |
---|
98 | |
---|
99 | sqsum += (ref - val)**2 |
---|
100 | denom += ref**2 |
---|
101 | |
---|
102 | print sqsum |
---|
103 | print sqsum/denom |
---|
104 | |
---|
105 | from pylab import * |
---|
106 | ion() |
---|
107 | hold(False) |
---|
108 | plot(reference_time, gauge_values[k], 'r.-', |
---|
109 | reference_time, model, 'k-') |
---|
110 | title('Gauge %s' %name) |
---|
111 | xlabel('time(s)') |
---|
112 | ylabel('stage (m)') |
---|
113 | legend(('Observed', 'Modelled'), shadow=True, loc='upper left') |
---|
114 | savefig(name, dpi = 300) |
---|
115 | |
---|
116 | raw_input('Next') |
---|
117 | show() |
---|
118 | |
---|
119 | |
---|
120 | |
---|
121 | #from pylab import * |
---|
122 | #plot(time, stage) |
---|
123 | #show() |
---|