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

Last change on this file since 826 was 754, checked in by ole, 20 years ago

Explicitly set alpha to zero in interpolation script

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 = "height"
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 == 'height':
108                z = fid.variables['z'][:]
109                stage = fid.variables['stage'][:,:]
110                quantity = stage - z  # 2D, using broadcasting
111            else:
112                quantity = fid.variables[quantity_name][:,:]   # 2D
113        except (KeyError, IndexError),e:
114            fid.close()
115            raise KeyError
116           
117        fid.close()
118        return x, y, volumes, time, quantity
119
120    def read_xya(self,point_file):
121        """
122        Read in an sww file.
123
124        Input;
125        point_file - the xya file
126
127        Output;
128        point_coordinates - the points (x,y) where the height values
129                             (w.r.t. time) are needed
130        """
131        from load_mesh.loadASCII import  load_xya_file
132       
133        point_dict = load_xya_file(point_file)
134        return point_dict['pointlist']
135
136
137    def write_depth_xya(self,point_file, delimiter = ','):
138        """
139        pre condition:
140        point attributes have been determined
141        (interpolate_xya has been called)
142        The time list is defined
143        """
144        from load_mesh.loadASCII import  export_xya_file
145
146        #create the first line, giving the times
147        if len(self.time) != 0:
148            for i in range(len(self.time)):
149                if i == 0:
150                    title = str(self.time[0])
151                else:
152                    title = title + delimiter + str(self.time[i])
153        else:
154            title  = ""
155       
156        #print ">" +  str(self.time) + "<"
157        #print ">" + title + "<"
158       
159        xya_dict = {}
160        xya_dict['pointlist'] = self.point_coordinates
161        xya_dict['pointattributelist'] = self.interpolated_quantity
162       
163        export_xya_file(point_file, xya_dict, title, delimiter = delimiter)
164       
165#-------------------------------------------------------------
166if __name__ == "__main__":
167    """
168    Load in an sww file and an xya file and return an xya file
169    """
170    import os, sys
171    usage = "usage: %s pyvolution_results.sww points.xya depth.xya [height|stage|(other quantities)]" %         os.path.basename(sys.argv[0])
172    if len(sys.argv) < 4:
173        print usage
174    else:
175        sww_file = sys.argv[1]
176        point_file_in = sys.argv[2]
177        point_file_out = sys.argv[3]
178        if len(sys.argv) == 5:
179            quantity_name = sys.argv[4]
180        else:
181            quantity_name = DEFAULT_QUANTITY   
182        #print "quantity",quantity
183        try:
184            interp = Interpolate_sww(sww_file, quantity_name)
185        except KeyError:
186            print "Error: Unknown quantity"
187            sys.exit(1)
188
189        interp.interpolate_xya(point_file_in)
190        interp.write_depth_xya(point_file_out)
191
192
193
194
195
196
197
198
199
Note: See TracBrowser for help on using the repository browser.