Band math expression using snappy

Hi there,

Currently trying to perform Band Maths on a image using snappy.

I want to build the expression using this threshold:
thres=m-std**2

Is there a way to use this external value inside the expression?

result.getBand('B8')
BandDescriptor = jpy.get_type('org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor')
targetBand1 = BandDescriptor()
targetBand1.name = 'B8'
targetBand1.type = 'float32'
targetBand1.expression = 'if B8 <', **thres**, 'then 1 else 0'
targetBands = jpy.array('org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor', 1)
targetBands[0] = targetBand1
parameters = HashMap()
parameters.put('targetBands', targetBands)
result_bof = GPF.createProduct('BandMaths', parameters, result)

Thanks in advance

Probably you already solved it.
But if not or for others how stumble over the same problem:
Have a look at this page w3schools.com - Python Strings and scroll down to the section String Format. There it is explained how to include variables in strings.

Hi there! Thanks for your reply.

Yes, I solved it:

result.getBand('B2')
BandDescriptor = jpy.get_type('org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor')
targetBand1 = BandDescriptor()
targetBand1.name = 'B2'
targetBand1.type = 'int8'
**targetBand1.expression = 'if B2 <' + str(thres)+ ' then 1 else 0'**
targetBands = jpy.array('org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor', 1)
targetBands[0] = targetBand1
parameters = HashMap()
parameters.put('targetBands', targetBands)
result_bof = GPF.createProduct('BandMaths', parameters, result)
1 Like

Hallo everyone,

It’s nice to see the BandMaths operator in snappy. Is it also possible to get it in pyroSAR workflow? and please just let me know where to apply this operator in the workflow of Radar Vegetation Index. Thank you in advance.

Greetings,
Karlmarx