BandmathOp in snappy

Dear all
I am having problems to translate an expression from GUI to snappy for Bandmaths operator.
Unfortunately, reading on the old topics, where @marpet suggested some solution, I couldn’t find a way to solve out that.
I have this expression in GUI

“if sr_b2 < 0 then sr_b2 == 0 else sr_b2”

and it put just the negative values of sr_b2 to 0, without changing the positive ones. For GUI, it works well and I get the values as expected.
How can I translate this expression in BandMathsOp in snappy?
My code for snappy is (following one of your examples):

parameters = HashMap()
BandDescriptor = snappy.jpy.get_type('org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor')
targetBand1 = BandDescriptor()
targetBand1.name = 'test'
targetBand1.type = 'float32'
targetBand1.expression = ??????
targetBand1.noDataValue = 0.0
targetBands = snappy.jpy.array('org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor', 1)
targetBands[0] = targetBand1
parameters.put('targetBands', targetBands)
output = snappy.GPF.createProduct("BandMaths", parameters, input)

Thank you in advance for your help.

S Savastano

Try the condition ? true_value : false_value construct:

(sr_b2 < 0) ?  0  : sr_b2
2 Likes

Hi George (@gnwiii),
yes it works, thank you a lot for your help.
Anyway, also this other translation is OK and I will leave it for who could need.

targetBand1.expression = 'if sr_b2 >' + str(0) + ' then sr_b2 else 0'

Moreover, to have the same results between GUI and snappy I have changed also the line for noDataValue that now is:

targetBand1.noDataValue = float('NaN')

S Savastano