这段代码可能很难理解,所以我会尽力解释。
import numpy as np
#Parameters
drainage_threshold = 0.5
# Arrays required for this script
elevation = np.array([[10, 9, 8],
[10, 11, 7],
[4, 5, 6]])
drainage_area = np.array([[0.9, 0.8, 0.7],
[0.1, 0.1, 0.6],
[0.5, 0.5, 0.5]])
grain_transport = np.array([[0.1, 0.2, 0.65],
[0, 0.5, 0.75],
[0.8,0.25, 0.1]])
nrows, ncols = elevation.shape
# Pad the arrays to prevent negative indexing
tmp = np.zeros((nrows+2, ncols+2), elevation.dtype)
tmp[1:-1, 1:-1] = elevation
elevation = tmp
tmp = np.zeros((nrows+2, ncols+2), grain_transport.dtype)
tmp[1:-1, 1:-1] = grain_transport
grain_transport = tmp
tmp = np.zeros((nrows+2, ncols+2), drainage_area.dtype)
tmp[1:-1, 1:-1] = drainage_area
drainage_area = tmp
# Create the new array to store where the sediment has moved to.
grain_n1_transport = np.zeros_like(grain_transport, dtype = float)
# Create a mask which only contains the cells with a drainage area greater than 0.5km2
grain_transport_mask = np.zeros_like(grain_transport, dtype = float)
np.putmask(grain_transport_mask, drainage_area >= drainage_threshold, grain_transport)
# Get the indices where the sediment transport is greater than 0
sort_idx = np.flatnonzero(grain_transport_mask)
# Now return those indices as a list
new_idx = zip(*np.unravel_index(sort_idx[::-1], elevation.shape))
# Now for the tricky bit I want to check the nine values in a 3by3 array and find the smallest value. This code achieves it but it has a lot of conditionals.
for i, j in new_idx:
print "The elevation of this grid cell is"
print elevation[i, j]
if 0 < elevation[i, j + 1] <= elevation[i, j] and (elevation[i - 1, j - 1] == 0 or elevation[i - 1, j - 1] >= elevation[i, j + 1]) and (elevation[i - 1, j] == 0 or elevation[i - 1, j] >= elevation[i, j + 1]) and (elevation[i - 1, j + 1] == 0 or elevation[i - 1, j + 1] >= elevation[i, j + 1]) and (elevation[i, j - 1] == 0 or elevation[i, j - 1] >= elevation[i, j + 1]) and (elevation[i + 1, j - 1] == 0 or elevation[i + 1 , j - 1] >= elevation[i, j + 1]) and (elevation[i + 1, j] == 0 or elevation[i + 1, j] >= elevation[i, j + 1]) and (elevation[i + 1, j + 1] == 0 or elevation[i + 1, j + 1] >= elevation[i, j + 1]):
print "The right cell is lower"
print "Therefore the sediment in this cell will be"
print grain_transport_mask[i, j]
elif 0 < elevation[i + 1 , j + 1] <= elevation[i, j] and (elevation[i - 1, j - 1] == 0 or elevation[i - 1, j - 1] >= elevation[i + 1, j + 1]) and (elevation[i - 1, j] == 0 or elevation[i - 1, j] >= elevation[i + 1, j + 1]) and (elevation[i - 1, j + 1] == 0 or elevation[i - 1, j + 1] >= elevation[i + 1, j + 1]) and (elevation[i, j - 1] == 0 or elevation[i, j - 1] >= elevation[i + 1, j + 1]) and (elevation[i + 1, j - 1] == 0 or elevation[i + 1 , j - 1] >= elevation[i, j + 1]) and (elevation[i + 1, j] == 0 or elevation[i + 1, j] >= elevation[i + 1, j + 1]) and (elevation[i, j + 1] == 0 or elevation[i, j + 1] >= elevation[i + 1, j + 1]):
print "The diagonal right cell is lower"
print "Therefore the sediment in this cell will be"
print grain_transport_mask[i, j] # this will use the grain_n1_transport
elif etc.....
基本上我希望代码遍历我拥有的索引列表并找到最低值的索引。然后,该脚本将与该关联的值移动到另一个数组(grain_transport_mask)。打印语句将在最终版本中删除,我试图理解我脑子里的逻辑。
谢谢,如果您有任何问题,我会很乐意添加更多信息。
尼克