Ignore:
Timestamp:
Feb 10, 2009, 11:11:04 AM (16 years ago)
Author:
rwilson
Message:

Initial commit of numpy changes. Still a long way to go.

Location:
branches/numpy
Files:
4 edited
1 copied

Legend:

Unmodified
Added
Removed
  • branches/numpy/anuga/coordinate_transforms/geo_reference.py

    r6149 r6304  
    1111from anuga.utilities.anuga_exceptions import ANUGAError, TitleError, ParsingError, \
    1212     ShapeError
    13 
    14 import Numeric as num
     13from anuga.config import netcdf_float, netcdf_int, netcdf_float32
     14
     15import numpy as num
    1516
    1617
     
    6465        self.xllcorner = xllcorner
    6566        self.yllcorner = yllcorner       
     67        #self.is_absolute = num.allclose([self.xllcorner, self.yllcorner], 0)
    6668           
    6769        if NetCDFObject is not None:
     
    99101       
    100102        # Fix some assertion failures
    101         if type(self.zone) == num.ArrayType and self.zone.shape == ():
     103        if isinstance(self.zone, num.ndarray) and self.zone.shape == ():
    102104            self.zone = self.zone[0]
    103         if type(self.xllcorner) == num.ArrayType and self.xllcorner.shape == ():
     105        if isinstance(self.xllcorner, num.ndarray) and self.xllcorner.shape == ():
    104106            self.xllcorner = self.xllcorner[0]
    105         if type(self.yllcorner) == num.ArrayType and self.yllcorner.shape == ():
     107        if isinstance(self.yllcorner, num.ndarray) and self.yllcorner.shape == ():
    106108            self.yllcorner = self.yllcorner[0]
    107109
    108         assert (type(self.xllcorner) == types.FloatType or\
    109                 type(self.xllcorner) == types.IntType)
    110         assert (type(self.yllcorner) == types.FloatType or\
    111                 type(self.yllcorner) == types.IntType)
    112         assert (type(self.zone) == types.IntType)
     110        assert (self.xllcorner.dtype.kind in num.typecodes['Float'] or
     111                self.xllcorner.dtype.kind in num.typecodes['Integer'])
     112        assert (self.yllcorner.dtype.kind in num.typecodes['Float'] or
     113                self.yllcorner.dtype.kind in num.typecodes['Integer'])
     114        assert (self.zone.dtype.kind in num.typecodes['Integer'])
    113115       
    114116        try:
     
    173175           
    174176        # Fix some assertion failures
    175         if(type(self.zone) == num.ArrayType and self.zone.shape == ()):
     177        if isinstance(self.zone, num.ndarray) and self.zone.shape == ():
    176178            self.zone = self.zone[0]
    177         if type(self.xllcorner) == num.ArrayType and self.xllcorner.shape == ():
     179        if isinstance(self.xllcorner, num.ndarray) and self.xllcorner.shape == ():
    178180            self.xllcorner = self.xllcorner[0]
    179         if type(self.yllcorner) == num.ArrayType and self.yllcorner.shape == ():
     181        if isinstance(self.yllcorner, num.ndarray) and self.yllcorner.shape == ():
    180182            self.yllcorner = self.yllcorner[0]
    181    
    182         assert (type(self.xllcorner) == types.FloatType)
    183         assert (type(self.yllcorner) == types.FloatType)
    184         assert (type(self.zone) == types.IntType)
    185        
     183
     184# useless asserts - see try/except code above
     185#        assert (type(self.xllcorner) == types.FloatType)
     186#        assert (type(self.yllcorner) == types.FloatType)
     187#        assert (type(self.zone) == types.IntType)
     188
    186189       
    187190    def change_points_geo_ref(self, points,
    188191                              points_geo_ref=None):
    189192        """
    190         Change the geo reference of a list or Numeric array of points to
     193        Change the geo reference of a list or numeric array of points to
    191194        be this reference.(The reference used for this object)
    192195        If the points do not have a geo ref, assume 'absolute' values
     
    197200            is_list = True
    198201
    199         points = ensure_numeric(points, num.Float)
     202        points = ensure_numeric(points, num.float)
    200203       
    201204        if len(points.shape) == 1:
     
    243246
    244247       
    245    
     248    ##
     249    # @brief
     250    # @param points
     251    # @return
     252    # @note
    246253    def get_absolute(self, points):
    247         """
    248         Given a set of points geo referenced to this instance,
     254        """Given a set of points geo referenced to this instance,
    249255        return the points as absolute values.
    250256        """
    251257
    252         #if self.is_absolute():
     258        #if self.is_absolute:
    253259        #    return points
     260
    254261        is_list = False
    255262        if type(points) == types.ListType:
    256263            is_list = True
    257264
    258         points = ensure_numeric(points, num.Float)
     265        points = ensure_numeric(points, num.float)
    259266        if len(points.shape) == 1:
    260267            #One point has been passed
     
    264271                #points = reshape(points, (1,2))
    265272
    266 
    267273        msg = 'Input must be an N x 2 array or list of (x,y) values. '
    268274        msg += 'I got an %d x %d array' %points.shape   
    269275        if not points.shape[1] == 2:
    270276            raise ShapeError, msg   
    271            
    272277       
    273278        # Add geo ref to points
     279        #if not self.is_absolute:
    274280        if not self.is_absolute():
    275281            points[:,0] += self.xllcorner
    276282            points[:,1] += self.yllcorner
    277 
     283            #self.is_absolute = True
    278284       
    279285        if is_list:
     
    296302            is_list = True
    297303
    298         points = ensure_numeric(points, num.Float)
     304        points = ensure_numeric(points, num.float)
    299305        if len(points.shape) == 1:
    300306            #One point has been passed
  • branches/numpy/anuga/coordinate_transforms/test_geo_reference.py

    r6149 r6304  
    99from anuga.config import netcdf_mode_r, netcdf_mode_w, netcdf_mode_a
    1010
    11 import Numeric as num
     11import numpy as num
    1212
    1313
     
    171171        #print "4 new_lofl",new_lofl
    172172
    173         self.failUnless(type(new_lofl) == num.ArrayType, ' failed')
     173        self.failUnless(isinstance(new_lofl, num.ndarray), ' failed')
    174174        self.failUnless(type(new_lofl) == type(lofl), ' failed')
    175175        lofl[:,0] -= x
     
    189189        #print "5 new_lofl",new_lofl
    190190
    191         self.failUnless(type(new_lofl) == num.ArrayType, ' failed')
     191        self.failUnless(isinstance(new_lofl, num.ndarray), ' failed')
    192192        self.failUnless(type(new_lofl) == type(lofl), ' failed')
    193193
     
    206206        #print "new_lofl",new_lofl
    207207
    208         self.failUnless(type(new_lofl) == num.ArrayType, ' failed')
     208        self.failUnless(isinstance(new_lofl, num.ndarray), ' failed')
    209209        self.failUnless(type(new_lofl) == type(lofl), ' failed')
    210210        for point,new_point in map(None,[lofl],new_lofl):
     
    229229            self.failUnless(point[0]+point_x-x==new_point[0], ' failed')
    230230            self.failUnless(point[1]+point_y-y==new_point[1], ' failed')
    231      
    232 
    233     def test_get_absolute(self):
     231
     232    def test_get_absolute_list(self):
     233        # test with supplied offsets
    234234        x = 7.0
    235235        y = 3.0
    236236       
    237         g = Geo_reference(56,x,y)
    238         lofl = [[3.0,34.0], [64.0,6.0]]
    239         new_lofl = g.get_absolute(lofl)
    240         #print "lofl",lofl
    241         #print "new_lofl",new_lofl
    242 
    243         self.failUnless(type(new_lofl) == types.ListType, ' failed')
    244         self.failUnless(type(new_lofl) == type(lofl), ' failed')
    245         for point,new_point in map(None,lofl,new_lofl):
    246             self.failUnless(point[0]+x==new_point[0], ' failed')
    247             self.failUnless(point[1]+y==new_point[1], ' failed')
    248 
     237        g = Geo_reference(56, x, y)
     238        points = [[3.0,34.0], [64.0,6.0]]
     239        new_points = g.get_absolute(points)
     240
     241        self.failUnless(type(new_points) == types.ListType, 'failed')
     242        self.failUnless(type(new_points) == type(points), 'failed')
     243        for point, new_point in map(None, points, new_points):
     244            self.failUnless(point[0]+x == new_point[0], 'failed')
     245            self.failUnless(point[1]+y == new_point[1], 'failed')
     246
     247        # test with no supplied offsets
     248        g = Geo_reference()
     249        points = [[3.0,34.0], [64.0,6.0]]
     250        new_points = g.get_absolute(points)
     251
     252        self.failUnless(type(new_points) == types.ListType, 'failed')
     253        self.failUnless(type(new_points) == type(points), 'failed')
     254        for point, new_point in map(None, points, new_points):
     255            self.failUnless(point[0] == new_point[0], 'failed')
     256            self.failUnless(point[1] == new_point[1], 'failed')
    249257           
     258        # test that calling get_absolute twice does the right thing
     259        # first call
     260        dx = 10.0
     261        dy = 10.0
     262        g = Geo_reference(56, dx, dy)
     263        points = [[3.0,34.0], [64.0,6.0]]
     264        expected_new_points = [[3.0+dx,34.0+dy], [64.0+dx,6.0+dy]]
     265        new_points = g.get_absolute(points)
     266
     267        self.failUnless(type(new_points) == types.ListType, 'failed')
     268        self.failUnless(type(new_points) == type(points), 'failed')
     269        for point, new_point in map(None, expected_new_points, new_points):
     270            self.failUnless(point[0] == new_point[0], 'failed')
     271            self.failUnless(point[1] == new_point[1], 'failed')
     272
     273        # and repeat from 'new_points = g.get_absolute(points)' above
     274        # to see if second call with same input gives same results.
     275        new_points = g.get_absolute(points)
     276
     277        self.failUnless(type(new_points) == types.ListType, 'failed')
     278        self.failUnless(type(new_points) == type(points), 'failed')
     279        for point, new_point in map(None, expected_new_points, new_points):
     280            self.failUnless(point[0] == new_point[0], 'failed')
     281            self.failUnless(point[1] == new_point[1], 'failed')
     282
     283    def test_get_absolute_array(self):
     284        '''Same test as test_get_absolute_list(), but with numeric arrays.'''
     285
     286        # test with supplied offsets
     287        x = 7.0
     288        y = 3.0
     289       
     290        g = Geo_reference(56, x, y)
     291        points = num.array([[3.0,34.0], [64.0,6.0]])
     292        new_points = g.get_absolute(points)
     293
     294        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
     295        self.failUnless(type(new_points) == type(points), 'failed')
     296        self.failUnless(num.alltrue(points == new_points), 'failed')
     297
     298        # test with no supplied offsets
    250299        g = Geo_reference()
    251         lofl = [[3.0,34.0], [64.0,6.0]]
    252         new_lofl = g.get_absolute(lofl)
    253         #print "lofl",lofl
    254         #print "new_lofl",new_lofl
    255 
    256         self.failUnless(type(new_lofl) == types.ListType, ' failed')
    257         self.failUnless(type(new_lofl) == type(lofl), ' failed')
    258         for point,new_point in map(None,lofl,new_lofl):
    259             self.failUnless(point[0]==new_point[0], ' failed')
    260             self.failUnless(point[1]==new_point[1], ' failed')
    261            
     300        points = num.array([[3.0,34.0], [64.0,6.0]])
     301        new_points = g.get_absolute(points)
     302
     303        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
     304        self.failUnless(type(new_points) == type(points), 'failed')
     305        self.failUnless(num.alltrue(points == new_points), 'failed')
     306
     307        # test that calling get_absolute twice does the right thing
     308        # first call
     309        dx = 10.0
     310        dy = 10.0
     311        g = Geo_reference(56, dx, dy)
     312        points = num.array([[3.0,34.0], [64.0,6.0]])
     313        expected_new_points = num.array([[3.0+dx,34.0+dy], [64.0+dx,6.0+dy]])
     314        new_points = g.get_absolute(points)
     315
     316        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
     317        self.failUnless(type(new_points) == type(points), 'failed')
     318        msg = ('First call of .get_absolute() returned %s\nexpected %s'
     319               % (str(new_points), str(expected_new_points)))
     320        self.failUnless(num.alltrue(expected_new_points == new_points), msg)
     321
     322        # and repeat from 'new_points = g.get_absolute(points)' above
     323        # to see if second call with same input gives same results.
     324        new_points = g.get_absolute(points)
     325
     326        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
     327        self.failUnless(type(new_points) == type(points), 'failed')
     328        msg = ('Second call of .get_absolute() returned %s\nexpected %s'
     329               % (str(new_points), str(expected_new_points)))
     330        self.failUnless(num.alltrue(expected_new_points == new_points), msg)
     331
     332        # and repeat again to see if *third* call with same input
     333        # gives same results.
     334        new_points = g.get_absolute(points)
     335
     336        self.failUnless(isinstance(new_points, num.ndarray), 'failed')
     337        self.failUnless(type(new_points) == type(points), 'failed')
     338        msg = ('Second call of .get_absolute() returned %s\nexpected %s'
     339               % (str(new_points), str(expected_new_points)))
     340        self.failUnless(num.alltrue(expected_new_points == new_points), msg)
     341
    262342    def test_is_absolute(self):
    263343       
     
    427507if __name__ == "__main__":
    428508
    429     suite = unittest.makeSuite(geo_referenceTestCase,'test')
     509    #suite = unittest.makeSuite(geo_referenceTestCase,'test')
     510    suite = unittest.makeSuite(geo_referenceTestCase,'test_get_absolute')
    430511    runner = unittest.TextTestRunner() #verbosity=2)
    431512    runner.run(suite)
  • branches/numpy/anuga/coordinate_transforms/test_lat_long_UTM_conversion.py

    r6149 r6304  
    1212from anuga.utilities.anuga_exceptions import ANUGAError
    1313
    14 import Numeric as num
     14import numpy as num
    1515
    1616
  • branches/numpy/anuga/coordinate_transforms/test_redfearn.py

    r6149 r6304  
    1111from anuga.utilities.anuga_exceptions import ANUGAError
    1212
    13 import Numeric as num
     13import numpy as num
    1414
    1515
Note: See TracChangeset for help on using the changeset viewer.