source: inundation/ga/storm_surge/pyvolution/interpolate_sww.py @ 1093

Last change on this file since 1093 was 934, checked in by duncan, 20 years ago

comments

File size: 6.2 KB
Line 
1""" Interpolation of a sww file.
2Used to interpolate height information from sww files.
3
4When using as a stand alone application,
5Input;
6 - The sww file with stage and bed elevation information
7 - An xya file specifying the points (x,y) where the height values
8   (w.r.t. time) are needed.
9
10Ouput;
11An xya file with x,y and height values w.r.t. time.
12
13The first row of the output xya file has the time value for each height
14Each following row has the format x,y,[height,]
15
16NOTE: stage = bed elevation + height
17
18   Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou
19   Geoscience Australia, 2004.   
20"""
21
22from Numeric import transpose
23from least_squares import Interpolation
24
25DEFAULT_QUANTITY = "depth"
26
27class Interpolate_sww:
28    def __init__(self,file_name, quantity_name):
29
30        #It's bad to have the quantity_name passed in here.
31        # But it works with how the program is currently used.
32        # Refactor when necessary. - DSG
33       
34        x, y, volumes, time, quantity = self.read_sww(file_name, quantity_name)
35        vertex_coordinates = transpose([x,y])
36       
37        if False:
38            print "****************************"
39            print "x "
40            print x
41            print "****************************"
42            print "Y "
43            print y
44            print "****************************"
45            print "V "
46            print volumes
47            print "****************************"
48            print "Time "
49            print time
50            print "****************************"
51            print "quantity "
52            print quantity
53            print "****************************"
54            print "vertex_coordinates"
55            print vertex_coordinates
56            print "****************************"
57       
58        self.interp = Interpolation(vertex_coordinates, volumes, alpha=0)
59        self.time = time
60
61        self.quantity_name = quantity_name
62        self.quantity = quantity
63       
64    def interpolate_xya(self,file_name):
65        """
66        Given a point file, interpolate the height w.r.t. time at the points
67        specified in the point file.
68
69        Input;
70        file_name - the xya file
71        """
72        self.point_coordinates = self.read_xya(file_name)
73        self.interp.build_interpolation_matrix_A(self.point_coordinates)
74        self.interpolated_quantity = self.interp.interpolate(transpose(self.quantity))
75       
76    def read_sww(self,file_name, quantity_name):
77        """
78        Read in an sww file.
79
80        Input;
81        file_name - the sww file
82
83        Output;
84        x - Vector of x values
85        y - Vector of y values
86        z - Vector of bed elevation
87        volumes - Array.  Each row has 3 values, representing
88                  the vertices that define the volume
89        time - Vector of the times where there is stage information
90        stage - array with respect to time and vertices (x,y)
91        """
92       
93        #FIXME Have this reader as part of data_manager?
94
95        from Scientific.IO.NetCDF import NetCDFFile             
96           
97        #Check contents
98        #Get NetCDF
99        fid = NetCDFFile(file_name, 'r') 
100     
101        # Get the variables
102        x = fid.variables['x'][:]
103        y = fid.variables['y'][:]
104        volumes = fid.variables['volumes'][:] 
105        time = fid.variables['time'][:]
106        try:
107            if quantity_name == 'depth':
108                z = fid.variables['elevation'][:]
109                stage = fid.variables['stage'][:,:]
110                quantity = stage - z  # 2D, using broadcasting
111                #print quantity
112            else:
113                quantity = fid.variables[quantity_name][:,:]   # 2D
114        except (KeyError, IndexError),e:
115            fid.close()
116            raise KeyError
117           
118        fid.close()
119        return x, y, volumes, time, quantity
120
121    def read_xya(self,point_file):
122        """
123        Read in an xya file.
124
125        Input;
126        point_file - the xya file
127
128        Output;
129        point_coordinates - the points (x,y) where the depth values
130                             (w.r.t. time) are needed
131        """
132        from load_mesh.loadASCII import load_points_file
133       
134        point_dict = load_points_file(point_file)
135        return point_dict['pointlist']
136
137
138    def write_depth_xya(self,point_file, delimiter = ','):
139        """
140        pre condition:
141        point attributes have been determined
142        (interpolate_xya has been called)
143        The time list is defined
144        """
145        from load_mesh.loadASCII import  export_xya_file
146
147        #create the first line, giving the times
148        if len(self.time) != 0:
149            for i in range(len(self.time)):
150                if i == 0:
151                    title = str(self.time[0])
152                else:
153                    title = title + delimiter + str(self.time[i])
154        else:
155            title  = ""
156       
157        #print ">" +  str(self.time) + "<"
158        #print ">" + title + "<"
159       
160        xya_dict = {}
161        xya_dict['pointlist'] = self.point_coordinates
162        xya_dict['pointattributelist'] = self.interpolated_quantity
163       
164        export_xya_file(point_file, xya_dict, title, delimiter = delimiter)
165       
166#-------------------------------------------------------------
167if __name__ == "__main__":
168    """
169    Load in an sww file and an xya file and return an xya file
170    """
171    import os, sys
172    usage = "usage: %s pyvolution_results.sww points.xya depth.xya [depth|stage|(other quantities)]" %         os.path.basename(sys.argv[0])
173    if len(sys.argv) < 4:
174        print usage
175    else:
176        sww_file = sys.argv[1]
177        point_file_in = sys.argv[2]
178        point_file_out = sys.argv[3]
179        if len(sys.argv) == 5:
180            quantity_name = sys.argv[4]
181        else:
182            quantity_name = DEFAULT_QUANTITY   
183        #print "quantity",quantity
184        try:
185            interp = Interpolate_sww(sww_file, quantity_name)
186        except KeyError:
187            print "Error: Unknown quantity"
188            sys.exit(1)
189
190        interp.interpolate_xya(point_file_in)
191        interp.write_depth_xya(point_file_out)
192
193
194
195
196
197
198
199
200
Note: See TracBrowser for help on using the repository browser.