Search code examples
pythonnumpyraster

Why when I try to execute 2 conditions in 1 function it give me just black raster?


Why when I try exercise 1 condition in 1 function it works, but when I try to add next - it give just black raster?

instead of average > 0.05*99 - I try to make: (average > 0.05*99)&(average < 0.20*99) (It is condition: more than 5% but less than 20% ).

May be something wrong with Python rules to write multiple conditions?

def computeThirdCondition(myArray):  
print "Executing Third Condition:"
#Set up empty array:
myArrayThird = numpy.zeros(myArray.shape).astype(float)    
thirdCondition = (theInputArray==11)|(theInputArray==12)*1  
for i in range (1,numpy.size(myArray,1)-1):
    for j in range (1,numpy.size(myArray,0)-1):
        average = 0.0
        for ii in range(i-6,i+7):
            for jj in range(j-4,j+5):
                average = average + thirdCondition[j][i]
        if (average > 0.05*99)&(average < 0.20*99): 
            myArrayThird[j][i] = 1                
return myArrayThird

Solution

  • In Python & is the bitwise and operator while logical and is simply and. Change your line from

    if (average > 0.05*99)&(average < 0.20*99): 
    

    to:

    if average > 0.05 * 99 and average < 0.20 * 99:
    

    or even:

    if 0.20 * 99 > average > 0.05 * 99: