Python script for Bandmaths


#1

I am trying to write python script for bandmaths:
import os
import sys
import glob
import gc
import snappy
from snappy import jpy
from snappy import GPF
from snappy import ProductIO
from snappy import HashMap
sys.path.append(’/opt/anaconda/bin/’)
file4 = ‘E:\proj\S1Adb.dim’
sourceProduct = snappy.ProductIO.readProduct(file4)
HashMap = jpy.get_type(‘java.util.HashMap’)
GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()

parameters = HashMap()
parameters.put(‘targetBands’, ‘’)
parameters.put(‘name’, ‘newBand’)
parameters.put(‘type’, ‘float32’)
parameters.put(‘expression’, ‘255*(Sigma0_VH_db<-19.39)’)
parameters.put(‘expression’, ‘255*(Sigma0_VV_db<-19.39)’)
parameters.put(‘noDataValue’, ‘0.0’)
thresh = GPF.createProduct(‘BandMaths’, parameters, sourceProduct)
ProductIO.writeProduct(thresh, ‘E:\proj\S1A_thresh.dim’, ‘BEAM-DIMAP’)

I am getting an error:

Traceback (most recent call last):
File “D:/programs/self_thresh.py”, line 23, in
thresh = GPF.createProduct(‘BandMaths’, parameters, sourceProduct)
RuntimeError: java.lang.IllegalStateException: no converter defined for value ‘targetBandDescriptors’

Please help me out.
Thanks.

@ABraun


#2

The parameterisation of the BandMaths operator is not correct.
Please have a look here:



#3

Thanks for your help.

The script you provided is only doing bandmaths only on one single band. I want to do bandmaths for multiple bands.

Can you please me.


#4

In this case you simply need to do the follwing section multiple times:

targetBand = BandDescriptor() 
targetBand.name = 'Sigma0_VV' 
targetBand.type = 'float32' 
targetBand.expression = '255*(Sigma0_VV&lt;2.22E-2)' 
targetBands[0] = targetBand

You just need to change the index in the targetBands array and of course you need to define this array with a different size, e.g:

targetBands = jpy.array('org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor', 6)