Biophysical parameter snappy

I am using SNAP 6.0. And was wondering to calculate Biophysical parameter using snappy command. I tried but there was an error like this :

from snappy import GPF
file_path = ‘C:/Users/Iqbal/.snap/snap-python/snappy/20180901/MEN/resample.dim’
product = ProductIO.readProduct(file_path)
list(product.getBandNames())
B4 = product.getBand(‘B4’)
B5 = product.getBand(‘B5’)

Width_4 = B4.getRasterWidth()
Height_4 = B4.getRasterHeight()
print("Band 4 Size: " + str(Width_4) +‘,’+ str(Height_4))

Width_5 = B5.getRasterWidth()
Height_5 = B5.getRasterHeight()
print("Band 5 Size: " + str(Width_5) +‘,’+ str(Height_5))

from snappy import jpy
HashMap = snappy.jpy.get_type(‘java.util.HashMap’)
snappy.GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
parameters = HashMap()
parameters.put(‘computeCab’, True)
parameters.put(‘computeCw’, True)
parameters.put(‘computeFapar’, True)
parameters.put(‘computeFcover’, True)
parameters.put(‘computeLAI’, True)
result = snappy.GPF.createProduct(‘Resample’,parameters,product)

Traceback (most recent call last):
File “”, line 1, in
RuntimeError: ambiguous Java method call, too many matching method overloads found

Could you help for this problem?

Thanks.

don’t know whether that’s the reason, but namespace-wise, I think, you are better of using

import snappy.jpy

instead of

from snappy import jpy

This also regards other module imports. Your way, jpy will be available as jpy, not as snappy.jpy.

1 Like

I am sorry I could use import snappy.jpy when I compute the parameters it would be error

from snappy import ProductIO
product = ProductIO.readProduct(‘C:/Users/Iqbal/.snap/snap-python/snappy/20180901/MEN/resample.data’)
from snappy import jpy
HashMap = snappy.jpy.get_type(‘java.util.HashMap’)
parameters = HashMap()
parameters.put(‘computeLAI’, True)
parameters.put(‘computeFapar’, True)
parameters.put(‘computeFcover’, True)
parameters.put(‘computeCab’, True)
parameters.put(‘computeCw’, True)
result = snappy.GPF.createProduct(‘BiophysicalOp’,parameters,product)
Traceback (most recent call last):
File “”, line 1, in
RuntimeError: ambiguous Java method call, too many matching method overloads found

my resample code :

import snappy
from snappy import ProductIO
file_path = ‘C:/Users/Iqbal/.snap/snap-python/snappy/20180901/MEN/snap/S2A_MSIL1C_20180901T023551_N0206_R089_T49MEN_20180901T061117.SAFE/MTD_MSIL1C.xml’
product = ProductIO.readProduct(file_path)
list(product.getBandNames())

from snappy import jpy
HashMap = snappy.jpy.get_type(‘java.util.HashMap’)

parameters = HashMap()
parameters.put(‘targetResolution’,10)
parameters.put(‘upsampling’,‘Nearest’)
parameters.put(‘Downsampling’,‘First’)
parameters.put(‘flagDownsampling’,‘First’)
parameters.put(‘resampleOnPyramidLevels’,True)
result = snappy.GPF.createProduct(‘Resample’,parameters,product)

ProductIO.writeProduct(product, ‘C:/Users/Iqbal/.snap/snap-python/snappy/20180901/MEN/resample.dim’, ‘BEAM-DIMAP’)

Which I see the xml graph in SNAP Desktop, then use the parameters in command line is ok. But when I am running biophysical after resample it has error like above.

Any solution?

I don’t know at which line the “ambiguous Java method call, too many matching method overloads found” occurs.
Is it this one?

result = snappy.GPF.createProduct(‘Resample’,parameters,product)

The traceback says: File “”, line 1, in
But this doesn’t make much sense.
In general, your code looks good, it must be just a small issue

One issue is probably that you want to write the result and not the product in the ProductIO.writeProduct line

what does “”, in line 1 refer to?

Yes, that’s my question too.
Normally there is a file name and the line number refers to the respective line in this file.

Have you used the code from the command line, line by line?

yes that is line by line. when I run the BiophysicalOp it has an error. I will try again removing this line :

result = snappy.GPF.createProduct(‘Resample’,parameters,product)

It still doesnt work too.

I also try using the gpt in command like this using the xml graph that build from graph builder like this in graph attachmentgraph.xml (1.2 KB)

then I run it and there is an error like this

