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