Merge masks in python snappy

Hello everybody,
My problem is that I can’t figure out how to merge some of the masks generated by Idepix into one “merged” mask (so that afterwards I can apply the land/sea mask on it).
What I want to achieve is basically the same result I would get with this button in SNAP GUI:

I’ve tried some approaches myself but none of them worked:
I created a mask using create method from org.esa.snap.core.datamodel.Mask.BandMathsType (http://step.esa.int/docs/v5.0/apidoc/engine/org/esa/snap/core/datamodel/Mask.BandMathsType.html#create-java.lang.String-java.lang.String-int-int-java.lang.String-java.awt.Color-double-) using the following code:

from snappy import ProductIO,jpy

source = sourceProduct
h = source.getSceneRasterHeight()
w = source.getSceneRasterWidth()

color = jpy.get_type('java.awt.Color')
mask = jpy.get_type('org.esa.snap.core.datamodel.Mask$BandMathsType')

mdescr = 'Pixels which are considered as clouds. Obtained by merging ''IDEPIX_CLOUD'', ''IDEPIX_CLOUD_BUFFER'', ''IDEPIX_CLOUD_SHADOW'' and ''IDEPIX_CIRRUS_SURE''' 
mexpr = 'IDEPIX_CLOUD OR IDEPIX_CLOUD_BUFFER OR IDEPIX_CLOUD_SHADOW OR IDEPIX_CIRRUS_SURE'

cloudmask = mask.create('CLOUDS',mdescr,w,h,mexpr,color.WHITE,0.7)
# EDIT AFTER FINDING THE APPROPRIATE METHOD
source.addMask(cloudmask)

How can I apply this created mask to the source product? I’ve tried with cloudmask.setOwner(sourceProduct) but it didn’t add the cloudmask to the sourceProduct’s masks.
Is my approach correct?
Anybody can help me with this issue?

Thanks a lot!
Davide

SOLVED
I solved this issue by using the method source.addMask(cloudmask)
I found it only now.
Sorry for the topic opened, at least I hope this can be useful for other users too.
Best regards

4 Likes

Just a note, if you really want to use the union of the masks as you have showed in the image, then you should use or instead of and.

You’re so right @marpet thank you for pointing this out!
I’m going to edit the post and correct it so that anybody else who might read will find the correct script.
Best regards