def subset(product, borderRectInGeoCoor):
        from snappy import jpy
        from snappy import ProductIO
        from snappy import GPF
        from snappy import HashMap

        xmin = borderRectInGeoCoor[0]
        ymin = borderRectInGeoCoor[1]
        xmax = borderRectInGeoCoor[2]
        ymax = borderRectInGeoCoor[3]

        p1 = '%s %s' % (xmin, ymin)
        p2 = '%s %s' % (xmin, ymax)
        p3 = '%s %s' % (xmax, ymax)
        p4 = '%s %s' % (xmax, ymin)
        wkt = "POLYGON((%s, %s, %s, %s, %s))" % (p1, p2, p3, p4, p1)
        WKTReader = jpy.get_type('com.vividsolutions.jts.io.WKTReader')
        geom = WKTReader().read(wkt)

        HashMap = jpy.get_type('java.util.HashMap')
        GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()

        parameters = HashMap()
        parameters.put('copyMetadata', True)
        parameters.put('geoRegion', geom)
        parameters.put('outputImageScaleInDb', False)
        subset = GPF.createProduct('Subset', parameters, product)
        return subset
Example #2
0
    def _band_math(self, product, name, expression):

        GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
        HashMap = jpy.get_type('java.util.HashMap')

        BandDescriptor = jpy.get_type(
            'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor')

        targetBand = BandDescriptor()
        targetBand.name = name
        targetBand.type = 'float32'
        targetBand.expression = expression

        bands = jpy.array(
            'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor', 1)
        bands[0] = targetBand

        parameters = HashMap()
        parameters.put('targetBands', bands)

        productMap = HashMap()
        if isinstance(product, list):
            for ind in range(len(product)):
                print('p{}'.format(ind + 1))
                productMap.put('p{}'.format(ind + 1), product[ind])
            result = GPF.createProduct('BandMaths', parameters, productMap)
        else:
            result = GPF.createProduct('BandMaths', parameters, product)

        return result
Example #3
0
def resample(DIR, band, resolution):
    """
    resamples a band of a SENTINEL product to a given target resolution

    :param DIR: base directory of Sentinel2 directory tree
    :param band: band name (e.g. B4)
    :param resolution: target resolution in meter (e.g 10)

    :return: resampled band
    """

    from snappy import GPF

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

    HashMap = jpy.get_type('java.util.HashMap')
    BandDescriptor = jpy.get_type(
        'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor')

    parameters = HashMap()
    parameters.put('targetResolution', resolution)
    parameters.put('upsampling', 'Bicubic')
    parameters.put('downsampling', 'Mean')
    parameters.put('flagDownsampling', 'FlagMedianAnd')
    parameters.put('resampleOnPyramidLevels', True)

    product = ProductIO.readProduct(DIR)
    product = GPF.createProduct('Resample', parameters, product)

    rsp_band = product.getBand(band)

    return rsp_band
Example #4
0
    def band_maths(self, expression):
        """
        Perform a SNAP GPF BandMath operation on the product, excluding non-vegetation and non-soil pixels.

        Args:

            expression: The band maths expression to execute
        """
        print("\tBandMaths, product={}".format(self.product.getName()))

        BandDescriptor = jpy.get_type(
            'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor')

        band = BandDescriptor()
        band.name = 'band_maths'
        band.type = 'float32'
        band.expression = expression
        band.noDataValue = np.nan

        bands = jpy.array(
            'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor', 1)
        bands[0] = band

        HashMap = jpy.get_type('java.util.HashMap')
        parameters = HashMap()
        parameters.put('targetBands', bands)

        self.product = GPF.createProduct('BandMaths', parameters, self.product)
        return self.product
Example #5
0
    def extract_values(self, file):
        if not exists(self.plots):
            mkdir(self.plots)
        coords = []
        with open(file, 'r') as f:
            for line in f:
                line = line.replace('\n', '')
                coords.append([float(x) for x in line.split(' ')])

        GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
        HashMap = jpy.get_type('java.util.HashMap')

        Coords = jpy.array('org.esa.snap.pixex.Coordinate', len(coords))
        Coord = jpy.get_type('org.esa.snap.pixex.Coordinate')
        for ind, coord in enumerate(coords):
            c = Coord('Coord{}'.format(ind), coord[0], coord[1], None)
            Coords[ind] = c

        parameters = HashMap()
        parameters.put('exportBands', True)
        parameters.put('exportTiePoints', False)
        parameters.put('exportMasks', False)
        parameters.put('coordinates', Coords)
        parameters.put('outputDir', '.')

        pre = self._extract_values(parameters, self.product_pre)
        post = self._extract_values(parameters, self.product_post)
        waves = pre.meta['comments'][-1].replace('\t', ' ')
        waves = waves.replace('Wavelength:', '').split(' ')
        waves = filter(lambda x: x != '', waves)
        waves = [float(x) for x in waves]
        waves = filter(lambda x: x != 0, waves)
        bands = ['B{}'.format(ind) for ind in range(1, 9)]
        bands += ['B8A', 'B9']
        bands += ['B{}'.format(ind) for ind in range(11, 13)]
        print(pre.colnames)
        print(post.colnames)

        for ind in range(len(pre)):
            f, ax = plt.subplots()
            ax.set_xlabel('Wavelength (nm)')
            ax.set_ylabel('dl')

            radiances = list(pre[bands][ind])
            ax.plot(waves, radiances, color='g', label='pre')
            ax.scatter(waves, radiances, color='g')

            radiances = list(post[bands][ind])
            ax.plot(waves, radiances, color='b', label='post')
            ax.scatter(waves, radiances, color='b')

            lat = pre['Latitude'][ind]
            lon = pre['Longitude'][ind]
            ax.set_title("{}, {}".format(lat, lon))
            plt.legend()
            save = "{}/{}.pdf".format(self.plots, pre['Name'][ind])
            f.savefig(save, bbox_inches="tight")
Example #6
0
    def BoundingBox(self, data):

        # Java - Python bridge
        from snappy import jpy

        # The GeoPos class represents a geographical position measured in longitudes and latitudes.
        # http://step.esa.int/docs/v2.0/apidoc/engine/org/esa/snap/core/datamodel/GeoPos.html
        geoPos = jpy.get_type('org.esa.snap.core.datamodel.GeoPos')

        # A PixelPos represents a position or point in a pixel coordinate system.
        # http://step.esa.int/docs/v2.0/apidoc/engine/org/esa/snap/core/datamodel/PixelPos.html
        pixelPos = jpy.get_type('org.esa.snap.core.datamodel.PixelPos')

        geoCoding = data.getSceneGeoCoding()
        sceneUL = pixelPos(0 + 0.5, 0 + 0.5)
        sceneUR = pixelPos(data.getSceneRasterWidth() - 1 + 0.5, 0 + 0.5)
        sceneLL = pixelPos(0 + 0.5, data.getSceneRasterHeight() - 1 + 0.5)
        sceneLR = pixelPos(data.getSceneRasterWidth() - 1 + 0.5,
                           data.getSceneRasterHeight() - 1 + 0.5)

        gp_ul = geoCoding.getGeoPos(sceneUL, geoPos())
        gp_ur = geoCoding.getGeoPos(sceneUR, geoPos())
        gp_ll = geoCoding.getGeoPos(sceneLL, geoPos())
        gp_lr = geoCoding.getGeoPos(sceneLR, geoPos())

        coo_left = [gp_ul.getLon(), gp_ll.getLon()]
        coo_right = [gp_ur.getLon(), gp_lr.getLon()]
        coo_lower = [gp_ll.getLat(), gp_lr.getLat()]
        coo_upper = [gp_ul.getLat(), gp_ur.getLat()]

        # Get Bounding Box
        bbox = [min(coo_left), max(coo_right), min(coo_lower), max(coo_upper)]

        # Get Pixel Size (GSD) [Degree]
        d_upper_EW = coo_right[0] - coo_left[0]
        d_lower_EW = coo_right[1] - coo_left[1]

        pxSzE_deg = d_upper_EW / data.getSceneRasterWidth()
        pxSzW_deg = d_lower_EW / data.getSceneRasterWidth()

        pxSz_EW_deg = (pxSzE_deg + pxSzW_deg) / 2.0

        d_left_NS = coo_upper[0] - coo_lower[0]
        d_right_NS = coo_upper[1] - coo_lower[1]

        pxSzS_deg = d_left_NS / data.getSceneRasterHeight()
        pxSzN_deg = d_right_NS / data.getSceneRasterHeight()
        pxSz_NS_deg = (pxSzS_deg + pxSzN_deg) / 2.0

        return bbox, pxSz_EW_deg, pxSz_NS_deg
