Index: trunk/tools/generic_tools.py
===================================================================
--- trunk/tools/generic_tools.py	(revision 829)
+++ trunk/tools/generic_tools.py	(revision 830)
@@ -30,5 +30,5 @@
 # ijlonlat: Function to provide the imin,jmin imax,jmax of a lon,lat box
 # incomming_flow: Function to determine if a fgrid-flow inflows to the central grid point
-# index_mat_way: Function to look for a value within a matrix following a sense
+# index_mat_way: Function to look for a value within a matrix following a direction
 # num_chainSnum: Function to pass a value to a `ChainStrNum' number
 # num_split: Function to split a string at each numeric value keeping the number as the last character of each cut
@@ -7062,9 +7062,10 @@
 #quit()
 
-def subbasin_point(trips, pt):
+def subbasin_point(trips, pt, direction):
     """ Function to provide sub-basins given a grid point following a matrix of trips
       trips= matrix of trips values
         1: N,  2: NE,  3: E,  4: SE,  5: S,  6: SW:  7: W,  8: NW
       pt= point within the trip matrix
+      direction= searching direction river-up ('left': clockwise, 'right': anti-clockwise)
     """
     fname = 'subbasin_point'
@@ -7095,16 +7096,12 @@
         Njipt = dictionary_key(subfinished, False)
         if Njipt is not None:
-#            print Njipt, 'points subflow:', ijsubflow[Njipt]
             for ipt in ijsubflow[Njipt]:
                 if ijfound[ipt] == False:
                     jipt = ijsubbasin[ipt]
                     break
-#            print '  working from point:', ipt, 'ji pair:', jipt
             ijfound[ipt] = True
             Nij = ipt
         else:
 # Keep searching since there are grid-points not found!
-#            print '  ' + fname + ': Keep searching since there are grid-points not found!!'
-#            print ijfound
             Nij = dictionary_key(ijfound, False)
             if Nij is None:
@@ -7115,9 +7112,7 @@
             parentNjipt = dictionary_key_list(ijsubflow, Nij)
             Njipt = chainSnum_levnext(parentNjipt, ijsubflow.keys())
-#            print '  ' + fname + "new sub-flow '" + Njipt + "' !!"
             subfinished[Njipt] = False
             Nij = np.max(ijfound.keys())
 
-#        print 'Lluis Nij:', Nij
 # Looking for which point of the sub-flow retake the search
         if Nij == 1:
@@ -7126,16 +7121,11 @@
 
         ardtrips = vals_around(trips,jipt)
-#        print '  ' + fname + 'ardtrips _______'
-#        print ardtrips
 
         arrive = incomming_flow(ardtrips)
         Narrive = np.sum(arrive)
-#        print Nij, '  ' + fname + ' Narrive:', Narrive
         if Narrive == 0:
             ijfound[Nij] = True
             subfinished[Njipt] = True
         else:
-#            print '  ' + fname + 'arrive _______'
-#            print arrive
             followvals = np.zeros((3,3), dtype=bool)
             followvals = arrive
@@ -7143,12 +7133,15 @@
 
             for ifollow in range(Narrive):
-#                print 'ifollow:',ifollow,'/',Narrive
 
 # We only want to work with that ij, which have not yet been found
                 while np.sum(followvals) != 0:
-#                    print Nij,'  Looking for a non-located point in subbasin ________'
-#                    print subbasin[jipt[0]-1:jipt[0]+2,jipt[1]-1:jipt[1]+2]
-                    jifollow = index_mat(followvals, True)
-                    jiequiv = jifollow - [1,1]
+
+# Searching direction river-up. 'left': starting from the left, 'right' starting from the right
+                    if direction == 'left':
+                        jifollow = index_mat_way(followvals, True, 'clockwise6')
+                    elif direction == 'right':
+                        jifollow = index_mat_way(followvals, True, 'anticlockwise6')
+
+                    jiequiv = jifollow[ifollow] - [1,1]
                     if subbasin[jipt[0]+jiequiv[0], jipt[1]+jiequiv[1]] == False:
                         Nij = np.max(ijfound.keys()) + 1
@@ -7159,8 +7152,8 @@
                                 Njipt = chainSnum_levnext(parentNjipt, ijsubflow.keys())
                             else:
+                                # First subflow
                                 Njipt = '11'
                         else:
                             Njipt = parentNjipt
-#                        print '  ' + fname + "new sub-flow '" + Njipt + "' !!"
                         subfinished[Njipt] = False
                         subbasin[jipt[0]+jiequiv[0], jipt[1]+jiequiv[1]] = True
@@ -7179,5 +7172,4 @@
                             ijfound[Nij] = True
                             subfinished[Njipt] = True
-#                            print Nij,"  subflow '" + Njipt + "' finished!!"
                             break
 
@@ -7187,13 +7179,4 @@
                       'the number of trips', TOTtrips,'!!'
                     quit()
-
-#            if ijsubflow.has_key(Njipt): 
-#                print "Lluis points of subflow: '" + Njipt + "' _______=", ijsubflow[Njipt]
-#                for isub in ijsubflow[Njipt]:
-#                    print '  ' , isub , ':', ijsubbasin[isub], ijfound[isub]
-#            if Nij == 10: print 'Nij = 9:', ijfound[9]
-
-#        print subbasin
-#        if Nij > 4: quit()
 
     return subbasin
@@ -7366,10 +7349,12 @@
 
 def index_mat_way(mat,val,way):
-    """ Function to look for a value within a matrix following a sense
+    """ Function to look for a value within a matrix following a way and direction
       mat: matrix
       val: value to search
       way: way of search
-        'anticlockwise12': looking in an anti-clockwise sens starting at 12
-        'clockwise12': looking in an clockwise sens starting at 12
+        'anticlockwise12': looking in an anti-clockwise direction starting at 12
+        'anticlockwise6': looking in an anti-clockwise direction starting at 6
+        'clockwise12': looking in an clockwise direction starting at 12
+        'clockwise6': looking in an clockwise direction starting at 6
     >>> mat = np.arange(20).reshape(4,5)
     >>> mat[2,1] = 3
@@ -7398,5 +7383,5 @@
     if way == 'anticlockwise12':
 # Sorting following a double criteria, first angle and then distance taken from the
-#   center in the anti-clockwise sens starting at 12
+#   center in the anti-clockwise direction starting at 12
         if Ndims > 2:
             print errormsg
@@ -7429,5 +7414,5 @@
     elif way == 'anticlockwise6':
 # Sorting following a double criteria, first angle and then distance taken from the
-#   center in the anti-clockwise sens starting at 6
+#   center in the anti-clockwise direction starting at 6
         if Ndims > 2:
             print errormsg
@@ -7460,5 +7445,5 @@
     elif way == 'clockwise12':
 # Sorting following a double criteria, first angle and then distance taken from the
-#   center in the cloc-kwise sens starting at 12
+#   center in the cloc-kwise direction starting at 12
         if Ndims > 2:
             print errormsg
@@ -7489,5 +7474,5 @@
     elif way == 'clockwise6':
 # Sorting following a double criteria, first angle and then distance taken from the
-#   center in the clockwise sens starting at 6
+#   center in the clockwise direction starting at 6
         if Ndims > 2:
             print errormsg
@@ -7532,8 +7517,4 @@
     return indices
 
-mat = np.arange(20).reshape(4,5)
-mat[2,1] = 3
-print index_mat_way(mat,3,'anticlockwise6')
-
 #quit()
 
