# Commits

committed 293b977

finish and pass the tests!

• Participants
• Parent commits 9dbd587
• Branches default

# File demfunctions.py

` import copy`
` `
` def fill_sinks(data):`
`+    '''`
`+    填充洼地`
`+    '''`
`     r_max = len(data)`
`     c_max = len(data[0])`
`     for r in xrange(len(data)):`
`     return data`
` `
` def lift(data,p):`
`+    '''`
`+    抬升平地`
`+    '''`
`     r_max = len(data)`
`     c_max = len(data[0])`
`     lifted = copy.deepcopy(data)`
` `
` def get_vect_martrix(data,x,y):`
`     '''`
`+    方向代码阵列`
`     x: x distance of each node on column`
`     y: y distance of each node on row`
`     '''`
`     r_max = len(data) # exactly len(data) - 1`
`     c_max = len(data[0]) # exactly len(data[0]) - 1`
`-    vect_martix = [[0 for c in range(c_max)] for r in range(r_max)]`
`+    vect_matrix = [[0 for c in range(c_max)] for r in range(r_max)]`
`     for r in xrange(len(data)):`
`         for c in xrange(len(data[r])):`
`             if r == 0:`
`-                vect_martix[r][c] = 7`
`+                vect_matrix[r][c] = 7`
`                 continue`
`             if r == r_max-1:`
`-                vect_martix[r][c] = 3`
`+                vect_matrix[r][c] = 3`
`                 continue`
`             if c == 0:`
`-                vect_martix[r][c] = 5`
`+                vect_matrix[r][c] = 5`
`                 continue`
`             if c == c_max-1:`
`-                vect_martix[r][c] = 1`
`+                vect_matrix[r][c] = 1`
`                 continue`
`             v0,v1,v2,v3,v4,v5,v6,v7 = [0 for i in range(8)]`
`             if r-1>=0 and c+1<=c_max-1:`
`                 which += 1`
`             if which == None:`
`                 raise 'error'`
`-            vect_martix[r][c] = which`
`-    return vect_martix`
`+            vect_matrix[r][c] = which`
`+    return vect_matrix`
`+`
`+def trackpath(data,r,c,r_ax,c_ax):`
`+    '''`
`+    跟踪水流方向`
`+    data: vect_matrix`
`+    '''`
`+    if r > r_ax-1 or r < 0 or c>c_ax-1 or c < 0:`
`+        return []`
`+    #if r>5 or r<0 or c>5 or c<0:`
`+    #    return[]`
`+    node = data[r][c]`
`+    path = [[r,c],]`
`+    if node == 0:`
`+        temp = trackpath(data,r-1,c+1,r_ax,c_ax)`
`+        if len(temp) >0:`
`+            path.extend(temp)`
`+    if node == 1:`
`+        temp = trackpath(data,r,c+1,r_ax,c_ax)`
`+        if len(temp) >0:`
`+            path.extend(temp)`
`+    if node == 2:`
`+        temp = trackpath(data,r+1,c+1,r_ax,c_ax)`
`+        if len(temp) >0:`
`+            path.extend(temp)`
`+    if node == 3:`
`+        temp = trackpath(data,r+1,c,r_ax,c_ax)`
`+        if len(temp) >0:`
`+            path.extend(temp)`
`+    if node == 4:`
`+        temp = trackpath(data,r+1,c-1,r_ax,c_ax)`
`+        if len(temp) >0:`
`+            path.extend(temp)`
`+    if node == 5:`
`+        temp = trackpath(data,r,c-1,r_ax,c_ax)`
`+        if len(temp) >0:`
`+            path.extend(temp)`
`+    if node == 6:`
`+        temp = trackpath(data,r-1,c-1,r_ax,c_ax)`
`+        if len(temp) >0:`
`+            path.extend(temp)`
`+    if node == 7:`
`+        temp = trackpath(data,r-1,c,r_ax,c_ax)`
`+        if len(temp) >0:`
`+            path.extend(temp)`
`+    return path`
` `
` def count_water(data):`
`+    '''`
`+    水流线等级`
`+    data: vect_matrix`
`+    '''`
`     r_max = len(data) # exactly len(data) - 1`
`     c_max = len(data[0]) # exactly len(data[0]) - 1`
`     water_matrix = [[0 for c in range(c_max)] for r in range(r_max)]`
`-    def trackpath(r,c):`
`-        if r > r_max-1 or r < 0 or c>c_max-1 or c < 0:`
`-            return []`
`-        #if r>5 or r<0 or c>5 or c<0:`
`-        #    return[]`
`-        node = data[r][c]`
`-        path = [[r,c],]`
`-        if node == 0:`
`-            temp = trackpath(r-1,c+1)`
`-            if len(temp) >0:`
`-                path.extend(temp)`
`-        if node == 1:`
`-            temp = trackpath(r,c+1)`
`-            if len(temp) >0:`
`-                path.extend(temp)`
`-        if node == 2:`
`-            temp = trackpath(r+1,c+1)`
`-            if len(temp) >0:`
`-                path.extend(temp)`
`-        if node == 3:`
`-            temp = trackpath(r+1,c)`
`-            if len(temp) >0:`
`-                path.extend(temp)`
`-        if node == 4:`
`-            temp = trackpath(r+1,c-1)`
`-            if len(temp) >0:`
`-                path.extend(temp)`
`-        if node == 5:`
`-            temp = trackpath(r,c-1)`
`-            if len(temp) >0:`
`-                path.extend(temp)`
`-        if node == 6:`
`-            temp = trackpath(r-1,c-1)`
`-            if len(temp) >0:`
`-                path.extend(temp)`
`-        if node == 7:`
`-            temp = trackpath(r-1,c)`
`-            if len(temp) >0:`
`-                path.extend(temp)`
`-        return path`
`     for r in xrange(len(data)):`
`         for c in xrange(len(data)):`
`-            path = trackpath(r,c)`
`+            path = trackpath(data,r,c,r_max,c_max)`
`             for node in path:`
`                 water_matrix[node[0]][node[1]] += 1`
`     return water_matrix`
` `
` def river_map(data,threshold=5):`
`+    '''`
`+    河流的0-1表示图`
`+    '''`
`     r_max = len(data) # exactly len(data) - 1`
`     c_max = len(data[0]) # exactly len(data[0]) - 1`
`     rivermap_matrix = [[0 for c in range(c_max)] for r in range(r_max)]`
`             if data[r][c] >=threshold:`
`                 rivermap_matrix[r][c] = 1`
`     return rivermap_matrix`
`+`
`+def river_paths(river_map_matrix,vect_matrix):`
`+    '''`
`+    get river paths`
`+    '''`
`+    paths = []`
`+    cross_sections = []`
`+    r_max = len(river_map_matrix) # exactly len(data) - 1`
`+    c_max = len(river_map_matrix[0]) # exactly len(data[0]) - 1`
`+    for r in xrange(len(river_map_matrix)):`
`+        for c in xrange(len(river_map_matrix[r])):`
`+            if river_map_matrix[r][c]: # node in the river`
`+                input_nodes = []`
`+                if r-1 >=0 and c+1 <= c_max-1:`
`+                    if river_map_matrix[r-1][c+1] == 1 and vect_matrix[r-1][c+1] == 4:`
`+                        input_nodes.append([r-1,c+1])`
`+                if c+1 <= c_max-1:`
`+                    if river_map_matrix[r][c+1] ==1 and vect_matrix[r][c+1] == 5:`
`+                        input_nodes.append([r,c+1])`
`+                if r+1 <= r_max-1 and c+1 <= c_max-1:`
`+                    if river_map_matrix[r+1][c+1] == 1 and vect_matrix[r+1][c+1] == 6:`
`+                        input_nodes.append([r+1,c+1])`
`+                if r+1 <= r_max-1:`
`+                    if river_map_matrix[r+1][c] == 1 and vect_matrix[r+1][c] == 7:`
`+                        input_nodes.append([r+1,c])`
`+                if r+1 <= r_max and c-1 >= 0:`
`+                    if river_map_matrix[r+1][c-1] == 1 and vect_matrix[r+1][c-1] == 0:`
`+                        input_nodes.append([r+1,c-1])`
`+                if c-1 >= 0:`
`+                    if river_map_matrix[r][c-1] == 1 and vect_matrix[r][c-1] == 1:`
`+                        input_nodes.append([r,c-1])`
`+                if r-1 >= 0 and c-1 >= 0:`
`+                    if river_map_matrix[r-1][c-1] == 1 and vect_matrix[r-1][c-1] == 2:`
`+                        input_nodes.append([r-1,c-1])`
`+                if r-1 >= 0:`
`+                    if river_map_matrix[r-1][c] == 1 and vect_matrix[r-1][c] == 3:`
`+                        input_nodes.append([r-1,c])`
`+                if len(input_nodes) == 0:`
`+                    # this node is the start point of the path`
`+                    paths.append(trackpath(vect_matrix,r,c,r_max,c_max))`
`+                else:`
`+                    if len(input_nodes) == 1:`
`+                        # this node is on the path`
`+                        pass`
`+                    else:`
`+                        # this node is the cross-section of path`
`+                        cross_sections.append([[r,c],input_nodes])`
`+    return {'cross_sections':cross_sections,'paths':paths}`
`+`
`+def water_grad(cross_sections,paths,r_max,c_max):`
`+    '''`
`+    determine the water grad on the basis of paths generated from river_paths function`
`+    '''`
`+    grads = [[0 for i in range(c_max)] for j in range(r_max)]`
`+    for x in range(len(paths)):`
`+        current_grad = 1`
`+        for y in range(len(paths[x])): # traceing from source of this river path`
`+            if paths[x][y] in [i[0] for i in cross_sections]: # is this node a cross section?`
`+                 if grads[paths[x][y][0]][paths[x][y][1]] < current_grad:`
`+                     current_grad += 1`
`+                     grads[paths[x][y][0]][paths[x][y][1]] = current_grad`
`+                 elif grads[paths[x][y][0]][paths[x][y][1]] >= current_grad:`
`+                     break`
`+            else:`
`+                grads[paths[x][y][0]][paths[x][y][1]] = current_grad`
`+    return grads`