Example #7
0
    def __init__(self):
        # HashMap
        #           Key-Value pairs.
        #           https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
        self.HashMap = jpy.get_type('java.util.HashMap')

        # Get snappy Operators
        GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()

        # Variables
        #           Describes a target band to be generated by this operator.
        #           http://step.esa.int/docs/v2.0/apidoc/engine/org/esa/snap/core/gpf/common/BandMathsOp.BandDescriptor.html
        self.Variables = jpy.get_type(
            'org.esa.snap.core.gpf.common.MosaicOp$Variable')
Example #8
0
def read_terrain_corrected_product(path_to_dim):
    print(path_to_dim)
    dataproduct_r = ProductIO.readProduct(path_to_dim)
    # will the computed band show up?
    for band in dataproduct_r.getBandNames():
        print(band)

    print('WRITING OUT TO GEOTIFF!!')

    sigma0_ortho_bands = ["Sigma0_VH_use_local_inci_angle_from_dem",
                            "Sigma0_VV_use_local_inci_angle_from_dem"]


    convertComputedBandToBand(dataproduct_r.getBand(sigma0_ortho_bands[0]))
    convertComputedBandToBand(dataproduct_r.getBand(sigma0_ortho_bands[1]))

    HashMap = jpy.get_type('java.util.HashMap')
    sub_parameters = HashMap()
    sub_parameters.put('bandNames', ",".join(sigma0_ortho_bands)) # Should eventually look at using a local SRTM 1Sec DEM (instead of auto downloading)

    subset = GPF.createProduct("Subset", sub_parameters, dataproduct_r)

    final_output_name = Path(Path(path_to_dim).parent, "test")

    print('writing out final result')

    # Get a progressMonitor object
    # monitor = self.createProgressMonitor()

    # print('WRITING OUT PRODUCT')

    ProductIO.writeProduct(subset, str(final_output_name), 'BEAM-DIMAP')
Example #9
0
    def _get_formats(method):
        """This function provides a human readable list of SNAP Read or Write operator formats.

        Args:
            None.

        Returns
            Human readable list of SNAP Write operator formats.

        Raises:
            None.
        """
        ProductIOPlugInManager = jpy.get_type(
            "org.esa.snap.core.dataio.ProductIOPlugInManager")

        if method == "Read":
            plugins = ProductIOPlugInManager.getInstance().getAllReaderPlugIns(
            )
        elif method == "Write":
            plugins = ProductIOPlugInManager.getInstance().getAllWriterPlugIns(
            )
        else:
            raise ValueError

        formats = []

        while plugins.hasNext():
            plugin = plugins.next()
            formats.append(plugin.getFormatNames()[0])

        return formats
Example #10
0
    def __init__(self):
        # HashMap
        #           Key-Value pairs.
        #           https://docs.oracle.com/javase/7/docs/api/java/util/HashMap.html
        self.HashMap = jpy.get_type('java.util.HashMap')

        # Get snappy Operators
        GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
Example #11
0
def subset(product, shpPath):
    parameters = HashMap()
    wkt = shpToWKT(shpPath)
    SubsetOp = jpy.get_type('org.esa.snap.core.gpf.common.SubsetOp')
    geometry = WKTReader().read(wkt)
    parameters.put('copyMetadata', True)
    parameters.put('geoRegion', geometry)
    return GPF.createProduct('Subset', parameters, product)
Example #12
0
def bandMathSnap(input_dim,
                 output_file,
                 expression_list,
                 format_file='float32'):

    if debug >= 2:
        print(cyan + "bandmathSnap() : " + bold + green +
              "Import Dim to SNAP : " + endC + input_dim)

    # Info input file
    product = ProductIO.readProduct(input_dim)
    width = product.getSceneRasterWidth()
    height = product.getSceneRasterHeight()
    name = product.getName()
    description = product.getDescription()
    band_names = product.getBandNames()

    if debug >= 2:
        print(cyan + "bandmathSnap() : " + bold + green +
              "Product: %s, %d x %d pixels, %s" %
              (name, width, height, description) + endC)
        print(cyan + "bandmathSnap() : " + bold + green + "Bands:   %s" %
              (list(band_names)) + endC)

    # Instance de GPF
    GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()

    # Def operateur SNAP
    operator = 'BandMaths'
    BandDescriptor = jpy.get_type(
        'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor')
    targetBands = jpy.array(
        'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor',
        len(expression_list))

    # Get des expressions d'entréées
    i = 0
    for expression in expression_list:
        targetBand = BandDescriptor()
        targetBand.name = 'band_' + str(i + 1)
        targetBand.type = format_file
        targetBand.expression = expression
        targetBands[i] = targetBand
        i += 1

    # Set des parametres
    parameters = HashMap()
    parameters.put('targetBands', targetBands)

    # Get snappy Operators
    result = GPF.createProduct(operator, parameters, product)
    ProductIO.writeProduct(result, output_file, 'BEAM-DIMAP')

    if debug >= 2:
        print(cyan + "bandmathSnap() : " + bold + green + "Writing Done : " +
              endC + str(output_file))

    return result
Example #13
0
def plot_RGB(basedir):

    from snappy import ProductIO
    from snappy import ProductUtils
    from snappy import ProgressMonitor
    from snappy import jpy

    from os.path import join
    from tempfile import mkstemp

    mtd = 'MTD_MSIL1C.xml'
    _, rgb_image = mkstemp(dir='.', prefix='RGB', suffix='.png')
    source = join(basedir, mtd)

    sourceProduct = ProductIO.readProduct(source)
    b2 = sourceProduct.getBand('B2')
    b3 = sourceProduct.getBand('B3')
    b4 = sourceProduct.getBand('B4')

    Color = jpy.get_type('java.awt.Color')
    ColorPoint = jpy.get_type(
        'org.esa.snap.core.datamodel.ColorPaletteDef$Point')
    ColorPaletteDef = jpy.get_type(
        'org.esa.snap.core.datamodel.ColorPaletteDef')
    ImageInfo = jpy.get_type('org.esa.snap.core.datamodel.ImageInfo')
    ImageLegend = jpy.get_type('org.esa.snap.core.datamodel.ImageLegend')
    ImageManager = jpy.get_type('org.esa.snap.core.image.ImageManager')
    JAI = jpy.get_type('javax.media.jai.JAI')
    RenderedImage = jpy.get_type('java.awt.image.RenderedImage')

    # Disable JAI native MediaLib extensions
    System = jpy.get_type('java.lang.System')
    System.setProperty('com.sun.media.jai.disableMediaLib', 'true')

    #
    legend = ImageLegend(b2.getImageInfo(), b2)
    legend.setHeaderText(b2.getName())

    # red = product.getBand('B4')
    # green = product.getBand('B3')
    # blue = product.getBand('B2')

    image_info = ProductUtils.createImageInfo([b4, b3, b2], True,
                                              ProgressMonitor.NULL)
    im = ImageManager.getInstance().createColoredBandImage([b4, b3, b2],
                                                           image_info, 0)
    JAI.create("filestore", im, rgb_image, 'PNG')

    return rgb_image
Example #14
0
def S1_GRD_Preprocessing(graph, input_url, output_url):
    input_url = str(input_url)
    output_url = str(output_url)

    graph.getNode("read").getConfiguration().getChild(0).setValue(input_url)
    graph.getNode("write").getConfiguration().getChild(0).setValue(output_url)

    ### Execute Graph
    GraphProc = GraphProcessor()

    ### or a more concise implementation
    ConcisePM = jpy.get_type(
        'com.bc.ceres.core.PrintWriterConciseProgressMonitor')
    System = jpy.get_type('java.lang.System')
    pm = PrintPM(System.out)
    # ProductIO.writeProduct(resultProduct, outPath, "NetCDF-CF", pm)

    # GraphProcessor.executeGraph(graph, ProgressMonitor.NULL)
    GraphProc.executeGraph(graph, pm)
Example #15
0
def subset(product, x, y, width, heigth, toPrint=True):
    # subset of the Sentinel-1 GRD product by specify a rectangle whose top most left corner is defined by x and y coordinates
    HashMap = jpy.get_type('java.util.HashMap')
    GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
    parameters = HashMap()
    parameters.put('copyMetadata', True)
    parameters.put('region', "%s,%s,%s,%s" % (x, y, width, height))
    subset = GPF.createProduct('Subset', parameters, product)
    if toPrint:
        print("Bands:   %s" % (list(subset.getBandNames())))
    return subset
