def initialize(self, context): source_product = context.getSourceProduct('source') print('initialize: source product location is', source_product.getFileLocation()) width = source_product.getSceneRasterWidth() height = source_product.getSceneRasterHeight() lower_band_name = context.getParameter('lowerName') if not lower_band_name: raise RuntimeError('Missing parameter "lowerName"') self.lower_band = self._get_band(source_product, lower_band_name) self.lower_factor = context.getParameter('lowerFactor') upper_band_name = context.getParameter('upperName') if not upper_band_name: raise RuntimeError('Missing parameter "upperName"') self.upper_band = self._get_band(source_product, upper_band_name) self.upper_factor = context.getParameter('upperFactor') print('initialize: lower_band =', self.lower_band, ', upper_band =', self.upper_band) print('initialize: lower_factor =', self.lower_factor, ', upper_factor =', self.upper_factor) ndvi_product = snappy.Product('py_NDVI', 'py_NDVI', width, height) snappy.ProductUtils.copyGeoCoding(source_product, ndvi_product) self.ndvi_band = ndvi_product.addBand('ndvi', snappy.ProductData.TYPE_FLOAT32) self.ndvi_flags_band = ndvi_product.addBand( 'ndvi_flags', snappy.ProductData.TYPE_UINT8) context.setTargetProduct(ndvi_product)
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): # Via the context object the source product which shall be processed can be retrieved source_product = context.getSourceProduct('source') print('initialize: source product location is', source_product.getFileLocation()) width = source_product.getSceneRasterWidth() height = source_product.getSceneRasterHeight() self.source_bands = [] for input_name, column_name in INPUT_NAMES: self.source_bands.append(source_product.getBand(column_name)) # As it is always a good idea to separate responsibilities the algorithmic methods are put # into an other class # Create the target product target_product = snappy.Product('py_FuzzyDectree', 'py_FuzzyDectree', width, height) # ProductUtils provides several useful helper methods to build the target product. # In most cases it is sufficient to copy the information from the source to the target. # That's why mainly copy methods exist like copyBand(...), copyGeoCoding(...), copyMetadata(...) snappy.ProductUtils.copyGeoCoding(source_product, target_product) snappy.ProductUtils.copyMetadata(source_product, target_product) # For copying the time information no helper method exists yet, but will come in SNAP 5.0 target_product.setStartTime(source_product.getStartTime()) target_product.setEndTime(source_product.getEndTime()) # Adding new bands to the target product is straight forward. self.target_bands = [] for output_name in OUTPUT_NAMES: target_band = target_product.addBand( output_name, snappy.ProductData.TYPE_FLOAT32) self.target_bands.append(target_band) self.final_class_band = self.add_final_class_band(target_product) self.fuzzy_max_value_band = target_product.addBand( 'fuzzy_max_value', snappy.ProductData.TYPE_FLOAT32) # todo: flag band/mask ?! # flag_coding = self.create_flag_coding() # group = target_product.getFlagCodingGroup() # group.add(flag_coding) # self.final_class_band.setSampleCoding(flag_coding) # self.create_mask(target_product) # Provide the created target product to the framework so the computeTileStack method can be called # properly and the data can be written to disk. context.setTargetProduct(target_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 initialize(self, context): # Via the context object the source product which shall be processed can be retrieved source_product = context.getSourceProduct('sourceProduct') print('initialize: source product location is', source_product.getFileLocation().toString()) #get name of Red band from parameter 'redName' red_band_name = context.getParameter('redName') #if no band name is given for red, raise an expection if not red_band_name: raise RuntimeError('Missing parameter "redName"') #get instance of red band from source_product using self._get_band() self.red_band = self._get_band(source_product, red_band_name) #get name of Red band from parameter 'nirName' nir_band_name = context.getParameter('nirName') #if no band name is given for nir, raise an expection if not nir_band_name: raise RuntimeError('Missing parameter "nirName"') #get instance of nir band from source_product using self._get_band() self.nir_band = self._get_band(source_product, nir_band_name) print('initialize: red_band =', self.red_band, ', nir_band =', self.nir_band) #width and height of source product. This will be used to create target product width = source_product.getSceneRasterWidth() height = source_product.getSceneRasterHeight() #create target product 'ndvi_product' with same size of source product ndvi_product = snappy.Product('py_NDVI', 'py_NDVI', width, height) snappy.ProductUtils.copyGeoCoding(source_product, ndvi_product) #add a band to ndvi_product to store the result of ndvi self.ndvi_band = ndvi_product.addBand('ndvi', snappy.ProductData.TYPE_FLOAT32) #add a band to ndvi_product to store ndvi_flags. self.ndvi_flags_band = ndvi_product.addBand('ndvi_flags', snappy.ProductData.TYPE_UINT8) context.setTargetProduct(ndvi_product)
def initialize(self, context): #read the source_product source_product = context.getSourceProduct('source') if source_product is None: source_product = context.getSourceProduct('sourceProduct') if source_product is None: return print('initialize: source product location is', source_product.getFileLocation()) #get the names of the bands to be used by operator self.processingBands = context.getParameter('band_names') self.band_list = [] for band_name in self.processingBands: if (band_name != 'NULL' and band_name != 'null'): b = source_product.getBand(band_name) self.band_list.append(b) #get width and height of the image width = self.band_list[0].getRasterWidth() height = self.band_list[0].getRasterHeight() #read the .xml file with nedr, sensor filters (load only the processing bands) self.sensor_xml_path = str(context.getParameter('xmlpath_sensor')) [self.sensor_filter, self.nedr ] = input_sensor_filter.read_sensor_filter(self.sensor_xml_path, self.processingBands) #read the .xml file with parameters, SIOP and substrates self.siop_xml_path = str(context.getParameter('xmlpath_siop')) self.par_xml_path = str(context.getParameter('xmlpath_parameters')) [self.siop, self.envmeta] = input_parameters.sam_par(self.siop_xml_path, self.par_xml_path) #read parameters self.error_name = context.getParameter('error_name') self.opt_met = context.getParameter('opt_method') #read the flag for rrs and shallow (True or False) self.above_rrs_flag = context.getParameter('above_rrs_flag') self.shallow_flag = context.getParameter('shallow_flag') self.relaxed = context.getParameter('relaxed_cons') self.image_info = {} self.image_info['sensor_filter'] = self.sensor_filter self.image_info['nedr'] = self.nedr [ self.wavelengths, self.siop, self.image_info, self.fixed_parameters, self.objective ] = input_prepare.input_prepare_2(self.siop, self.envmeta, self.image_info, self.error_name) #define the sambuca algorithm self.algo = main_sambuca_snap.main_sambuca() #create the target product sambuca_product = snappy.Product('sambuca', 'sambuca', width, height) #import metadata and geocoding from the source_product snappy.ProductUtils.copyGeoCoding(self.band_list[0], sambuca_product) snappy.ProductUtils.copyMetadata(source_product, sambuca_product) #create the ouput bands and add them to the output product self.depth_band = sambuca_product.addBand( 'depth', snappy.ProductData.TYPE_FLOAT32) self.depth_band.setDescription('The depth computed by SAMBUCA') self.depth_band.setNoDataValue(Float.NaN) self.depth_band.setNoDataValueUsed(True) self.sdi_band = sambuca_product.addBand( 'sdi', snappy.ProductData.TYPE_FLOAT32) self.sdi_band.setDescription('The sdi computed by SAMBUCA') self.sdi_band.setNoDataValue(Float.NaN) self.sdi_band.setNoDataValueUsed(True) self.kd_band = sambuca_product.addBand('kd(550)', snappy.ProductData.TYPE_FLOAT32) self.kd_band.setDescription('The kd computed by SAMBUCA') self.kd_band.setNoDataValue(Float.NaN) self.kd_band.setNoDataValueUsed(True) self.error_f_band = sambuca_product.addBand( 'error_f', snappy.ProductData.TYPE_FLOAT32) self.error_f_band.setDescription('The error_f computed by SAMBUCA') self.error_f_band.setNoDataValue(Float.NaN) self.error_f_band.setNoDataValueUsed(True) self.r_sub_band = sambuca_product.addBand( 'r_sub(550)', snappy.ProductData.TYPE_FLOAT32) self.r_sub_band.setDescription('The r_sub(550) computed by SAMBUCA') self.r_sub_band.setNoDataValue(Float.NaN) self.r_sub_band.setNoDataValueUsed(True) self.sub1_frac_band = sambuca_product.addBand( 'sub_1', snappy.ProductData.TYPE_FLOAT32) self.sub1_frac_band.setDescription('The sub_1 % computed by SAMBUCA') self.sub1_frac_band.setNoDataValue(Float.NaN) self.sub1_frac_band.setNoDataValueUsed(True) self.sub2_frac_band = sambuca_product.addBand( 'sub_2', snappy.ProductData.TYPE_FLOAT32) self.sub2_frac_band.setDescription('The sub_2 % computed by SAMBUCA') self.sub2_frac_band.setNoDataValue(Float.NaN) self.sub2_frac_band.setNoDataValueUsed(True) self.sub3_frac_band = sambuca_product.addBand( 'sub_3', snappy.ProductData.TYPE_FLOAT32) self.sub3_frac_band.setDescription('The sub_3 % computed by SAMBUCA') self.sub3_frac_band.setNoDataValue(Float.NaN) self.sub3_frac_band.setNoDataValueUsed(True) #self.nit_band = sambuca_product.addBand('nit', snappy.ProductData.TYPE_FLOAT64) #self.nit_band.setDescription('The number of iterations computed by SAMBUCA') #self.nit_band.setNoDataValue(Float.NaN) #self.nit_band.setNoDataValueUsed(True) #test for adding a virtual band with corrected depth #virtDepth = snappy.VirtualBand("depth_corrected", snappy.ProductData.TYPE_FLOAT32, sambuca_product.getSceneRasterWidth(), sambuca_product.getSceneRasterHeight(), "depth + 20.1") #sambuca_product.addBand(virtDepth) #set the target product context.setTargetProduct(sambuca_product)
def phaseToHeight(inputfile, NLOOKS): """ This is the central processing chain inputfile is a string of an interferogram file name NLOOKS is the number of range looks to be used for multi-looking # Multi-looking, Goldstein phase filtering, unwrapping and terrain correction are performed The output is then written to file in BEAM-DIMAP format """ TEMP1 = 'ML_fl_temp.dim' TEMP2 = 'height_temp.dim' OUT = '_'.join( [inputfile.split('.dim')[0], 'ML', str(NLOOKS), 'height_TC.dim']) image = ProductIO.readProduct(inputfile) # SNAP Processing ------------------------------- image = createP('Multilook', image, nRgLooks=NLOOKS) image = createP('GoldsteinPhaseFiltering', image, alpha=0.2, useCoherenceMask=True, coherenceThreshold=0.2, writeout=TEMP1) # Numpy processing ----------------------------- image = ProductIO.readProduct(TEMP1) phase = image.getBand( [x for x in list(image.getBandNames()) if 'Phase' in x][0]) phase_data = get_data(phase) recentered = zero_phase(phase_data) unwrapped = unwrap(recentered) unw_deramped = remove_plane(unwrapped, 10000) height = unw_deramped / get_kz(image) height = height.transpose().flatten() # Write to target Product-------------------------- W = image.getBandAt(0).getRasterWidth() H = image.getBandAt(0).getRasterHeight() targetP = snappy.Product('height_product', 'height_type', W, H) ProductUtils.copyMetadata(image, targetP) ProductUtils.copyTiePointGrids(image, targetP) targetP.setProductWriter(ProductIO.getProductWriter('BEAM-DIMAP')) for band in ['Phase', 'coh']: bandname = [x for x in list(image.getBandNames()) if band in x][0] ProductUtils.copyBand(bandname, image, band, targetP, True) targetP.setProductReader(ProductIO.getProductReader('BEAM-DIMAP')) ProductIO.writeProduct(targetP, TEMP2, 'BEAM-DIMAP') targetB = targetP.addBand('height', snappy.ProductData.TYPE_FLOAT32) targetB.setUnit('m') targetP.setProductWriter(ProductIO.getProductWriter('BEAM-DIMAP')) targetP.writeHeader(TEMP2) targetB.writePixels(0, 0, W, H, height) targetP.closeIO() # Terrain correction------------------------- image = ProductIO.readProduct(TEMP2) image = createP('Terrain-Correction', image, alignToStandardGrid='true', demName='SRTM 1Sec HGT', saveDEM='true') ProductIO.writeProduct(image, OUT, 'BEAM-DIMAP')
def initialize(self, operator): """ GPF initialize method :param operator :return: """ resource_root = os.path.dirname(__file__) f = open(tempfile.gettempdir() + '/cava_tcwv_modis.log', 'w') f.write('Python module location: ' + __file__ + '\n') f.write('Python module location parent: ' + resource_root + '\n') # get source product: source_product = operator.getSourceProduct('sourceProduct') if not source_product: raise RuntimeError('No source product specified or product not found - cannot continue.') # f.write('Start initialize: source product is' + source_product.getFileLocation().getAbsolutePath() + '\n') f.write('Start initialize: source product is' + source_product.getName() + '\n') # get parameters: self.temperature = operator.getParameter('temperature') # todo: get temperature field from ERA-Interim self.pressure = operator.getParameter('pressure') # todo: get pressure field from ERA-Interim self.prior_aot = operator.getParameter('prior_aot') # todo: clarify if only one AOT is needed if os.path.isdir(resource_root): land_lut = os.path.join(resource_root, 'luts/land/land_core_modis_aqua.nc4') ocean_lut = os.path.join(resource_root, 'luts/ocean/ocean_core_modis_aqua.nc4') shared_libs_dir = resource_root else: with zipfile.ZipFile(resource_root) as zf: auxpath = SystemUtils.getAuxDataPath() f.write('auxpath: ' + str(auxpath) + '\n') if not os.path.exists(os.path.join(str(auxpath), 'cawa/luts/land/land_core_modis_aqua.nc4')): land_lut = zf.extract('luts/land/land_core_modis_aqua.nc4', os.path.join(str(auxpath), 'cawa')) f.write('extracted LUT land: ' + land_lut + '\n') else: land_lut = os.path.join(str(auxpath), 'cawa/luts/land/land_core_modis_aqua.nc4') f.write('existing LUT land: ' + land_lut + '\n') if not os.path.exists(os.path.join(str(auxpath), 'cawa/luts/ocean/ocean_core_modis_aqua.nc4')): ocean_lut = zf.extract('luts/ocean/ocean_core_modis_aqua.nc4', os.path.join(str(auxpath), 'cawa')) f.write('extracted LUT ocean: ' + ocean_lut + '\n') else: ocean_lut = os.path.join(str(auxpath), 'cawa/luts/ocean/ocean_core_modis_aqua.nc4') f.write('existing LUT ocean: ' + ocean_lut + '\n') shared_libs_dir = tempfile.gettempdir() if not os.path.exists(shared_libs_dir + '/lib-python'): lib_interpolator = zf.extract('lib-python/interpolators.so', shared_libs_dir) lib_nd_interpolator = zf.extract('lib-python/nd_interpolator.so', shared_libs_dir) lib_oec = zf.extract('lib-python/optimal_estimation_core.so', shared_libs_dir) else: lib_interpolator = os.path.join(str(shared_libs_dir), 'lib-python/interpolators.so') lib_nd_interpolator = os.path.join(str(shared_libs_dir), 'lib-python/nd_interpolator.so') lib_oec = os.path.join(str(shared_libs_dir), 'lib-python/optimal_estimation_core.so') f.write('LUT land: ' + land_lut + '\n') f.write('LUT ocean: ' + ocean_lut + '\n') f.write('shared_libs_dir = %s' % (shared_libs_dir + '/lib-python') + '\n') # sys.path.append(shared_libs_dir + '/lib-python') sys.path.append(shared_libs_dir + '/libs') import cawa_tcwv_modis_core as cawa_core import cawa_utils as cu self.cawa = cawa_core.CawaTcwvModisCore(land_lut, ocean_lut) self.cawa_utils = cu.CawaUtils() width = source_product.getSceneRasterWidth() height = source_product.getSceneRasterHeight() f.write('Source product width, height = ...' + str(width) + ', ' + str(height) + '\n') # get source bands: self.rho_toa_2_band = self.get_band(source_product, 'EV_250_Aggr1km_RefSB_2') # RefSB is from Idepix product! self.rho_toa_5_band = self.get_band(source_product, 'EV_500_Aggr1km_RefSB_5') # RefSB is from Idepix product! self.rho_toa_17_band = self.get_band(source_product, 'EV_1KM_RefSB_17') self.rho_toa_18_band = self.get_band(source_product, 'EV_1KM_RefSB_18') self.rho_toa_19_band = self.get_band(source_product, 'EV_1KM_RefSB_19') self.sza_band = self.get_band(source_product, 'SolarZenith') self.vza_band = self.get_band(source_product, 'SensorZenith') self.saa_band = self.get_band(source_product, 'SolarAzimuth') self.vaa_band = self.get_band(source_product, 'SensorAzimuth') self.prior_t2m_band = None self.prior_msl_band = None self.prior_tcwv_band = None self.prior_wsp_band = None if cu.CawaUtils.band_exists('t2m', source_product.getBandNames()): self.prior_t2m_band = self.get_band(source_product, 't2m') if cu.CawaUtils.band_exists('msl', source_product.getBandNames()): self.prior_msl_band = self.get_band(source_product, 'msl') if cu.CawaUtils.band_exists('tcwv', source_product.getBandNames()): self.prior_tcwv_band = self.get_band(source_product, 'tcwv') if cu.CawaUtils.band_exists('ws', source_product.getBandNames()): self.prior_wsp_band = self.get_band(source_product, 'ws') #self.classif_band = self.get_band(source_product, 'pixel_classif_flags') self.classif_band = None if cu.CawaUtils.band_exists('pixel_classif_flags', source_product.getBandNames()): self.classif_band = self.get_band(source_product, 'pixel_classif_flags') elif cu.CawaUtils.band_exists('cloud_classif_flags', source_product.getBandNames()): self.classif_band = self.get_band(source_product, 'cloud_classif_flags') # setup target product: cawa_product = snappy.Product('pyCAWA', 'CAWA TCWV', width, height) cawa_product.setDescription('CAWA TCWV product') cawa_product.setStartTime(source_product.getStartTime()) cawa_product.setEndTime(source_product.getEndTime()) # setup target bands: self.tcwv_band = cawa_product.addBand('tcwv', snappy.ProductData.TYPE_FLOAT32) self.tcwv_band.setNoDataValue(TCWV_NODATA_VALUE) self.tcwv_band.setNoDataValueUsed(True) self.tcwv_band.setUnit('mm') self.tcwv_band.setDescription('Total column of water vapour') self.tcwv_flags_band = cawa_product.addBand('tcwv_flags', snappy.ProductData.TYPE_UINT8) self.tcwv_flags_band.setUnit('dl') self.tcwv_flags_band.setDescription('TCWV flags band') # copy flag bands, tie points, geocoding: snappy.ProductUtils.copyFlagBands(source_product, cawa_product, True) # snappy.ProductUtils.copyFlagBands(classif_product, cawa_product, True) # snappy.ProductUtils.copyTiePointGrids(source_product, cawa_product) # todo: wait for fix in SNAP source_product.transferGeoCodingTo(cawa_product, None) operator.setTargetProduct(cawa_product) f.write('end initialize.') f.close()
def initialize(self, operator): """ GPF initialize method :param operator :return: """ resource_root = os.path.dirname(__file__) f = open(tempfile.gettempdir() + '/cava_ctp.log', 'w') f.write('Python module location: ' + __file__ + '\n') # print('Python module location: ' + __file__ + '\n') f.write('Python module location parent: ' + resource_root + '\n') # get source product: source_product = operator.getSourceProduct('sourceProduct') if not source_product: raise RuntimeError('No source product specified or product not found - cannot continue.') f.write('Start initialize: source product is ' + source_product.getName() + '\n') print('Start initialize: source product is ' + source_product.getName() + '\n') if os.path.isdir(resource_root): f.write('resource_root is dir ' + '\n') print('resource_root is dir ' + '\n') cloud_lut = os.path.join(resource_root, 'luts/cloud_core_meris.nc4') str_coeffs_lut = os.path.join(resource_root, 'luts/stray_coeff_potenz4.nc') ws_alb_lut = os.path.join(resource_root, 'luts/ws_alb_10_2005.nc') spectral_fluxes_input_path = os.path.join(resource_root, 'luts/meris_sun_spectral_flux_rr_10_11.txt') shared_libs_dir = resource_root else: f.write('extracting resources... ' + '\n') with zipfile.ZipFile(resource_root) as zf: auxpath = SystemUtils.getAuxDataPath() f.write('auxpath: ' + str(auxpath) + '\n') if not os.path.exists(os.path.join(str(auxpath), 'cawa/luts/cloud_core_meris.nc4')): cloud_lut = zf.extract('luts/cloud_core_meris.nc4', os.path.join(str(auxpath), 'cawa')) f.write('extracted LUT cloud: ' + cloud_lut + '\n') else: cloud_lut = os.path.join(str(auxpath), 'cawa/luts/cloud_core_meris.nc4') f.write('existing LUT cloud: ' + cloud_lut + '\n') if not os.path.exists(os.path.join(str(auxpath), 'cawa/luts/stray_coeff_potenz4.nc')): str_coeffs_lut = zf.extract('luts/stray_coeff_potenz4.nc', os.path.join(str(auxpath), 'cawa')) f.write('extracted stray_coeff: ' + str_coeffs_lut + '\n') else: str_coeffs_lut = os.path.join(str(auxpath), 'cawa/luts/stray_coeff_potenz4.nc') f.write('existing stray_coeff: ' + str_coeffs_lut + '\n') if not os.path.exists(os.path.join(str(auxpath), 'cawa/luts/ws_alb_10_2005.nc')): ws_alb_lut = zf.extract('luts/ws_alb_10_2005.nc', os.path.join(str(auxpath), 'cawa')) f.write('extracted ws_alb_10: ' + ws_alb_lut + '\n') else: ws_alb_lut = os.path.join(str(auxpath), 'cawa/luts/ws_alb_10_2005.nc') f.write('existing ws_alb_10: ' + ws_alb_lut + '\n') if not os.path.exists(os.path.join(str(auxpath), 'cawa/luts/meris_sun_spectral_flux_rr_10_11.txt')): spectral_fluxes_input_path = zf.extract('luts/meris_sun_spectral_flux_rr_10_11.txt', os.path.join(str(auxpath), 'cawa')) f.write('extracted meris_sun_spectral_flux_rr_10_11: ' + spectral_fluxes_input_path + '\n') else: spectral_fluxes_input_path = os.path.join(str(auxpath), 'cawa/luts/meris_sun_spectral_flux_rr_10_11.txt') f.write('existing meris_sun_spectral_flux_rr_10_11: ' + spectral_fluxes_input_path + '\n') shared_libs_dir = tempfile.gettempdir() if not os.path.exists(shared_libs_dir + '/lib-python'): lib_interpolator = zf.extract('lib-python/interpolators.so', shared_libs_dir) lib_nd_interpolator = zf.extract('lib-python/nd_interpolator.so', shared_libs_dir) lib_oec = zf.extract('lib-python/optimal_estimation_core.so', shared_libs_dir) else: lib_interpolator = os.path.join(str(shared_libs_dir), 'lib-python/interpolators.so') lib_nd_interpolator = os.path.join(str(shared_libs_dir), 'lib-python/nd_interpolator.so') lib_oec = os.path.join(str(shared_libs_dir), 'lib-python/optimal_estimation_core.so') f.write('shared_libs_dir = %s' % (shared_libs_dir + '/lib-python') + '\n') sys.path.append(shared_libs_dir + '/lib-python') import cawa_ctp_meris_core as cawa_core import cawa_utils as cu self.cawa = cawa_core.CawaCtpMerisCore(cloud_lut) self.cawa_utils = cu.CawaUtils() width = source_product.getSceneRasterWidth() height = source_product.getSceneRasterHeight() f.write('Source product width, height = ...' + str(width) + ', ' + str(height) + '\n') # get source bands: self.rad_10_band = self.get_band(source_product, 'radiance_10') # reflectance is from Idepix product! self.rad_11_band = self.get_band(source_product, 'radiance_11') self.detector_index_band = self.get_band(source_product, 'detector_index') self.sza_band = self.get_band(source_product, 'sun_zenith') self.vza_band = self.get_band(source_product, 'view_zenith') self.saa_band = self.get_band(source_product, 'sun_azimuth') self.vaa_band = self.get_band(source_product, 'view_azimuth') self.lat_band = self.get_band(source_product, 'latitude') self.lon_band = self.get_band(source_product, 'longitude') self.alt_band = self.get_band(source_product, 'dem_alt') self.l1_flag_band = self.get_band(source_product, 'l1_flags') self.classif_band = None if cu.CawaUtils.band_exists('pixel_classif_flags', source_product.getBandNames()): self.classif_band = self.get_band(source_product, 'pixel_classif_flags') elif cu.CawaUtils.band_exists('cloud_classif_flags', source_product.getBandNames()): self.classif_band = self.get_band(source_product, 'cloud_classif_flags') # setup target product: cawa_product = snappy.Product('pyCAWA', 'CAWA CTP', width, height) cawa_product.setDescription('CAWA CTP product') cawa_product.setStartTime(source_product.getStartTime()) cawa_product.setEndTime(source_product.getEndTime()) # setup target bands: self.ctp_band = cawa_product.addBand('ctp', snappy.ProductData.TYPE_FLOAT32) self.ctp_band.setNoDataValue(CTP_NODATA_VALUE) self.ctp_band.setNoDataValueUsed(True) self.ctp_band.setUnit('hPa') self.ctp_band.setDescription('Cloud Top Pressure') self.ctp_flags_band = cawa_product.addBand('ctp_flags', snappy.ProductData.TYPE_UINT8) self.ctp_flags_band.setUnit('dl') self.ctp_flags_band.setDescription('CTP flags band') # copy flag bands, tie points, geocoding: snappy.ProductUtils.copyFlagBands(source_product, cawa_product, True) source_product.transferGeoCodingTo(cawa_product, None) with Dataset(str_coeffs_lut,'r') as stray_ncds: #get the full stray coeffs self.str_coeffs=np.array(stray_ncds.variables['STRAY'][:],order='F') self.lmd=np.array(stray_ncds.variables['LAMBDA'][:],order='F') # doy = 363 datestring = cu.CawaUtils.get_meris_rr_product_datestring(source_product.getName()) print('datestring: ' + datestring + '\n') doy = int(cu.CawaUtils.get_doy_from_yyyymmdd(datestring)) print('doy: ' + str(doy) + '\n') with Dataset(ws_alb_lut,'r') as wsalb_ncds: #get the full stray coeffs #get closest day of year doy_idx=np.abs(wsalb_ncds.variables['time'][:]-doy).argmin() self.alb = wsalb_ncds.variables['albedo'][doy_idx,:,:] # spectral_fluxes_input_path = os.path.join(resource_root, 'luts/meris_sun_spectral_flux_rr_10_11.txt') spectral_fluxes_table = cu.CawaUtils.get_table_from_csvfile(spectral_fluxes_input_path, ',', "") self.spectral_flux_10 = np.array(spectral_fluxes_table['E0_band10']) self.spectral_flux_11 = np.array(spectral_fluxes_table['E0_band11']) operator.setTargetProduct(cawa_product) f.write('end initialize.') f.close()
def initialize(self, context): # Via the context object the source product which shall be processed can be retrieved source_product = context.getSourceProduct('source') print('initialize: source product location is', source_product.getFileLocation()) width = source_product.getSceneRasterWidth() height = source_product.getSceneRasterHeight() # Retrieve a parameters defined in ndvi_op-info.xml lower_band_name = context.getParameter('lowerName') self.lower_factor = context.getParameter('lowerFactor') upper_band_name = context.getParameter('upperName') self.upper_factor = context.getParameter('upperFactor') self.lower_band = self._get_band(source_product, lower_band_name) self.upper_band = self._get_band(source_product, upper_band_name) print('initialize: lower_band =', self.lower_band, ', upper_band =', self.upper_band) print('initialize: lower_factor =', self.lower_factor, ', upper_factor =', self.upper_factor) # As it is always a good idea to separate responsibilities the algorithmic methods are put # into an other class self.algo = ndvi_algo.NdviAlgo(NDVI_LOW_THRESHOLD, NDVI_HIGH_THRESHOLD) # Create the target product ndvi_product = snappy.Product('py_NDVI', 'py_NDVI', width, height) # ProductUtils provides several useful helper methods to build the target product. # In most cases it is sufficient to copy the information from the source to the target. # That's why mainly copy methods exist like copyBand(...), copyGeoCoding(...), copyMetadata(...) snappy.ProductUtils.copyGeoCoding(source_product, ndvi_product) snappy.ProductUtils.copyMetadata(source_product, ndvi_product) # For copying the time information no helper method exists yet, but will come in SNAP 5.0 ndvi_product.setStartTime(source_product.getStartTime()) ndvi_product.setEndTime(source_product.getEndTime()) # Adding new bands to the target product is straight forward. self.ndvi_band = ndvi_product.addBand('ndvi', snappy.ProductData.TYPE_FLOAT32) self.ndvi_band.setDescription( 'The Normalized Difference Vegetation Index') self.ndvi_band.setNoDataValue(Float.NaN) self.ndvi_band.setNoDataValueUsed(True) self.ndvi_flags_band = ndvi_product.addBand( 'ndvi_flags', snappy.ProductData.TYPE_UINT8) self.ndvi_flags_band.setDescription('The flag information') # Create a flagCoding for the flag band. This helps to display the information properly within SNAP. ndviFlagCoding = FlagCoding('ndvi_flags') # The NDVI_LOW flag shall be at bit position 0 and has therefor the value 1, NDVI_HIGH has the # value 2 (bit 1) and so one low_flag = ndviFlagCoding.addFlag( "NDVI_LOW", 1, "NDVI below " + str(NDVI_LOW_THRESHOLD)) high_flag = ndviFlagCoding.addFlag( "NDVI_HIGH", 2, "NDVI above " + str(NDVI_HIGH_THRESHOLD)) neg_flag = ndviFlagCoding.addFlag("NDVI_NEG", 4, "NDVI negative") pos_flag = ndviFlagCoding.addFlag("NDVI_POS", 8, "NDVI positive") ndvi_product.getFlagCodingGroup().add(ndviFlagCoding) self.ndvi_flags_band.setSampleCoding(ndviFlagCoding) # Also for each flag a layer should be created ndvi_product.addMask('mask_' + low_flag.getName(), 'ndvi_flags.' + low_flag.getName(), low_flag.getDescription(), Color.YELLOW, 0.3) ndvi_product.addMask('mask_' + high_flag.getName(), 'ndvi_flags.' + high_flag.getName(), high_flag.getDescription(), Color.GREEN, 0.3) ndvi_product.addMask('mask_' + neg_flag.getName(), 'ndvi_flags.' + neg_flag.getName(), neg_flag.getDescription(), Color(255, 0, 0), 0.3) ndvi_product.addMask('mask_' + pos_flag.getName(), 'ndvi_flags.' + pos_flag.getName(), pos_flag.getDescription(), Color.BLUE, 0.3) # Provide the created target product to the framework so the computeTileStack method can be called # properly and the data can be written to disk. context.setTargetProduct(ndvi_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.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)
def setup(): from SnappySharedFactory import SnappySharedFactory factory = SnappySharedFactory() product = snappy.Product("product", "type", 15, 10) band1 = product.addBand("band1", "double") band1.ensureRasterData() band1.setPixels(0, 0, band1_array.shape[0], band1_array.shape[1], band1_array.flatten()) band1.setSpectralWavelength(500.0) band1.setSpectralBandwidth(20.0) band1.setUnit("radiance") band2 = product.addBand("band2", "double") band2.setUnit("unit") band2.ensureRasterData() band2.setPixels(0, 0, band2_array.shape[0], band2_array.shape[1], band2_array.flatten()) tiepointgrid1 = snappy.TiePointGrid("tiepointgrid1", w, h, 0, 0, 1, 1) tiepointgrid1.setUnit("Pa") product.addTiePointGrid(tiepointgrid1) longitude = product.addBand("longitude", "double") longitude.setUnit("degrees") longitude.ensureRasterData() longitude.setPixels(0, 0, longitude_array.shape[0], longitude_array.shape[1], longitude_array.flatten()) latitude = product.addBand("latitude", "double") latitude.setUnit("degrees") latitude.ensureRasterData() latitude.setPixels(0, 0, latitude_array.shape[0], latitude_array.shape[1], latitude_array.flatten()) band1_variable_dict = { 'name': "band1", 'dtype': 'float', 'vtype': 'data', 'units': 'radiance', 'ndims': 2, 'shape': (15, 10), 'wavelength': 500.0, 'bandwidth': 20.0, 'srf': None } band2_variable_dict = { 'name': "band2", 'dtype': 'float', 'vtype': 'data', 'units': 'unit', 'ndims': 2, 'shape': (15, 10) } tiepointgrid1_variable_dict = { 'name': "tiepointgrid1", 'dtype': 'float', 'vtype': 'data', 'units': 'Pa', 'ndims': 2, 'shape': (15, 10) } latitude_variable_dict = { 'name': "latitude", 'dtype': 'float', 'vtype': 'info', 'units': 'degrees', 'ndims': 2, 'shape': (15, 10) } longitude_variable_dict = { 'name': "longitude", 'dtype': 'float', 'vtype': 'info', 'units': 'degrees', 'ndims': 2, 'shape': (15, 10) } variables = [ SpectralVariable(band1_variable_dict), Variable(band2_variable_dict), Variable(tiepointgrid1_variable_dict), Variable(latitude_variable_dict), Variable(longitude_variable_dict) ] attributes = {"product_string": "type"} return factory, product, variables, attributes
def initialize(self, context): #read the source_product source_product = context.getSourceProduct('source') if source_product is None: source_product = context.getSourceProduct('sourceProduct') if source_product is None: return print('initialize: source product location is', source_product.getFileLocation()) #get width and height of the image width = source_product.getSceneRasterWidth() height = source_product.getSceneRasterHeight() #get the names of the bands band_names = source_product.getBandNames() band_n=list(band_names) #choose the band with wavelenght between 420 and 750 nm self.min_w=context.getParameter('min_wlen') self.max_w=context.getParameter('max_wlen') band_l=[] y=[] for band_name in band_n: b=source_product.getBand(band_name) w=b.getSpectralWavelength() if w>self.min_w and w<self.max_w: band_l.append(b) y.append(w) #we order the band_list self.band_list=[band_l for (y,band_l) in sorted(zip(y,band_l))] #read the .xml file with nedr, sensor filters, parameters, SIOP and substrates self.sensor_xml_path=str(context.getParameter('xmlpath_sensor')) [self.sensor_filter, self.nedr]=input_sensor_filter.read_sensor_filter(self.sensor_xml_path) #read the .xml file with parameters, SIOP and substrates self.siop_xml_path=str(context.getParameter('xmlpath_siop')) self.par_xml_path=str(context.getParameter('xmlpath_parameters')) [self.siop, self.envmeta]=input_parameters.sam_par(self.siop_xml_path, self.par_xml_path) #read parameters self.error_name=context.getParameter('error_name') self.opt_met=context.getParameter('opt_method') #read the flag for rrs and shallow (True or False) self.above_rrs_flag=context.getParameter('above_rrs_flag') self.shallow_flag=context.getParameter('shallow_flag') self.relaxed=context.getParameter('relaxed_cons') self.image_info={} self.image_info['sensor_filter']=self.sensor_filter self.image_info['nedr']=self.nedr [self.wavelengths, self.siop, self.image_info, self.fixed_parameters, self.objective]=input_prepare.input_prepare_2(self.siop, self.envmeta, self.image_info, self.error_name) #define the sambuca algorithm self.algo=main_sambuca_snap.main_sambuca() #create the target product sambuca_product=snappy.Product('sambuca', 'sambuca', width, height) #import metadata and geocoding from the source_product snappy.ProductUtils.copyGeoCoding(source_product, sambuca_product) snappy.ProductUtils.copyMetadata(source_product, sambuca_product) #create the ouput bands and add them to the output product self.depth_band = sambuca_product.addBand('depth', snappy.ProductData.TYPE_FLOAT32) self.depth_band.setDescription('The depth computed by SAMBUCA') self.depth_band.setNoDataValue(Float.NaN) self.depth_band.setNoDataValueUsed(True) self.sdi_band = sambuca_product.addBand('sdi', snappy.ProductData.TYPE_FLOAT32) self.sdi_band.setDescription('The sdi computed by SAMBUCA') self.sdi_band.setNoDataValue(Float.NaN) self.sdi_band.setNoDataValueUsed(True) self.kd_band = sambuca_product.addBand('kd(550)', snappy.ProductData.TYPE_FLOAT32) self.kd_band.setDescription('The kd computed by SAMBUCA') self.kd_band.setNoDataValue(Float.NaN) self.kd_band.setNoDataValueUsed(True) self.error_f_band = sambuca_product.addBand('error_f', snappy.ProductData.TYPE_FLOAT32) self.error_f_band.setDescription('The error_f computed by SAMBUCA') self.error_f_band.setNoDataValue(Float.NaN) self.error_f_band.setNoDataValueUsed(True) self.r_sub_band = sambuca_product.addBand('r_sub(550)', snappy.ProductData.TYPE_FLOAT32) self.r_sub_band.setDescription('The r_sub(550) computed by SAMBUCA') self.r_sub_band.setNoDataValue(Float.NaN) self.r_sub_band.setNoDataValueUsed(True) self.sub1_frac_band = sambuca_product.addBand('sub_1', snappy.ProductData.TYPE_FLOAT32) self.sub1_frac_band.setDescription('The sub_1 % computed by SAMBUCA') self.sub1_frac_band.setNoDataValue(Float.NaN) self.sub1_frac_band.setNoDataValueUsed(True) self.sub2_frac_band = sambuca_product.addBand('sub_2', snappy.ProductData.TYPE_FLOAT32) self.sub2_frac_band.setDescription('The sub_2 % computed by SAMBUCA') self.sub2_frac_band.setNoDataValue(Float.NaN) self.sub2_frac_band.setNoDataValueUsed(True) self.sub3_frac_band = sambuca_product.addBand('sub_3', snappy.ProductData.TYPE_FLOAT32) self.sub3_frac_band.setDescription('The sub_3 % computed by SAMBUCA') self.sub3_frac_band.setNoDataValue(Float.NaN) self.sub3_frac_band.setNoDataValueUsed(True) #self.nit_band = sambuca_product.addBand('nit', snappy.ProductData.TYPE_FLOAT64) #self.nit_band.setDescription('The number of iterations computed by SAMBUCA') #self.nit_band.setNoDataValue(Float.NaN) #self.nit_band.setNoDataValueUsed(True) #set the target product context.setTargetProduct(sambuca_product)
def initialize(self, operator): source_product = operator.getSourceProduct('sourceProduct') if not source_product: raise RuntimeError( 'No source product specified or product not found - cannot continue.' ) print('start initialize: source product is', source_product.getFileLocation().getAbsolutePath()) # pixel classification from Idepix: classif_product = operator.getSourceProduct('classifProduct') if not classif_product: raise RuntimeError( 'No pixel classification product specified or product not found - cannot continue.' ) print('Python module location: ' + __file__) resource_root = os.path.dirname(__file__) print('Python module location parent: ' + resource_root) print('get parameters ...') self.temperature = operator.getParameter('temperature') self.pressure = operator.getParameter('pressure') self.aot13 = operator.getParameter('aot_13') self.aot14 = operator.getParameter('aot_14') self.aot15 = operator.getParameter('aot_15') self.sig_aot13 = self.aot13 self.sig_aot14 = self.aot14 self.sig_aot15 = self.aot15 self.cawa_utils = cu.CawaUtils() with zipfile.ZipFile(resource_root) as zf: auxpath = SystemUtils.getAuxDataPath() print('auxpath: ' + str(auxpath)) lut_json = zf.extract('luts/wadamo_core_meris.json', os.path.join(str(auxpath), 'cawa')) self.cawa = cawa_core.cawa_core(lut_json) print('LUT json file: ' + lut_json) width = source_product.getSceneRasterWidth() height = source_product.getSceneRasterHeight() print('width, height = ...', width, height) print('get bands ...') # todo: add something similar for MODIS input self.rhoToa13Band = self.get_band(source_product, 'reflec_13') self.rhoToa14Band = self.get_band(source_product, 'reflec_14') self.rhoToa15Band = self.get_band(source_product, 'reflec_15') self.szaBand = self.get_band(source_product, 'sun_zenith') print('got band vza ...') self.vzaBand = self.get_band(source_product, 'view_zenith') self.vaaBand = self.get_band(source_product, 'view_azimuth') self.l1_flag_band = self.get_band(source_product, 'l1_flags') self.classif_band = self.get_band(classif_product, 'cloud_classif_flags') print('setup target product...') cawa_product = snappy.Product('pyCAWA', 'CAWA TCWV', width, height) cawa_product.setDescription('CAWA TCWV product') cawa_product.setStartTime(source_product.getStartTime()) cawa_product.setEndTime(source_product.getEndTime()) # cawa_product.setPreferredTileSize(width, 16) # cawa_product.setPreferredTileSize(width, height) # todo: wadamo_core does not yet support multi-threading with smaller tiles self.tcwvBand = cawa_product.addBand('tcwv', snappy.ProductData.TYPE_FLOAT32) self.tcwvBand.setNoDataValue(TCWV_NODATA_VALUE) self.tcwvBand.setNoDataValueUsed(True) self.tcwvBand.setUnit('mm') self.tcwvBand.setDescription('Total column of water vapour') # todo: flag band self.tcwvFlagsBand = cawa_product.addBand( 'tcwv_flags', snappy.ProductData.TYPE_UINT8) self.tcwvFlagsBand.setUnit('dl') self.tcwvFlagsBand.setDescription('TCWV flags band') lat_ac_band = self.copy_src_band(source_product, cawa_product, 'corr_latitude') lat_ac_band.setNoDataValue(LAT_NODATA_VALUE) lat_ac_band.setNoDataValueUsed(True) lon_ac_band = self.copy_src_band(source_product, cawa_product, 'corr_longitude') lat_ac_band.setNoDataValue(LON_NODATA_VALUE) lon_ac_band.setNoDataValueUsed(True) sza_ac_band = self.copy_src_band(source_product, cawa_product, 'sun_zenith') vza_ac_band = self.copy_src_band(source_product, cawa_product, 'view_zenith') vaa_ac_band = self.copy_src_band(source_product, cawa_product, 'sun_azimuth') vaa_ac_band = self.copy_src_band(source_product, cawa_product, 'view_azimuth') altitude_ac_band = self.copy_src_band(source_product, cawa_product, 'altitude') snappy.ProductUtils.copyFlagBands(source_product, cawa_product, True) snappy.ProductUtils.copyFlagBands(classif_product, cawa_product, True) # copy geocoding: source_product.transferGeoCodingTo(cawa_product, None) print('set target product...') operator.setTargetProduct(cawa_product) print('end initialize.')
def initialize(self, context): source_product = context.getSourceProduct() if source_product is None: raise RuntimeError("Source product is missing") print('vertical_wind_shear_op initialize: source product location is', source_product.getFileLocation()) # Retrieve parameters defined in vertical_wind_shear_op-info.xml self.wind_height = context.getParameter('windHeight') self.shear_exponent = context.getParameter('shearExponent') print('initialize vertical_wind_shear_op: wind_height =', self.wind_height, ', shear_exponent =', self.shear_exponent) width = source_product.getSceneRasterWidth() height = source_product.getSceneRasterHeight() print('initialize vertical_wind_shear_op: width =', width, ', height =', height) # Create the target product wind_speed_product = snappy.Product(context.getId(), 'vertical_wind_shear_op', width, height) # copy metadata from source product to the new product snappy.ProductUtils.copyMetadata(source_product, wind_speed_product) owi_parameters_inst = owi_parameters.OwiParameters(source_product) self.owi_wind_speed_band = owi_parameters_inst.get_wind_band() # add new bands to the target product # 1) add .._001_owiWindSpeed band wind_parameter_name = owi_parameters_inst.get_owi_wind_speed_name() self.wind_band = wind_speed_product.addBand( wind_parameter_name, snappy.ProductData.TYPE_FLOAT32) # self.wind_band.setNoDataValue(Float.NaN) self.wind_band.setNoDataValue(owi_parameters_inst.get_no_data()) self.wind_band.setNoDataValueUsed(True) self.wind_band.setUnit("m/s") self.wind_band.setDescription('Wind speed adjusted to ' + str(self.wind_height) + ' metres height above sea level') # 2) get .._001_owiLat data owi_lat = source_product.getRasterDataNode( owi_parameters_inst.get_owi_lat_name()) if owi_lat is None: raise RuntimeError( "Requires a Sentinel-1 Level-2 OCN source product: missing " + owi_parameters_inst.get_owi_lat_name() + " band") owi_lat_image = owi_lat.getGeophysicalImage() lat_image_data = owi_lat_image.getData() lat_data = numpy.zeros( lat_image_data.getWidth() * lat_image_data.getHeight(), numpy.float32) lat_data = lat_image_data.getPixels(0, 0, lat_image_data.getWidth(), lat_image_data.getHeight(), lat_data) # 3) get .._001_owiLon data owi_lon = source_product.getRasterDataNode( owi_parameters_inst.get_owi_lon_name()) if owi_lon is None: raise RuntimeError( "Requires a Sentinel-1 Level-2 OCN source product: missing " + owi_parameters_inst.get_owi_lon_name() + " band") owi_lon_image = owi_lon.getGeophysicalImage() lon_image_data = owi_lon_image.getData() lon_data = numpy.zeros( lon_image_data.getWidth() * lon_image_data.getHeight(), numpy.float32) lon_data = lon_image_data.getPixels(0, 0, lon_image_data.getWidth(), lon_image_data.getHeight(), lon_data) # Add lat/lon coordinates. Create a TiePointGrid using the lat/lon data from 2) and 3) lat_grid = TiePointGrid("lat", width, height, 0.0, 0.0, 1.0, 1.0, lat_data) lon_grid = TiePointGrid("lon", width, height, 0.0, 0.0, 1.0, 1.0, lon_data) wind_speed_product.addTiePointGrid(lat_grid) wind_speed_product.addTiePointGrid(lon_grid) tie_point_geocoding = TiePointGeoCoding(lat_grid, lon_grid) wind_speed_product.setSceneGeoCoding(tie_point_geocoding) # Provide the created target product to the framework so the computeTileStack method can be called context.setTargetProduct(wind_speed_product)