max() implemented with basic operators

90 views Asked by At

I'm wrangling some bad data in a raster image using GDAL with gdal_calc.py. I'm trying to clip value A at -100 like this:

max(A, -100)

However, only basic operators (+-/*) and logical operators (><, these return 0 or 1) are allowed. Is there a way to implement this? Got as far as returning 0 for values less than -100

A*(A>-100)
3

There are 3 answers

2
1010 On BEST ANSWER

another one:

(A+100)*(A>-100) - 100

here the min value will be displaced to 0 to match the lower bound, then displaced back to -100.

1
zw324 On

Maybe double the trick by changing the expression to A * (A > -100) + (-100) * (A < -100)?

0
encrest On

A * (A > - 100) + (-100 * (A < -100))

if A is greater than -100, this reduces to A + 0 if A is less than -100, this reduces to 0 + (-100)