def testJavaMemory():

    Runtime = jpy.get_type('java.lang.Runtime')
    max_memory = Runtime.getRuntime().maxMemory()
    total_memory = Runtime.getRuntime().totalMemory()
    free_memory = Runtime.getRuntime().freeMemory()
    mb = 1024 * 1024
    print(cyan + "testJavaMemory() : " + bold + green + 'max memory : ' + str(max_memory / mb) + ' MB' + endC)
    print(cyan + "testJavaMemory() : " + bold + green + 'total memory : ' + str(total_memory / mb) + ' MB' + endC)
    print(cyan + "testJavaMemory() : " + bold + green + 'free memory : '+ str(free_memory / mb) + ' MB' + endC)
    return
Example #17
0
def get_band_math_config(band_name, expression):
    parameters = HashMap()
    band_descriptor = jpy.get_type('org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor')
    target_band = band_descriptor()
    target_band.name = band_name
    target_band.type = 'float32'
    target_band.expression = expression
    target_bands = jpy.array('org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor', 1)
    target_bands[0] = target_band
    parameters.put('targetBands', target_bands)
    return parameters
Example #18
0
 def resample(self, product, reference_band):
     resampling_op = jpy.get_type(
         'org.esa.snap.core.gpf.common.resample.ResamplingOp')
     op = resampling_op()
     op.setSourceProduct(product)
     op.setParameter('referenceBand', reference_band)
     op.setParameter('upsampling', 'Nearest')
     op.setParameter('downsampling', 'Mean')
     op.setParameter('flagDownsampling', 'First')
     op.setParameter('resampleOnPyramidLevels', 'true')
     return op.getTargetProduct()
Example #19
0
def compute_vegeation_index(product, index):
    index = ''.join(index)
    GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
    HashMap = jpy.get_type('java.util.HashMap')
    BandDescriptor = jpy.get_type(
        'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor')
    targetBand = BandDescriptor()
    targetBand.name = index
    targetBand.type = 'float32'
    targetBand.expression = indices_expr[index]
    targetBands = jpy.array(
        'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor', 1)
    targetBands[0] = targetBand
    parameters = HashMap()
    parameters.put('targetBands', targetBands)
    print("Start to compute:" + indices_expr[index])
    result = GPF.createProduct('BandMaths', parameters, product)
    print('Expression computed: ' + indices_expr[index])
    print result.getBand(index)
    return result.getBand(index)
def Subset(data, x, y, w, h):

    print('Subsetting the image...')

    HashMap = jpy.get_type('java.util.HashMap')
    GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()

    parameters = HashMap()
    parameters.put('copyMetadata', True)
    parameters.put('region', "%s,%s,%s,%s" % (x, y, w, h))

    return GPF.createProduct('Subset', parameters, data)
Example #21
0
    def reproject(self):
        """
        Perform a SNAP GPF Reprojection operation on the product by reprojecting from WGS-84 to EPSG:3067.
        """
        print("\tReproject, product={}".format(self.product.getName()))

        HashMap = jpy.get_type('java.util.HashMap')
        parameters = HashMap()
        parameters.put('crs', 'EPSG:3067')

        self.product = GPF.createProduct('Reproject', parameters, self.product)
        return self.product
Example #22
0
    def __init__(self, product, wkt_footprint, working_dir, external_dem_dir):
        """Prepare to use SNAPPY as a subprocess

        This class prepares commands and runs SNAPPY as a python3.4 subprocess

        """

        self.working_dir = working_dir
        self.product_meta = product
        self.wkt_footprint = wkt_footprint
        self.dem_dir = external_dem_dir

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

        # Set object parameterisation dictionary
        self.HashMap = jpy.get_type('java.util.HashMap')

        # Set start time and loop counter
        self.start_time = datetime.datetime.now()  # for calculation
        self.start_time_string = self.start_time.strftime("%Y-%m-%d %H:%M:%S")

        print("Processing start time:", self.start_time)
        self.name = self.product_meta['name']

        self.name_with_safe = self.name + '.SAFE'

        self.product_path = os.path.join(self.working_dir, self.name + '.zip')
        self.preprocess_path = working_dir
        self.manifest_path = os.path.join(self.working_dir, self.name_with_safe, 'manifest.safe')

        # for rs2
        self.productxml_path = os.path.join(self.working_dir, self.name, 'product.xml')

        # Read S1 raw data product and assigns source bands for given file
        # readProduct(File file, String... formatNames)
        if product['name'].startswith("S1"):
            self.dataproduct_r = snappy.ProductIO.readProduct(self.manifest_path)
            # self.srcbands = ["Intensity_VV", "Intensity_VH", "Amplitude_VV", "Amplitude_VH"]
            self.srcbands = ["Intensity_VV", "Intensity_VH"]

            print("Reading S1 data product with polarization bands:", self.srcbands)
        # Read RS2 raw data product and assigns source bands for given file
        else:
            self.dataproduct_r = snappy.ProductIO.readProduct(self.productxml_path)
            # self.srcbands = ["Intensity_VV", "Intensity_VH", "Amplitude_VV", "Amplitude_VH"]
            print("Reading RS2 data product with polarization bands:", self.srcbands)

        self.intermediate_product = None

        self.operations_list = []
Example #23
0
def BandMathList(stack):
    product = ProductIO.readProduct(stack)
    band_names = product.getBandNames()

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

    BandDescriptor = jpy.get_type(
        'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor')

    bandlist = list(band_names)
    #targetBands = list()
    x = 0
    y = 1
    bandlength = len(bandlist)
    runs = (bandlength - 1)
    targetBands = jpy.array(
        'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor', runs)
    for i in bandlist:
        while y <= runs:
            #print('{}_minus_{}'.format(bandlist[x], bandlist[y]))
            #targetBand1 = '{}_minus_{}'.format(bandlist[x], bandlist[y])
            targetBand1 = BandDescriptor()
            targetBand1.name = '{}_minus_{}'.format(bandlist[x], bandlist[y])
            targetBand1.type = 'float32'
            targetBand1.expression = '(({} - {})<(-2))? 255 : 0'.format(
                bandlist[x], bandlist[y])

            print("Writing Band {} : {}_minus_{}".format(
                x, bandlist[x], bandlist[y]))
            #targetBands.append(targetBand1)
            targetBands[x] = targetBand1

            x = x + 1
            y = y + 1
    """targetBand1 = BandDescriptor()
    targetBand1.name = 'first_{}_minus_last_{}'.format(bandlist[0], bandlist[bandlength])
    targetBand1.type = 'float32'
    targetBand1.expression = '(({} - {})<(-2))? 255 : 0'.format(bandlist[x], bandlist[y])
    print("Writing Band first_{}_minus_last_{}".format(bandlist[0], bandlist[bandlength])
    targetBands[bandlength] = targetBand1"""

    parameters = HashMap()
    parameters.put('targetBands', targetBands)
    result = GPF.createProduct('BandMaths', parameters, product)

    print("Writing...")
    ProductIO.writeProduct(result, 'BandMaths.dim', 'BEAM-DIMAP')
    print("BandMaths.dim Done.")
    ProductIO.writeProduct(result, 'BandMaths.tif', "GeoTIFF-BigTIFF")
    print("BandMaths.tif Done.")
Example #24
0
def subset(product):
    SubsetOp = jpy.get_type('org.esa.snap.core.gpf.common.SubsetOp')
    #    WKTReader = jpy.get_type('com.vividsolutions.jts.io.WKTReader')
    #    wkt = 'POLYGON ((27.350865857300093 36.824908050376905,
    #		     27.76637805803395 36.82295594263548,
    #	 	     27.76444424458719 36.628100558767244,
    #                     27.349980428973755 36.63003894847389,
    #                    27.350865857300093 36.824908050376905))'
    #    geometry = WKTReader().read(wkt)
    op = SubsetOp()
    op.setSourceProduct(product)
    op.setRegion(Rectangle(0, 500, 500, 500))
    sub_product = op.getTargetProduct()
    print("subset product ready")
    return sub_product
def band_math(src, band_name, expression):
    parameters = HashMap()
    band_descriptor = jpy.get_type(
        'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor')
    target_band = band_descriptor()
    target_band.name = band_name
    target_band.type = 'float32'
    target_band.expression = expression

    target_bands = jpy.array(
        'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor', 1)
    target_bands[0] = target_band
    parameters.put('targetBands', target_bands)

    return GPF.createProduct("BandMaths", parameters, src)
