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 = '20060703_062936' # HAT |
---|
32 | #timestampdir = '20060703_062753' # LAT |
---|
33 | timestampdir = '20060703_062900' # MSL |
---|
34 | file_loc = project.outputdir + timestampdir + sep |
---|
35 | swwfile = file_loc + project.basename + '.sww' |
---|
36 | buildings_filename = project.buildings_filename |
---|
37 | time_min = None |
---|
38 | time_max = None |
---|
39 | |
---|
40 | def get_buildings_from_file(filename,bounding_polygon): |
---|
41 | from coordinate_transforms.redfearn import redfearn |
---|
42 | fid = open(filename) |
---|
43 | lines = fid.readlines() |
---|
44 | fid.close() |
---|
45 | buildings = [] |
---|
46 | line1 = lines[0] |
---|
47 | line11 = line1.split(',') |
---|
48 | for i in range(len(line11)): |
---|
49 | if line11[i].strip('\n').strip(' ') == 'LATITUDE': lat_index = i |
---|
50 | if line11[i].strip('\n').strip(' ') == 'LONGITUDE': lon_index = i |
---|
51 | if line11[i].strip('\n').strip(' ') == 'BUILDING_N': name_index = i |
---|
52 | for line in lines[1:]: |
---|
53 | fields = line.split(',') |
---|
54 | lat = float(fields[lat_index]) |
---|
55 | lon = float(fields[lon_index]) |
---|
56 | z, easting, northing = redfearn(lat,lon) |
---|
57 | utm_pt = [easting, northing] |
---|
58 | buildings.append(utm_pt) |
---|
59 | loc = fields[name_index] |
---|
60 | |
---|
61 | return buildings, lines |
---|
62 | |
---|
63 | print '\n Buildings obtained from: %s \n' %buildings_filename |
---|
64 | buildings, lines = get_buildings_from_file(buildings_filename,project.polyAll) |
---|
65 | |
---|
66 | sww_quantity = ['stage', 'elevation', 'xmomentum', 'ymomentum'] |
---|
67 | |
---|
68 | f = file_function(swwfile, |
---|
69 | quantities = sww_quantity, |
---|
70 | interpolation_points = buildings, |
---|
71 | verbose = True, |
---|
72 | use_cache = True) |
---|
73 | |
---|
74 | T = f.get_time() |
---|
75 | |
---|
76 | if time_min is None: |
---|
77 | time_min = min(T) |
---|
78 | else: |
---|
79 | if time_min < min(T): |
---|
80 | msg = 'Minimum time entered not correct - please try again' |
---|
81 | raise Exception, msg |
---|
82 | |
---|
83 | if time_max is None: |
---|
84 | time_max = max(T) |
---|
85 | else: |
---|
86 | if time_max > max(T): |
---|
87 | msg = 'Maximum time entered not correct - please try again' |
---|
88 | raise Exception, msg |
---|
89 | |
---|
90 | lines[0] = lines[0].strip() +\ |
---|
91 | ',MAX INUNDATION DEPTH (m)' +\ |
---|
92 | ',MAX MOMENTUM (m^2/s) \n' |
---|
93 | |
---|
94 | from utilities.polygon import inside_polygon |
---|
95 | N = len(buildings) |
---|
96 | for k, g in enumerate(buildings): |
---|
97 | if k%((N+10)/10)==0: print 'Building %d of %d' %(k, N) |
---|
98 | max_depth = 0.0 |
---|
99 | max_momentum = 0.0 |
---|
100 | max_velocity = 0.0 |
---|
101 | zero_depth = 0.0 |
---|
102 | zero_momentum = 0.0 |
---|
103 | if inside_polygon(g,project.polyAll) == True: |
---|
104 | for i, t in enumerate(T): |
---|
105 | w = f(t, point_id = k)[0] |
---|
106 | z = f(t, point_id = k)[1] |
---|
107 | uh = f(t, point_id = k)[2] |
---|
108 | vh = f(t, point_id = k)[3] |
---|
109 | depth = w-z |
---|
110 | m = sqrt(uh*uh + vh*vh) |
---|
111 | #vel = m / (depth + 1.e-30) |
---|
112 | if depth > max_depth: max_depth = depth |
---|
113 | if m > max_momentum: max_momentum = m |
---|
114 | else: |
---|
115 | max_depth = 0.0 |
---|
116 | max_momentum = 0.0 |
---|
117 | |
---|
118 | lines[k+1] = lines[k+1].strip() +\ |
---|
119 | ',%f' %max_depth +\ |
---|
120 | ',%f\n' %max_momentum |
---|
121 | |
---|
122 | |
---|
123 | augbuildingsfile = file_loc + 'augmented_buildings.csv' |
---|
124 | |
---|
125 | fid = open(augbuildingsfile, 'w') |
---|
126 | for line in lines: |
---|
127 | fid.write(line) |
---|
128 | fid.close() |
---|
129 | |
---|
130 | print '\n Augmented building file written to %s \n' %augbuildingsfile |
---|