source: production/onslow_2006/get_building_inundation.py @ 3290

Last change on this file since 3290 was 3285, checked in by sexton, 18 years ago

scripts to interrogate onslow outputs

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
31#timestampdir = '20060704_063005' # HAT
32#timestampdir = '20060704_063112' # LAT
33timestampdir = '20060704_063234' # MSL
34file_loc = project.outputdir + timestampdir + sep
35swwfile = file_loc + project.basename + '.sww'
36buildings_filename = project.buildings_filename
37time_min = None
38time_max = None
39
40def 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
63print '\n Buildings obtained from: %s \n' %buildings_filename
64buildings, lines = get_buildings_from_file(buildings_filename,project.polyAll)
65
66sww_quantity = ['stage', 'elevation', 'xmomentum', 'ymomentum']
67
68f = file_function(swwfile,
69                  quantities = sww_quantity,
70                  interpolation_points = buildings,
71                  verbose = True,
72                  use_cache = True)
73
74T = f.get_time()
75
76if time_min is None:
77    time_min = min(T)
78else:
79    if time_min < min(T):
80        msg = 'Minimum time entered not correct - please try again'
81        raise Exception, msg
82
83if time_max is None:
84    time_max = max(T)
85else:
86    if time_max > max(T):
87        msg = 'Maximum time entered not correct - please try again'
88        raise Exception, msg
89
90lines[0] = lines[0].strip() +\
91           ',MAX INUNDATION DEPTH (m)' +\
92           ',MAX MOMENTUM (m^2/s) \n'
93
94from utilities.polygon import inside_polygon
95N = len(buildings)
96for 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
123augbuildingsfile = file_loc + 'augmented_buildings.csv'
124
125fid = open(augbuildingsfile, 'w')
126for line in lines:
127    fid.write(line)
128fid.close()
129
130print '\n Augmented building file written to %s \n' %augbuildingsfile
Note: See TracBrowser for help on using the repository browser.