I prepared a mini example for my question. Assume we have a master numpy array
master =
array([[-1, -1, -1, 2, 2, 2],
[-1, -1, -1, 2, 2, 2],
[-1, -1, -1, 2, 2, 2],
[ 2, 2, 2, -1, -1, -1],
[ 2, 2, 2, -1, -1, -1],
[ 2, 2, 2, -1, -1, -1]])
Secondly we have a slave numpy array with the exact same shape:
slave=
array([[-1, -1, -1, 3, 3, 3],
[-1, -1, -1, 3, 3, 3],
[-1, -1, -1, 3, 3, 3],
[ 3, 3, 3, 3, 3, 3],
[ 3, 3, 3, 3, 3, 3],
[ 3, 3, 3, 3, 3, 3]])
What I'm looking for is the following:
result = fill master array with slave values where master = -1
result=
array([[-1, -1, -1, 2, 2, 2],
[-1, -1, -1, 2, 2, 2],
[-1, -1, -1, 2, 2, 2],
[ 2, 2, 2, 3, 3, 3],
[ 2, 2, 2, 3, 3, 3],
[ 2, 2, 2, 3, 3, 3]])
In my real world scenario I have dozens of arrays with more than 12 million values each and they all have nodata values on different places. I want to fill the master array with the other arrays, where master values are nodata.
I really searched and tried a lot, like extract boolean masks but I really do not know how to fill on the exact same index coordinates without iterating over all single cells.
Would be great if I get help from you...
Use:
result = np.where(master == -1, slave, master)