Index: /inundation/pyvolution/data_manager.py
===================================================================
--- /inundation/pyvolution/data_manager.py	(revision 2037)
+++ /inundation/pyvolution/data_manager.py	(revision 2038)
@@ -3355,15 +3355,14 @@
     latitudes = [bath_metadata['yllcorner']+y*bath_metadata['cellsize'] \
                  for y in range(number_of_latitudes)]
-
+    
+     # reverse order of lat, so the fist lat represents the first grid row
+    latitudes.reverse()
+    
+    #print "latitudes - before get_min_max_indexes",latitudes 
     kmin, kmax, lmin, lmax = get_min_max_indexes(latitudes,longitudes,
                                                  minlat=minlat, maxlat=maxlat,
                                                  minlon=minlon, maxlon=maxlon)
-    #print "latitudes", latitudes
-    #print "********************"
-    #print "kmin",kmin
-    #print "kmax",kmax
-    #print "lmin",lmin
-    #print "lmax",lmax
-    #print "bath_grid",bath_grid 
+    
+
     bath_grid = bath_grid[kmin:kmax,lmin:lmax]
     #print "bath_grid",bath_grid
@@ -3377,8 +3376,8 @@
     latitudes = latitudes[kmin:kmax]
     longitudes = longitudes[lmin:lmax]
-
-     # reverse order of lat, so the fist lat represents the first grid row
-    latitudes.reverse()
-   
+    number_of_latitudes = len(latitudes)
+    number_of_longitudes = len(longitudes)
+
+
     #Work out the times
     if len(elevation_files) > 1:
@@ -3530,8 +3529,10 @@
         _, v_momentum_grid =  _read_asc(vcur_dir + os.sep + vcur_files[j])
 
+        #print "elevation_grid",elevation_grid 
         #cut matrix to desired size
         elevation_grid = elevation_grid[kmin:kmax,lmin:lmax]
         u_momentum_grid = u_momentum_grid[kmin:kmax,lmin:lmax]
         v_momentum_grid = v_momentum_grid[kmin:kmax,lmin:lmax]
+        #print "elevation_grid",elevation_grid 
         # handle missing values
         missing = (elevation_grid == elevation_meta['NODATA_value'])
@@ -3550,5 +3551,4 @@
         for k in range(number_of_latitudes):      #Y direction
             for l in range(number_of_longitudes): #X direction
-                #w = zscale*amplitudes[j,k,l]/100 + mean_stage
                 w = zscale*elevation_grid[k,l] + mean_stage
                 stage[j,i] = w
