1 | """ |
---|
2 | |
---|
3 | Script for running a breaking wave simulation of Jon Hinwoods wave tank. |
---|
4 | Note: this is based on the frinction_ua_flume_2006 structure. |
---|
5 | |
---|
6 | |
---|
7 | Duncan Gray, GA - 2007 |
---|
8 | |
---|
9 | |
---|
10 | |
---|
11 | """ |
---|
12 | |
---|
13 | |
---|
14 | #---------------------------------------------------------------------------- |
---|
15 | # Import necessary modules |
---|
16 | #---------------------------------------------------------------------------- |
---|
17 | |
---|
18 | from Scientific.IO.NetCDF import NetCDFFile |
---|
19 | from Numeric import array, zeros, Float |
---|
20 | |
---|
21 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
22 | |
---|
23 | from interp import interp |
---|
24 | |
---|
25 | def prepare_time_boundary(filename): |
---|
26 | """Convert benchmark 2 time series to NetCDF tms file. |
---|
27 | This is a 'throw-away' code taylor made for files like |
---|
28 | 'Benchmark_2_input.txt' from the LWRU2 benchmark |
---|
29 | """ |
---|
30 | |
---|
31 | |
---|
32 | |
---|
33 | print 'Creating', filename |
---|
34 | |
---|
35 | # Read the ascii (.csv) version of this file |
---|
36 | fid = open(filename[:-4] + '.csv') |
---|
37 | |
---|
38 | # Skip first line |
---|
39 | line = fid.readline() |
---|
40 | |
---|
41 | # Read remaining lines |
---|
42 | lines = fid.readlines() |
---|
43 | fid.close() |
---|
44 | |
---|
45 | |
---|
46 | N = len(lines) |
---|
47 | T = zeros(N, Float) #Time |
---|
48 | Q = zeros(N, Float) #Stage |
---|
49 | X = zeros(N, Float) #XMomentum |
---|
50 | Y = zeros(N, Float) #YMomentum |
---|
51 | |
---|
52 | for i, line in enumerate(lines): |
---|
53 | fields = line.split(',') |
---|
54 | |
---|
55 | T[i] = float(fields[0]) |
---|
56 | Q[i] = depth = float(fields[1]) |
---|
57 | X[i] = float(fields[2]) * depth |
---|
58 | try: |
---|
59 | Y[i] = float(fields[3]) * depth |
---|
60 | except: |
---|
61 | pass |
---|
62 | |
---|
63 | |
---|
64 | # Create tms NetCDF file |
---|
65 | |
---|
66 | fid = NetCDFFile(filename, 'w') |
---|
67 | fid.institution = 'Geoscience Australia' |
---|
68 | fid.description = 'Input wave for Benchmark 2' |
---|
69 | fid.starttime = 0.0 |
---|
70 | fid.createDimension('number_of_timesteps', len(T)) |
---|
71 | fid.createVariable('time', Float, ('number_of_timesteps',)) |
---|
72 | fid.variables['time'][:] = T |
---|
73 | |
---|
74 | fid.createVariable('stage', Float, ('number_of_timesteps',)) |
---|
75 | fid.variables['stage'][:] = Q[:] |
---|
76 | |
---|
77 | fid.createVariable('xmomentum', Float, ('number_of_timesteps',)) |
---|
78 | fid.variables['xmomentum'][:] = X[:] |
---|
79 | |
---|
80 | fid.createVariable('ymomentum', Float, ('number_of_timesteps',)) |
---|
81 | fid.variables['ymomentum'][:] = Y[:] |
---|
82 | |
---|
83 | fid.close() |
---|
84 | |
---|
85 | |
---|
86 | def combine_velocity_depth(velocity_file, depth_file, out_file): |
---|
87 | """ |
---|
88 | |
---|
89 | Convert the rawish velocity and depth values, which have values at |
---|
90 | different times to a csv file, with values at the same time, with |
---|
91 | SI units. |
---|
92 | |
---|
93 | The format for the velocity file is; |
---|
94 | [time, sec], [x-velocity +ve is towards the wave generator, m/sec], |
---|
95 | [y-velocity], [z-velocity] |
---|
96 | |
---|
97 | The format for the pressure file is |
---|
98 | [time, sec], [mm above SWL for sensor A], many other sensors... |
---|
99 | """ |
---|
100 | |
---|
101 | # Read velocity file |
---|
102 | vfid = open(velocity_file) |
---|
103 | lines = vfid.readlines() |
---|
104 | vfid.close() |
---|
105 | |
---|
106 | |
---|
107 | n_velocity = len(lines) |
---|
108 | vtimes = zeros(n_velocity, Float) #Time |
---|
109 | velocities = zeros(n_velocity, Float) # |
---|
110 | |
---|
111 | for i, line in enumerate(lines): |
---|
112 | fields = line.split(',') |
---|
113 | |
---|
114 | vtimes[i] = float(fields[0]) |
---|
115 | velocities[i] = float(fields[1]) |
---|
116 | |
---|
117 | # Read the depth file |
---|
118 | dfid = open(depth_file) |
---|
119 | lines = dfid.readlines() |
---|
120 | dfid.close() |
---|
121 | |
---|
122 | |
---|
123 | n_depth = len(lines) |
---|
124 | dtimes = zeros(n_depth, Float) #Time |
---|
125 | depths = zeros(n_depth, Float) # |
---|
126 | |
---|
127 | for i, line in enumerate(lines): |
---|
128 | fields = line.split(',') |
---|
129 | |
---|
130 | dtimes[i] = float(fields[0]) |
---|
131 | depths[i] = float(fields[1]) |
---|
132 | |
---|
133 | depths_at_vtimes = interp(dtimes, depths, vtimes, missing=1e+20) |
---|
134 | depths_at_vtimes = ensure_numeric(depths_at_vtimes) |
---|
135 | depths_at_vtimes = depths_at_vtimes/1000.00 # convert from mm to m |
---|
136 | velocities = ensure_numeric(velocities) |
---|
137 | velocities = velocities * -1.0 # Swap axis around |
---|
138 | |
---|
139 | fid = open(out_file,'w') |
---|
140 | |
---|
141 | assert len(depths_at_vtimes) == len(vtimes) |
---|
142 | |
---|
143 | #for vtime, depth_at_vtime, velocity in map(vtimes, depths_at_vtimes, |
---|
144 | # velocities): |
---|
145 | for i in xrange(len(vtimes)): |
---|
146 | fid.write(str(vtimes[i]) + ',' + str(depths_at_vtimes[i]) \ |
---|
147 | + ',' + str(velocities[i])+'\n') |
---|
148 | |
---|
149 | fid.close() |
---|
150 | |
---|
151 | |
---|
152 | #------------------------------------------------------------------- |
---|
153 | if __name__ == "__main__": |
---|
154 | combine_velocity_depth('T2R7velfilt.csv','T2R7pressfilt.csv', 'cyeah.csv') |
---|
155 | |
---|