Changeset 356
- Timestamp:
- Oct 6, 2004, 10:50:22 AM (20 years ago)
- Location:
- inundation/ga/storm_surge/pyvolution
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
inundation/ga/storm_surge/pyvolution/interpolate_sww.py
r354 r356 1 1 """ Interpolation of a sww file. 2 Used to interpolate height information from sww files. 2 3 4 When using as a stand alone application, 5 Input; 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 10 Ouput; 11 An xya file with x,y and height values w.r.t. time. 12 13 The first row of the output xya file has the time value for each height 14 Each following row has the format x,y,[height,] 15 16 NOTE: stage = bed elevation + height 3 17 4 18 Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou … … 6 20 """ 7 21 8 #from general_mesh import General_Mesh 9 #from Numeric import zeros, array, Float, Int, dot, transpose 10 #from LinearAlgebra import solve_linear_equations 22 from Numeric import transpose 23 from least_squares import Interpolation 11 24 12 13 from Numeric import transpose14 15 # def read_sww(file_name):16 # import time, os17 # from Numeric import array, zeros, allclose, Float, concatenate18 # from Scientific.IO.NetCDF import NetCDFFile19 20 # #Check contents21 # #Get NetCDF22 # fid = NetCDFFile(file_name, 'r')23 24 # # Get the variables25 # x = fid.variables['x'][:]26 # y = fid.variables['y'][:]27 # # z = fid.variables['z'][:] # We don't care about the bed elevation28 # time = fid.variables['time'][:]29 # stage = fid.variables['stage'][:,:] # 2D30 31 32 # return x,y,time,stage33 25 34 26 class Interpolate_sww: 35 27 def __init__(self,file_name): 36 from least_squares import Interpolation37 28 38 29 39 x, y, volumes, time, stage = self.read_sww(file_name)30 x, y,z, volumes, time, stage = self.read_sww(file_name) 40 31 41 32 vertex_coordinates = transpose([x,y]) 42 33 43 if True:34 if False: 44 35 print "****************************" 45 print "x ",x 36 print "x " 37 print x 46 38 print "****************************" 47 print "Y ",y 39 print "Y " 40 print y 48 41 print "****************************" 49 print "V ",volumes 42 print "Z " 43 print z 50 44 print "****************************" 51 print "Time ", time 45 print "V " 46 print volumes 52 47 print "****************************" 53 print "Stage ",stage 48 print "Time " 49 print time 54 50 print "****************************" 55 print "vertex_coordinates",vertex_coordinates 51 print "Stage " 52 print stage 53 print "****************************" 54 print "vertex_coordinates" 55 print vertex_coordinates 56 56 print "****************************" 57 57 58 interp = Interpolation(vertex_coordinates, volumes ) 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 59 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. 60 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 61 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 """ 62 101 63 #FIXME Have this reader as part of data_manager?102 #FIXME Have this reader as part of data_manager? 64 103 65 104 #import time, os … … 74 113 x = fid.variables['x'][:] 75 114 y = fid.variables['y'][:] 76 # z = fid.variables['z'][:] # We don't care about the bed elevation115 z = fid.variables['z'][:] 77 116 volumes = fid.variables['volumes'][:] 78 117 time = fid.variables['time'][:] … … 81 120 fid.close() 82 121 83 return x, y, volumes, time, stage 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'] 84 139 85 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 86 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 = 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) 87 168 88 169 #------------------------------------------------------------- 89 170 if __name__ == "__main__": 90 171 """ 91 Load in an sww file and an xya file and return ??172 Load in an sww file and an xya file and return an xya file 92 173 """ 93 174 import os, sys 94 usage = "usage: %s pyvolution_results.sww point.xya " % os.path.basename(sys.argv[0])175 usage = "usage: %s pyvolution_results.sww point.xya point_attributes_out.xya" % os.path.basename(sys.argv[0]) 95 176 96 if len(sys.argv) < 2:177 if len(sys.argv) < 4: 97 178 print usage 98 179 else: 99 180 sww_file = sys.argv[1] 100 point_file = sys.argv[2] 101 #mesh_output_file = sys.argv[3] 102 #fit_to_mesh_file(mesh_file, point_file, mesh_output_file, alpha) 103 c = Interpolate_sww(sww_file) 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) 104 187 105 188 … … 111 194 112 195 113 114 -
inundation/ga/storm_surge/pyvolution/least_squares.py
r353 r356 25 25 from LinearAlgebra import solve_linear_equations 26 26 27 try: 28 from util import gradient 29 30 except ImportError, e: 31 #FIXME reduce the dependency of modules in pyvolution 32 def gradient(x0, y0, x1, y1, x2, y2, q0, q1, q2): 33 """ 34 """ 35 36 det = (y2-y0)*(x1-x0) - (y1-y0)*(x2-x0) 37 a = (y2-y0)*(q1-q0) - (y1-y0)*(q2-q0) 38 a /= det 39 40 b = (x1-x0)*(q2-q0) - (x2-x0)*(q1-q0) 41 b /= det 42 43 return a, b 44 27 45 ALPHA_INITIAL = 0.001 28 46 … … 238 256 #"element stiffness matrices: 239 257 240 from util import gradient241 258 242 259 m = self.mesh.coordinates.shape[0] #Nbr of basis functions (1/vertex) -
inundation/ga/storm_surge/pyvolution/test_interpolate_sww.py
r354 r356 14 14 from data_manager import * 15 15 #from config import epsilon 16 16 17 17 18 class dataTestCase(unittest.TestCase): … … 136 137 137 138 def test_interpolate_sww(self): 138 """Not a test, rather a look at the sww format139 """Not reaa unit test, rather a system test for 139 140 """ 140 141 141 142 import time, os 142 from Numeric import array, zeros, allclose, Float, concatenate 143 from Scientific.IO.NetCDF import NetCDFFile 143 from Numeric import array, zeros, allclose, Float, concatenate, \ 144 transpose 145 from Scientific.IO.NetCDF import NetCDFFile 146 import tempfile 147 from load_mesh.loadASCII import load_xya_file 144 148 145 149 self.domain.filename = 'datatest' + str(time.time()) … … 154 158 sww.store_timestep('level') 155 159 156 print "self.domain.filename",self.domain.filename 157 c = Interpolate_sww(sww.filename) 160 #print "self.domain.filename",self.domain.filename 161 interp = Interpolate_sww(sww.filename) 162 163 assert allclose(interp.time,[0.0,2.0]) 164 answer = [ 0.15, 0.1, 0., -0.3, -0.35, -0.4, -0.7, -0.8, -0.850] 165 #print "answer",answer 166 #print interp.stage[0] 167 stage_t = transpose(interp.stage) 168 assert allclose(stage_t[0], answer) 169 assert allclose(stage_t[1],stage_t[0]) 170 171 # create an .xya file 172 point_file = tempfile.mktemp(".xya") 173 fd = open(point_file,'w') 174 fd.write("# demo \n 0.0, 0.6,2.,4 \n 0.0, 0.9,4,8 \n 0.0,0.1,4.,8 \n 0.4,1.0,4.,8 \n") 175 fd.close() 176 177 interp.interpolate_xya(point_file) 178 179 180 answer = [[0.08, 0.08], [0.02, 0.02], [0.14, 0.14], [.08,.08]] 181 #print "answer",answer 182 assert allclose(interp.point_heights,answer) 183 184 # create an output .xya file 185 point_file_out = tempfile.mktemp(".xya") 186 interp.write_point_heights_xya(point_file_out) 187 188 #check the output file 189 xya_dict = load_xya_file(point_file_out) 190 191 assert allclose(interp.point_coordinates, xya_dict['pointlist']) 192 assert allclose(interp.point_heights, 193 xya_dict['pointattributelist'] ) 194 195 title = xya_dict['title'].split('\n') 196 #print "title",title 197 string_list = title[0].split(',') # assume a title has only one line 198 time_list = [] 199 for time in string_list: 200 time_list.append(float(time)) 201 #print "interp.time", interp.time 202 #print "time_list", time_list 203 assert allclose(interp.time, 204 time_list) 205 158 206 159 207 #Cleanup 160 208 os.remove(sww.filename) 209 os.remove(point_file_out) 161 210 162 211 #-------------------------------------------------------------
Note: See TracChangeset
for help on using the changeset viewer.