def copy_bands_to_file(src_file_path, dst_file_path, bands=None):
    # Get info from source product
    src_prod = ProductIO.readProduct(src_file_path)
    prod_name = src_prod.getName()
    prod_type = src_prod.getProductType()
    width = src_prod.getSceneRasterWidth()
    height = src_prod.getSceneRasterHeight()
    if bands is None:
        bands = src_prod.getBandNames()

    # Copy geocoding and selected bands from source to destination product
    dst_prod = Product(prod_name, prod_type, width, height)
    ProductUtils.copyGeoCoding(src_prod.getBandAt(0), dst_prod)
    for band in bands:
        r = ProductUtils.copyBand(band, src_prod, dst_prod, True)
        if r is None:
            src_prod.closeIO()
            raise RuntimeError(src_file_path + " does not contain band " +
                               band)

    # Write destination product to disk
    ext = os.path.splitext(dst_file_path)[1]
    if ext == '.dim':
        file_type = 'BEAM_DIMAP'
    elif ext == '.nc':
        file_type = 'NetCDF-CF'
    elif ext == '.tif':
        file_type = 'GeoTIFF-BigTIFF'
    else:
        file_type = 'GeoTIFF-BigTIFF'
    ProductIO.writeProduct(dst_prod, dst_file_path, file_type)
    src_prod.closeIO()
    dst_prod.closeIO()
def write_snappy_product(file_path, bands, product_name, geo_coding):
    try:
        (height, width) = bands[0]['band_data'].shape
    except AttributeError:
        raise RuntimeError(bands[0]['band_name'] + "contains no data.")
    product = Product(product_name, product_name, width, height)
    product.setSceneGeoCoding(geo_coding)

    # Ensure that output is saved in BEAM-DIMAP format,
    # otherwise writeHeader does not work.
    file_path = os.path.splitext(file_path)[0] + '.dim'

    # Bands have to be created before header is written
    # but header has to be written before band data is written.
    for b in bands:
        band = product.addBand(b['band_name'], ProductData.TYPE_FLOAT32)
        if 'description' in b.keys():
            band.setDescription(b['description'])
        if 'unit' in b.keys():
            band.setUnit(b['unit'])
    product.setProductWriter(ProductIO.getProductWriter('BEAM-DIMAP'))
    product.writeHeader(String(file_path))
    for b in bands:
        band = product.getBand(b['band_name'])
        band.writePixels(0, 0, width, height,
                         b['band_data'].astype(np.float32))
    product.closeIO()
Exemple #3
0
def do_vegetation_indices (source):
    print ('\tVegetation Indices ...')
    ## Input product and dimensions
    input_product = ProductIO.readProduct(source)
    width = input_product.getSceneRasterWidth()
    height = input_product.getSceneRasterHeight()
    product_name = input_product.getName()
    product_description = input_product.getDescription()
    product_band_names = input_product.getBandNames()

    GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()

    ## input product red and nir bands
    b4 = input_product.getBand('B4')
    b8 = input_product.getBand('B8')

    ## output product (ndvi) new band
    output_product = Product('NDVI', 'NDVI', width, height)
    ProductUtils.copyGeoCoding(input_product, output_product)
    output_band = output_product.addBand('ndvi', ProductData.TYPE_FLOAT32)

    ## output writer
    output_product_writer = ProductIO.getProductWriter('BEAM-DIMAP')
    output_product.setProductWriter(output_product_writer)
    output_product.writeHeader(product_name + '_ndvi.dim')

    ## compute & save ndvi line by line
    red_row = numpy.zeros(width, dtype=numpy.float32)
    nir_row = numpy.zeros(width, dtype=numpy.float32)

    for y in xrange (height):
        red_row = b4.readPixels(0, y, width, 1, red_row)
        nir_row = b8.readPixels(0, y, width, 1, nir_row)
        ndvi = (nir_row - red_row)/(nir_row + red_row)
        output = output_band.writePixels(0, y, width, 1, ndvi)

    output_product.CloseIO()
    return output
