1 | # --------------------------------------------------------------------------- |
---|
2 | # This python script reads in GEMSURGE outputs and writes them into a master |
---|
3 | # STS file that contains all the data across the entire model domain at all |
---|
4 | # timesteps. It requires a vertically flipped elevation ASCII grid which is |
---|
5 | # merged with the GEMSURGE data to calculate the required quantities of |
---|
6 | # xmomentum and ymomentum. |
---|
7 | # Written by Nariman Habili and Leharne Fountain, 2010 |
---|
8 | # --------------------------------------------------------------------------- |
---|
9 | |
---|
10 | import glob |
---|
11 | import os |
---|
12 | import gzip |
---|
13 | import pdb |
---|
14 | import numpy |
---|
15 | import math |
---|
16 | from Scientific.IO.NetCDF import NetCDFFile |
---|
17 | from anuga.coordinate_transforms.geo_reference import Geo_reference, write_NetCDF_georeference |
---|
18 | |
---|
19 | #----------------- |
---|
20 | # Directory setup |
---|
21 | #------------------- |
---|
22 | state = 'western_australia' |
---|
23 | scenario_folder = 'bunbury_storm_surge_scenario_2009' |
---|
24 | event = 'case_1' |
---|
25 | |
---|
26 | print "Processing event: ", event |
---|
27 | |
---|
28 | ENV_INUNDATIONHOME = 'INUNDATIONHOME' |
---|
29 | home = os.path.join(os.getenv(ENV_INUNDATIONHOME), 'data') # Absolute path for data folder |
---|
30 | # GEMS_folder = os.path.join(home, state, scenario_folder, 'GEMS') |
---|
31 | anuga_folder = os.path.join(home, state, scenario_folder, 'anuga') |
---|
32 | boundaries_folder = os.path.join(anuga_folder, 'boundaries') |
---|
33 | event_folder = os.path.join(boundaries_folder, event) |
---|
34 | |
---|
35 | # input_dir = os.path.join(GEMS_folder, event) |
---|
36 | |
---|
37 | #----------------------- |
---|
38 | # Input files from GEMS |
---|
39 | #----------------------- |
---|
40 | elevation_file_name = os.path.join(event_folder, 'buntopog_20m_flip.asc') # Name of the vertically flipped elevation grid |
---|
41 | |
---|
42 | print "Elevation file name: ", elevation_file_name |
---|
43 | |
---|
44 | grid_file_names = glob.glob(os.path.join(event_folder, 'gcom_txt19780404.0*.gz')) |
---|
45 | grid_file_names.sort() |
---|
46 | |
---|
47 | # grid_file_names_temp = [] |
---|
48 | # for i in range(0, 120, 4): |
---|
49 | # grid_file_names_temp.append(grid_file_names[i]) |
---|
50 | |
---|
51 | # grid_file_names = grid_file_names_temp |
---|
52 | |
---|
53 | # number_of_timesteps = len(grid_file_names) |
---|
54 | |
---|
55 | print "Number of timesteps: ", number_of_timesteps |
---|
56 | |
---|
57 | start_time = 0 # all times in seconds |
---|
58 | timestep = 720 # all times in seconds |
---|
59 | end_time = start_time + (timestep*number_of_timesteps) # all times in seconds |
---|
60 | refzone = 50 # UTM zone of model |
---|
61 | event_sts = os.path.join(event_folder, event) |
---|
62 | |
---|
63 | elevation = [] |
---|
64 | stage = [] |
---|
65 | speed = [] |
---|
66 | theta = [] |
---|
67 | |
---|
68 | elevation_file = open(elevation_file_name, 'rb') |
---|
69 | lines = elevation_file.readlines() |
---|
70 | elevation_file.close() |
---|
71 | |
---|
72 | # Strip off the ASCII header and also read ncols, nrows, |
---|
73 | # x_origin, y_origin, grid_size, no_data value, and read in elevation grid: |
---|
74 | for i, L in enumerate(lines): |
---|
75 | if i == 0: |
---|
76 | ncols = int(L.strip().split()[1]) |
---|
77 | if i == 1: |
---|
78 | nrows = int(L.strip().split()[1]) |
---|
79 | if i == 2: |
---|
80 | x_origin = int(L.strip().split()[1]) |
---|
81 | if i == 3: |
---|
82 | y_origin = int(L.strip().split()[1]) |
---|
83 | if i == 4: |
---|
84 | grid_size = int(L.strip().split()[1]) |
---|
85 | if i == 5: |
---|
86 | no_data = int(float(L.strip().split()[1])) |
---|
87 | if i > 5: |
---|
88 | elevation+= L.strip().split() |
---|
89 | |
---|
90 | print 'Number or columns: ', ncols |
---|
91 | print 'Number of rows: ', nrows |
---|
92 | print 'X origin: ', x_origin |
---|
93 | print 'Y origin: ', y_origin |
---|
94 | print 'Grid size: ', grid_size |
---|
95 | print 'No data value: ', no_data |
---|
96 | |
---|
97 | for f in grid_file_names: |
---|
98 | print 'file: ', f |
---|
99 | |
---|
100 | gz_file = gzip.open(f, 'rb') |
---|
101 | lines = gz_file.readlines() |
---|
102 | gz_file.close() |
---|
103 | |
---|
104 | for i, L in enumerate(lines): |
---|
105 | |
---|
106 | if i > 7 and i < (8 + nrows): # 7 refers to the number of rows of header info that we skip - always CHECK format |
---|
107 | stage += [L.strip().split()] |
---|
108 | |
---|
109 | if i > (9 + nrows) and i < (10 + 2*nrows): |
---|
110 | speed += [L.strip().split()] |
---|
111 | |
---|
112 | if i > (11 + 2*nrows): |
---|
113 | theta += [L.strip().split()] |
---|
114 | |
---|
115 | #------------------------------------------------------ |
---|
116 | # Create arrays of elevation, stage, speed and theta |
---|
117 | #------------------------------------------------------- |
---|
118 | print 'creating numpy arrays: elevation, stage, speed, theta' |
---|
119 | |
---|
120 | elevation = numpy.array(elevation).astype('d') |
---|
121 | |
---|
122 | stage_array = numpy.empty([number_of_timesteps*nrows, ncols], dtype=float) |
---|
123 | speed_array = numpy.empty([number_of_timesteps*nrows, ncols], dtype=float) |
---|
124 | theta_array = numpy.empty([number_of_timesteps*nrows, ncols], dtype=float) |
---|
125 | |
---|
126 | for i in range(number_of_timesteps): |
---|
127 | ii = nrows*i |
---|
128 | stage_array[ii:ii+nrows, :] = numpy.flipud(numpy.array(stage[ii : ii + nrows]).astype('d')) |
---|
129 | speed_array[ii:ii+nrows, :] = numpy.flipud(numpy.array(speed[ii:ii + nrows]).astype('d')) |
---|
130 | theta_array[ii:ii+nrows, :] = numpy.flipud(numpy.array(theta[ii:ii + nrows]).astype('d')) |
---|
131 | |
---|
132 | stage = stage_array.ravel() |
---|
133 | speed = speed_array.ravel() |
---|
134 | theta = theta_array.ravel() |
---|
135 | |
---|
136 | print 'Size of elevation array: ', elevation.size |
---|
137 | print 'Size of stage array: ', stage.size |
---|
138 | print 'Size of speed array: ', speed.size |
---|
139 | print 'Size of theta array: ', theta.size |
---|
140 | |
---|
141 | assert stage.size == speed.size == theta.size == ncols * nrows * number_of_timesteps |
---|
142 | assert stage.size == number_of_timesteps * elevation.size |
---|
143 | |
---|
144 | d = numpy.empty(elevation.size, dtype='d') |
---|
145 | depth = numpy.empty(stage.size, dtype='d') |
---|
146 | number_of_points = ncols * nrows |
---|
147 | |
---|
148 | # --------------------------------------------- |
---|
149 | # Create mask of no_data values across stage, speed and theta to ensure all three quantities |
---|
150 | # have no_data values in corresponding cells - this is a work-around until GEMS can produce a |
---|
151 | # a consistant dataset |
---|
152 | # --------------------------------------------- |
---|
153 | |
---|
154 | for i in xrange(number_of_timesteps): |
---|
155 | j = number_of_points * i |
---|
156 | k = j + number_of_points |
---|
157 | # no_value_mask = (stage[j:k] < -9000) + (speed[j:k] < -9000) + (theta[j:k] < -9000) |
---|
158 | no_value_index = numpy.where(((stage[j:k] < -100) + (speed[j:k] < 0) + (theta[j:k] < 0)) == True)[0] |
---|
159 | depth[j:k] = stage[j:k] - elevation |
---|
160 | numpy.put(stage[j:k], no_value_index, -9999) |
---|
161 | numpy.put(speed[j:k], no_value_index, -9999) |
---|
162 | numpy.put(theta[j:k], no_value_index, -9999) |
---|
163 | numpy.put(depth[j:k], no_value_index, 0) |
---|
164 | |
---|
165 | # for i in xrange(number_of_timesteps): |
---|
166 | # j = number_of_points * i |
---|
167 | # k = j + number_of_points |
---|
168 | # d = stage[j:k] - elevation # Assumes elevations below sea level are negative |
---|
169 | # depth[j:k] = [0 if x<-9000 else x for x in d] # sets stage to zero for no-data values of stage |
---|
170 | |
---|
171 | # depth[j:k] = [x if x>=0 and elevation>0 else 0 for x in d] # This is to correct for GEMS data which assumes stage=0 onshore when dry but should = elevation |
---|
172 | # for q in xrange(number_of_points): |
---|
173 | # if elevation[q] >= 0 and stage[j+q]==0: |
---|
174 | # depth[j+q] = 0 |
---|
175 | |
---|
176 | momentum = depth * speed |
---|
177 | |
---|
178 | xmomentum = numpy.empty(momentum.size, dtype='d') |
---|
179 | ymomentum = numpy.empty(momentum.size, dtype='d') |
---|
180 | |
---|
181 | print 'Calculating xmomentum and ymomentum' |
---|
182 | |
---|
183 | for i, t in enumerate(theta): |
---|
184 | |
---|
185 | mx = momentum[i]*math.sin(math.radians(t)) #Assuming t is in the "to" direction and 0 degrees is north |
---|
186 | my = momentum[i]*math.cos(math.radians(t)) |
---|
187 | |
---|
188 | if t > 0.0 and t < 90.0: |
---|
189 | assert mx > 0 and my > 0 |
---|
190 | |
---|
191 | elif t > 90.0 and t < 180.0: |
---|
192 | assert mx > 0 and my < 0 |
---|
193 | |
---|
194 | elif t > 180.0 and t < 270.0: |
---|
195 | assert mx < 0 and my < 0 |
---|
196 | |
---|
197 | elif t > 270.0 and t < 360.0: |
---|
198 | assert mx < 0 and my > 0 |
---|
199 | |
---|
200 | elif t == 0.0 or t == 360.0: |
---|
201 | assert my > 0 |
---|
202 | |
---|
203 | elif t == 90.0: |
---|
204 | assert mx > 0 |
---|
205 | |
---|
206 | elif t == 180.0: |
---|
207 | assert my < 0 |
---|
208 | |
---|
209 | elif t == 270.0: |
---|
210 | assert mx < 0 |
---|
211 | |
---|
212 | elif t == -9999: |
---|
213 | mx = 0 |
---|
214 | my = 0 |
---|
215 | else: |
---|
216 | print "Unexpected value of theta" |
---|
217 | exit() |
---|
218 | |
---|
219 | xmomentum[i] = mx |
---|
220 | ymomentum[i] = my |
---|
221 | |
---|
222 | assert math.sqrt(mx**2 + my**2) - momentum[i] < 1.e-06 |
---|
223 | |
---|
224 | x_origin_int = int(10000*x_origin) |
---|
225 | y_origin_int = int(10000*y_origin) |
---|
226 | grid_size_int = int(10000*grid_size) |
---|
227 | |
---|
228 | x = numpy.tile(numpy.arange(x_origin_int, (x_origin_int + ncols * grid_size_int), grid_size_int)/10000.0, nrows) |
---|
229 | y = numpy.repeat(numpy.arange(y_origin_int, (y_origin_int + nrows * grid_size_int), grid_size_int)/10000.0, ncols) |
---|
230 | |
---|
231 | assert x.size == y.size == number_of_points |
---|
232 | |
---|
233 | time = numpy.arange(start_time, end_time, timestep, dtype='i') |
---|
234 | |
---|
235 | assert time.size == number_of_timesteps |
---|
236 | assert momentum.size == stage.size |
---|
237 | |
---|
238 | # ----------------------------- |
---|
239 | # Create the STS file |
---|
240 | # ----------------------------- |
---|
241 | print "Creating the STS NetCDF file" |
---|
242 | |
---|
243 | fid = NetCDFFile(os.path.join(event_folder, event + '_test.sts'), 'w') |
---|
244 | fid.institution = 'Geoscience Australia' |
---|
245 | fid.description = "description" |
---|
246 | fid.starttime = 0.0 |
---|
247 | fid.ncols = ncols |
---|
248 | fid.nrows = nrows |
---|
249 | fid.grid_size = grid_size |
---|
250 | fid.no_data = no_data |
---|
251 | fid.createDimension('number_of_points', number_of_points) |
---|
252 | fid.createDimension('number_of_timesteps', number_of_timesteps) |
---|
253 | fid.createDimension('numbers_in_range', 2) |
---|
254 | |
---|
255 | fid.createVariable('x', 'd', ('number_of_points',)) |
---|
256 | fid.createVariable('y', 'd', ('number_of_points',)) |
---|
257 | fid.createVariable('elevation', 'd', ('number_of_points',)) |
---|
258 | fid.createVariable('elevation_range', 'd', ('numbers_in_range',)) |
---|
259 | fid.createVariable('time', 'i', ('number_of_timesteps',)) |
---|
260 | fid.createVariable('stage', 'd', ('number_of_timesteps', 'number_of_points')) |
---|
261 | fid.createVariable('stage_range', 'd', ('numbers_in_range', )) |
---|
262 | fid.createVariable('xmomentum', 'd', ('number_of_timesteps', 'number_of_points')) |
---|
263 | fid.createVariable('xmomentum_range', 'd', ('numbers_in_range',)) |
---|
264 | fid.createVariable('ymomentum', 'd', ('number_of_timesteps', 'number_of_points')) |
---|
265 | fid.createVariable('ymomentum_range', 'd', ('numbers_in_range',)) |
---|
266 | |
---|
267 | fid.variables['elevation_range'][:] = numpy.array([1e+036, -1e+036]) |
---|
268 | fid.variables['stage_range'][:] = numpy.array([1e+036, -1e+036]) |
---|
269 | fid.variables['xmomentum_range'][:] = numpy.array([1e+036, -1e+036]) |
---|
270 | fid.variables['ymomentum_range'][:] = numpy.array([1e+036, -1e+036]) |
---|
271 | fid.variables['elevation'][:] = elevation |
---|
272 | fid.variables['time'][:] = time |
---|
273 | |
---|
274 | s = fid.variables['stage'] |
---|
275 | xm = fid.variables['xmomentum'] |
---|
276 | ym = fid.variables['ymomentum'] |
---|
277 | |
---|
278 | for i in xrange(number_of_timesteps): |
---|
279 | ii = i*number_of_points |
---|
280 | s[i] = stage[ii : ii + number_of_points] |
---|
281 | xm[i] = xmomentum[ii : ii + number_of_points] |
---|
282 | ym[i] = ymomentum[ii : ii + number_of_points] |
---|
283 | |
---|
284 | origin = Geo_reference(refzone, min(x), min(y)) # Check this section for inputs in eastings and northings - it works for long and lat |
---|
285 | geo_ref = write_NetCDF_georeference(origin, fid) |
---|
286 | |
---|
287 | fid.variables['x'][:] = x - geo_ref.get_xllcorner() |
---|
288 | fid.variables['y'][:] = y - geo_ref.get_yllcorner() |
---|
289 | |
---|
290 | fid.close() |
---|