def initialize(self, context): self.source_product = context.getSourceProduct() if self.source_product.getProductType() != S3_OLCI_TYPE_STRING: raise RuntimeError('Source product must be of type "' + S3_OLCI_TYPE_STRING + '"') # todo - check if there is a granule self.toa_band_names = context.getParameter('band_names') self.rut_algo.k = self.get_k(context) self.rut_algo.unc_select = self.get_unc_select(context) scene_width = self.source_product.getSceneRasterWidth() scene_height = self.source_product.getSceneRasterHeight() rut_product = snappy.Product(self.source_product.getName() + '_rut', 'S3_OLCI_RUT', scene_width, scene_height) snappy.ProductUtils.copyGeoCoding(self.source_product, rut_product) self.sourceBandMap = {} for name in self.toa_band_names: source_band = self.source_product.getBand(name) unc_toa_band = snappy.Band(name + '_rut', snappy.ProductData.TYPE_UINT8, source_band.getRasterWidth(), source_band.getRasterHeight()) unc_toa_band.setDescription('Uncertainty of ' + name + ' (coverage factor k=' + str(self.rut_algo.k) + ')') unc_toa_band.setNoDataValue(250) unc_toa_band.setNoDataValueUsed(True) rut_product.addBand(unc_toa_band) self.sourceBandMap[unc_toa_band] = source_band context.setTargetProduct(rut_product)
def initialize(self, context): self.source_product = context.getSourceProduct() if self.source_product.getProductType() != S2_MSI_TYPE_STRING: raise RuntimeError('Source product must be of type "' + S2_MSI_TYPE_STRING + '"') self.product_meta, self.datastrip_meta, granules_meta = self.source_product.getMetadataRoot( ).getElements() # todo - check if there is a granule self.toa_band_names = context.getParameter('band_names') self.rut_algo.u_sun = self.get_u_sun(self.product_meta) self.rut_algo.quant = self.get_quant(self.product_meta) tecta = 0.0 for granule_meta in granules_meta.getElements(): tecta += self.get_tecta(granule_meta) self.rut_algo.tecta = tecta / granules_meta.getNumElements() self.rut_algo.k = self.get_k(context) self.rut_algo.unc_select = self.get_unc_select(context) scene_width = self.source_product.getSceneRasterWidth() scene_height = self.source_product.getSceneRasterHeight() rut_product = snappy.Product(self.source_product.getName() + '_rut', 'S2_RUT', scene_width, scene_height) snappy.ProductUtils.copyGeoCoding(self.source_product, rut_product) self.sourceBandMap = {} for name in self.toa_band_names: source_band = self.source_product.getBand(name) unc_toa_band = snappy.Band(name + '_rut', snappy.ProductData.TYPE_UINT8, source_band.getRasterWidth(), source_band.getRasterHeight()) unc_toa_band.setDescription('Uncertainty of ' + name + ' (coverage factor k=' + str(self.rut_algo.k) + ')') unc_toa_band.setNoDataValue(250) unc_toa_band.setNoDataValueUsed(True) rut_product.addBand(unc_toa_band) self.sourceBandMap[unc_toa_band] = source_band snappy.ProductUtils.copyGeoCoding(source_band, unc_toa_band) context.setTargetProduct(rut_product)
def convertComputedBandToBand(computedBand): """Work around to convert virtual band to real https://forum.step.esa.int/t/how-to-convert-virtual-bands-to-real-bands-using-snappy/3628/6 """ realBand = snappy.Band( computedBand.getName(), computedBand.getDataType(), computedBand.getRasterWidth(), computedBand.getRasterHeight() ) realBand.setDescription(computedBand.getDescription()) realBand.setValidPixelExpression(computedBand.getValidPixelExpression()) realBand.setUnit(computedBand.getUnit()) realBand.setSpectralWavelength(computedBand.getSpectralWavelength()) realBand.setGeophysicalNoDataValue(computedBand.getGeophysicalNoDataValue()) realBand.setNoDataValueUsed(computedBand.isNoDataValueUsed()) if (computedBand.isStxSet()): realBand.setStx(computedBand.getStx()) imageInfo = computedBand.getImageInfo() if (imageInfo != None): realBand.setImageInfo(imageInfo.clone()) product = computedBand.getProduct() # "Check if all the frame with the raster data are close" # missing some stuff with topComponent bandGroup = product.getBandGroup() bandIndex = bandGroup.indexOf(computedBand) bandGroup.remove(computedBand) bandGroup.add(bandIndex, realBand) realBand.setSourceImage(createSourceImage(computedBand, realBand))
def initialize(self, context): self.source_product = context.getSourceProduct() if self.source_product.getProductType() != S2_MSI_TYPE_STRING: raise RuntimeError('Source product must be of type "' + S2_MSI_TYPE_STRING + '"') self.mask_group = self.source_product.getMaskGroup() # obtain the masks from the product metadata_root = self.source_product.getMetadataRoot() self.product_meta = metadata_root.getElement('Level-1C_User_Product') self.datastrip_meta = metadata_root.getElement('Level-1C_DataStrip_ID') granules_meta = metadata_root.getElement('Granules') self.spacecraft = self.datastrip_meta.getElement('General_Info').getElement('Datatake_Info').getAttributeString( 'SPACECRAFT_NAME') # todo - check if there is a granule self.toa_band_names = context.getParameter('band_names') if not self.toa_band_names: raise RuntimeError( 'No S2 bands were selected. Please select the S2 bands from the "Processing parameters" tab') self.rut_algo.u_sun = self.get_u_sun(self.product_meta) self.rut_algo.quant = self.get_quant(self.product_meta) # tecta = 0.0 # for granule_meta in granules_meta.getElements(): # tecta += self.get_tecta(granule_meta) # self.rut_algo.tecta = tecta / granules_meta.getNumElements() self.source_sza = self.get_tecta() self.rut_algo.k = self.get_k(context) self.rut_algo.unc_select = self.get_unc_select(context) self.sourceBandMap = {} for name in self.toa_band_names: # TODO - Change the interface so that undesired bands (e.g azimuth) are not shown. if not name in S2_BAND_NAMES: # The band name is checked to confirm it is valid band. if ('view_' in name) or ('sun_' in name): continue # the angular bands are shown in the GUI and we simply jump to the next band if selected else: raise RuntimeError('Source band "' + name + '" is not valid and has not been processed') source_band = self.source_product.getBand(name) unc_toa_band = snappy.Band(name + '_rut', snappy.ProductData.TYPE_UINT8, source_band.getRasterWidth(), source_band.getRasterHeight()) unc_toa_band.setDescription('Uncertainty of ' + name + ' (coverage factor k=' + str(self.rut_algo.k) + ')') unc_toa_band.setNoDataValue(250) unc_toa_band.setNoDataValueUsed(True) self.targetBandList.append(unc_toa_band) self.sourceBandMap[unc_toa_band] = source_band snappy.ProductUtils.copyGeoCoding(source_band, unc_toa_band) masterband = self.get_masterband(self.targetBandList) rut_product = snappy.Product(self.source_product.getName() + '_rut', 'S2_RUT', masterband.getRasterWidth(), masterband.getRasterHeight()) # in-memory product snappy.ProductUtils.copyGeoCoding(masterband, rut_product) for band in self.targetBandList: rut_product.addBand(band) # The metadata from the RUT product is defined self.rut_product_meta = rut_product.getMetadataRoot() # Here we define the product metadata # SOURCE_PRODUCT sourceelem = MetadataElement('Source_product') data = snappy.ProductData.createInstance(self.source_product.getDisplayName()) sourceattr = MetadataAttribute("SOURCE_PRODUCT", snappy.ProductData.TYPE_ASCII, data.getNumElems()) sourceattr.setData(data) sourceelem.addAttribute(sourceattr) self.rut_product_meta.addElement(sourceelem) # COVERAGE_FACTOR sourceelem = MetadataElement('Coverage_factor') data = snappy.ProductData.createInstance(str(context.getParameter('coverage_factor'))) sourceattr = MetadataAttribute("COVERAGE_FACTOR", snappy.ProductData.TYPE_ASCII, data.getNumElems()) sourceattr.setData(data) sourceelem.addAttribute(sourceattr) self.rut_product_meta.addElement(sourceelem) # RUT_VERSION version = context.getSpi().getOperatorDescriptor().getVersion() # returns what written in the *-info.xml file sourceelem = MetadataElement('Version') data = snappy.ProductData.createInstance(version) sourceattr = MetadataAttribute("VERSION", snappy.ProductData.TYPE_ASCII, data.getNumElems()) sourceattr.setData(data) sourceelem.addAttribute(sourceattr) self.rut_product_meta.addElement(sourceelem) # CONTRIBUTOR LIST: List of selected ones sourceelem = MetadataElement('List_Contributors') contributors = ["INSTRUMENT_NOISE", "OOF_STRAYLIGHT-SYSTEMATIC", "OOF_STRAYLIGHT-RANDOM", "CROSSTALK", "ADC_QUANTISATION", "DS_STABILITY", "GAMMA_KNOWLEDGE", "DIFFUSER-ABSOLUTE_KNOWLEDGE", "DIFFUSER-TEMPORAL_KNOWLEDGE", "DIFFUSER-COSINE_EFFECT", "DIFFUSER-STRAYLIGHT_RESIDUAL", "L1C_IMAGE_QUANTISATION"] for i in range(0, len(contributors)): data = snappy.ProductData.createInstance(str(self.rut_algo.unc_select[i])) sourceattr = MetadataAttribute(contributors[i], snappy.ProductData.TYPE_ASCII, data.getNumElems()) sourceattr.setData(data) sourceelem.addAttribute(sourceattr) self.rut_product_meta.addElement(sourceelem) # DATE OF PROCESSING sourceelem = MetadataElement('Processing_datetime') data = snappy.ProductData.createInstance(str(datetime.datetime.now())) sourceattr = MetadataAttribute("PROCESSING_DATETIME", snappy.ProductData.TYPE_ASCII, data.getNumElems()) sourceattr.setData(data) sourceelem.addAttribute(sourceattr) self.rut_product_meta.addElement(sourceelem) context.setTargetProduct(rut_product)