Exemple #4
0
for S2_SAFE in os.listdir('products'):
    #NDVI:
    NDVI_im = S2_SAFE.split(".")[0] + "_NDVI"
    NIR_im = S2_SAFE.split(".")[0] + "_NIR"
    os.system('mkdir checked_products/' + NDVI_im)
    os.system('mkdir checked_products/' + NIR_im)
    S2_product = ProductIO.readProduct('products/' + S2_SAFE +
                                       '/GRANULE/output.dim')
    band_names = S2_product.getBandNames()
    width = S2_product.getSceneRasterWidth()
    height = S2_product.getSceneRasterHeight()
    b4 = S2_product.getBand('B4')
    b8 = S2_product.getBand('B8')

    newProduct = Product('NDVI', 'NDVI', width, height)

    newBand = newProduct.addBand('ndvi', ProductData.TYPE_FLOAT32)
    writer = ProductIO.getProductWriter('BEAM-DIMAP')
    ProductUtils.copyGeoCoding(S2_product, newProduct)
    newProduct.setProductWriter(writer)
    newProduct.writeHeader('NDVI.dim')
    rb4 = np.zeros(width, dtype=np.float32)
    rb8 = np.zeros(width, dtype=np.float32)
    for y in range(height):
        rb4 = b4.readPixels(0, y, width, 1, rb4)
        rb8 = b8.readPixels(0, y, width, 1, rb8)
        NDVI = (rb8 - rb4) / (rb8 + rb4)
        newBand.writePixels(0, y, width, 1, NDVI)
    newProduct.closeIO()
Exemple #5
0
    sys.exit(1)

print("Reading...")
sourceProduct = ProductIO.readProduct(sys.argv[1])
b1 = sourceProduct.getBand('reflec_5')
b2 = sourceProduct.getBand('reflec_7')
b3 = sourceProduct.getBand('reflec_9')
w1 = b1.getSpectralWavelength()
w2 = b2.getSpectralWavelength()
w3 = b3.getSpectralWavelength()
a = (w2 - w1) / (w3 - w1)
k = 1.03

width = sourceProduct.getSceneRasterWidth()
height = sourceProduct.getSceneRasterHeight()
targetProduct = Product('FLH_Product', 'FLH_Type', width, height)
targetBand = targetProduct.addBand('FLH', ProductData.TYPE_FLOAT32)
ProductUtils.copyGeoCoding(sourceProduct, targetProduct)
targetProduct.setProductWriter(ProductIO.getProductWriter('GeoTIFF'))

targetProduct.writeHeader(String('snappy_flh_output.tif'))

r1 = numpy.zeros(width, dtype=numpy.float32)
r2 = numpy.zeros(width, dtype=numpy.float32)
r3 = numpy.zeros(width, dtype=numpy.float32)

print("Writing...")

for y in range(height):
    b1.readPixels(0, y, width, 1, r1)
    b2.readPixels(0, y, width, 1, r2)
#SVR training
pipeline = make_pipeline(
    StandardScaler(),
    SVR(kernel='rbf', epsilon=0.105, C=250, gamma=2.8),
)
SVRmodel = pipeline.fit(X, Y)

# Predictfor validation data
valX = X
y_out = pipeline.predict(valX)

##---------------------------------------------------------------------------------
bandc11 = product.getBand('C11')
bandc22 = product.getBand('C22')

laiProduct = Product('LAI', 'LAI', width, height)
laiBand = laiProduct.addBand('lai', ProductData.TYPE_FLOAT32)
laiFlagsBand = laiProduct.addBand('lai_flags', ProductData.TYPE_UINT8)
writer = ProductIO.getProductWriter('BEAM-DIMAP')

ProductUtils.copyGeoCoding(product, laiProduct)
ProductUtils.copyMetadata(product, laiProduct)
ProductUtils.copyTiePointGrids(product, laiProduct)

