Changeset 356


Ignore:
Timestamp:
Oct 6, 2004, 10:50:22 AM (20 years ago)
Author:
duncan
Message:

minor work on interpolate sww

Location:
inundation/ga/storm_surge/pyvolution
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • inundation/ga/storm_surge/pyvolution/interpolate_sww.py

    r354 r356  
    11""" Interpolation of a sww file.
     2Used to interpolate height information from sww files.
    23
     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
    317
    418   Ole Nielsen, Stephen Roberts, Duncan Gray, Christopher Zoppou
     
    620"""
    721
    8 #from general_mesh import General_Mesh
    9 #from Numeric import zeros, array, Float, Int, dot, transpose
    10 #from LinearAlgebra import solve_linear_equations
     22from Numeric import transpose
     23from least_squares import Interpolation
    1124
    12 
    13 from Numeric import transpose
    14 
    15 # def read_sww(file_name):
    16 #         import time, os
    17 #         from Numeric import array, zeros, allclose, Float, concatenate
    18 #         from Scientific.IO.NetCDF import NetCDFFile           
    19 
    20 #         #Check contents
    21 #         #Get NetCDF
    22 #         fid = NetCDFFile(file_name, 'r')
    23      
    24 #         # Get the variables
    25 #         x = fid.variables['x'][:]
    26 #         y = fid.variables['y'][:]
    27 #         # z = fid.variables['z'][:] # We don't care about the bed elevation
    28 #         time = fid.variables['time'][:]
    29 #         stage = fid.variables['stage'][:,:]   # 2D   
    30        
    31        
    32 #         return x,y,time,stage
    3325
    3426class Interpolate_sww:
    3527    def __init__(self,file_name):
    36         from least_squares import Interpolation
    3728
    3829       
    39         x, y, volumes, time, stage = self.read_sww(file_name)       
     30        x, y,z, volumes, time, stage = self.read_sww(file_name)       
    4031
    4132        vertex_coordinates = transpose([x,y])
    4233       
    43         if True:
     34        if False:
    4435            print "****************************"
    45             print "x ",x
     36            print "x "
     37            print x
    4638            print "****************************"
    47             print "Y ",y
     39            print "Y "
     40            print y
    4841            print "****************************"
    49             print "V ",volumes
     42            print "Z "
     43            print z
    5044            print "****************************"
    51             print "Time ", time
     45            print "V "
     46            print volumes
    5247            print "****************************"
    53             print "Stage ",stage
     48            print "Time "
     49            print time
    5450            print "****************************"
    55             print "vertex_coordinates",vertex_coordinates
     51            print "Stage "
     52            print stage
     53            print "****************************"
     54            print "vertex_coordinates"
     55            print vertex_coordinates
    5656            print "****************************"
    5757
    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
    5963       
     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.
    6068
     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       
    6185    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        """
    62101       
    63     #FIXME Have this reader as part of data_manager?
     102        #FIXME Have this reader as part of data_manager?
    64103
    65104        #import time, os
     
    74113        x = fid.variables['x'][:]
    75114        y = fid.variables['y'][:]
    76         # z = fid.variables['z'][:] # We don't care about the bed elevation
     115        z = fid.variables['z'][:]
    77116        volumes = fid.variables['volumes'][:]
    78117        time = fid.variables['time'][:]
     
    81120        fid.close()
    82121       
    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']
    84139
    85140
     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
    86149
     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)
    87168       
    88169#-------------------------------------------------------------
    89170if __name__ == "__main__":
    90171    """
    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
    92173    """
    93174    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])
    95176
    96     if len(sys.argv) < 2:
     177    if len(sys.argv) < 4:
    97178        print usage
    98179    else:
    99180        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)
    104187
    105188
     
    111194
    112195
    113 
    114 
  • inundation/ga/storm_surge/pyvolution/least_squares.py

    r353 r356  
    2525from LinearAlgebra import solve_linear_equations
    2626
     27try:
     28    from util import gradient
     29
     30except 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
    2745ALPHA_INITIAL = 0.001
    2846
     
    238256        #"element stiffness matrices:
    239257
    240         from util import gradient
    241258       
    242259        m = self.mesh.coordinates.shape[0] #Nbr of basis functions (1/vertex)
  • inundation/ga/storm_surge/pyvolution/test_interpolate_sww.py

    r354 r356  
    1414from data_manager import *
    1515#from config import epsilon
     16
    1617
    1718class dataTestCase(unittest.TestCase):
     
    136137 
    137138    def test_interpolate_sww(self):
    138         """Not a test, rather a look at the sww format
     139        """Not reaa unit test, rather a system test for
    139140        """
    140141
    141142        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     
    144148
    145149        self.domain.filename = 'datatest' + str(time.time())
     
    154158        sww.store_timestep('level')
    155159
    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       
    158206       
    159207        #Cleanup
    160208        os.remove(sww.filename)
     209        os.remove(point_file_out)
    161210 
    162211#-------------------------------------------------------------
Note: See TracChangeset for help on using the changeset viewer.