简化条件,避免重复移动numpy wind

2021-10-17 14:56:17 发布

您现在位置:Python中文网/ 问答频道 /正文

这个代码可能很难理解,所以我会尽我所能解释。你知道吗

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\u transport\u mask)中。打印语句将在最终版本中删除我试图理解我头脑中的逻辑。你知道吗

谢谢,如果你有任何问题,我很乐意补充更多的信息。你知道吗

尼克