Changeset 1681


Ignore:
Timestamp:
Aug 4, 2005, 3:40:49 PM (19 years ago)
Author:
ole
Message:

Played with set_quantity

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

Legend:

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

    r1662 r1681  
    141141
    142142
    143     def set_quantity(self, name, X, location='vertices', indexes = None):
     143    def set_quantity(self, name,
     144                     X,
     145                     location='vertices',
     146                     indexes = None):
    144147        """Set values for named quantity
    145148
     
    170173              Q is a quantity
    171174              b is either a scalar, a quantity or a compatible array
     175              pts file
    172176            location: Where values are to be stored.
    173177                      Permissible options are: vertices, edges, centroid
     
    180184
    181185
    182 
    183186        """
    184187
     
    192195
    193196        #Set value
    194         self.quantities[name].set_values(X, location, indexes = indexes)
     197        self.quantities[name].set_values(X,
     198                                         location,
     199                                         indexes = indexes)
     200
    195201
    196202
  • inundation/ga/storm_surge/pyvolution/least_squares.py

    r1671 r1681  
    890890       
    891891    Optional input:
    892         quantity_names:     List of keys into quantities dictionary
     892        quantity_names:     List of keys into the quantities dictionary
    893893        vertex_coordinates: mx2 array of coordinates (Float)
    894894        triangles:          nx3 array of indices into vertex_coordinates (Int)
  • inundation/ga/storm_surge/pyvolution/quantity.py

    r1657 r1681  
    7979
    8080
    81     def set_values(self, X, location='vertices', indexes = None):
     81    def set_values(self, X,
     82                   location='vertices',
     83                   indexes = None):
    8284        """Set values for quantity
    8385
     
    107109        In any other case, only values for the specified locations
    108110        will be assigned and the others will be left undefined.
     111
    109112        """
    110113
     
    112115            msg = 'Invalid location: %s' %location
    113116            raise msg
     117
     118        #FIXME Do we really need 'location'?
    114119
    115120        if X is None:
     
    170175                        self.vertex_values[i_vertex,:] = X
    171176
    172         else:
     177        elif type(X) in [Numeric.ArrayType, types.ListType]:
    173178            #Use array specific method
    174179            self.set_array_values(X, location, indexes = indexes)
    175 
     180        elif type(X) == types.StringType:
     181            #Assume X is a filename
     182            self.set_values_from_file(X) #FIXME: More parameters
     183
     184       
    176185        if location == 'vertices' or location == 'unique vertices':
    177186            #Intialise centroid and edge_values
     
    383392
    384393
     394
     395    def set_values_from_file(self, filename,
     396                             attribute_name = None,
     397                             verbose = False):
     398        """Set quantity based on arbitrary points in .pts file using least_squares
     399
     400        attribute_name selects name of attribute present in file.
     401        If not specified try to use whatever is available in file.
     402        """
     403
     404        #FIXME location, indices
     405
     406       
     407        import util, least_squares
     408
     409        points, attributes = util.read_xya(filename)
     410
     411        if attribute_name is None:
     412            names = attributes.keys()
     413            attribute_name = names[0]
     414
     415           
     416        if verbose:
     417            print 'Using attribute %s from file %s' %(attribute_name, filename)
     418            print 'Available attributes: %s' %(names)
     419           
     420
     421        try:
     422            z = attributes[attribute_name]
     423        except:
     424            msg = 'Could not extract attribute %s from file %s'\
     425                  %(attribute_name, filename)
     426            raise msg
     427       
     428
     429        #Fit attributes to mesh
     430        X = least_squares.fit_to_mesh(self.domain.coordinates,
     431                                      self.domain.triangles,
     432                                      points,
     433                                      z,
     434                                      verbose=verbose)
     435
     436
     437
     438        #Recursively set value using fitted array
     439        print 'shape', X.shape
     440        self.set_values(X, location='vertices')  #FIXME, indexes = None):       
     441       
     442
     443
     444
    385445    # FIXME have a get_vertex_values as well, so the 'stage' quantity can be
    386446    # set, based on the elevation
  • inundation/ga/storm_surge/pyvolution/util.py

    r1671 r1681  
    138138                  interpolation_points = None, verbose = False):
    139139    """If quantities is not specified, derive them from domain
    140     (if that is specified)
     140    (if that is specified)
     141
     142
     143    see get_netcdf_file_function
    141144    """
    142145
     
    198201       
    199202
    200     import time, calendar
     203    import time, calendar, types
    201204    from config import time_format
    202205    from Scientific.IO.NetCDF import NetCDFFile
     
    207210    fid = NetCDFFile(filename, 'r')
    208211
     212    if type(quantity_names) == types.StringType:
     213        quantity_names = [quantity_names]       
     214   
    209215    if quantity_names is None or len(quantity_names) < 1:
    210216        #Get quantities from file
     
    230236
    231237    #Decide whether this data has a spatial dimension
     238    #FIXME: Let us split this up into two functions: One that reads
     239    #sww and one that reads tms (time series only)
    232240    spatial = True
    233241    for quantity in ['x', 'y']:
     
    344352        attributes = {}
    345353        for key in keys:
    346             attributes[key] = fid.variables[key]
     354            if key != 'points':
     355                attributes[key] = fid.variables[key]
    347356        #Don't close - arrays are needed outside this function,
    348357        #alternatively take a copy here
Note: See TracChangeset for help on using the changeset viewer.