Collocation operator on Python

Hello everyone. I’m working on a Python code using snappy to automatically perform all the SNAP operators to obtain a 2D displacement map. Before performing the “Horizontal/Vertical Motion” operator, the products must be collocated. So I tried to perform the operator following what is indicated in SNAP-Command line by calling “gpt -h Collocate”:

path_ascending = Path.joinpath(path_products_ascending, “terraincorrection_ascending.dim”)
terrain_correction_ascending = ProductIO.readProduct(str(path_ascending))

path_descending = Path.joinpath(path_products_descending, “terraincorrection_descending.dim”)
terrain_correction_descending = ProductIO.readProduct(str(path_descending))

products_paths = [str(path_ascending), str(path_descending)]

#Collocate
parameters_collocate = HashMap()
parameters_collocate.put(“copySecondaryMetadata”, “false”)
parameters_collocate.put(“masterProductName”, str(terrain_correction_ascending))
parameters_collocate.put(“renameMasterComponents”, “false”)
parameters_collocate.put(“renameSlaveComponents”, “false”)
parameters_collocate.put(“resamplingType”, “NEAREST_NEIGHBOUR”
parameters_collocate.put(“sourceProductPaths”, str(products_path))
parameters_collocate.put(“targetProductType”, “COLLOCATED”)
coll_prod = GPF.createProduct(“Collocate”, parameters_collocate, terrain_correction_ascending)

At this point, I got a error on the last line:
RuntimeError: org.esa.snap.core.gpf.OperatorException: Operator ‘CollocateOp’: Value for ‘Master product name’ must be of type ‘String’.

But if I change the product with a str(terrain_correction_ascending), the error is:
ValueError: cannot conert a Python ‘str’ to a Java ‘org.esa.snap.core.datamodel.Product’

this last error occurs even if I put as product the array of ascending and descending products, in both forms str(products_paths) or simply products_paths

Can someone help me on understanding where I am wrong?

Thank you in advance!

Hello Navre,

The configuration of the collocation operator with source products got a bit confusing.
There is already a ticket for this problem.
[SNAP-1123] Consolidate source product parameters of CollocateOp - JIRA (atlassian.net)

In you case the following should help:
replace
parameters_collocate.put(“masterProductName”, str(terrain_correction_ascending))
with
parameters_collocate.put(“master”, terrain_correction_ascending)
or
parameters_collocate.put(“master”, str(path_ascending))
alternatively
parameters_collocate.put(“masterProductName”, terrain_correction_ascending.getName())
might work too.

The masterProductName parameter is used to identify the master from the list of sourceProducts or sourceProductPaths.

See also: Snappy: Collocate, set 'masterProduct' - #5 by marpet

1 Like

@marpet it works! Thank you a lot!

1 Like

Hello
To fix the error, use the product path as a string (str(path_ascending) ) and convert the list of product paths to a comma-separated string (','.join(products_paths) ) in your parameters_collocate HashMap. This should resolve the data type issues.
You can also check :development - STEP Forum

Advertisement deleted from @stevediaz 's post and account silenced for 5 days.