- Timestamp:
- Feb 16, 2009, 9:00:59 AM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
anuga_work/production/busselton/standardised_version/run_model.py
r6329 r6343 22 22 # Standard modules 23 23 import os 24 import os.path 24 25 import time 26 from time import localtime, strftime, gmtime 25 27 26 28 # Related major packages 29 from Scientific.IO.NetCDF import NetCDFFile 30 import Numeric as num 31 27 32 from anuga.interface import create_domain_from_regions 28 33 from anuga.interface import Transmissive_stage_zero_momentum_boundary … … 32 37 from anuga.interface import create_sts_boundary 33 38 from anuga.interface import csv2building_polygons 39 <<<<<<< .mine 40 ======= 34 41 from file_length import file_length 35 42 43 >>>>>>> .r6341 36 44 from anuga.shallow_water.data_manager import start_screen_catcher 37 45 from anuga.shallow_water.data_manager import copy_code_files 46 from anuga.shallow_water.data_manager import urs2sts 38 47 from anuga.utilities.polygon import read_polygon, Polygon_function 39 48 40 49 # Application specific imports 41 50 from setup_model import project 42 import build_urs_boundary as bub 43 51 52 53 #------------------------------------------------------------------------------- 54 # Get gauges (timeseries of index points) 55 #------------------------------------------------------------------------------- 56 def get_sts_gauge_data(filename, verbose=False): 57 fid = NetCDFFile(filename+'.sts', 'r') #Open existing file for read 58 permutation = fid.variables['permutation'][:] 59 x = fid.variables['x'][:] + fid.xllcorner #x-coordinates of vertices 60 y = fid.variables['y'][:] + fid.yllcorner #y-coordinates of vertices 61 points = num.transpose(num.asarray([x.tolist(), y.tolist()])) 62 time = fid.variables['time'][:] + fid.starttime 63 elevation = fid.variables['elevation'][:] 64 65 basename = 'sts_gauge' 66 quantity_names = ['stage', 'xmomentum', 'ymomentum'] 67 quantities = {} 68 for i, name in enumerate(quantity_names): 69 quantities[name] = fid.variables[name][:] 70 71 #--------------------------------------------------------------------------- 72 # Get maximum wave height throughout timeseries at each index point 73 #--------------------------------------------------------------------------- 74 75 maxname = 'max_sts_stage.csv' 76 fid_max = open(os.path.join(project.event_sts, maxname), 'w') 77 fid_max.write('index, x, y, max_stage \n') 78 for j in range(len(x)): 79 index = permutation[j] 80 stage = quantities['stage'][:,j] 81 xmomentum = quantities['xmomentum'][:,j] 82 ymomentum = quantities['ymomentum'][:,j] 83 84 fid_max.write('%d, %.6f, %.6f, %.6f\n' % (index, x[j], y[j], max(stage))) 85 86 #--------------------------------------------------------------------------- 87 # Get minimum wave height throughout timeseries at each index point 88 #--------------------------------------------------------------------------- 89 90 minname = 'min_sts_stage.csv' 91 fid_min = open(os.path.join(project.event_sts, minname), 'w') 92 fid_min.write('index, x, y, max_stage \n') 93 for j in range(len(x)): 94 index = permutation[j] 95 stage = quantities['stage'][:,j] 96 xmomentum = quantities['xmomentum'][:,j] 97 ymomentum = quantities['ymomentum'][:,j] 98 99 fid_min.write('%d, %.6f, %.6f, %.6f\n' %(index, x[j], y[j], min(stage))) 100 101 out_file = os.path.join(project.event_sts, 102 basename+'_'+str(index)+'.csv') 103 fid_sts = open(out_file, 'w') 104 fid_sts.write('time, stage, xmomentum, ymomentum \n') 105 106 #----------------------------------------------------------------------- 107 # End of the get gauges 108 #----------------------------------------------------------------------- 109 for k in range(len(time)-1): 110 fid_sts.write('%.6f, %.6f, %.6f, %.6f\n' 111 % (time[k], stage[k], xmomentum[k], ymomentum[k])) 112 113 fid_sts.close() 114 115 fid.close() 116 117 return quantities,elevation,time 118 119 120 ## 121 # @brief Build boundary STS files from one or more MUX files. 122 # @param mux_dir Directory containing the MUX files. 123 # @param event_file Path to meta-file containing mux files+weight data. 124 # @param output_dir Directory to write STS data to. 125 # @note 'event_file' is produced by EventSelection. 126 def build_urs_boundary(mux_dir, event_file, output_dir): 127 '''Build a boundary STS file from a set of MUX files.''' 128 129 print 'build_urs_boundary: mux_dir=%s' % mux_dir 130 print 'build_urs_boundary: event_file=%s' % event_file 131 print 'build_urs_boundary: output_dir=%s' % output_dir 132 133 # if we are using an EventSelection multi-mux file 134 if project.multi_mux: 135 # get the mux+weight data from the event file 136 mux_event_file = os.path.join(mux_dir, event_file) 137 try: 138 fd = open(mux_event_file, 'r') 139 mux_data = fd.readlines() 140 fd.close() 141 except IOError, e: 142 msg = 'File %s cannot be read: %s' % (mux_event_file, str(e)) 143 raise Exception, msg 144 except: 145 raise 146 147 # first line of file is # filenames+weight in rest of file 148 num_lines = int(mux_data[0].strip()) 149 mux_data = mux_data[1:] 150 print 'number of sources %d' % num_lines 151 152 # quick sanity check on input mux meta-file 153 if num_lines != len(mux_data): 154 msg = ('Bad file %s: %d data lines, but line 1 count is %d' 155 % (event_file, len(mux_data), num_lines)) 156 raise Exception, msg 157 158 # Create filename and weights lists. 159 # Must chop GRD filename just after '*.grd'. 160 mux_filenames = [] 161 for line in mux_data: 162 muxname = line.strip().split()[0] 163 split_index = muxname.index('.grd') 164 muxname = muxname[:split_index+len('.grd')] 165 muxname = os.path.join(mux_dir, muxname) 166 mux_filenames.append(muxname) 167 168 mux_weights = [float(line.strip().split()[1]) for line in mux_data] 169 170 # Call legacy function to create STS file. 171 # This should be replaced in the future. 172 print 'reading', project.urs_order 173 print 'creating STS file' 174 print 'mux_filenames=%s' % str(mux_filenames) 175 print 'basename_out=%s' % str(output_dir) 176 print 'ordering_filename=%s' % str(project.urs_order) 177 print 'weights=%s' % str(mux_weights) 178 print 'mean_stage=%s' % str(project.tide) 179 urs2sts(mux_filenames, 180 basename_out=output_dir, 181 ordering_filename=project.urs_order, 182 weights=mux_weights, 183 mean_stage=project.tide, 184 verbose=True) 185 else: # a single mux stem file 186 urs_filenames = [os.path.join(mux_dir, event_file)] 187 188 weight_factor = 1.0 189 weights = weight_factor*num.ones(len(urs_filenames), num.float) 190 191 order_filename = os.path.join(project.order_filename_dir) 192 193 print 'reading', order_filename 194 # Create ordered sts file 195 print 'creating sts file' 196 urs2sts(urs_filenames, 197 basename_out=os.path.join(project.boundaries_dir, 198 project.scenario_name), 199 ordering_filename=order_filename, 200 weights=weights, 201 mean_stage=project.tide, 202 verbose=True) 203 204 # report on stuff so far 205 quantities, elevation, time = get_sts_gauge_data(project.event_folder, 206 verbose=False) 207 print len(elevation), len(quantities['stage'][0,:]) 44 208 45 209 #------------------------------------------------------------------------------- … … 62 226 63 227 # Create the STS file 228 <<<<<<< .mine 229 build_urs_boundary(project.mux_data_folder, 230 project.mux_input, 231 os.path.join(project.event_folder, 232 project.scenario_name)) 233 ======= 64 234 print 'project.mux_data_folder=%s' % project.mux_data_folder 65 235 bub.build_urs_boundary(project.mux_input_filename, 66 236 os.path.join(project.event_folder, 67 237 project.scenario_name)) 238 >>>>>>> .r6341 68 239 69 240 # Read in boundary from ordered sts file
Note: See TracChangeset
for help on using the changeset viewer.