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
RevLine 
[354]1""" Interpolation of a sww file.
[356]2Used to interpolate height information from sww files.
[354]3
[356]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.
[354]9
[356]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
[354]18   Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou
19   Geoscience Australia, 2004.   
20"""
21
22from Numeric import transpose
[356]23from least_squares import Interpolation
[354]24
[631]25DEFAULT_QUANTITY = "height"
[354]26
27class Interpolate_sww:
[631]28    def __init__(self,file_name, quantity_name):
[354]29
[631]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
[354]33       
[631]34        x, y, volumes, time, quantity = self.read_sww(file_name, quantity_name)
[354]35        vertex_coordinates = transpose([x,y])
36       
[356]37        if False:
[354]38            print "****************************"
[356]39            print "x "
40            print x
[354]41            print "****************************"
[356]42            print "Y "
43            print y
[354]44            print "****************************"
[356]45            print "V "
46            print volumes
[354]47            print "****************************"
[356]48            print "Time "
49            print time
[354]50            print "****************************"
[631]51            print "quantity "
52            print quantity
[354]53            print "****************************"
[356]54            print "vertex_coordinates"
55            print vertex_coordinates
56            print "****************************"
[354]57
[754]58        self.interp = Interpolation(vertex_coordinates, volumes, alpha=0)
[356]59        self.time = time
[631]60
61        self.quantity_name = quantity_name
62        self.quantity = quantity
[354]63       
[356]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.
[354]68
[356]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)
[754]74        self.interpolated_quantity = self.interp.interpolate(transpose(self.quantity))
[356]75       
[631]76    def read_sww(self,file_name, quantity_name):
[356]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        """
[354]92       
[356]93        #FIXME Have this reader as part of data_manager?
[354]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'][:]
[631]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           
[354]117        fid.close()
[631]118        return x, y, volumes, time, quantity
[354]119
[356]120    def read_xya(self,point_file):
121        """
122        Read in an sww file.
[354]123
[356]124        Input;
125        point_file - the xya file
[354]126
[356]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
[354]132       
[356]133        point_dict = load_xya_file(point_file)
134        return point_dict['pointlist']
135
136
[628]137    def write_depth_xya(self,point_file, delimiter = ','):
[356]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:
[374]150                    title = str(self.time[0])
[356]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
[631]161        xya_dict['pointattributelist'] = self.interpolated_quantity
[356]162       
[600]163        export_xya_file(point_file, xya_dict, title, delimiter = delimiter)
[356]164       
[354]165#-------------------------------------------------------------
166if __name__ == "__main__":
167    """
[356]168    Load in an sww file and an xya file and return an xya file
[354]169    """
170    import os, sys
[631]171    usage = "usage: %s pyvolution_results.sww points.xya depth.xya [height|stage|(other quantities)]" %         os.path.basename(sys.argv[0])
[356]172    if len(sys.argv) < 4:
[354]173        print usage
174    else:
175        sww_file = sys.argv[1]
[356]176        point_file_in = sys.argv[2]
177        point_file_out = sys.argv[3]
[631]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
[356]189        interp.interpolate_xya(point_file_in)
[628]190        interp.write_depth_xya(point_file_out)
[354]191
192
193
194
195
196
197
198
199
Note: See TracBrowser for help on using the repository browser.