laiFlagCoding = FlagCoding('lai_flags')
laiFlagCoding.addFlag("LAI_LOW", 1, "LAI below 0")
laiFlagCoding.addFlag("LAI_HIGH", 2, "LAI above 5")
group = laiProduct.getFlagCodingGroup()
#print(dir(group))
group.add(laiFlagCoding)
Exemple #7
0
    def create_product(self):
        from snappy import Product, ProductUtils, ProductIO, ProductData, String

        product = self.product
        ac_product = Product('L2h', 'L2h', self.width, self.height)
        writer = ProductIO.getProductWriter('BEAM-DIMAP')
        ac_product.setProductWriter(writer)
        ProductUtils.copyGeoCoding(product, ac_product)
        ProductUtils.copyMetadata(product, ac_product)
        ac_product.setStartTime(product.getStartTime())
        ac_product.setEndTime(product.getEndTime())

        # add metadata: ancillary data used for processing
        meta = jpy.get_type('org.esa.snap.core.datamodel.MetadataElement')
        att = jpy.get_type('org.esa.snap.core.datamodel.MetadataAttribute')
        # att(name=string,type=int), type: 41L->ascii; 12L->int32;
        att0 = att('AERONET file', ProductData.TYPE_ASCII)
        att0.setDataElems(self.aeronetfile)
        att1 = att('AOT', ProductData.TYPE_ASCII)
        att1.setDataElems(str(self.aot))

        meta = meta('L2')
        meta.setName('Ancillary Data')
        meta.addAttribute(att0)
        meta.addAttribute(att1)
        ac_product.getMetadataRoot().addElement(meta)

        # add data
        # Water-leaving radiance + sunglint
        for iband in range(self.N):
            bname = "Lnw_g_" + self.band_names[iband]
            acband = ac_product.addBand(bname, ProductData.TYPE_FLOAT32)
            acband.setSpectralWavelength(self.wl[iband])
            acband.setSpectralBandwidth(self.B[iband].getSpectralBandwidth())
            acband.setModified(True)
            acband.setNoDataValue(np.nan)
            acband.setNoDataValueUsed(True)
            acband.setValidPixelExpression(bname + ' >= -1')
            ac_product.getBand(bname).setDescription(
                "Water-leaving plus sunglint normalized radiance (Lnw + Lg) in mW cm-2 sr-1 μm-1 at "
                + self.band_names[iband])

        # Water-leaving radiance
        for iband in range(self.N):
            bname = "Lnw_" + self.band_names[iband]
            acband = ac_product.addBand(bname, ProductData.TYPE_FLOAT32)
            acband.setSpectralWavelength(self.wl[iband])
            acband.setSpectralBandwidth(self.B[iband].getSpectralBandwidth())
            acband.setModified(True)
            acband.setNoDataValue(np.nan)
            acband.setNoDataValueUsed(True)
            acband.setValidPixelExpression(bname + ' >= -1')
            ac_product.getBand(bname).setDescription(
                "Normalized water-leaving radiance in mW cm-2 sr-1 μm-1 at " +
                self.band_names[iband])

        # Sunglint reflection factor
        # for iband in range(self.N):
        bname = "BRDFg"  # + self.band_names[iband]
        acband = ac_product.addBand(bname, ProductData.TYPE_FLOAT32)
        # acband.setSpectralWavelength(self.wl[iband])
        # acband.setSpectralBandwidth(self.b[iband].getSpectralBandwidth())
        acband.setModified(True)
        acband.setNoDataValue(np.nan)
        acband.setNoDataValueUsed(True)
        acband.setValidPixelExpression(bname + ' >= 0')
        ac_product.getBand(bname).setDescription(
            "Glint reflection factor (BRDF) ")  # + self.band_names[iband])

        # Viewing geometry
        acband = ac_product.addBand("SZA", ProductData.TYPE_FLOAT32)
        acband.setModified(True)
        acband.setNoDataValue(np.nan)
        acband.setNoDataValueUsed(True)
        ac_product.getBand("SZA").setDescription("Solar zenith angle in deg.")

        acband = ac_product.addBand("VZA", ProductData.TYPE_FLOAT32)
        acband.setModified(True)
        acband.setNoDataValue(np.nan)
        acband.setNoDataValueUsed(True)
        ac_product.getBand("VZA").setDescription(
            "Mean viewing zenith angle in deg.")

        acband = ac_product.addBand("AZI", ProductData.TYPE_FLOAT32)
        acband.setModified(True)
        acband.setNoDataValue(np.nan)
        acband.setNoDataValueUsed(True)
        ac_product.getBand("AZI").setDescription(
            "Mean relative azimuth angle in deg.")

        ac_product.setAutoGrouping("Lnw:Lnw_g_")
        ac_product.writeHeader(String(self.outfile + ".dim"))
        self.l2_product = ac_product
