I want to calculate ndvi whith cloud mask using snappy in python. I have tried to use product.getMaskGroup().get(‘opaque_clouds_10m’)
But I don’t know how to apply
mask_group = product.getMaskGroup()
mask_names = mask_group.getNodeNames()
#Select masks
mask_scl = mask_group.get('opaque_clouds_10m') # ok
#Select Red and NIR bands
red_band = product.getBand('B4')
nir_band = product.getBand('B8')
#output product (ndvi) & new band
output_product = Product('NDVI', 'NDVI', width, height)
ProductUtils.copyGeoCoding(product, output_product)
output_band = output_product.addBand('ndvi', ProductData.TYPE_FLOAT32)
#output writer
output_product_writer = ProductIO.getProductWriter('GeoTiff')
output_product.setProductWriter(output_product_writer)
output_product.writeHeader('ndvi_output2.tif') #ok
output_product.writeHeader(name+'ndvi.tif')
#compute & save ndvi line by line
red_row = numpy.zeros(width, dtype=numpy.float32)
nir_row = numpy.zeros(width, dtype=numpy.float32)
mask_scl2 = jpy.cast(mask_scl, Mask)
slc_data = numpy.zeros(width * height, numpy.uint32)
mask_scl2.readPixels(0, 0, width, height,slc_data)
for y in range(height):
print("processing line ", y, " of ", height)
red_row = red_band.readPixels(0, y, width, 1, red_row)
nir_row = nir_band.readPixels(0, y, width, 1, nir_row)
red_band.readPixels(0, 0, w, h, slc_data)
nir_band.readPixels(0, 0, w, h, slc_data)
I need to create a numpy maks condition?
#create a numpy mask condition
invalid_mask = numpy.where(mask_array == 0, 1, 0)
masked_data = numpy.ma.array(data_array, mask=invalid_mask, fill_value=numpy.nan)
ndvi = numpy.divide((nir_row - red_row),(nir_row + red_row))
output_band.writePixels(0, y, width, 1, ndvi)
output_product.closeIO()
Can you help me?
Thank you