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