Ok, here's where I think you're getting confused. The leftmost term is what's turning the background values to -999 - this expression: (b1 le 0)*(-999). But the thing is, the *result* of that expression has -999's in it, not the *input* values to it. So b1 itself still has zeroes as background. So later in the rightmost part when you do (float(b2)/float(b1)), b1 still has zeroes as background and there's your divide by zero error. So, while this is probably going to make for a very complicated expression, you need to validate the input images(b1, b2, etc.) each time you use them (or at least every time you're going to be dividing with them). If you just want to divide two bands and have the resulting image have -999 as a background, you might try something like this (this is very off the top of my head, so no promises that it actually works, but it should get you started):
( (b2 le 0)*(-999) + float(b2) ) / ( (b1 le 0) + float(b1) )
So in the numerator, (b2 le 0) is the mask that gives you a 1 on a background pixel, 0 for non-background, then you multiply the 1's and 0's by -999, so that changes background pixels to -999, and all the rest are still zero. Add that to b2, and the resulting numerator is either -999 at background, or b2 where not background.
Then in the denominator, the mask this time remains as all 0's or 1's, then is added to b1 in the same manner as the numerator. And when you finally do the ratio, anything not a background value is going to have a resulting pixel value of b2/b1, and the background pixels will have values of -999/1 = -999.
I think should work, or at least get you out of the gate. One more thing, you seem to be using MATH_DOIT. If it were me writing code to do all this, if my band math expression got too complicated, i'd be tempted to just go ahead and write the code that does the math myself, skipping MATH_DOIT, just so it'd be easier to come back to later.
Hope that helps,
Jeff
|