Add a new band to a product from another product

Hello everyone,
In order to retrieve the vertical displacement, I need to operate via Band Maths with two different products, but actually it’s not possible on SNAP. So I’m trying with snappy on Python. Since the two products are characterized by only one single band, the idea was to create a new product with the two bands deriving from the original products, in order to apply Band Maths on them easily. A first idea was to use the operator Band Merge, which literally “Allows copying raster data from any number of source products to a specified ‘master’ product.”. So I converted in GeoTIFF-BigTIFF one of the two products in order to insert this product as another band in the “master” product, but actually the final result is only the master product without the new band, even though on Python the operation is completed at 100%. Am I doing something wrong? Is there a simple way with which I can interact with two different products?
Thanks to who will help

The merge operator is actually the appropriate way to bring two products together.
The BandMaths should work.too. Both products need to have the same size and should cover the same region. The geographical requirement can be disabled by setting the parameter geographicError to NaN.

1 Like

Actually, with BandMerge my code is

file1 = “path.dim” #here i put the path of the file
descending_displacement = snappy.ProductIO.readProduct(file1)
file2 = “path.dim”
ascending_displacement = snappy.ProductIO.readProduct(file2)

#I’m working with two products obtained after the phase to displacement, so the only band contained in both products is “displacement”. Both products have the same dimensions.

parameters_merge = snappy.HashMap()
parameters_merge.put(“geographicError”, “1.0E-5f”)
parameters_merge.put(“sourceBands”, “displacement”)
merge = snappy.GPF.createProduct(“BandMerge”, parameters_merge, ascending_subset)
output = “path” #here I put the path with the name of the product
snappy.ProductIO.writeProduct(merge, output, “BEAM-DIMAP”)

Obviously, in this way I am only interacting with one product, so my question is how this operator actually works. I tried to put the entire path of the products on sourceBands but it gives me an error (RuntimeError: no matching Java method overloads found), and the same occurs by putting in createProduct both ascending_subset and descending_subset.

The merge operator is a bit unhandy to use with snappy.
But you find an example here:

Generally speaking, you have to provide names for the input products by using a map.

HashMap = jpy.get_type(‘java.util.HashMap’)
sourceProducts= HashMap()
sourceProducts.put(‘masterProduct’, red)
sourceProducts.put(‘slaveProduct1’, green)

Those names need to be used in the definition of the bands which shall be merged.
There you can also change the name, by setting the newName value.

NodeDescriptor = jpy.get_type(‘org.esa.snap.core.gpf.common.MergeOp$NodeDescriptor’)
include_1 = NodeDescriptor()
include_1.setProductId(‘masterProduct’)
include_1.setNamePattern(‘band_1’)
include_1.setNewName(‘red’)
...
1 Like

Thank you!