[5076] | 1 | """ |
---|
| 2 | Script for running a breaking wave simulation of Jon Hinwoods wave tank. |
---|
| 3 | Note: this is based on the frinction_ua_flume_2006 structure. |
---|
| 4 | |
---|
| 5 | |
---|
| 6 | Duncan Gray, GA - 2007 |
---|
| 7 | |
---|
| 8 | """ |
---|
| 9 | |
---|
| 10 | |
---|
| 11 | #---------------------------------------------------------------------------- |
---|
| 12 | # Import necessary modules |
---|
| 13 | #---------------------------------------------------------------------------- |
---|
| 14 | |
---|
| 15 | from Scientific.IO.NetCDF import NetCDFFile |
---|
| 16 | from Numeric import array, zeros, Float |
---|
[5459] | 17 | from os.path import join |
---|
[5350] | 18 | |
---|
| 19 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
[5076] | 20 | |
---|
[5681] | 21 | from anuga.utilities.interp import interp |
---|
[5076] | 22 | |
---|
[5392] | 23 | |
---|
[5459] | 24 | import project |
---|
| 25 | |
---|
[5392] | 26 | # from os import getenv |
---|
| 27 | # from os.path import join |
---|
| 28 | # home = getenv('INUNDATIONHOME') |
---|
| 29 | # Hinwood_dir = join(home,'data','flumes','Hinwood_2008') |
---|
| 30 | # raw_data_dir = join(Hinwood_dir, 'raw_data') |
---|
| 31 | |
---|
| 32 | def csv2tms(filename, offshore_bed_elevation): |
---|
[5710] | 33 | """ |
---|
| 34 | Convert Hinwood boundary file to NetCDF tms file. |
---|
[5392] | 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'. |
---|
[5370] | 37 | |
---|
[5076] | 38 | """ |
---|
| 39 | |
---|
| 40 | print 'Creating', filename |
---|
| 41 | |
---|
| 42 | # Read the ascii (.csv) version of this file |
---|
| 43 | fid = open(filename[:-4] + '.csv') |
---|
| 44 | |
---|
| 45 | # Read remaining lines |
---|
| 46 | lines = fid.readlines() |
---|
| 47 | fid.close() |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | N = len(lines) |
---|
| 51 | T = zeros(N, Float) #Time |
---|
| 52 | Q = zeros(N, Float) #Stage |
---|
| 53 | X = zeros(N, Float) #XMomentum |
---|
| 54 | Y = zeros(N, Float) #YMomentum |
---|
| 55 | |
---|
| 56 | for i, line in enumerate(lines): |
---|
| 57 | fields = line.split(',') |
---|
| 58 | |
---|
| 59 | T[i] = float(fields[0]) |
---|
[5392] | 60 | Q[i] = float(fields[1]) |
---|
| 61 | depth = Q[i] - offshore_bed_elevation |
---|
[5076] | 62 | X[i] = float(fields[2]) * depth |
---|
[5350] | 63 | try: |
---|
| 64 | Y[i] = float(fields[3]) * depth |
---|
| 65 | except: |
---|
| 66 | pass |
---|
[5076] | 67 | |
---|
| 68 | |
---|
[5370] | 69 | |
---|
[5076] | 70 | # Create tms NetCDF file |
---|
| 71 | fid = NetCDFFile(filename, 'w') |
---|
| 72 | fid.institution = 'Geoscience Australia' |
---|
| 73 | fid.description = 'Input wave for Benchmark 2' |
---|
| 74 | fid.starttime = 0.0 |
---|
| 75 | fid.createDimension('number_of_timesteps', len(T)) |
---|
| 76 | fid.createVariable('time', Float, ('number_of_timesteps',)) |
---|
| 77 | fid.variables['time'][:] = T |
---|
| 78 | |
---|
| 79 | fid.createVariable('stage', Float, ('number_of_timesteps',)) |
---|
| 80 | fid.variables['stage'][:] = Q[:] |
---|
| 81 | |
---|
| 82 | fid.createVariable('xmomentum', Float, ('number_of_timesteps',)) |
---|
| 83 | fid.variables['xmomentum'][:] = X[:] |
---|
| 84 | |
---|
| 85 | fid.createVariable('ymomentum', Float, ('number_of_timesteps',)) |
---|
| 86 | fid.variables['ymomentum'][:] = Y[:] |
---|
| 87 | |
---|
| 88 | fid.close() |
---|
| 89 | |
---|
[5350] | 90 | |
---|
[5395] | 91 | def combine_velocity_depth(velocity_file, depth_file, out_file, metadata_dic): |
---|
[5350] | 92 | """ |
---|
| 93 | |
---|
[5370] | 94 | Convert the raw velocity and depth values, which have values at |
---|
[5350] | 95 | different times to a csv file, with values at the same time, with |
---|
| 96 | SI units. |
---|
| 97 | |
---|
[5370] | 98 | Set the depth values to be at the same times as the velocity values. |
---|
| 99 | |
---|
[5350] | 100 | The format for the velocity file is; |
---|
[5395] | 101 | [time, sec], [x-velocity +ve is towards the wave generator, cm/sec], |
---|
[5350] | 102 | [y-velocity], [z-velocity] |
---|
| 103 | |
---|
| 104 | The format for the pressure file is |
---|
| 105 | [time, sec], [mm above SWL for sensor A], many other sensors... |
---|
[5395] | 106 | |
---|
| 107 | The format of the output file is; |
---|
| 108 | [time, sec], [m above SWL for sensor A], |
---|
| 109 | [x-velocity +ve is towards the shore, m/sec], |
---|
| 110 | [y-velocity +ve is towards the left of the tank, looking from the |
---|
| 111 | generator to the shore, m/sec] |
---|
| 112 | |
---|
| 113 | |
---|
[5350] | 114 | """ |
---|
[5370] | 115 | missing = 1e+20 |
---|
| 116 | |
---|
[5350] | 117 | # Read velocity file |
---|
[5459] | 118 | #print "*********************" |
---|
| 119 | #print "velocity_file", velocity_file |
---|
[5350] | 120 | vfid = open(velocity_file) |
---|
| 121 | lines = vfid.readlines() |
---|
| 122 | vfid.close() |
---|
| 123 | |
---|
| 124 | |
---|
| 125 | n_velocity = len(lines) |
---|
| 126 | vtimes = zeros(n_velocity, Float) #Time |
---|
[5370] | 127 | x_velocities = zeros(n_velocity, Float) # |
---|
| 128 | y_velocities = zeros(n_velocity, Float) # |
---|
[5350] | 129 | |
---|
| 130 | for i, line in enumerate(lines): |
---|
[5370] | 131 | fields = line.split() #(',') |
---|
[5350] | 132 | |
---|
| 133 | vtimes[i] = float(fields[0]) |
---|
[5370] | 134 | x_velocities[i] = float(fields[1]) |
---|
| 135 | y_velocities[i] = float(fields[2]) |
---|
[5350] | 136 | |
---|
| 137 | # Read the depth file |
---|
[5459] | 138 | #print "depth_file", depth_file |
---|
[5350] | 139 | dfid = open(depth_file) |
---|
| 140 | lines = dfid.readlines() |
---|
| 141 | dfid.close() |
---|
| 142 | |
---|
[5370] | 143 | |
---|
[5350] | 144 | n_depth = len(lines) |
---|
[5370] | 145 | n_sensors = len(lines[0].split()) |
---|
[5350] | 146 | dtimes = zeros(n_depth, Float) #Time |
---|
| 147 | depths = zeros(n_depth, Float) # |
---|
[5370] | 148 | sensors = zeros((n_depth,n_sensors), Float) |
---|
[5350] | 149 | |
---|
| 150 | for i, line in enumerate(lines): |
---|
[5370] | 151 | fields = line.split() #(',') |
---|
| 152 | fields = [float(j) for j in fields] |
---|
| 153 | dtimes[i] = fields[0] |
---|
| 154 | depths[i] = fields[1] |
---|
| 155 | sensors[i] = fields |
---|
| 156 | #print "dtimes", dtimes |
---|
| 157 | #print "depths", depths |
---|
| 158 | #print "vtimes", vtimes |
---|
| 159 | depths_at_vtimes = interp( depths, dtimes, |
---|
| 160 | vtimes, missing=missing) |
---|
| 161 | depths_at_vtimes = ensure_numeric(depths_at_vtimes) |
---|
[5350] | 162 | |
---|
[5370] | 163 | #print "len(dtimes)", len(vtimes) |
---|
| 164 | #print "len(depths_at_vtimes)", len(depths_at_vtimes) |
---|
[5459] | 165 | #print "metadata_dic['scenario_id']", metadata_dic['scenario_id'] |
---|
| 166 | # for i in range(len(depths_at_vtimes)): |
---|
[5370] | 167 | # print "i", i |
---|
| 168 | # print "vtimes[i]", vtimes[i] |
---|
| 169 | # print "depths_at_vtimes[i]", depths_at_vtimes[i] |
---|
[5459] | 170 | |
---|
| 171 | #print "depths_at_vtimes", depths_at_vtimes |
---|
[5350] | 172 | depths_at_vtimes = depths_at_vtimes/1000.00 # convert from mm to m |
---|
[5370] | 173 | missing=missing/1000.00 # Do to missing what is done to depths_at_vtimes |
---|
| 174 | x_velocities = ensure_numeric(x_velocities) |
---|
[5395] | 175 | # Swap axis around convert cm/sec to m/sec |
---|
| 176 | x_velocities = x_velocities * -0.01 |
---|
[5370] | 177 | y_velocities = ensure_numeric(y_velocities) |
---|
[5395] | 178 | # Swap axis around convert cm/sec to m/sec |
---|
| 179 | y_velocities = y_velocities * -0.01 |
---|
[5350] | 180 | |
---|
| 181 | fid = open(out_file,'w') |
---|
| 182 | |
---|
| 183 | assert len(depths_at_vtimes) == len(vtimes) |
---|
[5370] | 184 | start_time = None |
---|
| 185 | #start_time = 0.0 |
---|
[5350] | 186 | #for vtime, depth_at_vtime, velocity in map(vtimes, depths_at_vtimes, |
---|
| 187 | # velocities): |
---|
| 188 | for i in xrange(len(vtimes)): |
---|
[5370] | 189 | if not depths_at_vtimes[i] == missing: |
---|
| 190 | # Make the times start at zero. |
---|
| 191 | if start_time is None: |
---|
[5395] | 192 | start_time = vtimes[i] |
---|
| 193 | final_time = vtimes[i]-start_time |
---|
| 194 | fid.write(str(final_time) \ |
---|
[5370] | 195 | + ',' + str(depths_at_vtimes[i]) \ |
---|
| 196 | + ',' + str(x_velocities[i]) \ |
---|
| 197 | + ',' + str(y_velocities[i])+'\n') |
---|
[5350] | 198 | |
---|
| 199 | fid.close() |
---|
[5459] | 200 | print "The start time for is", start_time |
---|
| 201 | if not start_time == metadata_dic['ANUGA_start_time']: |
---|
| 202 | raise ValueError, "The calc'ed and recorded start times are different" |
---|
| 203 | # Modify the sensor array to reflect the new start time |
---|
| 204 | sensors[:,0] -= start_time |
---|
[5350] | 205 | |
---|
[5370] | 206 | # Since there is a new time reference save the depth info using this |
---|
| 207 | # new reference. |
---|
[5395] | 208 | fid = open(depth_file[:-4] + '_exp_depth.csv','w') |
---|
[5370] | 209 | #print "depth_file", depth_file |
---|
| 210 | #print "start_time", start_time |
---|
[5395] | 211 | |
---|
[5455] | 212 | |
---|
| 213 | bed_elevation_list = metadata_dic['gauge_bed_elevation'] |
---|
| 214 | # +2 due to Time column and gauge A |
---|
| 215 | max_j = len(bed_elevation_list)+2 |
---|
[5395] | 216 | # Write a header |
---|
| 217 | fid.write('Time') |
---|
| 218 | for gauge_x in metadata_dic['gauge_x']: |
---|
| 219 | fid.write(',' + str(gauge_x)) |
---|
| 220 | fid.write('\n') |
---|
| 221 | for i in xrange(len(dtimes)): |
---|
| 222 | fid.write(str(sensors[i,0])) # Time |
---|
| 223 | # Don't write sensor A. |
---|
| 224 | # It is the boundary condition. |
---|
[5455] | 225 | for j, bed_elevation in map(None, |
---|
| 226 | xrange(2,max_j), |
---|
| 227 | bed_elevation_list): |
---|
[5395] | 228 | # depth, m |
---|
| 229 | gauge = sensors[i,j]/1000 - bed_elevation # Convert from mm to m |
---|
| 230 | fid.write(',' + str(gauge)) |
---|
[5370] | 231 | fid.write('\n') |
---|
| 232 | fid.close() |
---|
[5455] | 233 | |
---|
| 234 | |
---|
| 235 | # Since there is a new time reference save the stage info using this |
---|
| 236 | # new reference. |
---|
| 237 | fid = open(depth_file[:-4] + '_exp_stage.csv','w') |
---|
| 238 | |
---|
| 239 | # Write a header |
---|
| 240 | fid.write('Time') |
---|
| 241 | for gauge_x in metadata_dic['gauge_x']: |
---|
| 242 | fid.write(',' + str(gauge_x)) |
---|
| 243 | fid.write('\n') |
---|
| 244 | for i in xrange(len(dtimes)): |
---|
| 245 | fid.write(str(sensors[i,0])) # Time |
---|
| 246 | # Don't write sensor A. |
---|
| 247 | # It is the boundary condition. |
---|
| 248 | for j in xrange(2,max_j): |
---|
| 249 | # stage, m |
---|
| 250 | gauge = sensors[i,j]/1000 # Convert from mm to m |
---|
| 251 | fid.write(',' + str(gauge)) |
---|
| 252 | fid.write('\n') |
---|
| 253 | fid.close() |
---|
| 254 | |
---|
[5395] | 255 | return final_time |
---|
[5370] | 256 | |
---|
[5392] | 257 | def prepare_time_boundary(metadata_dic, raw_data_dir, output_dir): |
---|
| 258 | """ |
---|
[5459] | 259 | Use this if a project instance has already been created. |
---|
[5392] | 260 | """ |
---|
| 261 | scenario_id = metadata_dic['scenario_id'] |
---|
| 262 | velocity_file = join(raw_data_dir,scenario_id+'velfilt.txt') |
---|
| 263 | depth_file = join(raw_data_dir,scenario_id+'pressfilt.txt') |
---|
| 264 | out_file = join(output_dir, scenario_id+'_boundary.csv') |
---|
[5395] | 265 | #out_depth_file = join(output_dir, scenario_id+'_exp_depth.csv') |
---|
[5350] | 266 | |
---|
[5395] | 267 | final_time = combine_velocity_depth(velocity_file, depth_file, out_file, |
---|
[5459] | 268 | metadata_dic) |
---|
| 269 | #print "metadata_dic['xleft'][1]", metadata_dic['xleft'][1] |
---|
[5392] | 270 | if metadata_dic['xleft'][1] >= 0.0: |
---|
| 271 | # This should be a -ve value, since the still water level is the |
---|
| 272 | # z origin. |
---|
[5395] | 273 | print "Warning: The z origin seems incorrect." |
---|
| 274 | tsm_file = out_file[:-4] + '.tsm' |
---|
[5392] | 275 | csv2tms(tsm_file, metadata_dic['xleft'][1]) |
---|
[5395] | 276 | return final_time |
---|
[5459] | 277 | |
---|
| 278 | # Don't do this, since run-dam changes the metadata_dic['xleft'][1], |
---|
| 279 | # which is used by this function |
---|
| 280 | def prepare_time_boundary_for_scenarios_warning(scenarios, |
---|
| 281 | outputdir_tag): |
---|
| 282 | |
---|
| 283 | for run_data in scenarios: |
---|
| 284 | id = run_data['scenario_id'] |
---|
| 285 | outputdir_name = id + outputdir_tag |
---|
| 286 | pro_instance = project.Project(['data','flumes','Hinwood_2008'], |
---|
| 287 | outputdir_name=outputdir_name) |
---|
| 288 | prepare_time_boundary(run_data, |
---|
| 289 | pro_instance.raw_data_dir, |
---|
| 290 | pro_instance.boundarydir) |
---|
| 291 | |
---|
[5370] | 292 | |
---|
[5350] | 293 | #------------------------------------------------------------------- |
---|
[5076] | 294 | if __name__ == "__main__": |
---|
[5459] | 295 | |
---|
| 296 | import sys; sys.exit() |
---|
| 297 | |
---|
| 298 | from scenarios import scenarios |
---|
| 299 | outputdir_tag = "XXXX" |
---|
| 300 | prepare_time_boundary_for_scenarios(scenarios, |
---|
| 301 | outputdir_tag) |
---|