[2896] | 1 | """ Script for calculating maximum inundation at building locations |
---|
| 2 | |
---|
| 3 | Inputs: |
---|
| 4 | |
---|
| 5 | swwfile - name of sww file |
---|
| 6 | - assume that all conserved quantities have been stored |
---|
| 7 | |
---|
| 8 | buildings_filename - name of file containing building data, sourced |
---|
| 9 | from NBED |
---|
| 10 | |
---|
| 11 | time_min - beginning of user defined time range |
---|
| 12 | - default will be first available time found in swwfile |
---|
| 13 | |
---|
| 14 | time_max - end of user defined time range |
---|
| 15 | - default will be last available time found in swwfile |
---|
| 16 | |
---|
| 17 | Output: |
---|
| 18 | |
---|
| 19 | - building_filename (.csv) augmented to include maximum depth, momentum and |
---|
| 20 | velocity and stored in same location as swwfile |
---|
| 21 | Name = augmented_buildings.csv |
---|
| 22 | |
---|
| 23 | """ |
---|
| 24 | |
---|
| 25 | from pyvolution.util import file_function |
---|
| 26 | import project |
---|
| 27 | from os import sep |
---|
| 28 | from math import sqrt |
---|
| 29 | |
---|
| 30 | # Inputs |
---|
| 31 | #timestampdir = '' # HAT |
---|
| 32 | timestampdir = '' # LAT |
---|
| 33 | file_loc = project.outputdir + timestampdir + sep |
---|
| 34 | swwfile = file_loc + project.basename + '.sww' |
---|
| 35 | buildings_filename = project.buildings_filename |
---|
| 36 | time_min = None |
---|
| 37 | time_max = None |
---|
| 38 | |
---|
| 39 | def get_buildings_from_file(filename): |
---|
| 40 | from coordinate_transforms.redfearn import redfearn |
---|
| 41 | fid = open(filename) |
---|
| 42 | lines = fid.readlines() |
---|
| 43 | fid.close() |
---|
| 44 | buildings = [] |
---|
| 45 | line1 = lines[0] |
---|
| 46 | line11 = line1.split(',') |
---|
| 47 | for i in range(len(line11)): |
---|
| 48 | if line11[i].strip('\n').strip(' ') == 'LATITUDE': lat_index = i |
---|
| 49 | if line11[i].strip('\n').strip(' ') == 'LONGITUDE': lon_index = i |
---|
| 50 | if line11[i].strip('\n').strip(' ') == 'BUILDING_N': name_index = i |
---|
| 51 | for line in lines[1:]: |
---|
| 52 | fields = line.split(',') |
---|
| 53 | lat = float(fields[lat_index]) |
---|
| 54 | lon = float(fields[lon_index]) |
---|
| 55 | z, easting, northing = redfearn(lat,lon) |
---|
| 56 | buildings.append([easting, northing]) |
---|
| 57 | loc = fields[name_index] |
---|
| 58 | |
---|
| 59 | return buildings, lines |
---|
| 60 | |
---|
| 61 | print '\n Buildings obtained from: %s \n' %buildings_filename |
---|
| 62 | buildings, lines = get_buildings_from_file(buildings_filename) |
---|
| 63 | |
---|
| 64 | sww_quantity = ['stage', 'elevation', 'xmomentum', 'ymomentum'] |
---|
| 65 | |
---|
| 66 | f = file_function(swwfile, |
---|
| 67 | quantities = sww_quantity, |
---|
| 68 | interpolation_points = buildings, |
---|
| 69 | verbose = True, |
---|
| 70 | use_cache = True) |
---|
| 71 | |
---|
| 72 | T = f.get_time() |
---|
| 73 | |
---|
| 74 | if time_min is None: |
---|
| 75 | time_min = min(T) |
---|
| 76 | else: |
---|
| 77 | if time_min < min(T): |
---|
| 78 | msg = 'Minimum time entered not correct - please try again' |
---|
| 79 | raise Exception, msg |
---|
| 80 | |
---|
| 81 | if time_max is None: |
---|
| 82 | time_max = max(T) |
---|
| 83 | else: |
---|
| 84 | if time_max > max(T): |
---|
| 85 | msg = 'Maximum time entered not correct - please try again' |
---|
| 86 | raise Exception, msg |
---|
| 87 | |
---|
| 88 | lines[0] = lines[0].strip() +\ |
---|
| 89 | ',MAX INUNDATION DEPTH (m)' +\ |
---|
| 90 | ',MAX MOMENTUM (m^2/s)' +\ |
---|
| 91 | ',MAX SPEED (m/s) \n' |
---|
| 92 | |
---|
| 93 | for k, g in enumerate(buildings): |
---|
| 94 | print 'Building %d of %d' %(k, len(buildings)) |
---|
| 95 | max_depth = 0 |
---|
| 96 | max_momentum = 0 |
---|
| 97 | max_velocity = 0 |
---|
| 98 | for i, t in enumerate(T): |
---|
| 99 | w = f(t, point_id = k)[0] |
---|
| 100 | z = f(t, point_id = k)[1] |
---|
| 101 | uh = f(t, point_id = k)[2] |
---|
| 102 | vh = f(t, point_id = k)[3] |
---|
| 103 | depth = w-z |
---|
| 104 | m = sqrt(uh*uh + vh*vh) |
---|
| 105 | vel = m / (depth + 1.e-30) |
---|
| 106 | if depth > max_depth: max_depth = w-z |
---|
| 107 | if m > max_momentum: max_momentum = m |
---|
| 108 | if vel > max_velocity: max_velocity = vel |
---|
| 109 | |
---|
| 110 | lines[k+1] = lines[k+1].strip() +\ |
---|
| 111 | ',%f' %max_depth +\ |
---|
| 112 | ',%f' %max_momentum +\ |
---|
| 113 | ',%f\n' %max_velocity |
---|
| 114 | |
---|
| 115 | augbuildingsfile = file_loc + 'augmented_buildings.csv' |
---|
| 116 | |
---|
| 117 | fid = open(augbuildingsfile, 'w') |
---|
| 118 | for line in lines: |
---|
| 119 | fid.write(line) |
---|
| 120 | fid.close() |
---|
| 121 | |
---|
| 122 | print '\n Augmented building file written to %s \n' %augbuildingsfile |
---|