Nov-19-2018, 09:20 AM
Hi :)
if you can access your array with ibound_array[i,0,j] you have a three dimensional array, though the middle value only holds one value (in this case an array). When you build your array two dimensional you should be able to access it with ibound_array[i,j] and still can store a value in it. Or is the middle dimension of importance? if so, then ignore this part ;) :D
You have to be careful with the shape. Let's say you have an array like
1. make your array bigger by inserting a 0 padding around the boarders. for example
(with both methods you would still just use if methods ;) )
One major problem I see is that you have the return part in your loop section so after the first iteration the loop will stop
if you can access your array with ibound_array[i,0,j] you have a three dimensional array, though the middle value only holds one value (in this case an array). When you build your array two dimensional you should be able to access it with ibound_array[i,j] and still can store a value in it. Or is the middle dimension of importance? if so, then ignore this part ;) :D
You have to be careful with the shape. Let's say you have an array like
arr = np.array([[1,2,3],[4,5,6]])then your shape will be (2,3) which means that the rows are represented by the first dimension an the columns by the second dimension (like you did). BUT currently your for loops are generating column indices as i and row indices as j. Currently you are using i for the rows and j for the columns. Just switch them up. finally you can make the script more easy. There are two options.
1. make your array bigger by inserting a 0 padding around the boarders. for example
arr = np.array([[1,2,3],[4,5,6]])would now be
arr = np.array([[0,0,0,0,0],[0,1,2,3,0],[0,4,5,6,0],[0,0,0,0,0]])Now you just need to let your indices ignore the boarders and cell_up, cell_down, etc. will have valid values. Your code would look like this:
def find_lonely_cells(ibound_array): col_n = ibound_array.shape[-1] #results in 150 columns, but displays 149 row_n = ibound_array.shape[0] #results in 120 rows (layers), but displays 119 # go through the ibound_arr cell by cell for i in xrange(1, row_n - 1): for j in xrange(1, col_n - 1): cell_up = ibound_array[i,j-1] cell_down = ibound_array[i,j+1] cell_right = ibound_array[i+1,j] cell_left = ibound_array[i-1,j] if (cell_up == 0 and cell_right == 0 and cell_down == 0 and cell_left == 0): ibound_array[i,j] == 0 return ibound_array2. You could reduce the if cases by doing this:
def find_lonely_cells(ibound_array): col_n = ibound_array.shape[-1] #results in 150 columns, but displays 149 row_n = ibound_array.shape[0] #results in 120 rows (layers), but displays 119 # go through the ibound_arr cell by cell for i in xrange(row_n): for j in xrange(col_n): cell_up = if j == 0 then 0 else ibound_array[i,j-1] cell_down = if j == col_n - 1 then 0 else ibound_array[i,j+1] cell_right = if i == row_n -1 then 0 else ibound_array[i+1,j] cell_left = if i == 0 then 0 else ibound_array[i-1,j] if (cell_up == 0 and cell_right == 0 and cell_down == 0 and cell_left == 0): ibound_array[i,j] == 0 return ibound_arrayI personally would prefer the first method.
(with both methods you would still just use if methods ;) )
One major problem I see is that you have the return part in your loop section so after the first iteration the loop will stop