Example #26
0
    def resample(self):
        """
        Perform a SNAP GPF Resampling operation on the product by resizing every band to have spatial resolution of 10m.

        Example code:
        http://forum.step.esa.int/t/resample-all-bands-of-an-l2a-image/5032
        """
        print("\tResample, product={}".format(self.product.getName()))

        HashMap = jpy.get_type('java.util.HashMap')
        parameters = HashMap()
        parameters.put('targetResolution', 10)

        self.product = GPF.createProduct('Resample', parameters, self.product)
        return self.product
Example #27
0
    def _resample(self, name):
        p = ProductIO.readProduct(name)  # path of the xml file

        GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
        HashMap = jpy.get_type('java.util.HashMap')

        parameters = HashMap()
        parameters.put('targetResolution', 10)
        parameters.put('upsampling', 'Nearest')
        parameters.put('downsampling', 'First')
        parameters.put('upsampling', 'Nearest')
        parameters.put('flagDownsampling', 'First')
        parameters.put('resampleOnPyramidLevels', True)

        product = GPF.createProduct('Resample', parameters, p)
        return product
Example #28
0
def convertDim2Tiff(input_dim,
                    output_file,
                    name_file,
                    format_file='float32',
                    type_file='GeoTIFF'):

    if debug >= 2:
        print(cyan + "convertDim2Tiff() : " + bold + green +
              "Import Dim to SNAP : " + endC + input_dim)

    # Info input file
    product = ProductIO.readProduct(input_dim)
    band = product.getBand(name_file)

    width = product.getSceneRasterWidth()
    height = product.getSceneRasterHeight()

    # Instance de GPF
    GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()

    # Def operateur SNAP
    operator = 'BandMaths'
    BandDescriptor = jpy.get_type(
        'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor')
    targetBands = jpy.array(
        'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor', 1)

    # Get des expressions d'entréées
    targetBand = BandDescriptor()
    targetBand.name = 'band_0'
    targetBand.type = format_file
    targetBand.expression = name_file
    targetBands[0] = targetBand

    # Set des parametres
    parameters = HashMap()
    parameters.put('targetBands', targetBands)

    result = GPF.createProduct(operator, parameters, product)
    ProductIO.writeProduct(result, output_file, type_file)

    if debug >= 2:
        print(cyan + "convertDim2Tiff() : " + bold + green +
              "Writing Done : " + endC + str(output_file))

    return
Example #29
0
def landMask(file):
    p = ProductIO.readProduct(file)
    
    HashMap = jpy.get_type('java.util.HashMap')
    params = HashMap()

    band = 'Intensity_VH'
    params.put('sourceBands',band)
    params.put('landMask', False)
    land_sea_mask_product = GPF.createProduct('Land-Sea-Mask', params, p) 

    imgBand = land_sea_mask_product.getBand(band)
    image = imgBand.createColorIndexedImage(ProgressMonitor.NULL)
    name = File('landMask.tif')
    imageIO.write(image, 'tif', name)
    
    return ("Processed band:" + str(name)) , str(name)
Example #30
0
def resample(product, params):
    #    product = read_product(product)
    width = product.getSceneRasterWidth()
    height = product.getSceneRasterHeight()
    name = product.getName()
    description = product.getDescription()
    band_names = product.getBandNames()

    print("Product: %s, %d x %d pixels" % (name, width, height))

    print("Bands:   %s" % (list(band_names)))

    HashMap = jpy.get_type('java.util.HashMap')
    parameters = HashMap()
    parameters.put('targetResolution', params)
    result = GPF.createProduct('Resample', parameters, product)
    return result
