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

Last change on this file since 5392 was 5392, checked in by duncan, 15 years ago

Current Hinwood scenario

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