@@ -3563,5 +3563,5 @@
                         minlon=None, maxlon=None):
     """
-    return max, min indexes of the lat and long arrays to cover the area
+    return max, min indexes (for slicing) of the lat and long arrays to cover the area
     specified with min/max lat/long
 
@@ -3571,21 +3571,28 @@
     has a section outside of the latitudes/longitudes area.)
 
-    assume latitudess & longitudes are sorted, from low to high
-    """
-
-    
-  #Cut out a smaller extent.
+    assume latitudess & longitudes are sorted,
+    long - from low to high (west to east, eg 148 - 151)
+    lat - from high to low (north to south, eg -35 - -38)
+    """
+
+    # reverse order of lat, so it's in ascending order
+    latitudes.reverse()
+    largest_lat_index = len(latitudes)-1
+    #Cut out a smaller extent.
     if minlat == None:
-        lat_min_index =0
+        lat_min_index = 0
     else:
         lat_min_index = searchsorted(latitudes, minlat)-1
         if lat_min_index <0:
-            lat_min_index =0
+            lat_min_index = 0
             
 
     if maxlat == None:
-        lat_max_index = len(latitudes)
+        lat_max_index = largest_lat_index #len(latitudes)
     else:
-        lat_max_index = searchsorted(latitudes, maxlat)+1
+        lat_max_index = searchsorted(latitudes, maxlat)
+        if lat_max_index > largest_lat_index:
+            lat_max_index = largest_lat_index
+        
     if minlon == None:
         lon_min_index = 0
@@ -3598,6 +3605,13 @@
         lon_max_index = len(longitudes)
     else:
-        lon_max_index = searchsorted(longitudes, maxlon)+1
-
+        lon_max_index = searchsorted(longitudes, maxlon)
+
+    #Take into account that the latitude list was reversed 
+    latitudes.reverse() # Python passes by reference, need to swap back
+    lat_min_index, lat_max_index = largest_lat_index - lat_max_index , \
+                                   largest_lat_index - lat_min_index
+    lat_max_index = lat_max_index + 1 # taking into account how slicing works
+    lon_max_index = lon_max_index + 1 # taking into account how slicing works
+    
     return lat_min_index, lat_max_index, lon_min_index, lon_max_index
         
Index: /inundation/pyvolution/test_data_manager.py
===================================================================
--- /inundation/pyvolution/test_data_manager.py	(revision 2037)
+++ /inundation/pyvolution/test_data_manager.py	(revision 2038)
@@ -3393,5 +3393,5 @@
 
     def test_get_min_max_indexes(self):
-        latitudes = [0,1,2,3]
+        latitudes = [3,2,1,0]
         longitudes = [0,10,20,30]
 
@@ -3435,5 +3435,5 @@
         #print "longitudes_news",longitudes_news
         
-        self.failUnless(latitudes_new == [1, 2] and \
+        self.failUnless(latitudes_new == [2, 1] and \
                         longitudes_news == [10, 20],
                          'failed')
@@ -3450,6 +3450,6 @@
         #print "longitudes_news",longitudes_news
         
-        self.failUnless(latitudes_new == [0,1, 2] and \
-                        longitudes_news == [0,10, 20],
+        self.failUnless(latitudes_new == [2, 1, 0] and \
+                        longitudes_news == [0, 10, 20],
                          'failed')
         ## 5th test
@@ -3463,6 +3463,6 @@
         #print "longitudes_news",longitudes_news
         
-        self.failUnless(latitudes_new == [0,1, 2] and \
-                        longitudes_news == [0,10, 20],
+        self.failUnless(latitudes_new == [2, 1, 0] and \
+                        longitudes_news == [0, 10, 20],
                          'failed')
         
@@ -3478,5 +3478,5 @@
         #print "longitudes_news",longitudes_news
         
-        self.failUnless(latitudes_new == [1, 2, 3] and \
+        self.failUnless(latitudes_new == [3, 2, 1] and \
                         longitudes_news == [10, 20, 30],
                          'failed')
@@ -3496,5 +3496,5 @@
         #print "longitudes_news",longitudes_news
         
-        self.failUnless(latitudes_new == [1, 2] and \
+        self.failUnless(latitudes_new == [2, 1] and \
                         longitudes_news == [10, 20],
                          'failed')
@@ -3504,7 +3504,9 @@
         
     def test_get_min_max_indexes2(self):
-        latitudes = [-45,-40,-35,-30]
+        latitudes = [-30,-35,-40,-45]
         longitudes = [148,149,150,151]
 
+        m2d = array([[0,1,2,3],[4,5,6,7],[8,9,10,11],[12,13,14,15]])
+        
         # k - lat
         # l - lon
@@ -3514,4 +3516,35 @@
         #print "kmin",kmin;print "kmax",kmax
         #print "lmin",lmin;print "lmax",lmax
+        #print "m2d", m2d 
+        #print "latitudes", latitudes
+        #print "longitudes",longitudes
+        #print "latitudes[kmax]", latitudes[kmax] 
+        latitudes_new = latitudes[kmin:kmax]
+        longitudes_new = longitudes[lmin:lmax]
+        m2d = m2d[kmin:kmax,lmin:lmax]
+        #print "m2d", m2d
+        #print "latitudes_new", latitudes_new
+        #print "longitudes_new",longitudes_new
+        
+        self.failUnless(latitudes_new == [-30, -35, -40] and \
+                        longitudes_new == [148, 149,150],
+                         'failed')
+        self.failUnless(m2d == [[0,1,2],[4,5,6],[8,9,10]],
+                         'failed')
+        
+    def test_get_min_max_indexes3(self):
+        latitudes = [-30,-35,-40,-45,-50,-55,-60]
+        longitudes = [148,149,150,151]
+
+        # k - lat
+        # l - lon
+        kmin, kmax, lmin, lmax = get_min_max_indexes (latitudes,longitudes,
+                                                      -43,-37,148.5,149.5)
+
+        
+        #print "kmin",kmin;print "kmax",kmax
+        #print "lmin",lmin;print "lmax",lmax
+        #print "latitudes", latitudes
+        #print "longitudes",longitudes
         latitudes_new = latitudes[kmin:kmax]
         longitudes_news = longitudes[lmin:lmax]
@@ -3519,20 +3552,21 @@
         #print "longitudes_news",longitudes_news
         
-        self.failUnless(latitudes_new == [-40, -35, -30] and \
+        self.failUnless(latitudes_new == [-35, -40, -45] and \
                         longitudes_news == [148, 149,150],
                          'failed')
         
-    def test_get_min_max_indexes3(self):
-        latitudes = [-60,-55,-50,-45,-40,-35,-30]
+    def test_get_min_max_indexes4(self):
+        latitudes = [-30,-35,-40,-45,-50,-55,-60]
         longitudes = [148,149,150,151]
 
         # k - lat
         # l - lon
-        kmin, kmax, lmin, lmax = get_min_max_indexes (latitudes,longitudes,
-                                                      -37,-27,147,149.5)
+        kmin, kmax, lmin, lmax = get_min_max_indexes (latitudes,longitudes)
 
         
         #print "kmin",kmin;print "kmax",kmax
         #print "lmin",lmin;print "lmax",lmax
+        #print "latitudes", latitudes
+        #print "longitudes",longitudes
         latitudes_new = latitudes[kmin:kmax]
         longitudes_news = longitudes[lmin:lmax]
@@ -3540,11 +3574,11 @@
         #print "longitudes_news",longitudes_news
         
-        self.failUnless(latitudes_new == [-40, -35, -30] and \
-                        longitudes_news == [148, 149,150],
+        self.failUnless(latitudes_new == latitudes  and \
+                        longitudes_news == longitudes,
                          'failed')
         
 #-------------------------------------------------------------
 if __name__ == "__main__":
-    #suite = unittest.makeSuite(Test_Data_Manager,'test_get_min_max_indexes')
+    #suite = unittest.makeSuite(Test_Data_Manager,'test_asc_csiro2sww4')
     suite = unittest.makeSuite(Test_Data_Manager,'test')
     #suite = unittest.makeSuite(Test_Data_Manager,'xxxtest')