Example #31
0
def main(args=sys.argv[1:]):
    if len(args) != 1:
        print("usage: raycorr-processor <SENSOR>")
        sys.exit(1)

    SENSOR = args[0]
    # SENSOR = 'OLCI'
    # SENSOR = 'MERIS'

    # PRODPATH = "C:\\Users\\carsten\\Dropbox\\Carsten\\SWProjects\\Rayleigh-Correction\\testdata\\"
    # AUXPATH = "C:\\Users\\carsten\\Dropbox\\Carsten\\Tagesordner\\20160104\\Rayleigh-Correction-Processor\\"
    # O3PATH="C:\\Users\\carsten\\Dropbox\\Carsten\\SWProjects\\Rayleigh-Correction\\raycorr\\"
    PRODPATH = "D:\\Dropbox\\Carsten\\SWProjects\\Rayleigh-Correction\\testdata\\"
    # AUXPATH = "D:\\Dropbox\\Carsten\\Tagesordner\\20160104\\Rayleigh-Correction-Processor\\"
    O3PATH="D:\\Dropbox\\Carsten\\SWProjects\\Rayleigh-Correction\\raycorr\\"

    DEMFactory = jpy.get_type('org.esa.snap.dem.dataio.DEMFactory')
    Resampling = jpy.get_type('org.esa.snap.core.dataop.resamp.Resampling')
    GeoPos = jpy.get_type('org.esa.snap.core.datamodel.GeoPos')

    if (SENSOR=='MERIS'):
        IN_FILE = PRODPATH+"subset_1_of_MER_RR__1PTACR20050713_094325_000002592039_00022_17611_0000.dim"
        OUT_FILE = PRODPATH+'Testprodukt1_MER_RR_20050713.dim'
    else:
        if (SENSOR=='OLCI'):
            IN_FILE = PRODPATH+'subset_3_of_S3A_OL_1_EFR____20160509T103945_20160509T104245_20160509T124907_0180_004_051_1979_SVL_O_NR_001.dim'
            OUT_FILE = PRODPATH+'Testproduct3_OL_1_EFR____20160509T103945.dim'
        else:
            print("Sensor ",SENSOR," not supported - exit")
            return
    file = IN_FILE

    # AUX_FILE = AUXPATH+'ADF\\MER_ATP_AXVACR20091026_144725_20021224_121445_20200101_000000'

    # adf = ADF(AUX_FILE)
    # ray_coeff_matrix = adf.ray_coeff_matrix
    # rayADF = readRayADF(AUX_FILE)

    # new_aux = OrderedDict()
    # new_aux['tau_ray'] = rayADF['tR']
    # new_aux['theta'] = rayADF['theta']
    # new_aux['ray_albedo_lut'] = rayADF['rayAlbLUT']
    # new_aux['ray_coeff_matrix'] = ray_coeff_matrix
    # with open('raycorr_auxdata.json', 'w') as fp:
    #         json.dumps(new_aux, fp, cls=JSONNumpyEncoder, indent=2)
    # fp.close()
    with open('../test/raycorr_auxdata.json', 'r') as fp:
        obj = json.load(fp, object_hook=json_as_numpy)
    # json_str = json.dumps(new_aux, cls=JSONNumpyEncoder, indent=2)
    # print(json_str)
    # obj = json.loads(json_str, object_hook=json_as_numpy)
    # rayADF = new_aux
    rayADF = obj
    ray_coeff_matrix=rayADF['ray_coeff_matrix']

    print("Reading...")
    product = ProductIO.readProduct(file)
    width = product.getSceneRasterWidth()
    height = product.getSceneRasterHeight()
    name = product.getName()
    description = product.getDescription()
    band_names = product.getBandNames()

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

    raycorProduct = Product('RayCorr', 'RayCorr', width, height)
    writer = ProductIO.getProductWriter('BEAM-DIMAP')
    raycorProduct.setProductWriter(writer)

    if (SENSOR == 'MERIS'):
        nbands = product.getNumBands() - 2  # the last 2 bands are l1flags and detector index; we don't need them
        band_name = ["radiance_1"]
        for i in range(1,nbands):
            band_name += ["radiance_" + str(i+1)]
    if (SENSOR == 'OLCI'):
        nbands = 21
        band_name = ["Oa01_radiance"]
        sf_name = ["solar_flux_band_1"]
        for i in range(1,nbands):
            if (i < 9):
                band_name += ["Oa0" + str(i + 1) + "_radiance"]
                sf_name += ["solar_flux_band_" + str(i + 1)]
            else:
                band_name += ["Oa" + str(i + 1) + "_radiance"]
                sf_name += ["solar_flux_band_" + str(i + 1)]

    # Create TOA reflectance and Rayleig optical thickness bands
    for i in range(nbands):
        # bsource = product.getBandAt(i)
        bsource = product.getBand(band_name[i])
        btoa_name = "rtoa_" + str(i + 1)
        toareflBand = raycorProduct.addBand(btoa_name, ProductData.TYPE_FLOAT32)
        ProductUtils.copySpectralBandProperties(bsource, toareflBand)

        btaur_name = "taur_" + str(i + 1)
        taurBand = raycorProduct.addBand(btaur_name, ProductData.TYPE_FLOAT32)
        ProductUtils.copySpectralBandProperties(bsource, taurBand)

        brhor_name = "rRay_" + str(i + 1)
        rhorBand = raycorProduct.addBand(brhor_name, ProductData.TYPE_FLOAT32)
        ProductUtils.copySpectralBandProperties(bsource, rhorBand)
        # Fourier Terms, during debugging only
        brhorF1_name = "rRayF1_" + str(i + 1)
        rhorF1Band = raycorProduct.addBand(brhorF1_name, ProductData.TYPE_FLOAT32)
        ProductUtils.copySpectralBandProperties(bsource, rhorF1Band)
        brhorF2_name = "rRayF2_" + str(i + 1)
        rhorF2Band = raycorProduct.addBand(brhorF2_name, ProductData.TYPE_FLOAT32)
        ProductUtils.copySpectralBandProperties(bsource, rhorF2Band)
        brhorF3_name = "rRayF3_" + str(i + 1)
        rhorF3Band = raycorProduct.addBand(brhorF3_name, ProductData.TYPE_FLOAT32)
        ProductUtils.copySpectralBandProperties(bsource, rhorF3Band)
        rayTransS_name = "transSRay_" + str(i + 1)
        rayTransSBand = raycorProduct.addBand(rayTransS_name, ProductData.TYPE_FLOAT32)
        ProductUtils.copySpectralBandProperties(bsource, rayTransSBand)
        rayTransV_name = "transVRay_" + str(i + 1)
        rayTransVBand = raycorProduct.addBand(rayTransV_name, ProductData.TYPE_FLOAT32)
        ProductUtils.copySpectralBandProperties(bsource, rayTransVBand)
        sARay_name = "sARay_" + str(i + 1)
        sARayBand = raycorProduct.addBand(sARay_name, ProductData.TYPE_FLOAT32)
        ProductUtils.copySpectralBandProperties(bsource, sARayBand)
        rtoaR_name = "rtoaRay_" + str(i + 1)
        rtoaRBand = raycorProduct.addBand(rtoaR_name, ProductData.TYPE_FLOAT32)
        ProductUtils.copySpectralBandProperties(bsource, rtoaRBand)
        rBRR_name = "rBRR_" + str(i + 1)
        rBRRBand = raycorProduct.addBand(rBRR_name, ProductData.TYPE_FLOAT32)
        ProductUtils.copySpectralBandProperties(bsource, rBRRBand)
        spf_name = "sphericalAlbedoFactor_" + str(i + 1)
        spfBand = raycorProduct.addBand(spf_name, ProductData.TYPE_FLOAT32)
        ProductUtils.copySpectralBandProperties(bsource, spfBand)
        # simple Rayleigh reflectance (Roland's formular)
        rRaySimple_name = "RayleighSimple_" + str(i + 1)
        rRaySimpleBand = raycorProduct.addBand(rRaySimple_name, ProductData.TYPE_FLOAT32)
        ProductUtils.copySpectralBandProperties(bsource, rRaySimpleBand)
        # gaseous absorption corrected TOA reflectances
        rho_ng_name = "rtoa_ng_" + str(i + 1)
        rho_ngBand = raycorProduct.addBand(rho_ng_name, ProductData.TYPE_FLOAT32)
        ProductUtils.copySpectralBandProperties(bsource, rho_ngBand)
        # simple Rayleigh optical thickness, for debugging
        taurS_name = "taurS_" + str(i + 1)
        taurSBand = raycorProduct.addBand(taurS_name, ProductData.TYPE_FLOAT32)
        ProductUtils.copySpectralBandProperties(bsource, taurSBand)

    raycorProduct.setAutoGrouping(
            'rtoa:taur:rRay:rRayF1:rRayF2:rRayF3:transSRay:transVRay:sARay:rtoaRay:rBRR:sphericalAlbedoFactor:RayleighSimple:rtoa_ng:taurS')

    airmassBand = raycorProduct.addBand('airmass', ProductData.TYPE_FLOAT32)
    azidiffBand = raycorProduct.addBand('azidiff', ProductData.TYPE_FLOAT32)
    altBand = raycorProduct.addBand('altitude', ProductData.TYPE_FLOAT32)

    # Create flag coding
    raycorFlagsBand = raycorProduct.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 = raycorProduct.getFlagCodingGroup()
    group.add(raycorFlagCoding)
    raycorFlagsBand.setSampleCoding(raycorFlagCoding)

    # add geocoding and create the product on disk (meta data, empty bands)
    ProductUtils.copyGeoCoding(product, raycorProduct) #geocoding is copied when tie point grids are copied,
    ProductUtils.copyTiePointGrids(product, raycorProduct)
    raycorProduct.writeHeader(OUT_FILE)

    # Calculate and write toa reflectances and Rayleigh optical thickness
    # ===================================================================
    # some stuff needed to get the altitude from an external DEM; can be omitted if altitude is used from the product
    # resamplingMethod = 'NEAREST_NEIGHBOUR'  # Resampling.NEAREST_NEIGHBOUR.getName()
    resamplingMethod = Resampling.NEAREST_NEIGHBOUR.getName()
    demName = 'GETASSE30'  # alternative 'SRTM 3Sec'
    dem = DEMFactory.createElevationModel(demName, resamplingMethod)

    # constants
    AVO = 6.0221367E+23  # Avogadro's number
    m_a_zero = 28.9595  # Mean molecular weight of dry ait (zero CO2)
    g0_45 = 980.616  # Acceleration of gravity (sea level and 458 latitude)
    Ns = 2.5469E19  # Molecular density of gas in molecules / cm3

    # constants describing the state of the atmosphere and which we don't know; better values may be used if known
    CO2 = 3.E-4  # CO2 concentration at pixel; typical values are 300 to 360 ppm
    C_CO2 = CO2 * 100  # CO2 concentration in ppm
    m_a = 15.0556 * CO2 + m_a_zero  # mean molecular weight of dry air as function of actual CO2

    # other constants
    PA = 0.9587256  # Rayleigh Phase function, molecular asymetry factor 1
    PB = 1. - PA  # Rayleigh Phase function, molecular asymetry factor 2
    tpoly = rayADF['tau_ray']  # Polynomial coefficients for Rayleigh transmittance
    h2o_cor_poly = np.array(
            [0.3832989, 1.6527957, -1.5635101, 0.5311913])  # Polynomial coefficients for WV transmission @ 709nm
    # absorb_ozon = np.array([0.0, 0.0002174, 0.0034448, 0.0205669, 0.0400134, 0.105446, 0.1081787, 0.0501634, 0.0410249, \
    #                         0.0349671, 0.0187495, 0.0086322, 0.0, 0.0, 0.0, 0.0084989, 0.0018944, 0.0012369, 0.0, 0.0, 0.0000488]) # OLCI
    # absorb_ozon = np.array([0.0002174, 0.0034448, 0.0205669, 0.0400134, 0.105446, 0.1081787, 0.0501634,  \
    #                         0.0349671, 0.0187495, 0.0086322, 0.0, 0.0084989, 0.0018944, 0.0012369, 0.0]) # MERIS
    O3_FILE = O3PATH+'ozone-highres.txt'
    ozoneO = O3(O3_FILE)
    absorb_ozon = ozoneO.convolveInstrument(SENSOR)

    # arrays which are needed to store some stuff
    E0 = np.zeros(width, dtype=np.float32)
    radiance = np.zeros(width, dtype=np.float32)
    reflectance = np.zeros((nbands, width), dtype=np.float32)
    taur = np.zeros((nbands, width), dtype=np.float32)
    sigma = np.zeros(nbands, dtype=np.float32)
    airmass = np.zeros(width, dtype=np.float32)
    azidiff = np.zeros(width, dtype=np.float32)
    PR = np.zeros(3, dtype=np.float32)  # Fourier coefficients of the Rayleigh Phase function
    rho_Rf = np.zeros(3, dtype=np.float32)  # Fourier terms of the Rayleigh primary scattering reflectance
    rho_Rm = np.zeros((3, nbands, width),
                      dtype=np.float32)  # Fourier terms of the Rayleigh scattering reflectance, corrected for multiple scattering
    rho_R = np.zeros((nbands, width), dtype=np.float32)  # first approximation of Rayleigh reflectance
    rho_toaR = np.zeros((nbands, width), dtype=np.float32)  # toa reflectance corrected for Rayleigh scattering
    rho_BRR = np.zeros((nbands, width),
                       dtype=np.float32)  # top of aerosol reflectance, which is equal to bottom of Rayleigh reflectance
    sphericalFactor = np.zeros((nbands, width),
                               dtype=np.float32)  # spherical Albedo Correction Factor (for testing only, can be integrated into the equation later)
    rRaySimple = np.zeros((nbands, width),
                          dtype=np.float32)  # simple Rayleigh reflectance formular, after Roland (for testing only)
    rho_ng = np.zeros((nbands, width),
                      dtype=np.float32)  # toa reflectance corrected for gaseous absorption (rho_ng = "rho no gas")
    X2 = np.zeros(width, dtype=np.float32)  # temporary variable used for WV correction algorithm for gaseous absorption
    trans709 = np.zeros(width,
                        dtype=np.float32)  # WV transmission at 709nm, used for WV correction algorithm for gaseous absorption
    taurS = np.zeros((nbands, width), dtype=np.float32)  # simple Rayleigh optical thickness, for debugging only

    if (SENSOR == 'MERIS'):
        dem_alt = 'dem_alt'
        atm_press = 'atm_press'
        ozone = 'ozone'
        latitude = 'latitude'
        longitude = 'longitude'
        sun_zenith = 'sun_zenith'
        view_zenith = 'view_zenith'
        sun_azimuth = 'sun_azimuth'
        view_azimuth = 'view_azimuth'
        # water vapour correction:
        # MERIS band 9 @ 709nm to be corrected; WV absorption 900nm = band 15, WV reference 885nm= band 14
        b709 = 8  # the band to be corrected
        bWVRef = 13  # the reference reflectance outside WV absorption band
        bWV = 14  # the reflectance within the WV absorption band
    if (SENSOR == 'OLCI'):
        dem_alt = 'N/A'
        atm_press = 'sea_level_pressure'
        ozone = 'total_ozone'
        latitude = 'TP_latitude'
        longitude = 'TP_longitude'
        sun_zenith = 'SZA'
        view_zenith = 'OZA'
        sun_azimuth = 'SAA'
        view_azimuth = 'OAA'
        # water vapour correction:
        # OLCI band 11 @ 709nm, WV absorption 900nm = band 19, WV reference 885nm = band 18
        b709 = 11 # the band to be corrected
        bWVRef=17 # the reference reflectance outside WV absorption band
        bWV=18 # the reference reflectance outside WV absorption band

    if (SENSOR == 'MERIS'): # check if this is required at all!
        tp_alt = product.getTiePointGrid(dem_alt)
    alt = np.zeros(width, dtype=np.float32)

    tp_press = product.getTiePointGrid(atm_press)
    press0 = np.zeros(width, dtype=np.float32)

    tp_ozone = product.getTiePointGrid(ozone)
    ozone = np.zeros(width, dtype=np.float32)

    tp_latitude = product.getTiePointGrid(latitude)
    lat = np.zeros(width, dtype=np.float32)
    tp_longitude = product.getTiePointGrid(longitude)
    lon = np.zeros(width, dtype=np.float32)

    tp_theta_s = product.getTiePointGrid(sun_zenith)
    theta_s = np.zeros(width, dtype=np.float32)

    tp_theta_v = product.getTiePointGrid(view_zenith)
    theta_v = np.zeros(width, dtype=np.float32)

    tp_azi_s = product.getTiePointGrid(sun_azimuth)
    azi_s = np.zeros(width, dtype=np.float32)

    tp_azi_v = product.getTiePointGrid(view_azimuth)
    azi_v = np.zeros(width, dtype=np.float32)

    # Rayleigh multiple scattering
    # - Coefficients LUT
    dimTheta = 12
    dimThetaS = dimThetaV = dimTheta
    gridThetaS = rayADF['theta']
    gridThetaV = rayADF['theta']
    gridGeometry = [gridThetaS, gridThetaV]
    RayScattCoeffA = ray_coeff_matrix[:, :, :, 0]
    RayScattCoeffB = ray_coeff_matrix[:, :, :, 1]
    RayScattCoeffC = ray_coeff_matrix[:, :, :, 2]
    RayScattCoeffD = ray_coeff_matrix[:, :, :, 3]
    # - Fourier terms
    a = np.zeros(3, dtype=np.float32)
    b = np.zeros(3, dtype=np.float32)
    c = np.zeros(3, dtype=np.float32)
    d = np.zeros(3, dtype=np.float32)
    rayMultiCorr = np.zeros(3, dtype=np.float32)

    # Rayleigh transmittances and spherical albedo
    tR_thetaS = np.zeros((nbands, width), dtype=np.float32)  # Rayleigh Transmittance sun - surface
    tR_thetaV = np.zeros((nbands, width), dtype=np.float32)  # Rayleigh Transmittance surface - sun
    dimTaur = 17
    taurTab = np.linspace(0.0, 1.0, num=dimTaur)
    rayAlb_f = interp1d(taurTab, rayADF['ray_albedo_lut'])
    sARay = np.zeros((nbands, width), dtype=np.float32)  # Rayleigh spherical albedo

    print("Processing ...")
    # Calculate the Rayleigh cross section, which depends only on wavelength but not on air pressure
    for i in range(nbands):
        print("processing Rayleigh cross section of band", i)
