Thanks, the mask is created now thanks! … however it creates virtual bands tmpbands which is doubling my bandcount! I would ideally like a product with just the masked pixels (and preferably noData = numpy.nan)
UPDATE:
I am applying the Land-Sea-Mask with ‘bypass = True’ to just create a polygon mask in the mask group node (see full code below).
I have read Snappy: Mask (Clouds and others)
and readValidMask and isPixelValid methods
from:[quote=“marpet, post:8, topic:1654”]
When you call getValidMaskImage() on the result of product.getMaskGroup().get(‘opaque_clouds_10m’) then you retrieve the valid mask for the cirrus mask.
[/quote]
I then try and fetch the mask using:
land_sea_mask = land_sea_mask_product.getMaskGroup().get(str(geom_name)) land_sea_mask.getValidMaskImage()
I get the error:
AttributeError: 'org.esa.snap.core.datamodel.ProductNode' object has no attribute 'getValidMaskImage'
i think because getValidMaskImage() works on org.esa.snap.core.datamodel
@marpet Can you help tell me what I am missing to apply this mask to bands in a for loop?
Thanks in advance!
I currently have:
geom_name = 'shape'
product = ProductIO.readProduct('my_s1_dim_here')
polygons_wkt = "POLYGON ((-0.379473496337121 51.806829421554639...)"
#add the wkt geometry to the product using a seperate method which returns a product with added vector
product_with_vector = self.add_wkt_vector_geom(product , geom_name, polygons_wkt)
#create the mask in the product
params = HashMap()
# parameters.put('landMask', False)
params.put('useSRTM', SRTM)
params.put('geometry', geom_name)
params.put('invertGeometry', invert)
params.put('byPass', bypass)
#create a mask inside a product using Land-Sea-Mask (bypass = True)
land_sea_mask_product = GPF.createProduct('Land-Sea-Mask', params, product_with_vector)
h = land_sea_mask_product.getRasterHeight()
w = land_sea_mask_product.getRasterWidth()
bands = list(land_sea_mask_product.getBandNames())
k = int(len(land_sea_mask_product.getBands()))
#retrieve the mask
land_sea_mask = land_sea_mask_product.getMaskGroup().get(str(geom_name))
valid_mask = land_sea_mask.getValidMaskImage()
#create array for mask (this probably doesnt need ot be remade with each loop!)
mask_array = np.zeros(w, dtype=np.uint8)
#make new product and set writer
mask_product = Product('masked', 'masked', w, h)
ProductUtils.copyGeoCoding(product , mask_product)
writer = ProductIO.getProductWriter('BEAM-DIMAP')
mask_product.setProductWriter(writer)
mask_product.writeHeader(String('mask_from_wkt.dim'))
#apply to all bands
for band_index in range(k):
print('reading band index (k): '+str(band_index+1))
#add a band to be masked
masked_band = mask_product.addBand(bands[band_index]+'masked', ProductData.TYPE_FLOAT32)
#set the band noData values
masked_band.setNoDataValue(np.nan)
masked_band.setNoDataValueUsed(True)
#array for mask values
masked_array = np.zeros(shape=(w, h), dtype=np.float32)
#load the band to read
load_band = land_sea_mask_product.getBandAt(band_index)
#read the pixels into an array
load_band.readPixels(0, 0, w, h, masked_array)
#read to the the mask
valid_mask.readValidMask(0, 0, w, h, mask_array)
# #create a numpy mask condition
invalid_mask = np.where(mask_array == 0, 1, 0)
#apply the mask to our masked array
numpy_mask = np.ma.array(masked_array, mask=invalid_mask, fill_value=np.nan)
#write the masked band
masked_band.writePixels(0, 0, w, h, numpy_mask)
print('Masking finished')
return mask_product