Exemple #8
0
def write_BalticP_AC_Product(product,
                             baltic__product_path,
                             sensor,
                             data_dict,
                             singleBand_dict=None):
    File = jpy.get_type('java.io.File')
    width = product.getSceneRasterWidth()
    height = product.getSceneRasterHeight()
    bandShape = (height, width)

    balticPACProduct = Product('balticPAC', 'balticPAC', width, height)
    balticPACProduct.setFileLocation(File(baltic__product_path))

    ProductUtils.copyGeoCoding(product, balticPACProduct)
    ProductUtils.copyTiePointGrids(product, balticPACProduct)

    if (sensor == 'OLCI'):
        nbands = 21
        band_name = ["Oa01_radiance"]
        for i in range(1, nbands):
            if (i < 9):
                band_name += ["Oa0" + str(i + 1) + "_radiance"]
            else:
                band_name += ["Oa" + str(i + 1) + "_radiance"]

    # Create empty bands for rhow, rhown, uncertainties for rhow
    for i in range(nbands):
        bsource = product.getBand(band_name[i])  # TOA radiance

        for key in data_dict.keys():
            brtoa_name = key + "_" + str(i + 1)
            rtoaBand = balticPACProduct.addBand(brtoa_name,
                                                ProductData.TYPE_FLOAT32)
            ProductUtils.copySpectralBandProperties(bsource, rtoaBand)
            rtoaBand.setNoDataValue(np.nan)
            rtoaBand.setNoDataValueUsed(True)

    dataNames = [*data_dict.keys()]
    autoGroupingString = dataNames[0]
    for key in dataNames[1:]:
        autoGroupingString += ':' + key
    balticPACProduct.setAutoGrouping(autoGroupingString)

    if not singleBand_dict is None:
        for key in singleBand_dict.keys():
            singleBand = balticPACProduct.addBand(key,
                                                  ProductData.TYPE_FLOAT32)
            singleBand.setNoDataValue(np.nan)
            singleBand.setNoDataValueUsed(True)

    writer = ProductIO.getProductWriter('BEAM-DIMAP')
    balticPACProduct.setProductWriter(writer)
    balticPACProduct.writeHeader(baltic__product_path)
    writer.writeProductNodes(balticPACProduct, baltic__product_path)

    # set datarhow, rhown, uncertainties for rhow
    for key in data_dict.keys():
        x = data_dict[key].get('data')
        if not x is None:
            for i in range(nbands):
                brtoa_name = key + "_" + str(i + 1)
                rtoaBand = balticPACProduct.getBand(brtoa_name)
                out = np.array(x[:, i]).reshape(bandShape)
                rtoaBand.writeRasterData(
                    0, 0, width, height,
                    snp.ProductData.createInstance(np.float32(out)),
                    ProgressMonitor.NULL)

    if not singleBand_dict is None:
        for key in singleBand_dict.keys():
            x = singleBand_dict[key].get('data')
            if not x is None:
                singleBand = balticPACProduct.getBand(key)
                out = np.array(x).reshape(bandShape)
                singleBand.writeRasterData(
                    0, 0, width, height,
                    snp.ProductData.createInstance(np.float32(out)),
                    ProgressMonitor.NULL)

    # # Create flag coding
    # raycorFlagsBand = balticPACProduct.addBand('raycor_flags', ProductData.TYPE_UINT8)
    # raycorFlagCoding = FlagCoding('raycor_flags')
    # raycorFlagCoding.addFlag("testflag_1", 1, "Flag 1 for Rayleigh Correction")
    # raycorFlagCoding.addFlag("testflag_2", 2, "Flag 2 for Rayleigh Correction")
    # group = balticPACProduct.getFlagCodingGroup()
    # group.add(raycorFlagCoding)
    # raycorFlagsBand.setSampleCoding(raycorFlagCoding)

    balticPACProduct.closeIO()