#        b_source = product.getBandAt(i)
        b_source = product.getBand(band_name[i])
        lam = b_source.getSpectralWavelength()  # wavelength of band i in nm
        lam = lam / 1000.0  # wavelength in micrometer
        lam2 = lam / 10000.0  # wavelength in cm
        F_N2 = 1.034 + 0.000317 / (lam ** 2)  # King factor of N2
        F_O2 = 1.096 + 0.001385 / (lam ** 2) + 0.0001448 / (lam ** 4)  # King factor of O2
        F_air = (78.084 * F_N2 + 20.946 * F_O2 + 0.934 * 1 + C_CO2 * 1.15) / (
            78.084 + 20.946 + 0.934 + C_CO2)  # depolarization ratio or King Factor, (6+3rho)/(6-7rho)
        n_ratio = 1 + 0.54 * (CO2 - 0.0003)
        n_1_300 = (8060.51 + (2480990. / (132.274 - lam ** (-2))) + (17455.7 / (39.32957 - lam ** (-2)))) / 100000000.0
        nCO2 = n_ratio * (1 + n_1_300)  # reflective index at CO2
        sigma[i] = (24 * math.pi ** 3 * (nCO2 ** 2 - 1) ** 2) / (lam2 ** 4 * Ns ** 2 * (nCO2 ** 2 + 2) ** 2) * F_air

    for y in range(height):
        print("processing line ", y, " of ", height)
        # start radiance to reflectance conversion
        theta_s = tp_theta_s.readPixels(0, y, width, 1, theta_s)  # sun zenith angle in degree
        for i in range(nbands):
            b_source = product.getBand(band_name[i])
            radiance = b_source.readPixels(0, y, width, 1, radiance)
            if (SENSOR == 'MERIS'):
                E0.fill(b_source.getSolarFlux())
            if (SENSOR == 'OLCI'):
                    b_source = product.getBand(sf_name[i])
                    E0 = b_source.readPixels(0, y, width, 1, E0)
            reflectance[i] = radiance * math.pi / (E0 * np.cos(np.radians(theta_s)))
            b_out = raycorProduct.getBand("rtoa_" + str(i + 1))
            b_out.writePixels(0, y, width, 1, reflectance[i])
        # radiance to reflectance conversion completed

        # this is dummy code to create a flag
        flag1 = np.zeros(width, dtype=np.bool_)
        flag2 = np.zeros(width, dtype=np.bool_)
        raycorFlags = flag1 + 2 * flag2
        raycorFlagsBand.writePixels(0, y, width, 1, raycorFlags)
        # end flags dummy code

    # raycorProduct.closeIO()
    # if (0==1):
        lat = tp_latitude.readPixels(0, y, width, 1, lat)
        lon = tp_longitude.readPixels(0, y, width, 1, lon)

        # start Rayleigh optical thickness calculation
        # alt = tp_alt.readPixels(0, y, width, 1, alt)  # using the tie-point DEM in a MERIS product
        # get the altitude from an external DEM
        for x in range(width): alt[x] = dem.getElevation(GeoPos(lat[x], lon[x]))

        press0 = tp_press.readPixels(0, y, width, 1, press0)
        ozone = tp_ozone.readPixels(0, y, width, 1, ozone)

        theta_s = tp_theta_s.readPixels(0, y, width, 1, theta_s)  # sun zenith angle in degree
        theta_v = tp_theta_v.readPixels(0, y, width, 1, theta_v)  # view zenith angle in degree
        azi_s = tp_azi_s.readPixels(0, y, width, 1, azi_s)  # sun azimuth angle in degree
        azi_v = tp_azi_v.readPixels(0, y, width, 1, azi_v)  # view azimuth angle in degree

        # gaseous absorption correction
        rho_ng = reflectance  # to start: gaseous corrected reflectances equals toa reflectances
        # water vapour correction:
        # MERIS band 9 @ 709nm to be corrected; WV absorption 900nm = band 15, WV reference 885nm= band 14
        # b709 = 8  # the band to be corrected
        # bWVRef = 13  # the reference reflectance outside WV absorption band
        # bWV = 14  # the reflectance within the WV absorption band
        # OLCI band 11 @ 709nm, WV absorption 900nm = band 19, WV reference 885nm = band 18
        # b709 = 11 # the band to be corrected
        # bWVRef=17 # the reference reflectance outside WV absorption band
        # bWV=18 # the reference reflectance outside WV absorption band
        for i in range(width):
            if (reflectance[(bWV, i)] > 0):
                X2[i] = reflectance[(bWV, i)] / reflectance[(bWVRef, i)]
            else:
                X2[i] = 1
        trans709 = h2o_cor_poly[0] + (h2o_cor_poly[1] + (h2o_cor_poly[2] + h2o_cor_poly[3] * X2) * X2) * X2
        rho_ng[b709] /= trans709
        # ozone correction
        model_ozone = 0
        for x in range(width):
            ts = math.radians(theta_s[x])  # sun zenith angle in radian
            cts = math.cos(ts)  # cosine of sun zenith angle
            sts = math.sin(ts)  # sinus of sun zenith angle
            tv = math.radians(theta_v[x])  # view zenith angle in radian
            ctv = math.cos(tv)  # cosine of view zenith angle
            stv = math.sin(tv)  # sinus of view zenith angle
            for i in range(nbands):
                trans_ozoned12 = math.exp(-(absorb_ozon[i] * ozone[x] / 1000.0 - model_ozone) / cts)
                trans_ozoneu12 = math.exp(-(absorb_ozon[i] * ozone[x] / 1000.0 - model_ozone) / ctv)
                trans_ozone12 = trans_ozoned12 * trans_ozoneu12
                rho_ng[(i, x)] /= trans_ozone12
        # here we can decide if we continue with gaseous corrected reflectances or not
        reflectance = rho_ng

        # Now calculate the pixel dependent terms (like pressure) and finally the Rayleigh optical thickness
        for x in range(width):
            # Calculation to get the pressure
            z = alt[x]  # altitude at pixel in meters, taken from MERIS tie-point grid
            z = max(z, 0)  # clip to sea level
            Psurf0 = press0[x]  # pressure at sea level in hPa, taken from MERIS tie-point grid
            Psurf = Psurf0 * (
                                 1. - 0.0065 * z / 288.15) ** 5.255  # air pressure at the pixel (i.e. at altitude) in hPa, using the international pressure equation
            P = Psurf * 1000.  # air pressure at pixel location in dyn / cm2, which is hPa * 1000
            # calculation to get the constant of gravity at the pixel altitude, taking the air mass above into account
            dphi = math.radians(lat[x])  # latitude in radians
            cos2phi = math.cos(2 * dphi)
            g0 = g0_45 * (1 - 0.0026373 * cos2phi + 0.0000059 * cos2phi ** 2)
            zs = 0.73737 * z + 5517.56  # effective mass-weighted altitude
            g = g0 - (0.0003085462 + 0.000000227 * cos2phi) * zs + (0.00000000007254 + 0.0000000000001 * cos2phi) * \
                                                                   zs ** 2 - (1.517E-17 + 6E-20 * cos2phi) * zs ** 3
            # calculations to get the Rayeigh optical thickness
            factor = (P * AVO) / (m_a * g)
            for i in range(nbands):
                taur[(i, x)] = sigma[i] * factor

            # Calculate Rayleigh Phase function
            ts = math.radians(theta_s[x])  # sun zenith angle in radian
            cts = math.cos(ts)  # cosine of sun zenith angle
            sts = math.sin(ts)  # sinus of sun zenith angle
            tv = math.radians(theta_v[x])  # view zenith angle in radian
            ctv = math.cos(tv)  # cosine of view zenith angle
            stv = math.sin(tv)  # sinus of view zenith angle
            airmass[x] = 1 / cts + 1 / ctv  # air mass
            # Rayleigh Phase function, 3 Fourier terms
            PR[0] = 3. * PA / 4. * (1. + cts ** 2 * ctv ** 2 + (sts ** 2 * stv ** 2) / 2.) + PB
            PR[1] = -3. * PA / 4. * cts * ctv * sts * stv
            PR[2] = 3. * PA / 16. * sts ** 2 * stv ** 2
            # Calculate azimuth difference
            azs = math.radians(azi_s[x])
            azv = math.radians(azi_v[x])
            cosdeltaphi = math.cos(azv - azs)
            azidiff[x] = math.acos(cosdeltaphi)  # azimuth difference in radian
            # Fourier components of multiple scattering
            for j in [0, 1, 2]:
                a[j] = interpn(gridGeometry, RayScattCoeffA[j, :, :], [theta_s[x], theta_v[x]], method='linear',
                               bounds_error=False, fill_value=None)
                b[j] = interpn(gridGeometry, RayScattCoeffB[j, :, :], [theta_s[x], theta_v[x]], method='linear',
                               bounds_error=False, fill_value=None)
                c[j] = interpn(gridGeometry, RayScattCoeffC[j, :, :], [theta_s[x], theta_v[x]], method='linear',
                               bounds_error=False, fill_value=None)
                d[j] = interpn(gridGeometry, RayScattCoeffD[j, :, :], [theta_s[x], theta_v[x]], method='linear',
                               bounds_error=False, fill_value=None)

            for i in range(nbands):
                # Fourier series, loop
                for j in [0, 1, 2]:
                    # Rayleigh primary scattering
                    rho_Rf[j] = (PR[j] / (4.0 * (cts + ctv))) * (1. - math.exp(-airmass[x] * taur[(i, x)]))
                    # correction for multiple scattering
                    rayMultiCorr[j] = a[j] + b[j] * taur[(i, x)] + c[j] * taur[(i, x)] ** 2 + d[j] * taur[(i, x)] ** 3
                    rho_Rm[(j, i, x)] = rho_Rf[j] * rayMultiCorr[j]
                # rho_Rm[(0, i, x)]  = rho_Rf[0]
                # rho_Rm[(1, i, x)]  = 0.
                # rho_Rm[(2, i, x)]  = 0.
                # Fourier sum to get the Rayleigh Reflectance
                rho_R[(i, x)] = rho_Rm[(0, i, x)] + 2.0 * rho_Rm[(1, i, x)] * math.cos(azidiff[x]) + 2. * rho_Rm[
                    (2, i, x)] * math.cos(2. * azidiff[x])
                # complete the Rayleigh correction: see MERIS DPM PDF-p251 or DPM 9-16
                # polynomial coefficients tpoly0, tpoly1 and tpoly2 from MERIS LUT
                tRs = ((2. / 3. + cts) + (2. / 3. - cts) * math.exp(-taur[(i, x)] / cts)) / (4. / 3. + taur[(i, x)])
                tR_thetaS[(i, x)] = tpoly[0] + tpoly[1] * tRs + tpoly[
                                                                    2] * tRs ** 2  # Rayleigh Transmittance sun - surface
                tRv = ((2. / 3. + ctv) + (2. / 3. - ctv) * math.exp(-taur[(i, x)] / ctv)) / (4. / 3. + taur[(i, x)])
                tR_thetaV[(i, x)] = tpoly[0] + tpoly[1] * tRv + tpoly[
                                                                    2] * tRv ** 2  # Rayleigh Transmittance surface - sensor

                sARay[(i, x)] = rayAlb_f(taur[(i, x)])  # Rayleigh spherical albedo

                rho_toaR[(i, x)] = (reflectance[(i, x)] - rho_R[(i, x)]) / (
                    tR_thetaS[(i, x)] * tR_thetaV[(i, x)])  # toa reflectance corrected for Rayleigh scattering
                sphericalFactor[(i, x)] = 1.0 / (1.0 + sARay[(i, x)] * rho_toaR[
                    (i, x)])  # factor used in the next equation to account for the spherical albedo
                rho_BRR[(i, x)] = rho_toaR[(i, x)] * sphericalFactor[
                    (i, x)]  # top of aerosol reflectance, which is equal to bottom of Rayleigh reflectance

            # simple Rayleigh correction
            azi_diff_deg = math.fabs(azi_v[x] - azi_s[x])
            if (azi_diff_deg > 180.0):
                azi_diff_deg = 360.0 - azi_diff_deg
            azi_diff_rad = math.radians(azi_diff_deg)
            cos_scat_ang = (-ctv * cts) - (stv * sts * math.cos(azi_diff_rad))
            phase_rayl_min = 0.75 * (1.0 + cos_scat_ang * cos_scat_ang)
            for i in range(nbands):
                # b_source = product.getBandAt(i)
                b_source = product.getBand(band_name[i])
                lam = b_source.getSpectralWavelength()
                taurS[(i, x)] = math.exp(-4.637) * math.pow((lam / 1000.0), -4.0679)
                pressureAtms = press0[x] * math.exp(-alt[x] / 8000.0)
                pressureFactor = taurS[(i, x)] / 1013.0
                taurS[(i, x)] = pressureAtms * pressureFactor
                rRaySimple[(i, x)] = cts * taurS[(i, x)] * phase_rayl_min / (4 * 3.1415926) * (1 / ctv) * 3.1415926

        # Write bands to product
        airmassBand.writePixels(0, y, width, 1, airmass)
        azidiffBand.writePixels(0, y, width, 1, azidiff)
        altBand.writePixels(0, y, width, 1, alt)

        for i in range(nbands):
            taurBand = raycorProduct.getBand("taur_" + str(i + 1))
            taurBand.writePixels(0, y, width, 1, taur[i])
            rhorBand = raycorProduct.getBand("rRay_" + str(i + 1))
            rhorBand.writePixels(0, y, width, 1, rho_R[i])
            rhorF1Band = raycorProduct.getBand("rRayF1_" + str(i + 1))
            rhorF1Band.writePixels(0, y, width, 1, rho_Rm[0, i])
            rhorF2Band = raycorProduct.getBand("rRayF2_" + str(i + 1))
            rhorF2Band.writePixels(0, y, width, 1, rho_Rm[1, i])
            rhorF3Band = raycorProduct.getBand("rRayF3_" + str(i + 1))
            rhorF3Band.writePixels(0, y, width, 1, rho_Rm[2, i])
            rayTransSBand = raycorProduct.getBand("transSRay_" + str(i + 1))
            rayTransSBand.writePixels(0, y, width, 1, tR_thetaS[i])
            rayTransVBand = raycorProduct.getBand("transVRay_" + str(i + 1))
            rayTransVBand.writePixels(0, y, width, 1, tR_thetaV[i])
            sARayBand = raycorProduct.getBand("sARay_" + str(i + 1))
            sARayBand.writePixels(0, y, width, 1, sARay[i])
            rtoaRBand = raycorProduct.getBand("rtoaRay_" + str(i + 1))
            rtoaRBand.writePixels(0, y, width, 1, rho_toaR[i])
            rBRRBand = raycorProduct.getBand("rBRR_" + str(i + 1))
            rBRRBand.writePixels(0, y, width, 1, rho_BRR[i])
            spfBand = raycorProduct.getBand("sphericalAlbedoFactor_" + str(i + 1))
            spfBand.writePixels(0, y, width, 1, sphericalFactor[i])
            rRaySimpleBand = raycorProduct.getBand("RayleighSimple_" + str(i + 1))
            rRaySimpleBand.writePixels(0, y, width, 1, rRaySimple[i])
            rho_ngBand = raycorProduct.getBand("rtoa_ng_" + str(i + 1))
            rho_ngBand.writePixels(0, y, width, 1, rho_ng[i])
            taurSBand = raycorProduct.getBand("taurS_" + str(i + 1))
            taurSBand.writePixels(0, y, width, 1, taurS[i])
            # Rayleigh calculation completed

    raycorProduct.closeIO()

    print("Done.")
