Ignore:
Timestamp:
Nov 26, 2004, 12:23:25 PM (20 years ago)
Author:
duncan
Message:

user can choose the quantity to interpolate

File:
1 edited

Legend:

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

    r628 r631  
    2323from least_squares import Interpolation
    2424
     25DEFAULT_QUANTITY = "height"
    2526
    2627class Interpolate_sww:
    27     def __init__(self,file_name):
     28    def __init__(self,file_name, quantity_name):
    2829
     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
    2933       
    30         x, y,z, volumes, time, stage = self.read_sww(file_name)       
    31 
     34        x, y, volumes, time, quantity = self.read_sww(file_name, quantity_name)
    3235        vertex_coordinates = transpose([x,y])
    3336       
     
    4043            print y
    4144            print "****************************"
    42             print "Z "
    43             print z
    44             print "****************************"
    4545            print "V "
    4646            print volumes
     
    4949            print time
    5050            print "****************************"
    51             print "Stage "
    52             print stage
     51            print "quantity "
     52            print quantity
    5353            print "****************************"
    5454            print "vertex_coordinates"
     
    5858        self.interp = Interpolation(vertex_coordinates, volumes )
    5959        self.time = time
    60         self.stage = transpose(stage)
    61         self.bed_elevation = z
    62         #print "self.stage",self.stage
     60
     61        self.quantity_name = quantity_name
     62        self.quantity = quantity
    6363       
    6464    def interpolate_xya(self,file_name):
     
    7070        file_name - the xya file
    7171        """
    72         #
    7372        self.point_coordinates = self.read_xya(file_name)
    7473        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.depth = transpose(transpose(point_stage) - point_bed_elevation)
     74        self.interpolated_quantity = self.interp.interpolate( transpose(self.quantity))
    8175       
    82         #print "self.depth", self.depth
    83        
    84     def read_sww(self,file_name):
     76    def read_sww(self,file_name, quantity_name):
    8577        """
    8678        Read in an sww file.
     
    10193        #FIXME Have this reader as part of data_manager?
    10294
    103         #import time, os
    104         #from Numeric import array, zeros, allclose, Float, concatenate
    10595        from Scientific.IO.NetCDF import NetCDFFile             
    10696           
     
    112102        x = fid.variables['x'][:]
    113103        y = fid.variables['y'][:]
    114         z = fid.variables['z'][:]
    115104        volumes = fid.variables['volumes'][:]
    116105        time = fid.variables['time'][:]
    117         stage = fid.variables['stage'][:,:]   # 2D
    118        
     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           
    119117        fid.close()
    120        
    121         return x, y, z, volumes, time, stage
     118        return x, y, volumes, time, quantity
    122119
    123120    def read_xya(self,point_file):
     
    162159        xya_dict = {}
    163160        xya_dict['pointlist'] = self.point_coordinates
    164         xya_dict['pointattributelist'] = self.depth
     161        xya_dict['pointattributelist'] = self.interpolated_quantity
    165162       
    166163        export_xya_file(point_file, xya_dict, title, delimiter = delimiter)
     
    172169    """
    173170    import os, sys
    174     usage = "usage: %s pyvolution_results.sww points.xya depth.xya" %         os.path.basename(sys.argv[0])
    175 
     171    usage = "usage: %s pyvolution_results.sww points.xya depth.xya [height|stage|(other quantities)]" %         os.path.basename(sys.argv[0])
    176172    if len(sys.argv) < 4:
    177173        print usage
     
    180176        point_file_in = sys.argv[2]
    181177        point_file_out = sys.argv[3]
    182        
    183         interp = Interpolate_sww(sww_file)
     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
    184189        interp.interpolate_xya(point_file_in)
    185190        interp.write_depth_xya(point_file_out)
Note: See TracChangeset for help on using the changeset viewer.