Exemple #9
0
# check if band index given is correct
if not sys.argv[2] in ['2', '3', '4', '8']:
    print 'Incorrect band index'

# get cli arguments
product_file = sys.argv[1]
band_index = sys.argv[2]
band_name = 'B' + band_index
product_name = {
    'B2': 'blue',
    'B3': 'green',
    'B4': 'red',
    'B8': 'nir',
}[band_name]

# input product: open and get dimensions & name
input_product = ProductIO.readProduct(product_file)
product_width = input_product.getSceneRasterWidth()
product_height = input_product.getSceneRasterHeight()
product_name = input_product.getName()

# output product: copy selected band & save product
output_product = Product(product_name, product_name, product_width,
                         product_height)
ProductUtils.copyGeoCoding(input_product, output_product)
ProductUtils.copyBand(band_name, input_product, output_product, True)
ProductIO.writeProduct(output_product, product_name + '.band.dim',
                       'BEAM-DIMAP')
output_product.closeIO()
Exemple #10
0
if len(sys.argv) < 2:
    print 'Product file requires'
    sys.exit(1)

# input product & dimensions
input_product = ProductIO.readProduct(sys.argv[1])
width = input_product.getSceneRasterWidth()
height = input_product.getSceneRasterHeight()
product_name = input_product.getName()

# input product red & nir bands
red_band = input_product.getBand('B4')
nir_band = input_product.getBand('B8')

# output product (ndvi) & new band
output_product = Product('NDVI', 'NDVI', width, height)
ProductUtils.copyGeoCoding(input_product, output_product)
output_band = output_product.addBand('ndvi', ProductData.TYPE_FLOAT32)

# output writer
output_product_writer = ProductIO.getProductWriter('BEAM-DIMAP')
output_product.setProductWriter(output_product_writer)
output_product.writeHeader(product_name + '.ndvi.dim')

# compute & save ndvi line by line
red_row = numpy.zeros(width, dtype=numpy.float32)
nir_row = numpy.zeros(width, dtype=numpy.float32)

for y in xrange(height):
    red_row = red_band.readPixels(0, y, width, 1, red_row)
    nir_row = nir_band.readPixels(0, y, width, 1, nir_row)