file = sys.argv[1]

print("Reading...")
product = ProductIO.readProduct(file)
width = product.getSceneRasterWidth()
height = product.getSceneRasterHeight()
name = product.getName()
description = product.getDescription()
band_names = product.getBandNames()

print("Product: %s, %d x %d pixels, %s" % (name, width, height, description))
print("Bands:   %s" % (list(band_names)))

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

HashMap = jpy.get_type('java.util.HashMap')
BandDescriptor = jpy.get_type('org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor')

targetBand1 = BandDescriptor()
targetBand1.name = 'band_1'
targetBand1.type = 'float32'
targetBand1.expression = '(radiance_10 - radiance_7) / (radiance_10 + radiance_7)'

targetBand2 = BandDescriptor()
targetBand2.name = 'band_2'
targetBand2.type = 'float32'
targetBand2.expression = '(radiance_9 - radiance_6) / (radiance_9 + radiance_6)'

targetBands = jpy.array('org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor', 2)
targetBands[0] = targetBand1
targetBands[1] = targetBand2
from snappy import jpy

ProductIOPlugInManager = jpy.get_type('org.esa.snap.core.dataio.ProductIOPlugInManager')
ProductReaderPlugIn = jpy.get_type('org.esa.snap.core.dataio.ProductReaderPlugIn')
ProductWriterPlugIn = jpy.get_type('org.esa.snap.core.dataio.ProductWriterPlugIn')