gpt C:\Users\Iqbal.snap\snap-python\snappy\20180901\MEN\graph.xml C:\Users\Iqbal.snap\snap-python\snappy\20180901\MEN\biophysical.dim
INFO: org.esa.snap.python.gpf.PyOperatorSpi: Python operator ‘py_sambuca_snap_op’ registered (Python module: ‘sambuca_snap_op’, class: ‘sambuca_snap_op’, root: ‘C:\Users\Iqbal\AppData\Roaming\SNAP\modules\org-esa-sen2coral-sen2coral-inversion.jar’)
INFO: org.esa.snap.python.gpf.PyOperatorSpi: Python operator ‘S2RutOp’ registered (Python module: ‘s2_rut’, class: ‘S2RutOp’, root: ‘C:\Users\Iqbal\AppData\Roaming\SNAP\modules\org-esa-snap-snap-rut.jar’)
INFO: org.esa.snap.core.gpf.operators.tooladapter.ToolAdapterIO: Initializing external tool adapters
Executing processing graph
INFO: org.hsqldb.persist.Logger: dataFileCache open start
done.
java.lang.IllegalArgumentException: Angle 9,796,329,224,981.223 is too high.
at org.geotools.measure.AngleFormat.format(AngleFormat.java:703)
at org.geotools.measure.AngleFormat.format(AngleFormat.java:940)
at org.geotools.measure.AngleFormat.format(AngleFormat.java:862)
at org.geotools.measure.Angle.toString(Angle.java:165)
at java.text.MessageFormat.subformat(Unknown Source)
at java.text.MessageFormat.format(Unknown Source)
at java.text.Format.format(Unknown Source)
at org.geotools.resources.IndexedResourceBundle.getString(IndexedResourceBundle.java:491)
at org.geotools.resources.i18n.Errors.format(Errors.java:77)
at org.geotools.referencing.operation.projection.MapProjection.verifyGeographicRanges(MapProjection.java:508)
at org.geotools.referencing.operation.projection.MapProjection.access$000(MapProjection.java:86)
at org.geotools.referencing.operation.projection.MapProjection$Inverse.transform(MapProjection.java:1066)
at org.geotools.referencing.operation.projection.MapProjection$Inverse.transform(MapProjection.java:1105)
at org.geotools.referencing.operation.transform.AbstractMathTransform.transform(AbstractMathTransform.java:237)
at org.geotools.referencing.operation.transform.ConcatenatedTransformDirect.transform(ConcatenatedTransformDirect.java:68)
at org.geotools.referencing.CRS.transform(CRS.java:1345)
at org.geotools.referencing.CRS.transform(CRS.java:1413)
at org.geotools.geometry.jts.ReferencedEnvelope.transform(ReferencedEnvelope.java:689)
at org.geotools.geometry.jts.ReferencedEnvelope.transform(ReferencedEnvelope.java:635)
at org.esa.snap.core.datamodel.CrsGeoCoding.detect180MeridianCrossing(CrsGeoCoding.java:367)
at org.esa.snap.core.datamodel.CrsGeoCoding.(CrsGeoCoding.java:162)
at org.esa.snap.core.dataio.dimap.DimapProductHelpers.createCrsGeoCoding(DimapProductHelpers.java:578)
at org.esa.snap.core.dataio.dimap.DimapProductHelpers.createGeoCoding(DimapProductHelpers.java:289)
at org.esa.snap.core.dataio.dimap.DimapProductReader.initGeoCodings(DimapProductReader.java:164)
at org.esa.snap.core.dataio.dimap.DimapProductReader.processProduct(DimapProductReader.java:153)
at org.esa.snap.core.dataio.dimap.DimapProductReader.readProductNodesImpl(DimapProductReader.java:117)
at org.esa.snap.core.dataio.AbstractProductReader.readProductNodes(AbstractProductReader.java:169)
at org.esa.snap.core.gpf.common.ReadOp.initialize(ReadOp.java:116)
at org.esa.snap.core.gpf.internal.OperatorContext.initializeOperator(OperatorContext.java:486)
at org.esa.snap.core.gpf.internal.OperatorContext.getTargetProduct(OperatorContext.java:273)
at org.esa.snap.core.gpf.Operator.getTargetProduct(Operator.java:387)
at org.esa.snap.core.gpf.graph.NodeContext.initTargetProduct(NodeContext.java:77)
at org.esa.snap.core.gpf.graph.GraphContext.initNodeContext(GraphContext.java:195)
at org.esa.snap.core.gpf.graph.GraphContext.initNodeContext(GraphContext.java:178)
at org.esa.snap.core.gpf.graph.GraphContext.initNodeContext(GraphContext.java:178)
at org.esa.snap.core.gpf.graph.GraphContext.initOutput(GraphContext.java:162)
at org.esa.snap.core.gpf.graph.GraphContext.(GraphContext.java:91)
at org.esa.snap.core.gpf.graph.GraphContext.(GraphContext.java:64)
at org.esa.snap.core.gpf.graph.GraphProcessor.executeGraph(GraphProcessor.java:128)
at org.esa.snap.core.gpf.main.DefaultCommandLineContext.executeGraph(DefaultCommandLineContext.java:86)
at org.esa.snap.core.gpf.main.CommandLineTool.executeGraph(CommandLineTool.java:534)
at org.esa.snap.core.gpf.main.CommandLineTool.runGraph(CommandLineTool.java:388)
at org.esa.snap.core.gpf.main.CommandLineTool.runGraphOrOperator(CommandLineTool.java:287)
at org.esa.snap.core.gpf.main.CommandLineTool.run(CommandLineTool.java:188)
at org.esa.snap.core.gpf.main.CommandLineTool.run(CommandLineTool.java:121)
at org.esa.snap.core.gpf.main.GPT.run(GPT.java:54)
at org.esa.snap.core.gpf.main.GPT.main(GPT.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.esa.snap.runtime.Launcher.lambda$run$0(Launcher.java:55)
at org.esa.snap.runtime.Engine.runClientCode(Engine.java:189)
at org.esa.snap.runtime.Launcher.run(Launcher.java:51)
at org.esa.snap.runtime.Launcher.main(Launcher.java:31)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.exe4j.runtime.LauncherEngine.launch(LauncherEngine.java:62)
at com.exe4j.runtime.WinLauncher.main(WinLauncher.java:101)
at com.install4j.runtime.launcher.WinLauncher.main(WinLauncher.java:16)

Error: Angle 9,796,329,224,981.223 is too high.

The error is not related to the Biophysical operator, but to the input product.
How have you resampled the product? Which parameters?
Can you open the product in SNAP Desktop with this product?

The step I use is first to resample using the command line snappy then try to use biophysical parameter and it has error like above but for ‘NdviOp’ is ok. I try to open the result of resample in SNAP it doesnt show anything or the open file. Now I am trying to run again but save it in my external disk. Hope it is ok. I’ll let you know after this.

I try to save in external disk, it doesnt work or cant open the resample product process from command line snappy. But the processing of resample using command line snappy is working. Hope you can help what is the problem? Thank you.

Without more information about how you resample it is hard to say what’s going on.
Can you provide the resampled data?
I’ll send you a link where you can upload it.

I had a look at your data.
The one processed with SNAP Desktop is healthy. The one from the command line done with gpt is not resampled. It still has the bands in multiple resolutions. Actually, this should also work. The Dimap format supports it. But for some reason, the product is corrupted. I don’t know why.

In general, if you want to resample on the command line you only need to do:

gpt resample -PreferenceBand=B2 -t G:\EOData\SENTINEL2\S2A_MSIL1C_20170719T103021_N0205_R108_T33UUA_20170719T103023_10m.dim "G:\EOData\SENTINEL2\S2A_MSIL1C_20170719T103021_N0205_R108_T33UUA_20170719T103023.SAFE\MTD_MSIL1C.xml"

Here the band B2 specifies the target resolution (10m). But you can also do:

gpt resample -PtargetResolution=60 -t G:\EOData\SENTINEL2\S2A_MSIL1C_20170719T103021_N0205_R108_T33UUA_20170719T103023_60m.dim "G:\EOData\SENTINEL2\S2A_MSIL1C_20170719T103021_N0205_R108_T33UUA_20170719T103023.SAFE\MTD_MSIL1C.xml"

This will resample to 60m.

I want to ask there is PreferenceBand=B2 -t,what is the function for? and why only B2 why not other bands?
from the command line below :

gpt resample -PreferenceBand=B2 -t G:\EOData\SENTINEL2\S2A_MSIL1C_20170719T103021_N0205_R108_T33UUA_20170719T103023_10m.dim “G:\EOData\SENTINEL2\S2A_MSIL1C_20170719T103021_N0205_R108_T33UUA_20170719T103023.SAFE\MTD_MSIL1C.xml”

-PreferenceBand=B2 - this selects the band which defines the target resolution. So no need to specify other bands. All bands will be resampled to the resolution of this band.
-t <FILE_PATH> - the ‘-t’ option defines the location of the target file.

You can get more help by calling gpt -h for general help. Or gpt resample -h for the resampling operator

in addition to what @marpet replied: no need to use B2; B2 is just one example. you can choose whichever of the bands you want as the reference - all (other) bands will be resampled to your chosen reference band’s resolution.

thank you it works using gpt. But i want to know why using previous cant work? any idea to solve using python like above?