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

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

comments

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        #print "self.interp.A",self.interp.A
76
77        #Change this if you want to generalise this application -
78        # This is height specific
79        point_stage =  self.interp.interpolate( self.stage)
80        point_bed_elevation = self.interp.interpolate( self.bed_elevation)
81        self.point_heights = transpose(transpose(point_stage) - point_bed_elevation)
82       
83        #print "self.point_heights", self.point_heights
84       
85    def read_sww(self,file_name):
86        """
87        Read in an sww file.
88
89        Input;
90        file_name - the sww file
91
92        Output;
93        x - Vector of x values
94        y - Vector of y values
95        z - Vector of bed elevation
96        volumes - Array.  Each row has 3 values, representing
97                  the vertices that define the volume
98        time - Vector of the times where there is stage information
99        stage - array with respect to time and vertices (x,y)
100        """
101       
102        #FIXME Have this reader as part of data_manager?
103
104        #import time, os
105        #from Numeric import array, zeros, allclose, Float, concatenate
106        from Scientific.IO.NetCDF import NetCDFFile             
107           
108        #Check contents
109        #Get NetCDF
110        fid = NetCDFFile(file_name, 'r') 
111     
112        # Get the variables
113        x = fid.variables['x'][:]
114        y = fid.variables['y'][:]
115        z = fid.variables['z'][:] 
116        volumes = fid.variables['volumes'][:] 
117        time = fid.variables['time'][:]
118        stage = fid.variables['stage'][:,:]   # 2D 
119       
120        fid.close()
121       
122        return x, y, z, volumes, time, stage
123
124    def read_xya(self,point_file):
125        """
126        Read in an sww file.
127
128        Input;
129        point_file - the xya file
130
131        Output;
132        point_coordinates - the points (x,y) where the height values
133                             (w.r.t. time) are needed
134        """
135        from load_mesh.loadASCII import  load_xya_file
136       
137        point_dict = load_xya_file(point_file)
138        return point_dict['pointlist']
139
140
141    def write_point_heights_xya(self,point_file, delimiter = ','):
142        """
143        pre condition:
144        point attributes have been determined
145        (interpolate_xya has been called)
146        The time list is defined
147        """
148        from load_mesh.loadASCII import  export_xya_file
149
150        #create the first line, giving the times
151        if len(self.time) != 0:
152            for i in range(len(self.time)):
153                if i == 0:
154                    title = "x,y," + str(self.time[0])
155                else:
156                    title = title + delimiter + str(self.time[i])
157        else:
158            title  = ""
159       
160        #print ">" +  str(self.time) + "<"
161        #print ">" + title + "<"
162       
163        xya_dict = {}
164        xya_dict['pointlist'] = self.point_coordinates
165        xya_dict['pointattributelist'] = self.point_heights
166       
167        export_xya_file( point_file, xya_dict, title, delimiter = delimiter)
168       
169#-------------------------------------------------------------
170if __name__ == "__main__":
171    """
172    Load in an sww file and an xya file and return an xya file
173    """
174    import os, sys
175    usage = "usage: %s pyvolution_results.sww points.xya point_heights.xya" %         os.path.basename(sys.argv[0])
176
177    if len(sys.argv) < 4:
178        print usage
179    else:
180        sww_file = sys.argv[1]
181        point_file_in = sys.argv[2]
182        point_file_out = sys.argv[3]
183       
184        interp = Interpolate_sww(sww_file)
185        interp.interpolate_xya(point_file_in)
186        interp.write_point_heights_xya(point_file_out)
187
188
189
190
191
192
193
194
195
Note: See TracBrowser for help on using the repository browser.