source: production/onslow_2006/get_building_inundation.py @ 3031

Last change on this file since 3031 was 2944, checked in by sexton, 19 years ago

updates

File size: 4.1 KB
Line 
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
25from pyvolution.util import file_function
26import project
27from os import sep
28from math import sqrt
29
30# Inputs
31timestampdir = '20060426_004129' # HAT
32#timestampdir = '20060426_004237' # LAT
33file_loc = project.outputdir + timestampdir + sep
34swwfile = file_loc + project.basename + '.sww'
35buildings_filename = project.buildings_filename
36time_min = None
37time_max = None
38
39def get_buildings_from_file(filename,bounding_polygon):
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        utm_pt = [easting, northing]
57        buildings.append(utm_pt)
58        loc = fields[name_index]
59   
60    return buildings, lines
61
62print '\n Buildings obtained from: %s \n' %buildings_filename
63buildings, lines = get_buildings_from_file(buildings_filename,project.polyAll)
64
65sww_quantity = ['stage', 'elevation', 'xmomentum', 'ymomentum']
66
67f = file_function(swwfile,
68                  quantities = sww_quantity,
69                  interpolation_points = buildings,
70                  verbose = True,
71                  use_cache = True)
72
73T = f.get_time()
74
75if time_min is None:
76    time_min = min(T)
77else:
78    if time_min < min(T):
79        msg = 'Minimum time entered not correct - please try again'
80        raise Exception, msg
81
82if time_max is None:
83    time_max = max(T)
84else:
85    if time_max > max(T):
86        msg = 'Maximum time entered not correct - please try again'
87        raise Exception, msg
88
89lines[0] = lines[0].strip() +\
90           ',MAX INUNDATION DEPTH (m)' +\
91           ',MAX MOMENTUM (m^2/s) \n'
92
93from utilities.polygon import inside_polygon
94N = len(buildings)
95for k, g in enumerate(buildings):
96    if k%((N+10)/10)==0: print 'Building %d of %d' %(k, N)
97    max_depth = 0.0
98    max_momentum = 0.0
99    max_velocity = 0.0
100    zero_depth = 0.0
101    zero_momentum = 0.0
102    if inside_polygon(g,project.polyAll) == True:   
103        for i, t in enumerate(T):
104            w = f(t, point_id = k)[0]
105            z = f(t, point_id = k)[1]
106            uh = f(t, point_id = k)[2]
107            vh = f(t, point_id = k)[3]
108            depth = w-z
109            m = sqrt(uh*uh + vh*vh)   
110            #vel = m / (depth + 1.e-30)
111            if depth > max_depth: max_depth = depth
112            if m > max_momentum: max_momentum = m
113    else:
114        max_depth = 0.0
115        max_momentum = 0.0
116
117    lines[k+1] = lines[k+1].strip() +\
118                 ',%f' %max_depth +\
119                 ',%f\n' %max_momentum
120
121
122augbuildingsfile = file_loc + 'augmented_buildings.csv'
123
124fid = open(augbuildingsfile, 'w')
125for line in lines:
126    fid.write(line)
127fid.close()
128
129print '\n Augmented building file written to %s \n' %augbuildingsfile
Note: See TracBrowser for help on using the repository browser.