source: anuga_work/development/Hinwood_2008/prepare_time_boundary.py @ 5390

Last change on this file since 5390 was 5370, checked in by duncan, 17 years ago

Hinwood scenario

File size: 6.4 KB
Line 
1"""
2
3Script for running a breaking wave simulation of Jon Hinwoods wave tank.
4Note: this is based on the frinction_ua_flume_2006 structure.
5
6
7Duncan Gray, GA - 2007
8
9
10
11"""
12
13
14#----------------------------------------------------------------------------
15# Import necessary modules
16#----------------------------------------------------------------------------
17
18from Scientific.IO.NetCDF import NetCDFFile
19from Numeric import array, zeros, Float
20 
21from anuga.utilities.numerical_tools import ensure_numeric
22
23from interp import interp
24
25def prepare_time_boundary(filename):
26    """Convert benchmark 2 time series to NetCDF tms file.
27   
28    """
29
30
31
32    print 'Creating', filename
33
34    # Read the ascii (.csv) version of this file
35    fid = open(filename[:-4] + '.csv')
36
37    # Read remaining lines
38    lines = fid.readlines()
39    fid.close()
40
41
42    N = len(lines)
43    T = zeros(N, Float)  #Time
44    Q = zeros(N, Float)  #Stage
45    X = zeros(N, Float)  #XMomentum
46    Y = zeros(N, Float)  #YMomentum
47
48    for i, line in enumerate(lines):
49        fields = line.split(',')
50
51        T[i] = float(fields[0])
52        Q[i] = depth = float(fields[1])
53        X[i] = float(fields[2]) * depth
54        try:
55            Y[i] = float(fields[3]) * depth
56        except:
57            pass
58
59
60   
61    # Create tms NetCDF file
62    fid = NetCDFFile(filename, 'w')
63    fid.institution = 'Geoscience Australia'
64    fid.description = 'Input wave for Benchmark 2'
65    fid.starttime = 0.0
66    fid.createDimension('number_of_timesteps', len(T))
67    fid.createVariable('time', Float, ('number_of_timesteps',))
68    fid.variables['time'][:] = T
69
70    fid.createVariable('stage', Float, ('number_of_timesteps',))
71    fid.variables['stage'][:] = Q[:]
72
73    fid.createVariable('xmomentum', Float, ('number_of_timesteps',))
74    fid.variables['xmomentum'][:] =  X[:]
75
76    fid.createVariable('ymomentum', Float, ('number_of_timesteps',))
77    fid.variables['ymomentum'][:] =  Y[:]
78
79    fid.close()
80
81
82def combine_velocity_depth(velocity_file, depth_file, out_file):
83    """
84   
85    Convert the raw velocity and depth values, which have values at
86    different times to a csv file, with values at the same time, with
87    SI units.
88
89    Set the depth values to be at the same times as the velocity values.
90
91    The format for the velocity file is;
92    [time, sec], [x-velocity +ve is towards the wave generator, m/sec],
93    [y-velocity], [z-velocity]
94
95    The format for the pressure file is
96    [time, sec], [mm above SWL for sensor A], many other sensors...
97    """
98    missing = 1e+20
99   
100    # Read velocity file
101    vfid = open(velocity_file)
102    lines = vfid.readlines()
103    vfid.close()
104
105   
106    n_velocity = len(lines)
107    vtimes = zeros(n_velocity, Float)  #Time
108    x_velocities = zeros(n_velocity, Float)  #
109    y_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        x_velocities[i] = float(fields[1])
116        y_velocities[i] = float(fields[2])
117
118    # Read the depth file
119    dfid = open(depth_file)
120    lines = dfid.readlines()
121    dfid.close()
122
123   
124    n_depth = len(lines)
125    n_sensors = len(lines[0].split())
126    dtimes = zeros(n_depth, Float)  #Time
127    depths = zeros(n_depth, Float)  #
128    sensors = zeros((n_depth,n_sensors), Float)
129   
130    for i, line in enumerate(lines):
131        fields = line.split() #(',')
132        fields = [float(j) for j in fields]
133        dtimes[i] = fields[0]
134        depths[i] = fields[1]
135        sensors[i] = fields
136    #print "dtimes", dtimes
137    #print "depths", depths
138    #print "vtimes", vtimes
139    depths_at_vtimes = interp( depths, dtimes,
140                               vtimes, missing=missing)
141    depths_at_vtimes = ensure_numeric(depths_at_vtimes)
142
143    #print "len(dtimes)", len(vtimes)
144    #print "len(depths_at_vtimes)", len(depths_at_vtimes)
145#     for i in range(len(depths_at_vtimes)):
146#         print "i", i
147#         print "vtimes[i]", vtimes[i]
148#         print "depths_at_vtimes[i]", depths_at_vtimes[i]
149#     print "depths_at_vtimes",  depths_at_vtimes
150    depths_at_vtimes = depths_at_vtimes/1000.00 # convert from mm to m
151    missing=missing/1000.00 # Do to missing what is done to depths_at_vtimes
152    x_velocities = ensure_numeric(x_velocities)
153    x_velocities = x_velocities * -1.0 # Swap axis around
154    y_velocities = ensure_numeric(y_velocities)
155    y_velocities = y_velocities * -1.0 # Swap axis around
156   
157    fid = open(out_file,'w')
158
159    assert len(depths_at_vtimes) == len(vtimes)
160    start_time = None
161    #start_time = 0.0
162    #for vtime, depth_at_vtime, velocity in map(vtimes, depths_at_vtimes,
163    #                                           velocities):
164    for i in xrange(len(vtimes)):
165        if not depths_at_vtimes[i] == missing:
166            # Make the times start at zero.
167            if start_time is None:
168                start_time = vtimes[i]           
169            fid.write(str(vtimes[i]-start_time) \
170                      + ',' + str(depths_at_vtimes[i]) \
171                      + ',' + str(x_velocities[i]) \
172                      + ',' + str(y_velocities[i])+'\n')
173
174    fid.close()
175   
176    # Since there is a new time reference save the depth info using this
177    # new reference.
178    fid = open(depth_file[:-4] + '_new_time'+depth_file[-4:],'w')
179    sensors[:,0] -= start_time
180    #print "depth_file", depth_file
181    #print "start_time", start_time
182    for i in xrange(len(dtimes)):
183        fid.write(str(sensors[i,0]))
184        for j in xrange(1,len(sensors[0])):
185            fid.write(' ' + str(sensors[i,j]))
186        fid.write('\n')
187    fid.close()
188
189   
190   
191#-------------------------------------------------------------------
192if __name__ == "__main__":
193    from os import getenv
194    from os.path import join
195    home = getenv('INUNDATIONHOME')
196    Hinwood_dir = join(home,'data','flumes','Hinwood_2008')
197    raw_data_dir = join(Hinwood_dir, 'raw_data')
198
199    # Test 1 Run 5
200    combine_velocity_depth(join(raw_data_dir,'T1R5velfilt.txt'),
201                           join(raw_data_dir,'T1R5pressfilt.txt'),
202                           join(Hinwood_dir, 'T1R5_boundary.csv'))   
203    # Create the tsm file
204    prepare_time_boundary(join(Hinwood_dir, 'T1R5_boundary.tsm'))
205   
206    # Test 2 Run 7
207    combine_velocity_depth(join(raw_data_dir,'T2R7velfilt.txt'),
208                           join(raw_data_dir,'T2R7pressfilt.txt'),
209                           join(Hinwood_dir, 'T2R7_boundary.csv'))
210    # Create the tsm file
211    prepare_time_boundary(join(Hinwood_dir, 'T2R7_boundary.tsm'))
Note: See TracBrowser for help on using the repository browser.