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' # Baseline TC Alby event |
---|
25 | |
---|
26 | ENV_INUNDATIONHOME = 'INUNDATIONHOME' |
---|
27 | home = os.path.join(os.getenv(ENV_INUNDATIONHOME), 'data') # Absolute path for data folder |
---|
28 | # GEMS_folder = os.path.join(home, state, scenario_folder, 'GEMS') |
---|
29 | anuga_folder = os.path.join(home, state, scenario_folder, 'anuga') |
---|
30 | boundaries_folder = os.path.join(anuga_folder, 'boundaries') |
---|
31 | event_folder = os.path.join(boundaries_folder, event) |
---|
32 | |
---|
33 | # input_dir = os.path.join(GEMS_folder, event) |
---|
34 | |
---|
35 | #----------------------- |
---|
36 | # Input files from GEMS |
---|
37 | #----------------------- |
---|
38 | elevation_file_name = os.path.join(event_folder, ***'topog_flip.asc'***) # Name of the vertically flipped elevation grid |
---|
39 | grid_file_names = glob.glob(os.path.join(event_folder, '*.gz')) |
---|
40 | grid_file_names.sort() |
---|
41 | number_of_timesteps = len(grid_file_names) |
---|
42 | grid_size = 20 #0.0005 # cellsize from the GEMSURGE output grids |
---|
43 | start_time = 0 # all times in seconds |
---|
44 | end_time = 142560 #88200 # all times in seconds |
---|
45 | timestep = 720 #1800 # all times in seconds |
---|
46 | refzone = 50 # UTM zone |
---|
47 | x_origin = ***#115.550 #364221.03 # xllcorner from GEMSURGE output grids |
---|
48 | y_origin = ***#-32.790 #6371062.71 # yllcorner from GEMSURGE output grids |
---|
49 | event_sts = os.path.join(event_folder, event) |
---|
50 | |
---|
51 | assert number_of_timesteps * timestep == end_time - start_time |
---|
52 | |
---|
53 | elevation = [] |
---|
54 | stage = [] |
---|
55 | speed = [] |
---|
56 | theta = [] |
---|
57 | |
---|
58 | elevation_file = open(elevation_file_name, 'rb') |
---|
59 | lines = elevation_file.readlines() |
---|
60 | elevation_file.close() |
---|
61 | |
---|
62 | # Strip off the ASCII header and also read ncols, nrows, |
---|
63 | # x_origin, y_origin, grid_size, no_data value, and read in elevation grid: |
---|
64 | for i, L in enumerate(lines): |
---|
65 | if i == 0: |
---|
66 | ncols = int(L.strip().split()[1]) |
---|
67 | if i == 1: |
---|
68 | nrows = int(L.strip().split()[1]) |
---|
69 | # if i == 2: |
---|
70 | # x_origin = int(float(L.strip().split()[1])) # this assumes an integer xllcorner |
---|
71 | # if i == 3: |
---|
72 | # y_origin = int(float(L.strip().split()[1])) # this assumes an integer yllcorner |
---|
73 | # if i == 4: |
---|
74 | # grid_size = int(float(L.strip().split()[1])) |
---|
75 | # if i == 5: |
---|
76 | # no_data = int(float(L.strip().split()[1])) |
---|
77 | if i > 5: |
---|
78 | elevation+= L.strip().split() |
---|
79 | |
---|
80 | print 'Number or columns: ', ncols |
---|
81 | print 'Number of rows: ', nrows |
---|
82 | print 'Grid size: ', grid_size |
---|
83 | print 'X origin: ', x_origin |
---|
84 | print 'Y origin: ', y_origin |
---|
85 | |
---|
86 | for f in grid_file_names: |
---|
87 | print 'file: ', f |
---|
88 | |
---|
89 | gz_file = gzip.open(f, 'rb') |
---|
90 | lines = gz_file.readlines() |
---|
91 | gz_file.close() |
---|
92 | |
---|
93 | for i, L in enumerate(lines): |
---|
94 | |
---|
95 | if i > 7 and i < (8 + nrows): # Number 7 refers to the number of rows of header info that we skip - CHECK format |
---|
96 | stage += [L.strip().split()] |
---|
97 | |
---|
98 | if i > (9 + nrows) and i < (10 + 2*nrows): |
---|
99 | speed += [L.strip().split()] |
---|
100 | |
---|
101 | if i > (11 + 2*nrows): |
---|
102 | theta += [L.strip().split()] |
---|
103 | |
---|
104 | #------------------------------------------------------ |
---|
105 | # Create arrays of elevation, stage, speed and theta |
---|
106 | #------------------------------------------------------- |
---|
107 | print 'creating numpy arrays: elevation, stage, speed, theta' |
---|
108 | |
---|
109 | elevation = numpy.array(elevation).astype('d') |
---|
110 | |
---|
111 | stage_array = numpy.empty([number_of_timesteps*nrows, ncols], dtype=float) |
---|
112 | speed_array = numpy.empty([number_of_timesteps*nrows, ncols], dtype=float) |
---|
113 | theta_array = numpy.empty([number_of_timesteps*nrows, ncols], dtype=float) |
---|
114 | |
---|
115 | for i in range(number_of_timesteps): |
---|
116 | ii = nrows*i |
---|
117 | stage_array[ii:ii+nrows, :] = numpy.flipud(numpy.array(stage[ii : ii + nrows]).astype('d')) |
---|
118 | speed_array[ii:ii+nrows, :] = numpy.flipud(numpy.array(speed[ii:ii + nrows]).astype('d')) |
---|
119 | theta_array[ii:ii+nrows, :] = numpy.flipud(numpy.array(theta[ii:ii + nrows]).astype('d')) |
---|
120 | |
---|
121 | stage = stage_array.ravel() |
---|
122 | speed = speed_array.ravel() |
---|
123 | theta = theta_array.ravel() |
---|
124 | |
---|
125 | print 'Size of elevation array: ', elevation.size |
---|
126 | print 'Size of stage array: ', stage.size |
---|
127 | print 'Size of speed array: ', speed.size |
---|
128 | print 'Size of theta array: ', theta.size |
---|
129 | |
---|
130 | assert stage.size == speed.size == theta.size == ncols * nrows * number_of_timesteps |
---|
131 | assert stage.size == number_of_timesteps * elevation.size |
---|
132 | |
---|
133 | depth = numpy.empty(stage.size, dtype='d') |
---|
134 | number_of_points = ncols * nrows |
---|
135 | |
---|
136 | for i in xrange(number_of_timesteps): |
---|
137 | j = number_of_points * i |
---|
138 | k = j + number_of_points |
---|
139 | depth[j:k] = stage[j:k] - elevation # Check GEMS elevations below sea level are negative |
---|
140 | |
---|
141 | momentum = depth * speed |
---|
142 | |
---|
143 | xmomentum = numpy.empty(momentum.size, dtype='d') |
---|
144 | ymomentum = numpy.empty(momentum.size, dtype='d') |
---|
145 | |
---|
146 | print 'Calculating xmomentum and ymomentum' |
---|
147 | |
---|
148 | for i, t in enumerate(theta): |
---|
149 | |
---|
150 | if t > 0.0 and t < 90.0: |
---|
151 | mx = momentum[i]*math.sin(math.radians(t)) #Assuming t is in the "to" direction and 0 degrees is north |
---|
152 | my = momentum[i]*math.cos(math.radians(t)) |
---|
153 | assert mx > 0 and my > 0 |
---|
154 | if t > 90.0 and t < 180.0: |
---|
155 | mx = momentum[i]*math.sin(math.radians(180 - t)) |
---|
156 | my = momentum[i]*math.cos(math.radians(180 - t)) |
---|
157 | assert mx > 0 and my < 0 |
---|
158 | if t > 180.0 and t < 270.0: |
---|
159 | mx = momentum[i]*math.cos(math.radians(270 - t)) |
---|
160 | my = momentum[i]*math.sin(math.radians(270 - t)) |
---|
161 | assert mx < 0 and my < 0 |
---|
162 | if t > 270.0 and t < 360.0: |
---|
163 | mx = momentum[i]*math.sin(math.radians(360 - t)) |
---|
164 | my = momentum[i]*math.cos(math.radians(360 - t)) |
---|
165 | assert mx < 0 and my > 0 |
---|
166 | if t == 0.0 or t == 360.0: |
---|
167 | mx = 0 |
---|
168 | my = momentum[i] |
---|
169 | assert my > 0 |
---|
170 | if t == 90.0: |
---|
171 | mx = momentum[i] |
---|
172 | my = 0 |
---|
173 | assert mx > 0 |
---|
174 | if t == 180.0: |
---|
175 | mx = 0 |
---|
176 | my = momentum[i] |
---|
177 | assert my < 0 |
---|
178 | if t == 270.0: |
---|
179 | mx = momentum[i] |
---|
180 | my = 0 |
---|
181 | assert mx < 0 |
---|
182 | |
---|
183 | xmomentum[i] = mx |
---|
184 | ymomentum[i] = my |
---|
185 | |
---|
186 | assert mx[i]^2 + my[i]^2 == momentum[i] |
---|
187 | |
---|
188 | x_origin_int = int(10000*x_origin) |
---|
189 | y_origin_int = int(10000*y_origin) |
---|
190 | grid_size_int = int(10000*grid_size) |
---|
191 | |
---|
192 | x = numpy.tile(numpy.arange(x_origin_int, (x_origin_int + ncols * grid_size_int), grid_size_int)/10000.0, nrows) |
---|
193 | y = numpy.repeat(numpy.arange(y_origin_int, (y_origin_int + nrows * grid_size_int), grid_size_int)/10000.0, ncols) |
---|
194 | |
---|
195 | assert x.size == y.size == number_of_points |
---|
196 | |
---|
197 | time = numpy.arange(start_time, end_time, timestep, dtype='i') |
---|
198 | |
---|
199 | assert time.size == number_of_timesteps |
---|
200 | assert momentum.size == stage.size |
---|
201 | |
---|
202 | #----------------------------- |
---|
203 | # Create the STS file |
---|
204 | #----------------------------- |
---|
205 | print "Creating the STS NetCDF file" |
---|
206 | |
---|
207 | fid = NetCDFFile(os.path.join(event_folder, event + '_master.sts'), 'w') |
---|
208 | fid.institution = 'Geoscience Australia' |
---|
209 | fid.description = "description" |
---|
210 | fid.starttime = 0.0 |
---|
211 | fid.ncols = ncols |
---|
212 | fid.nrows = nrows |
---|
213 | fid.grid_size = grid_size |
---|
214 | fid.no_data = no_data |
---|
215 | fid.createDimension('number_of_points', number_of_points) |
---|
216 | fid.createDimension('number_of_timesteps', number_of_timesteps) |
---|
217 | fid.createDimension('numbers_in_range', 2) |
---|
218 | |
---|
219 | fid.createVariable('x', 'd', ('number_of_points',)) |
---|
220 | fid.createVariable('y', 'd', ('number_of_points',)) |
---|
221 | fid.createVariable('elevation', 'd', ('number_of_points',)) |
---|
222 | fid.createVariable('elevation_range', 'd', ('numbers_in_range',)) |
---|
223 | fid.createVariable('time', 'i', ('number_of_timesteps',)) |
---|
224 | fid.createVariable('stage', 'd', ('number_of_timesteps', 'number_of_points')) |
---|
225 | fid.createVariable('stage_range', 'd', ('numbers_in_range', )) |
---|
226 | fid.createVariable('xmomentum', 'd', ('number_of_timesteps', 'number_of_points')) |
---|
227 | fid.createVariable('xmomentum_range', 'd', ('numbers_in_range',)) |
---|
228 | fid.createVariable('ymomentum', 'd', ('number_of_timesteps', 'number_of_points')) |
---|
229 | fid.createVariable('ymomentum_range', 'd', ('numbers_in_range',)) |
---|
230 | |
---|
231 | fid.variables['elevation_range'][:] = numpy.array([1e+036, -1e+036]) |
---|
232 | fid.variables['stage_range'][:] = numpy.array([1e+036, -1e+036]) |
---|
233 | fid.variables['xmomentum_range'][:] = numpy.array([1e+036, -1e+036]) |
---|
234 | fid.variables['ymomentum_range'][:] = numpy.array([1e+036, -1e+036]) |
---|
235 | fid.variables['elevation'][:] = elevation |
---|
236 | fid.variables['time'][:] = time |
---|
237 | |
---|
238 | s = fid.variables['stage'] |
---|
239 | xm = fid.variables['xmomentum'] |
---|
240 | ym = fid.variables['ymomentum'] |
---|
241 | |
---|
242 | for i in xrange(number_of_timesteps): |
---|
243 | ii = i*number_of_points |
---|
244 | s[i] = stage[ii : ii + number_of_points] |
---|
245 | xm[i] = xmomentum[ii : ii + number_of_points] |
---|
246 | ym[i] = ymomentum[ii : ii + number_of_points] |
---|
247 | |
---|
248 | origin = Geo_reference(refzone, min(x), min(y)) # Check this section for inputs in eastings and northings - it works for long and lat |
---|
249 | geo_ref = write_NetCDF_georeference(origin, fid) |
---|
250 | |
---|
251 | fid.variables['x'][:] = x - geo_ref.get_xllcorner() |
---|
252 | fid.variables['y'][:] = y - geo_ref.get_yllcorner() |
---|
253 | |
---|
254 | fid.close() |
---|