Changeset 6308 for qesdi


Ignore:
Timestamp:
15/01/10 08:44:39 (9 years ago)
Author:
pnorton
Message:

Applied the fix to _mergeBounds so that it will cope with reversed bounds.

Location:
qesdi/geoplot/trunk/lib/geoplot
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • qesdi/geoplot/trunk/lib/geoplot/grid_builder_base.py

    r6104 r6308  
    267267        right-hand edge of grid box (x, y) is the same as the left-hand 
    268268        edge of grid box (x + 1, y) and similarly in y. 
    269      
     269 
     270        There are 4 different types of bounds that might be received: 
     271 
     272            1. Descending with each bound low-to-high 
     273            2. Descending with each bound high-to-low 
     274            3. Ascending with each bound low-to-high 
     275            4. Ascending with each bound high-to-low 
     276 
     277        In order to cater for these we just use all unique values in the bounds 
     278        (which is fine given that we assume grid boxes are contiguous) and sort them. 
     279        If bounds are in descending order then we reverse the 1D array so it is also 
     280        descending. 
     281  
    270282        @param lonBounds: The longitude bounds array 
    271283        @param latBounds: the latitude bounds array 
    272284        """ 
    273      
    274         # Get grid dimensions 
    275         n = bounds.shape[0] 
    276      
    277         # Take the lower bounds as the mesh point 
    278         # except for the last index where the upper bounds is taken 
    279         merged = N.resize(bounds[:, 0], (n + 1,)) 
    280         merged[-1] = bounds[-1, 1] 
    281      
    282         return merged 
     285 
     286        # Set whether descending 
     287        descending = False 
     288        if bounds[0, 0] > bounds[-1, 0]: 
     289            descending = True 
     290 
     291        unique_bounds = N.unique(bounds) 
     292                
     293        # Check length is correct 
     294        if len(unique_bounds) != (bounds.shape[0] + 1): 
     295            log.error("Length of merged bounds ('%s') does not equal length of bounds + 1 ('%s')." % (len(unique_bounds), bounds.shape[0]))  
     296 
     297        # Sort it and then reverse it if descending  
     298        unique_bounds.sort()          
     299         
     300        if descending == True: 
     301            merged = unique_bounds[::-1] 
     302        else: 
     303            merged = unique_bounds 
     304 
     305        return merged  
    283306     
    284307     
  • qesdi/geoplot/trunk/lib/geoplot/tests/unit/test_grid_builder_base.py

    r5403 r6308  
    146146         
    147147    def test_011_mergeBounds(self): 
    148         unmergedBounds = N.array([[0.75, 1.25], [1.25, 1.75],  
    149                                   [1.75, 2.25], [2.25, 2.75]]) 
     148        unmergedBounds = N.array([[0.75, 1.25], [1.25, 1.75], [1.75, 2.25], [2.25, 2.75]]) 
    150149         
    151150        mergedBounds = GridBuilderBase._mergeBounds(unmergedBounds) 
    152151        nose.tools.assert_equal(mergedBounds.tolist(), [0.75, 1.25, 1.75, 2.25, 2.75])  
    153      
     152         
     153        # ascending, in order [low, high] 
     154        unmergedBounds = N.array([[1.0, 2.0], [2.0, 3.0], [3.0, 4.0]]) 
     155        mergedBounds = GridBuilderBase._mergeBounds(unmergedBounds) 
     156        nose.tools.assert_equal(mergedBounds.tolist(), [1.0, 2.0, 3.0, 4.0]) 
     157     
     158        # ascending in order [high, low] 
     159        unmergedBounds = N.array([[2.0, 1.0], [3.0, 2.0], [4.0, 3.0]]) 
     160        mergedBounds = GridBuilderBase._mergeBounds(unmergedBounds) 
     161        nose.tools.assert_equal(mergedBounds.tolist(), [1.0, 2.0, 3.0, 4.0]) 
     162         
     163        # decending in order [low, high] 
     164        unmergedBounds = N.array([[3.0, 4.0], [2.0, 3.0], [1.0, 2.0]]) 
     165        mergedBounds = GridBuilderBase._mergeBounds(unmergedBounds) 
     166        nose.tools.assert_equal(mergedBounds.tolist(), [4.0, 3.0, 2.0, 1.0]) 
     167         
     168        # decending in order [high, low] 
     169        unmergedBounds = N.array([[4.0, 3.0], [3.0, 2.0], [2.0, 1.0]]) 
     170        mergedBounds = GridBuilderBase._mergeBounds(unmergedBounds) 
     171        nose.tools.assert_equal(mergedBounds.tolist(), [4.0, 3.0, 2.0, 1.0]) 
     172             
    154173    def test_012_getBoundsFromAxis(self): 
    155174        axis =  cdms.createAxis(N.array([10.0,20.0,30.0,40.0,50.0])) 
Note: See TracChangeset for help on using the changeset viewer.