Nov-18-2018, 12:57 PM
Hi!
I could really use some help with the following assignment I received.
Basically I created a profile in a 2D array, with active (1) and inactive (0) cells. But I have some 'disconnected' cells in my array, where basically a cell with a value of 1, is surrounded by cells with a value of 0. I would like to delete those cells, by turning their value into 1. I have written therefore the following function down below:
I hope someone can advice me on the mistakes I might have in the script, and how to improve it..? I was only allowed to use if-statements here, so that's why I used this method.
Thank you!
I could really use some help with the following assignment I received.
Basically I created a profile in a 2D array, with active (1) and inactive (0) cells. But I have some 'disconnected' cells in my array, where basically a cell with a value of 1, is surrounded by cells with a value of 0. I would like to delete those cells, by turning their value into 1. I have written therefore the following function down below:
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(col_n): for j in xrange(row_n): cell_up = ibound_array[i,0,j-1] cell_down = ibound_array[i,0,j+1] cell_right = ibound_array[i+1,0,j] cell_left = ibound_array[i-1,0,j] if i == 0: if j == 0: if (cell_right == 0 and cell_down == 0): ibound_array [i,0,j] == 0 if (row_n -1) < j > 0: if (cell_up == 0 and cell_right == 0 and cell_down == 0): ibound_array[i,0,j] == 0 elif i == (col_n - 1): if j == 0: if (cell_left == 0 and cell_down == 0): ibound_array [i,0,j] == 0 if (row_n - 1) > j > 0: if (cell_up == 0 and cell_left == 0 and cell_down == 0): ibound_array[i,0,j] == 0 elif j == 0: if i == 0: if (cell_right == 0 and cell_down == 0): ibound_array [i,0,j] == 0 if (col_n -1) < i > 0: if (cell_left == 0 and cell_right == 0 and cell_down == 0): ibound_array[i,0,j] == 0 elif j == (row_n - 1): if i == 0: if (cell_up == 0 and cell_right == 0): ibound_array [i,0,j] == 0 if (col_n - 1) > i > 0: if (cell_up == 0 and cell_left == 0 and cell_right == 0): ibound_array[i,0,j] == 0 else: if (col_n - 1) > i > 0 and (row_n-1) > j > 0: if (cell_up == 0 and cell_right == 0 and cell_down == 0 and cell_left == 0): ibound_array[i,0,j] == 0 return ibound_arraySo the ibound_array that I have is 2D, therefore I have the value 0 for [i,0,j], which stands for 0 rows.
I hope someone can advice me on the mistakes I might have in the script, and how to improve it..? I was only allowed to use if-statements here, so that's why I used this method.
Thank you!