read_plugins = ProductIOPlugInManager.getInstance().getAllReaderPlugIns()
write_plugins = ProductIOPlugInManager.getInstance().getAllWriterPlugIns()

print('Writer formats:')
while write_plugins.hasNext():
    plugin = write_plugins.next()
    print('  ', plugin.getFormatNames()[0], plugin.getDefaultFileExtensions()[0])

print(' ')

print('Reader formats:')
while read_plugins.hasNext():
    plugin = read_plugins.next()
    print('  ', plugin.getFormatNames()[0], plugin.getDefaultFileExtensions()[0])
Example #34
0
file = sys.argv[1]

print("Reading...")
product = ProductIO.readProduct(file)
width = product.getSceneRasterWidth()
height = product.getSceneRasterHeight()
name = product.getName()
description = product.getDescription()
band_names = product.getBandNames()

print("Product: %s, %d x %d pixels, %s" % (name, width, height, description))
print("Bands:   %s" % (list(band_names)))

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

HashMap = jpy.get_type('java.util.HashMap')
BandDescriptor = jpy.get_type('org.esa.snap.gpf.operators.standard.BandMathsOp$BandDescriptor')

targetBand1 = BandDescriptor()
targetBand1.name = 'band_1'
targetBand1.type = 'float32'
targetBand1.expression = '(radiance_10 - radiance_7) / (radiance_10 + radiance_7)'

targetBand2 = BandDescriptor()
targetBand2.name = 'band_2'
targetBand2.type = 'float32'
targetBand2.expression = '(radiance_9 - radiance_6) / (radiance_9 + radiance_6)'

targetBands = jpy.array('org.esa.snap.gpf.operators.standard.BandMathsOp$BandDescriptor', 2)
targetBands[0] = targetBand1
targetBands[1] = targetBand2
Example #35
0
file = sys.argv[1]

print("Reading...")
product = ProductIO.readProduct(file)
width = product.getSceneRasterWidth()
height = product.getSceneRasterHeight()
name = product.getName()
description = product.getDescription()
band_names = product.getBandNames()

print("Product: %s, %d x %d pixels, %s" % (name, width, height, description))
print("Bands:   %s" % (list(band_names)))

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

BandDescriptor = jpy.get_type('org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor')

targetBand1 = BandDescriptor()
targetBand1.name = 'band_1'
targetBand1.type = 'float32'
targetBand1.expression = '(radiance_10 - radiance_7) / (radiance_10 + radiance_7)'

targetBand2 = BandDescriptor()
targetBand2.name = 'band_2'
targetBand2.type = 'float32'
targetBand2.expression = '(radiance_9 - radiance_6) / (radiance_9 + radiance_6)'

targetBands = jpy.array('org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor', 2)
targetBands[0] = targetBand1
targetBands[1] = targetBand2