1 | # --------------------------------------------------------------------------- |
---|
2 | # This script reads in both the master STS (output from process_gems_grids.py) |
---|
3 | # and the gems boundary order file (defined as gems_order in project.py) |
---|
4 | # to generate a boundary STS file which contains data just at the boundary |
---|
5 | # points for all timesteps, which is input into run_model.py in ANUGA. |
---|
6 | # Written by Nariman Habili and Leharne Fountain, 2010 |
---|
7 | # --------------------------------------------------------------------------- |
---|
8 | |
---|
9 | import os |
---|
10 | import numpy |
---|
11 | from Scientific.IO.NetCDF import NetCDFFile |
---|
12 | from anuga.utilities.polygon import read_polygon |
---|
13 | import sys |
---|
14 | # sys.path.append('W:\\sandpits\\lfountain\\anuga_work\\production\\mandurah_storm_surge_2009\\gems_comparison') |
---|
15 | #sys.path.append('/nas/gemd/georisk_models/inundation/sandpits/lfountain/anuga_work/production/mandurah_storm_surge_2009/gems_comparison') |
---|
16 | import project |
---|
17 | from Scientific.IO.NetCDF import NetCDFFile |
---|
18 | from anuga.coordinate_transforms.geo_reference import Geo_reference, write_NetCDF_georeference |
---|
19 | |
---|
20 | #----------------- |
---|
21 | # Directory setup |
---|
22 | #------------------- |
---|
23 | state = 'western_australia' |
---|
24 | scenario_folder = 'bunbury_storm_surge_scenario_2009' |
---|
25 | event = '20100527_gcom_12min' # Baseline TC Alby event |
---|
26 | |
---|
27 | ENV_INUNDATIONHOME = 'INUNDATIONHOME' |
---|
28 | home = os.path.join(os.getenv(ENV_INUNDATIONHOME), 'data') # Absolute path for data folder |
---|
29 | # GEMS_folder = os.path.join(home, state, scenario_folder, 'GEMS') |
---|
30 | anuga_folder = os.path.join(home, state, scenario_folder, 'anuga') |
---|
31 | boundaries_folder = os.path.join(anuga_folder, 'boundaries') |
---|
32 | event_folder = os.path.join(boundaries_folder, event) |
---|
33 | |
---|
34 | #-------------------------------------------------------- |
---|
35 | # Define boundary points to extract from master STS file |
---|
36 | #-------------------------------------------------------- |
---|
37 | gems_boundary = numpy.array(read_polygon(project.gems_order)) |
---|
38 | #gems_boundary = numpy.array(read_polygon(project.gauges)) |
---|
39 | number_of_points = gems_boundary.shape[0] |
---|
40 | print 'hello', number_of_points |
---|
41 | print 'points', gems_boundary |
---|
42 | |
---|
43 | #--------------------------------------------------------- |
---|
44 | # Read master sts file generated from GEMS ascii grids |
---|
45 | #--------------------------------------------------------- |
---|
46 | print "Reading master NetCDF file" |
---|
47 | infile = NetCDFFile(os.path.join(event_folder, event + '_master_2_1.sts'), 'rl') #rl for large netcdf files |
---|
48 | |
---|
49 | grid_size = infile.grid_size |
---|
50 | ncols = infile.ncols |
---|
51 | nrows = infile.nrows |
---|
52 | no_data = infile.no_data |
---|
53 | refzone = infile.zone |
---|
54 | x_origin = infile.xllcorner |
---|
55 | y_origin = infile.yllcorner |
---|
56 | x = infile.variables['x'][:] |
---|
57 | y = infile.variables['y'][:] |
---|
58 | time = infile.variables['time'][:] |
---|
59 | elevation = infile.variables['elevation'][:] |
---|
60 | stage_array = infile.variables['stage'][:] |
---|
61 | xmomentum_array = infile.variables['xmomentum'][:] |
---|
62 | ymomentum_array = infile.variables['ymomentum'][:] |
---|
63 | number_of_timesteps = stage_array.shape[0] |
---|
64 | |
---|
65 | infile.close() |
---|
66 | |
---|
67 | #-------------------------------------------- |
---|
68 | # Creating index from boundary points |
---|
69 | #-------------------------------------------- |
---|
70 | print "Creating index from boundary points" # finding location of boundary points in master file |
---|
71 | |
---|
72 | origin = numpy.array([x_origin[0], y_origin[0]]) |
---|
73 | index_array_2D = ((gems_boundary - origin)/grid_size).astype(int) |
---|
74 | assert index_array_2D[:,0].min() >= 0 and index_array_2D[:,0].max() <= ncols # These test that the boundary points lie within the data area |
---|
75 | assert index_array_2D[:,1].min() >= 0 and index_array_2D[:,1].max() <= nrows |
---|
76 | |
---|
77 | index_array_1D = numpy.empty(index_array_2D.shape[0], int) |
---|
78 | index_array_1D = index_array_2D[:,1]*ncols + index_array_2D[:,0] |
---|
79 | |
---|
80 | #-------------------------------------------------- |
---|
81 | # Extract values from master sts at index points |
---|
82 | #-------------------------------------------------- |
---|
83 | print "Extract values from master sts at index points" |
---|
84 | |
---|
85 | x = x.take(index_array_1D) |
---|
86 | y = y.take(index_array_1D) |
---|
87 | elevation = elevation.take(index_array_1D) |
---|
88 | stage = numpy.empty([number_of_timesteps, number_of_points], float) |
---|
89 | xmomentum = numpy.empty([number_of_timesteps, number_of_points], float) |
---|
90 | ymomentum = numpy.empty([number_of_timesteps, number_of_points], float) |
---|
91 | |
---|
92 | for i, k in enumerate(index_array_1D): |
---|
93 | stage[:,i] = stage_array[:,k] |
---|
94 | xmomentum[:,i] = xmomentum_array[:,k] |
---|
95 | ymomentum[:,i] = ymomentum_array[:,k] |
---|
96 | |
---|
97 | #----------------------------- |
---|
98 | # Create the boundary STS file |
---|
99 | #----------------------------- |
---|
100 | print "Creating the boundary STS NetCDF file" |
---|
101 | |
---|
102 | fid = NetCDFFile(os.path.join(event_folder, event + '_boundary.sts'), 'w') |
---|
103 | fid.institution = 'Geoscience Australia' |
---|
104 | fid.description = "description" |
---|
105 | fid.starttime = 0.0 |
---|
106 | fid.ncols = ncols |
---|
107 | fid.nrows = nrows |
---|
108 | fid.grid_size = grid_size |
---|
109 | fid.no_data = no_data |
---|
110 | fid.createDimension('number_of_points', number_of_points) |
---|
111 | fid.createDimension('number_of_timesteps', number_of_timesteps) |
---|
112 | fid.createDimension('numbers_in_range', 2) |
---|
113 | |
---|
114 | fid.createVariable('x', 'd', ('number_of_points',)) |
---|
115 | fid.createVariable('y', 'd', ('number_of_points',)) |
---|
116 | fid.createVariable('elevation', 'd', ('number_of_points',)) |
---|
117 | fid.createVariable('elevation_range', 'd', ('numbers_in_range',)) |
---|
118 | fid.createVariable('time', 'i', ('number_of_timesteps',)) |
---|
119 | fid.createVariable('stage', 'd', ('number_of_timesteps', 'number_of_points')) |
---|
120 | fid.createVariable('stage_range', 'd', ('numbers_in_range', )) |
---|
121 | fid.createVariable('xmomentum', 'd', ('number_of_timesteps', 'number_of_points')) |
---|
122 | fid.createVariable('xmomentum_range', 'd', ('numbers_in_range',)) |
---|
123 | fid.createVariable('ymomentum', 'd', ('number_of_timesteps', 'number_of_points')) |
---|
124 | fid.createVariable('ymomentum_range', 'd', ('numbers_in_range',)) |
---|
125 | |
---|
126 | fid.variables['elevation_range'][:] = numpy.array([1e+036, -1e+036]) |
---|
127 | fid.variables['stage_range'][:] = numpy.array([1e+036, -1e+036]) |
---|
128 | fid.variables['xmomentum_range'][:] = numpy.array([1e+036, -1e+036]) |
---|
129 | fid.variables['ymomentum_range'][:] = numpy.array([1e+036, -1e+036]) |
---|
130 | fid.variables['elevation'][:] = elevation |
---|
131 | fid.variables['time'][:] = time |
---|
132 | |
---|
133 | s = fid.variables['stage'] |
---|
134 | xm = fid.variables['xmomentum'] |
---|
135 | ym = fid.variables['ymomentum'] |
---|
136 | |
---|
137 | for i in xrange(number_of_timesteps): |
---|
138 | s[i] = stage[i,:] |
---|
139 | xm[i] = xmomentum[i,:] |
---|
140 | ym[i] = ymomentum[i,:] |
---|
141 | |
---|
142 | #--------------------------------------------------------------------------- |
---|
143 | # Get timeseries values for wave height and components of momentum |
---|
144 | #--------------------------------------------------------------------------- |
---|
145 | |
---|
146 | basename = 'sts_gauge_tide' |
---|
147 | for i in xrange(number_of_points): |
---|
148 | out_file = os.path.join(event_folder, |
---|
149 | basename+'_'+str(i)+'.csv') |
---|
150 | fid_sts = open(out_file, 'w') |
---|
151 | fid_sts.write('time, stage, xmomentum, ymomentum, elevation \n') |
---|
152 | for j in xrange(number_of_timesteps): |
---|
153 | fid_sts.write('%.6f, %.6f, %.6f, %.6f, %.6f\n' |
---|
154 | % (time[j], stage[j,i], xmomentum[j,i], ymomentum[j,i], elevation[i])) |
---|
155 | |
---|
156 | fid_sts.close() |
---|
157 | |
---|
158 | origin = Geo_reference(refzone, x_origin, y_origin) |
---|
159 | geo_ref = write_NetCDF_georeference(origin, fid) |
---|
160 | |
---|
161 | fid.variables['x'][:] = x |
---|
162 | fid.variables['y'][:] = y |
---|
163 | |
---|
164 | fid.close() |
---|
165 | |
---|
166 | sys.exit() |
---|
167 | |
---|
168 | print 'get timeseries' |
---|
169 | #------------------------------------------------------------------------------- |
---|
170 | # Get gauges (timeseries of index points) |
---|
171 | #------------------------------------------------------------------------------- |
---|
172 | |
---|
173 | basename = 'sts_gauge' |
---|
174 | quantity_names = ['stage', 'xmomentum', 'ymomentum'] |
---|
175 | quantities = {} |
---|
176 | for i, name in enumerate(quantity_names): |
---|
177 | quantities[name] = fid.variables[name][:] |
---|
178 | |
---|
179 | |
---|
180 | for j in range(len(x)): |
---|
181 | index = j # permutation[j] |
---|
182 | stage = quantities['stage'][:,j] |
---|
183 | #print 'hello', j, stage |
---|
184 | xmomentum = quantities['xmomentum'][:,j] |
---|
185 | ymomentum = quantities['ymomentum'][:,j] |
---|
186 | |
---|
187 | out_file = os.path.join(event_folder, |
---|
188 | basename+'_'+str(index)+'.csv') |
---|
189 | fid_sts = open(out_file, 'w') |
---|
190 | fid_sts.write('time, stage, xmomentum, ymomentum \n') |
---|
191 | |
---|
192 | #----------------------------------------------------------------------- |
---|
193 | # End of the get gauges |
---|
194 | #----------------------------------------------------------------------- |
---|
195 | for k in range(len(time)-1): |
---|
196 | fid_sts.write('%.6f, %.6f, %.6f, %.6f\n' |
---|
197 | % (time[k], stage[k], xmomentum[k], ymomentum[k])) |
---|
198 | |
---|
199 | |
---|