# File test.py

`-from demfunctions import (fill_sinks,lift,get_vect_martrix,count_water,river_map)`
`+from demfunctions import (fill_sinks,lift,get_vect_martrix,count_water,river_map,river_paths,water_grad)`
` import pprint`
` `
` #data_const = [[1,1,3,4,5,5,7,5,4],`
` #`
` #print test_lift()`
` `
`-def test_get_vect_martrix():`
`+#def test_get_vect_matrix():`
`+#    data = [[1,1,3,4,5,5,7,5,4],`
`+#            [1,2,4,4,4,4,6,5,3],`
`+#            [4,4,3,4,3,3,6,7,5],`
`+#            [3,3,2,3,2,2,4,6,7],`
`+#            [1,2,2,2,1,2,3,4,5],`
`+#            [3,3,2,1,1,2,3,6,6],`
`+#            [2,3,3,1,1,2,5,7,8],`
`+#            [1,2,3,3,3,3,4,8,6],`
`+#            [1,1,2,3,3,4,5,7,8]]`
`+#    data_modified = fill_sinks(data)`
`+#    data_lifted = lift(data_modified,1e-5)`
`+#    return get_vect_martrix(data_lifted,1,1)`
`+#`
`+#print test_get_vect_matrix()`
`+`
`+#def test_count_water():`
`+#    data = [[1,1,3,4,5,5,7,5,4],`
`+#            [1,2,4,4,4,4,6,5,3],`
`+#            [4,4,3,4,3,3,6,7,5],`
`+#            [3,3,2,3,2,2,4,6,7],`
`+#            [1,2,2,2,1,2,3,4,5],`
`+#            [3,3,2,1,1,2,3,6,6],`
`+#            [2,3,3,1,1,2,5,7,8],`
`+#            [1,2,3,3,3,3,4,8,6],`
`+#            [1,1,2,3,3,4,5,7,8]]`
`+#    data_modified = fill_sinks(data)`
`+#    data_lifted = lift(data_modified,1e-5)`
`+#    vect_martrix = get_vect_martrix(data_lifted,1,1)`
`+#    return count_water(vect_martrix)`
`+#print test_count_water()`
`+#`
`+#def test_river_map():`
`+#    data = [[1,1,3,4,5,5,7,5,4],`
`+#            [1,2,4,4,4,4,6,5,3],`
`+#            [4,4,3,4,3,3,6,7,5],`
`+#            [3,3,2,3,2,2,4,6,7],`
`+#            [1,2,2,2,1,2,3,4,5],`
`+#            [3,3,2,1,1,2,3,6,6],`
`+#            [2,3,3,1,1,2,5,7,8],`
`+#            [1,2,3,3,3,3,4,8,6],`
`+#            [1,1,2,3,3,4,5,7,8]]`
`+#    data_modified = fill_sinks(data)`
`+#    data_lifted = lift(data_modified,1e-5)`
`+#    vect_martrix = get_vect_martrix(data_lifted,1,1)`
`+#    return river_map(count_water(vect_martrix),5)`
`+#`
`+#pprint.pprint(test_river_map())`
`+`
`+#def test_river_paths():`
`+#    data = [[1,1,3,4,5,5,7,5,4],`
`+#            [1,2,4,4,4,4,6,5,3],`
`+#            [4,4,3,4,3,3,6,7,5],`
`+#            [3,3,2,3,2,2,4,6,7],`
`+#            [1,2,2,2,1,2,3,4,5],`
`+#            [3,3,2,1,1,2,3,6,6],`
`+#            [2,3,3,1,1,2,5,7,8],`
`+#            [1,2,3,3,3,3,4,8,6],`
`+#            [1,1,2,3,3,4,5,7,8]]`
`+#    data_modified = fill_sinks(data)`
`+#    data_lifted = lift(data_modified,1e-5)`
`+#    vect_martrix = get_vect_martrix(data_lifted,1,1)`
`+#    map_ = river_map(count_water(vect_martrix),5)`
`+#    return river_paths(map_,vect_martrix)`
`+#`
`+#pprint.pprint(test_river_paths())`
`+`
`+def test_water_grad():`
`     data = [[1,1,3,4,5,5,7,5,4],`
`             [1,2,4,4,4,4,6,5,3],`
`             [4,4,3,4,3,3,6,7,5],`
`             [2,3,3,1,1,2,5,7,8],`
`             [1,2,3,3,3,3,4,8,6],`
`             [1,1,2,3,3,4,5,7,8]]`
`-    data_modified = fill_sinks(data)`
`-    data_lifted = lift(data_modified,1e-5)`
`-    return get_vect_martrix(data_lifted,1,1)`
`-`
`-print test_get_vect_martrix()`
`-`
`-def test_count_water():`
`-    data = [[1,1,3,4,5,5,7,5,4],`
`-            [1,2,4,4,4,4,6,5,3],`
`-            [4,4,3,4,3,3,6,7,5],`
`-            [3,3,2,3,2,2,4,6,7],`
`-            [1,2,2,2,1,2,3,4,5],`
`-            [3,3,2,1,1,2,3,6,6],`
`-            [2,3,3,1,1,2,5,7,8],`
`-            [1,2,3,3,3,3,4,8,6],`
`-            [1,1,2,3,3,4,5,7,8]]`
`+    r_max = len(data)`
`+    c_max = len(data[0])`
`     data_modified = fill_sinks(data)`
`     data_lifted = lift(data_modified,1e-5)`
`     vect_martrix = get_vect_martrix(data_lifted,1,1)`
`-    return count_water(vect_martrix)`
`-print test_count_water()`
`+    map_ = river_map(count_water(vect_martrix),5)`
`+    path_dict = river_paths(map_,vect_martrix)`
`+    #return path_dict`
`+    #return water_grad(**path_dict)`
`+    return water_grad(path_dict['cross_sections'],path_dict['paths'],r_max,c_max)`
` `
`-def test_river_map():`
`-    data = [[1,1,3,4,5,5,7,5,4],`
`-            [1,2,4,4,4,4,6,5,3],`
`-            [4,4,3,4,3,3,6,7,5],`
`-            [3,3,2,3,2,2,4,6,7],`
`-            [1,2,2,2,1,2,3,4,5],`
`-            [3,3,2,1,1,2,3,6,6],`
`-            [2,3,3,1,1,2,5,7,8],`
`-            [1,2,3,3,3,3,4,8,6],`
`-            [1,1,2,3,3,4,5,7,8]]`
`-    data_modified = fill_sinks(data)`
`-    data_lifted = lift(data_modified,1e-5)`
`-    vect_martrix = get_vect_martrix(data_lifted,1,1)`
`-    return river_map(count_water(vect_martrix),5)`
`-`
`-pprint.pprint(test_river_map())`
`-`
`+pprint.pprint(test_water_grad())`