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

Last change on this file since 626 was 608, checked in by duncan, 20 years ago

change least_squares algorithm, so it increases its cell search, if the point is not within any of the initial cells triangles. 1st cut.

File size: 5.9 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
25
26class Interpolate_sww:
27    def __init__(self,file_name):
28
29       
30        x, y,z, volumes, time, stage = self.read_sww(file_name)       
31
32        vertex_coordinates = transpose([x,y])
33       
34        if False:
35            print "****************************"
36            print "x "
37            print x
38            print "****************************"
39            print "Y "
40            print y
41            print "****************************"
42            print "Z "
43            print z
44            print "****************************"
45            print "V "
46            print volumes
47            print "****************************"
48            print "Time "
49            print time
50            print "****************************"
51            print "Stage "
52            print stage
53            print "****************************"
54            print "vertex_coordinates"
55            print vertex_coordinates
56            print "****************************"
57
58        self.interp = Interpolation(vertex_coordinates, volumes )
59        self.time = time
60        self.stage = transpose(stage)
61        self.bed_elevation = z
62        #print "self.stage",self.stage
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        #
73        self.point_coordinates = self.read_xya(file_name)
74        self.interp.build_interpolation_matrix_A(self.point_coordinates)
75
76        #Change this if you want to generalise this application -
77        # This is height specific
78        point_stage =  self.interp.interpolate( self.stage)
79        point_bed_elevation = self.interp.interpolate( self.bed_elevation)
80        self.point_heights = transpose(transpose(point_stage) - point_bed_elevation)
81       
82        #print "self.point_heights", self.point_heights
83       
84    def read_sww(self,file_name):
85        """
86        Read in an sww file.
87
88        Input;
89        file_name - the sww file
90
91        Output;
92        x - Vector of x values
93        y - Vector of y values
94        z - Vector of bed elevation
95        volumes - Array.  Each row has 3 values, representing
96                  the vertices that define the volume
97        time - Vector of the times where there is stage information
98        stage - array with respect to time and vertices (x,y)
99        """
100       
101        #FIXME Have this reader as part of data_manager?
102
103        #import time, os
104        #from Numeric import array, zeros, allclose, Float, concatenate
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        z = fid.variables['z'][:] 
115        volumes = fid.variables['volumes'][:] 
116        time = fid.variables['time'][:]
117        stage = fid.variables['stage'][:,:]   # 2D
118       
119        fid.close()
120       
121        return x, y, z, volumes, time, stage
122
123    def read_xya(self,point_file):
124        """
125        Read in an sww file.
126
127        Input;
128        point_file - the xya file
129
130        Output;
131        point_coordinates - the points (x,y) where the height values
132                             (w.r.t. time) are needed
133        """
134        from load_mesh.loadASCII import  load_xya_file
135       
136        point_dict = load_xya_file(point_file)
137        return point_dict['pointlist']
138
139
140    def write_point_heights_xya(self,point_file, delimiter = ','):
141        """
142        pre condition:
143        point attributes have been determined
144        (interpolate_xya has been called)
145        The time list is defined
146        """
147        from load_mesh.loadASCII import  export_xya_file
148
149        #create the first line, giving the times
150        if len(self.time) != 0:
151            for i in range(len(self.time)):
152                if i == 0:
153                    title = str(self.time[0])
154                else:
155                    title = title + delimiter + str(self.time[i])
156        else:
157            title  = ""
158       
159        #print ">" +  str(self.time) + "<"
160        #print ">" + title + "<"
161       
162        xya_dict = {}
163        xya_dict['pointlist'] = self.point_coordinates
164        xya_dict['pointattributelist'] = self.point_heights
165       
166        export_xya_file(point_file, xya_dict, title, delimiter = delimiter)
167       
168#-------------------------------------------------------------
169if __name__ == "__main__":
170    """
171    Load in an sww file and an xya file and return an xya file
172    """
173    import os, sys
174    usage = "usage: %s pyvolution_results.sww points.xya point_heights.xya" %         os.path.basename(sys.argv[0])
175
176    if len(sys.argv) < 4:
177        print usage
178    else:
179        sww_file = sys.argv[1]
180        point_file_in = sys.argv[2]
181        point_file_out = sys.argv[3]
182       
183        interp = Interpolate_sww(sww_file)
184        interp.interpolate_xya(point_file_in)
185        interp.write_point_heights_xya(point_file_out)
186
187
188
189
190
191
192
193
194
Note: See TracBrowser for help on using the repository browser.