Exemple #11
0
def process_product(file, sensor):
    in_product = ProductIO.readProduct(file)
    width = in_product.getSceneRasterWidth()
    height = in_product.getSceneRasterHeight()
    in_name = in_product.getName()
    in_description = in_product.getDescription()
    in_band_names = in_product.getBandNames()

    c2x_log.info("Product:     %s, %s" % (in_name, in_description))
    c2x_log.debug("Raster size: %d x %d pixels" % (width, height))
    c2x_log.debug("Start time:  " + str(in_product.getStartTime()))
    c2x_log.debug("End time:    " + str(in_product.getEndTime()))
    c2x_log.debug("Bands:       %s" % (list(in_band_names)))

    # Output product Definition
    # 1. define the target product and its file format
    c2x_product = Product('%s_%s' % (in_name, PRODUCT_TYPE), '%s' % PRODUCT_TYPE, width, height)
    writer = ProductIO.getProductWriter('BEAM-DIMAP')
    c2x_product.setProductWriter(writer)
    fpath = in_product.getFileLocation().getAbsolutePath()
    fpath = os.path.split(fpath)[0] + "/out/" + os.path.split(fpath)[1]
    fpath = fpath.split(".")[0]
    fpath = "{0}_{1}.dim".format(fpath, PRODUCT_TYPE.lower())
    c2x_product.setFileLocation(File(fpath))

    sensor_outputs = sensor["outputs"]
    sensor_wavelengths = sensor["wavelengths"]

    # 2. define the bands for the results of the different algorithms
    outbands = dict()
    for cnt in range(len(sensor_outputs)):
        cnt = sensor_outputs[cnt]
        outbands[cnt[0]] = c2x_product.addBand(cnt[0], cnt[1])

    # 3. copy tie point grids from input product to target product
    ProductUtils.copyTiePointGrids(in_product, c2x_product)
    ProductUtils.copyMetadata(in_product, c2x_product)
    ProductUtils.copyGeoCoding(in_product, c2x_product)
    ProductUtils.copyFlagBands(in_product, c2x_product, False)

    # 4. write the header to disk
    location = c2x_product.getFileLocation()
    c2x_product.writeHeader(location)

    # assigning aux arrays
    rhow_arrays = dict()
    for wls in sensor_wavelengths:
        rhow_arrays[str(wls)] = np.zeros(width, dtype=np.float32)

    #  get all specified bands from input product
    c2x_log.info("Processing and writing to %s" % file)
    algo_names = dict()
    for cnt in range(len(sensor_outputs)):
        algo_names[cnt] = sensor_outputs[cnt][0]
    c2x_log.debug("Processing with following algos: %s " % list(algo_names.values()))

    bsource = dict()
    for i in range(len(sensor_wavelengths)):
        band_name = create_source_band_name(sensor_wavelengths[i])
        bsource[band_name] = in_product.getBand(sensor[band_name])

    flag_bands = []
    for b in in_product.getBands():
        if b.isFlagBand():
            flag_bands.append(b)

    flags_data = np.zeros (width, dtype=np.int32)

    # loop through the product line by line and application of algorithms
    for y in range(height):
        rhow = dict()
        for wl in sensor_wavelengths:
            source_band = bsource[create_source_band_name(wl)]
            # dealing with no-data; setting no-data to to NaN
            invalidMask = read_invalid_mask(source_band, width, y)
            source_band.readPixels(0, y, width, 1, rhow_arrays[str(wl)])
            rhow["band" + str(wl)] = np.ma.array(rhow_arrays[str(wl)], mask=invalidMask, fill_value=np.nan)
        for algo in range(len(sensor_outputs)):
            res = sensor_outputs[algo][2](rhow, sensor_outputs[algo][4], sensor_outputs[algo][5])
            name = sensor_outputs[algo][0]
            outbands[name].writePixels(0, y, width, 1, res)
        for fband in flag_bands:
            fband.readPixels(0, y, width, 1, flags_data)
            c2x_product.getBand(fband.getName()).writePixels(0, y, width, 1, flags_data)

    # all computations and writing is completed; close all data streams and finish the program
    c2x_product.closeIO()

    print("Done.")
    return 0
Exemple #12
0
product = ProductIO.readProduct(File)
width = product.getSceneRasterWidth()
height = product.getSceneRasterHeight()
name = product.getName()
description = product.getDescription()
band_names = product.getBandNames()

print("Product:     %s, %s" % (name, description))
print("Raster size: %d x %d pixels" % (width, height))
print("Start time:  " + str(product.getStartTime()))
print("Description: %s" % description)
print("End time:    " + str(product.getEndTime()))
print("Bands:       %s" % (list(band_names)))


KNNProduct = Product('KNN', 'KNN', width, height)
KNNBand = KNNProduct.addBand('KNN', ProductData.TYPE_FLOAT32)
KNNFlagsBand = KNNProduct.addBand('KNN_flags', ProductData.TYPE_UINT8)
writer = ProductIO.getProductWriter('BEAM-DIMAP')

ProductUtils.copyGeoCoding(product, KNNProduct)

KNNFlagCoding = FlagCoding('KNN_flags')
KNNFlagCoding.addFlag("1", 1, "KNN above 0")
KNNFlagCoding.addFlag("2", 2, "KNN above 1")
KNNFlagCoding.addFlag("3", 3, "KNN above 2")
KNNFlagCoding.addFlag("4", 4, "KNN above 3")
KNNFlagCoding.addFlag("5", 5, "KNN above 4")
KNNFlagCoding.addFlag("6", 6, "KNN above 5")
group = KNNProduct.getFlagCodingGroup()
group.add(KNNFlagCoding)