Ignore:
Timestamp:
Jan 20, 2006, 11:04:18 AM (19 years ago)
Author:
ole
Message:

Fixed missing geo reference in set_quantity and added tests

File:
1 edited

Legend:

Unmodified
Added
Removed
  • inundation/pyvolution/quantity.py

    r2233 r2262  
    167167                   quantity = None,   # Another quantity
    168168                   function = None,   # Callable object: f(x,y)
    169                    points = None, values = None, #Input for least squares
     169                   points = None, values = None, data_georef = None, #Input for least squares
    170170                   filename = None, attribute_name = None, #Input from file
    171171                   alpha = None,
     
    201201          If points is specified, values is an array of length N containing
    202202          attribute values for each point.
     203
     204        data_georef:
     205          If points is specified, geo_reference applies to each point.       
    203206         
    204207        filename:
     
    305308            assert values is not None, msg
    306309            self.set_values_from_points(points, values, alpha,
    307                                         location, indices, verbose,
    308                                         use_cache)
     310                                        location, indices,
     311                                        data_georef = data_georef,
     312                                        verbose = verbose,
     313                                        use_cache = use_cache)
    309314        elif filename is not None:
    310315            self.set_values_from_file(filename, attribute_name, alpha,
    311                                       location, indices, verbose,
    312                                       use_cache)
     316                                      location, indices,
     317                                      verbose = verbose,
     318                                      use_cache = use_cache)
    313319        else:
    314320            raise 'This can\'t happen :-)'
     
    513519        #FIXME: Should check that function returns something sensible and
    514520        #raise a meaningfull exception if it returns None for example
     521
     522        #FIXME: Should supply absolute coordinates
    515523
    516524        from Numeric import take
     
    546554
    547555    def set_values_from_points(self, points, values, alpha,
    548                                location, indices, verbose, use_cache):
     556                               location, indices,
     557                               data_georef = None,
     558                               verbose = False,
     559                               use_cache = False):
    549560        """Set quantity values from arbitray data points using least squares
    550561        """
     
    553564        from util import ensure_numeric
    554565        from least_squares import fit_to_mesh
     566        from coordinate_transforms.geo_reference import Geo_reference
     567       
    555568
    556569        points = ensure_numeric(points, Float)
     
    565578        triangles = self.domain.triangles
    566579
     580
     581        #Take care of georeferencing
     582        if data_georef is None:
     583            data_georef = Geo_reference()
     584
     585
     586        mesh_georef = self.domain.geo_reference
     587
     588        #print mesh_georef
     589        #print data_georef
     590        #print points
     591           
     592
     593        #Call least squares method           
     594        args = (coordinates, triangles, points, values)
     595        kwargs = {'data_origin': data_georef.get_origin(),
     596                  'mesh_origin': mesh_georef.get_origin(),
     597                  'alpha': alpha,
     598                  'verbose': verbose}
     599
     600        #print kwargs
     601       
    567602        if use_cache is True:
    568603            try:
     
    573608                raise msg
    574609
    575             args = (coordinates, triangles, points, values)
    576             kwargs = {'alpha': alpha, 'verbose': verbose}
    577610            vertex_attributes = cache(fit_to_mesh,
    578611                                      args, kwargs,
    579612                                      verbose = verbose)
    580613        else:
    581             vertex_attributes = fit_to_mesh(coordinates,
    582                                             triangles,
    583                                             points,
    584                                             values,
    585                                             alpha = alpha,
    586                                             verbose = verbose)
    587 
    588 
     614
     615            vertex_attributes = apply(fit_to_mesh,
     616                                      args, kwargs)
     617           
     618        #Call underlying method using array values   
    589619        self.set_values_from_array(vertex_attributes,
    590620                                   location, indices, verbose)
     
    595625
    596626    def set_values_from_file(self, filename, attribute_name, alpha,
    597                              location, indices, verbose, use_cache):
     627                             location, indices,
     628                             verbose = False,
     629                             use_cache = False):
    598630        """Set quantity based on arbitrary points in .pts file
    599631        using least_squares attribute_name selects name of attribute
     
    602634        """
    603635
     636        from load_mesh.loadASCII import import_points_file
    604637
    605638        from types import StringType
     
    609642
    610643        #Read from (NetCDF) file
    611         from load_mesh.loadASCII import import_points_file
    612644        points_dict = import_points_file(filename)
    613645        points = points_dict['pointlist']
     
    634666
    635667
    636         #Call least squares method
     668        #Take care of georeferencing
     669        if points_dict.has_key('geo_reference') and \
     670               points_dict['geo_reference'] is not None:
     671            data_georef = points_dict['geo_reference']
     672        else:
     673            data_georef = None
     674
     675
     676        #Call underlying method for points
    637677        self.set_values_from_points(points, z, alpha,
    638                                     location, indices, verbose, use_cache)
     678                                    location, indices,
     679                                    data_georef = data_georef,
     680                                    verbose = verbose,
     681                                    use_cache = use_cache)
    639682
    640683
Note: See TracChangeset for help on using the changeset viewer.