1 | """ |
---|
2 | Script for building boundary to run tsunami inundation scenario for onslow, |
---|
3 | WA, Australia. The boundary is based on the National Hazard Map. |
---|
4 | |
---|
5 | Input: order_filename from project.py |
---|
6 | Output: creates a sts file and csv files stored in project.boundaries_dir |
---|
7 | The run_patong.py is reliant on the output of this script. |
---|
8 | """ |
---|
9 | import os |
---|
10 | from os import sep |
---|
11 | import project |
---|
12 | |
---|
13 | from anuga.utilities.numerical_tools import ensure_numeric |
---|
14 | from Scientific.IO.NetCDF import NetCDFFile |
---|
15 | from Numeric import asarray,transpose,sqrt,argmax,argmin,arange,Float,\ |
---|
16 | compress,zeros,fabs,allclose,ones |
---|
17 | from anuga.utilities.polygon import inside_polygon,read_polygon |
---|
18 | from time import localtime, strftime, gmtime |
---|
19 | from anuga.shallow_water.data_manager import urs2sts,create_sts_boundary |
---|
20 | |
---|
21 | try: |
---|
22 | from pylab import plot,show,xlabel,ylabel,legend,title,savefig,hold |
---|
23 | except: |
---|
24 | print 'Cannot import pylab plotting will not work. Csv files are still created' |
---|
25 | |
---|
26 | #-------------------------------------------------------------------------- |
---|
27 | # Create sts boundary from mux2files |
---|
28 | #-------------------------------------------------------------------------- |
---|
29 | |
---|
30 | dir=os.path.join(project.boundaries_dir,'mux') |
---|
31 | |
---|
32 | # Refer to event_ |
---|
33 | urs_filenames = [os.path.join(dir,'tgs.out')] |
---|
34 | |
---|
35 | if len(urs_filenames) == 1: #change per event |
---|
36 | weight_factor = 1 |
---|
37 | weights=weight_factor*ones(len(urs_filenames),Float) |
---|
38 | |
---|
39 | scenario_name=project.scenario_name |
---|
40 | order_filename=os.path.join(project.order_filename_dir) |
---|
41 | |
---|
42 | print 'reading', order_filename |
---|
43 | # Create ordered sts file |
---|
44 | print 'creating sts file' |
---|
45 | |
---|
46 | urs2sts(urs_filenames, |
---|
47 | basename_out=os.path.join(project.boundaries_dir,scenario_name), |
---|
48 | ordering_filename=order_filename, |
---|
49 | weights=weights, |
---|
50 | mean_stage=project.tide, |
---|
51 | verbose=True) |
---|
52 | else: |
---|
53 | print 'number of sources do not match event.list' |
---|
54 | |
---|
55 | #------------------------------------------------------------------------------ |
---|
56 | # Get gauges (timeseries of index points) |
---|
57 | #------------------------------------------------------------------------------ |
---|
58 | def get_sts_gauge_data(filename,verbose=False): |
---|
59 | from Numeric import asarray,transpose,sqrt,argmax,argmin,arange,Float,\ |
---|
60 | compress,zeros,fabs,take |
---|
61 | fid = NetCDFFile(filename+'.sts', 'r') #Open existing file for read |
---|
62 | permutation = fid.variables['permutation'][:] |
---|
63 | x = fid.variables['x'][:]+fid.xllcorner #x-coordinates of vertices |
---|
64 | y = fid.variables['y'][:]+fid.yllcorner #y-coordinates of vertices |
---|
65 | points=transpose(asarray([x.tolist(),y.tolist()])) |
---|
66 | time=fid.variables['time'][:]+fid.starttime |
---|
67 | elevation=fid.variables['elevation'][:] |
---|
68 | |
---|
69 | basename='sts_gauge' |
---|
70 | quantity_names=['stage','xmomentum','ymomentum'] |
---|
71 | quantities = {} |
---|
72 | for i, name in enumerate(quantity_names): |
---|
73 | quantities[name] = fid.variables[name][:] |
---|
74 | |
---|
75 | #------------------------------------------------------------------------------ |
---|
76 | # Get Maxium wave height throughout timeseries at each index point |
---|
77 | #------------------------------------------------------------------------------ |
---|
78 | |
---|
79 | maxname = 'max_sts_stage.csv' |
---|
80 | fid_max = open(project.boundaries_dir+sep+maxname,'w') |
---|
81 | s = 'index, x, y, max_stage \n' |
---|
82 | fid_max.write(s) |
---|
83 | for j in range(len(x)): |
---|
84 | index= permutation[j] |
---|
85 | stage = quantities['stage'][:,j] |
---|
86 | xmomentum = quantities['xmomentum'][:,j] |
---|
87 | ymomentum = quantities['ymomentum'][:,j] |
---|
88 | |
---|
89 | s = '%d, %.6f, %.6f, %.6f\n' %(index, x[j], y[j], max(stage)) |
---|
90 | fid_max.write(s) |
---|
91 | |
---|
92 | #------------------------------------------------------------------------------ |
---|
93 | # Get Minium wave height throughout timeseries at each index point |
---|
94 | #------------------------------------------------------------------------------ |
---|
95 | |
---|
96 | minname = 'min_sts_stage.csv' |
---|
97 | fid_min = open(project.boundaries_dir+sep+minname,'w') |
---|
98 | s = 'index, x, y, max_stage \n' |
---|
99 | fid_min.write(s) |
---|
100 | for j in range(len(x)): |
---|
101 | index= permutation[j] |
---|
102 | stage = quantities['stage'][:,j] |
---|
103 | xmomentum = quantities['xmomentum'][:,j] |
---|
104 | ymomentum = quantities['ymomentum'][:,j] |
---|
105 | |
---|
106 | s = '%d, %.6f, %.6f, %.6f\n' %(index, x[j], y[j], min(stage)) |
---|
107 | fid_min.write(s) |
---|
108 | |
---|
109 | |
---|
110 | |
---|
111 | fid_sts = open(project.boundaries_dir+sep+basename+'_'+ str(index)+'.csv', 'w') |
---|
112 | s = 'time, stage, xmomentum, ymomentum \n' |
---|
113 | fid_sts.write(s) |
---|
114 | |
---|
115 | #------------------------------------------------------------------------------ |
---|
116 | # End of the get gauges |
---|
117 | #------------------------------------------------------------------------------ |
---|
118 | for k in range(len(time)-1): |
---|
119 | s = '%.6f, %.6f, %.6f, %.6f\n' %(time[k], stage[k], xmomentum[k], ymomentum[k]) |
---|
120 | fid_sts.write(s) |
---|
121 | |
---|
122 | fid_sts.close() |
---|
123 | |
---|
124 | fid.close() |
---|
125 | |
---|
126 | |
---|
127 | return quantities,elevation,time |
---|
128 | |
---|
129 | quantities,elevation,time=get_sts_gauge_data(os.path.join(project.boundaries_dir,project.scenario_name),verbose=False) |
---|
130 | |
---|
131 | print len(elevation), len(quantities['stage'][0,:]) |
---|
132 | |
---|