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

Last change on this file since 1508 was 1396, checked in by duncan, 19 years ago

load_mesh refactoring

File size: 5.7 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
22##FIXME (DSG-DSG)  no sww file? give a better error message.
23
24from Numeric import transpose
25from least_squares import Interpolation
26
27DEFAULT_QUANTITY = "depth"
28
29class Interpolate_sww:
30    def __init__(self, file_name, quantity_name):
31
32        #It's bad to have the quantity_name passed in here.
33        # But it works with how the program is currently used.
34        # Refactor when necessary. - DSG
35       
36        x, y, volumes, time, quantity = self.read_sww(file_name, quantity_name)
37        vertex_coordinates = transpose([x,y])
38       
39        if False:
40            print "****************************"
41            print "x "
42            print x
43            print "****************************"
44            print "Y "
45            print y
46            print "****************************"
47            print "V "
48            print volumes
49            print "****************************"
50            print "Time "
51            print time
52            print "****************************"
53            print "quantity "
54            print quantity
55            print "****************************"
56            print "vertex_coordinates"
57            print vertex_coordinates
58            print "****************************"
59       
60        self.interp = Interpolation(vertex_coordinates, volumes, alpha=0)
61        self.time = time
62
63        self.quantity_name = quantity_name
64        self.quantity = quantity
65       
66    def interpolate_xya(self, file_name):
67        """
68        Given a point file, interpolate the height w.r.t. time at the points
69        specified in the point file.
70
71        Input;
72        file_name - the xya file
73        """
74       
75        from load_mesh.loadASCII import import_points_file
76       
77        point_dict = import_points_file(file_name)
78        self.point_coordinates = point_dict['pointlist']
79        self.interp.build_interpolation_matrix_A(self.point_coordinates)
80        self.interpolated_quantity_raw = self.interp.interpolate(transpose(self.quantity))
81        self.interpolated_quantity = {}
82        for i,time_slice in enumerate(self.time):
83            self.interpolated_quantity[str(time_slice)] = self.interpolated_quantity_raw[:,i]
84       
85           
86    def read_sww(self,file_name, quantity_name):
87        """
88        Read in an sww file.
89
90        Input;
91        file_name - the sww file
92
93        Output;
94        x - Vector of x values
95        y - Vector of y values
96        z - Vector of bed elevation
97        volumes - Array.  Each row has 3 values, representing
98                  the vertices that define the volume
99        time - Vector of the times where there is stage information
100        stage - array with respect to time and vertices (x,y)
101        """
102       
103        #FIXME Have this reader as part of data_manager?
104
105        from Scientific.IO.NetCDF import NetCDFFile             
106           
107        #Check contents
108        #Get NetCDF
109        fid = NetCDFFile(file_name, 'r') 
110     
111        # Get the variables
112        x = fid.variables['x'][:]
113        y = fid.variables['y'][:]
114        volumes = fid.variables['volumes'][:] 
115        time = fid.variables['time'][:]
116        try:
117            if quantity_name == 'depth':
118                z = fid.variables['elevation'][:]
119                stage = fid.variables['stage'][:,:]
120                quantity = stage - z  # 2D, using broadcasting
121                #print quantity
122            else:
123                quantity = fid.variables[quantity_name][:,:]   # 2D
124        except (KeyError, IndexError),e:
125            fid.close()
126            raise KeyError
127           
128        fid.close()
129        return x, y, volumes, time, quantity
130
131    def write_depth_xya(self,point_file, delimiter = ','):
132        """
133        pre condition:
134        point attributes have been determined
135        (interpolate_xya has been called)
136        The time list is defined
137        """
138        from load_mesh.loadASCII import  export_points_file
139
140       
141        xya_dict = {}
142        xya_dict['pointlist'] = self.point_coordinates
143        xya_dict['attributelist'] = self.interpolated_quantity
144        export_points_file(point_file, xya_dict)
145       
146#-------------------------------------------------------------
147if __name__ == "__main__":
148    """
149    Load in an sww file and an xya file and return an xya file
150    """
151    import os, sys
152    usage = "usage: %s pyvolution_results.sww points.xya depth.xya [depth|stage|(other quantities)]" %         os.path.basename(sys.argv[0])
153    if len(sys.argv) < 4:
154        print usage
155    else:
156        sww_file = sys.argv[1]
157        point_file_in = sys.argv[2]
158        point_file_out = sys.argv[3]
159        if len(sys.argv) == 5:
160            quantity_name = sys.argv[4]
161        else:
162            quantity_name = DEFAULT_QUANTITY   
163        #print "quantity",quantity
164        try:
165            interp = Interpolate_sww(sww_file, quantity_name)
166        except KeyError:
167            print "Error: Unknown quantity"
168            sys.exit(1)
169
170        interp.interpolate_xya(point_file_in)
171        interp.write_depth_xya(point_file_out)
172
173
174
175
176
177
178
179
180
Note: See TracBrowser for help on using the repository browser.