예제 #1
0
def readMetadata(inputPath, name, toPrint=True):
    # Extract information about the Sentinel-1 GRD product:

    # s1prd = inputPath + "%s.SAFE/manifest.safe" % (name)
    s1prd = inputPath + "%s" % (name)
    reader = ProductIO.getProductReader("SENTINEL-1")
    product = reader.readProductNodes(s1prd, None)
    metadata = {}
    width = product.getSceneRasterWidth()
    height = product.getSceneRasterHeight()
    name = product.getName()
    band_names = list(product.getBandNames())

    # params = HashMap()
    # orbit = GPF.createProduct("Apply-Orbit-File", params, product)
    # orbit = orbit.getName()

    metadata["name"] = name
    metadata["band_names"] = band_names
    metadata["width"] = width
    metadata["heigth"] = height
    # metadata["orbit"] = orbit

    if toPrint:
        print("Product: %s, %d x %d pixels" % (name, width, height))
        print("Bands:   %s" % (band_names))
    return metadata
def getGeoTiffImage(sarDownloadFilePath,
                    geopandasDataFilePath,
                    geoDataIndex,
                    dstPath=None):
    '''
    Get GeoTiff image from a .SAFE folder extracted after download
    :type sarDownloadFilePath: string or list of string
    :type geopandasDataFilePath: string
    :type geoDataIndex: int
    :type dstPath: string

    :param sarDownloadFilePath: directory (or list of directory) of .SAFE folder(s)
    :param geopandasDataFilePath: directory of geopandas dataframe file
    :param geoDataIndex: geoDataIndex of data needed to retrieve in geopandas Dataframe
    :param dstPath: directory of destination file, must have '.tif' extension


    :return: None

    :example: sarHelpers.getGeoTiffImage(sarDownloadFilePath='S1A_IW_GRDH_1SDV_20170221T225238_20170221T225303_015388_019405_9C41.SAFE',
                                           geopandasDataFilePath='mekongReservoirs',
                                           geoDataIndex=0,
                                           dstPath='geotiff/1.tif')
    '''

    from snappy import jpy
    from snappy import ProductIO
    from snappy import GPF
    from snappy import HashMap

    s1meta = "manifest.safe"
    s1product = "%s/%s" % (sarDownloadFilePath, s1meta)
    reader = ProductIO.getProductReader("SENTINEL-1")
    product = reader.readProductNodes(s1product, None)
    parameters = HashMap()

    borderRectInGeoCoor = Utils.getGeoDataBorder(geopandasDataFilePath,
                                                 geoDataIndex)
    subset = Utils.subset(product, borderRectInGeoCoor)
    calibrate = Utils.calibrate(subset)
    terrain = Utils.terrainCorrection(calibrate)
    terrainDB = GPF.createProduct("LinearToFromdB", parameters, terrain)
    speckle = Utils.speckleFilter(terrainDB)

    if dstPath is None:
        dstPath = sarImgPath[:-4] + '.tif'

    ProductIO.writeProduct(speckle, dstPath, 'GeoTiff')

    product.dispose()
    subset.dispose()
    calibrate.dispose()
    terrain.dispose()
    speckle.dispose()
    del product, subset, calibrate, terrain, terrainDB, speckle
    return dstPath
예제 #3
0
def open_prod(inpath, s3_instrument, resolution):
    """Open SNAP product.

     Use snappy to open a Sentinel-3 product. If the instrument is SLSTR, then
     the 2 resolution products are returned (500m and 1km).

    Args:
        inpath (str): Path to a S3 OLCI image xfdumanisfest.xml file
        s3_instrument (str): S3 instrument (OLCI or SLSTR)
        resolution (str): For SLSTR, resolution of the product to be
            opened (0.5 or 1 k)

    Returns:
        (java.lang.Object): snappy java object: SNAP image product
     """
    # Open satellite product with SNAP
    try:
        if s3_instrument == "OLCI":
            prod = ProductIO.readProduct(inpath)

        elif s3_instrument == "SLSTR":
            # Reader based on input
            if resolution == "500":
                reader = ProductIO.getProductReader("Sen3_SLSTRL1B_500m")
            elif resolution == "1000":
                reader = ProductIO.getProductReader("Sen3_SLSTRL1B_1km")
            else:
                raise ValueError("Wrong SLSTR resolution, set to 500 or 1000m")

            prod = reader.readProductNodes(inpath, None)

        else:
            raise ValueError(
                "Only Sentinel-3 OLCI and SLSTR are currently" " supported."
            )

    except IOError:
        print("Error: SNAP cannot read specified file!")

    return prod
예제 #4
0
def readMetadata(sentinel_1_path, toPrint=True):
    # Extract information about the Sentinel-1 GRD product:
    sentinel_1_metadata = "manifest.safe"
    s1prd = "data/%s/%s.SAFE/%s" % (sentinel_1_path, sentinel_1_path,
                                    sentinel_1_metadata)
    reader = ProductIO.getProductReader("SENTINEL-1")
    product = reader.readProductNodes(s1prd, None)
    width = product.getSceneRasterWidth()
    height = product.getSceneRasterHeight()
    name = product.getName()
    band_names = product.getBandNames()

    if toPrint:
        print("Product: %s, %d x %d pixels" % (name, width, height))
        print("Bands:   %s" % (list(band_names)))
    return product
예제 #5
0
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')
예제 #6
0
def list_bands(product):

    reader = ProductIO.getProductReader('BEAM-DIMAP')
    product = reader.readProductNodes(product, None)

    return list(product.getBandNames())
예제 #7
0
def _process(s1_identifier):
    data_path = os.getcwd()
    #s1_identifier = 'S1A_IW_GRDH_1SDV_20160416T215858_20160416T215923_010852_0103DC_FAC8'
    s1meta = "manifest.safe"
    s1prd = os.path.join(data_path, s1_identifier, s1_identifier + '.SAFE',
                         s1meta)
    #print(s1prd)

    reader = ProductIO.getProductReader("SENTINEL-1")
    product = reader.readProductNodes(s1prd, None)

    print("Bands:\n", list(product.getBandNames()), "Length:",
          len(product.getBandNames()))

    # ### ThermalNoiseRemoval step

    # HashMap = jpy.get_type('java.util.HashMap')
    # GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
    #
    # parameters = HashMap()
    #
    # parameters.put('selectedPolarisations', polarisation)
    # parameters.put('removeThermalNoise', 'true')
    # parameters.put('reIntroduceThermalNoise', 'false')
    #
    # thermal_noise_removal = GPF.createProduct('ThermalNoiseRemoval', parameters, product)

    # ## Apply-Orbit-File

    # parameters = HashMap()
    #
    # parameters.put('orbitType', 'Sentinel Precise (Auto Download)')
    # parameters.put('polyDegree', '3')
    # parameters.put('continueOnFail', 'false')
    #
    # apply_orbit_file = GPF.createProduct('Apply-Orbit-File', parameters, thermal_noise_removal)

    # # Subset
    parameters = HashMap()

    parameters.put('sourceBands', 'Amplitude_{}'.format(polarisation))
    #parameters.put('region', '')
    parameters.put('geoRegion', wkt)
    parameters.put('subSamplingX', '1')
    parameters.put('subSamplingY', '1')
    parameters.put('fullSwath', 'false')
    parameters.put('tiePointGridNames', '')
    parameters.put('copyMetadata', 'true')

    subset = GPF.createProduct('Subset', parameters, product)
    print('Subset {} done'.format(wkt))
    ## Calibration
    parameters = HashMap()

    parameters.put('auxFile', 'Product Auxiliary File')
    parameters.put('outputImageInComplex', 'false')
    parameters.put('outputImageScaleInDb', 'false')
    parameters.put('createGammaBand', 'true')
    parameters.put('createBetaBand', 'false')
    parameters.put('selectedPolarisations', polarisation)
    parameters.put('outputSigmaBand', 'false')
    parameters.put('outputGammaBand', 'true')
    parameters.put('outputBetaBand', 'false')

    calibration = GPF.createProduct('Calibration', parameters, subset)
    print('Calibration done')

    ## Speckle-Filter

    #Variablen von oben hier benutzt.
    filterSizeX = '5'
    filterSizeY = '5'

    parameters = HashMap()

    parameters.put('sourceBands', 'Gamma0_{}'.format(polarisation))
    #parameters.put('filter', 'Lee')
    parameters.put('filter', 'Boxcar')
    parameters.put('filterSizeX', filterSizeX)
    parameters.put('filterSizeY', filterSizeY)
    parameters.put('dampingFactor', '2')
    parameters.put('estimateENL', 'true')
    parameters.put('enl', '1.0')
    parameters.put('numLooksStr', '1')
    parameters.put('targetWindowSizeStr', '3x3')
    parameters.put('sigmaStr', '0.9')
    parameters.put('anSize', '50')

    speckle_filter = GPF.createProduct('Speckle-Filter', parameters,
                                       calibration)
    print('Speckle-Filter done')

    ## Multilook
    azLooks = 3
    rgLooks = 3

    parameters = HashMap()
    parameters.put('sourceBands', 'Gamma0_{}'.format(polarisation))

    parameters.put('grSquarePixel', False)
    parameters.put('IndependentLooks', True)

    parameters.put('nRgLooks', rgLooks)
    parameters.put('nAzLooks', azLooks)
    parameters.put('outputIntensity', True)
    multilook = GPF.createProduct('Multilook', parameters, speckle_filter)
    print('Multilook done')

    ## Terrain-Correction
    parameters = HashMap()

    parameters.put('sourceBands', 'Gamma0_{}'.format(polarisation))
    parameters.put('demName', 'SRTM 3Sec')
    parameters.put('externalDEMFile', '')
    parameters.put('externalDEMNoDataValue', '0.0')
    parameters.put('externalDEMApplyEGM', 'true')
    parameters.put('demResamplingMethod', 'BILINEAR_INTERPOLATION')
    parameters.put('imgResamplingMethod', 'BILINEAR_INTERPOLATION')
    parameters.put('pixelSpacingInMeter', '30.0')
    parameters.put('pixelSpacingInDegree', '2.6949458523585647E-4')
    #parameters.put('pixelSpacingInDegree', '8.983152841195215E-5')
    parameters.put('mapProjection', 'AUTO:42001')
    parameters.put('nodataValueAtSea', 'true')
    parameters.put('saveDEM', 'false')
    parameters.put('saveLatLon', 'false')
    parameters.put('saveIncidenceAngleFromEllipsoid', 'false')
    parameters.put('saveProjectedLocalIncidenceAngle', 'false')
    parameters.put('saveSelectedSourceBand', 'true')
    parameters.put('outputComplex', 'false')
    parameters.put('applyRadiometricNormalization', 'false')
    parameters.put('saveSigmaNought', 'false')
    parameters.put('saveGammaNought', 'false')
    parameters.put('saveBetaNought', 'false')
    parameters.put('incidenceAngleForSigma0',
                   'Use projected local incidence angle from DEM')
    parameters.put('incidenceAngleForGamma0',
                   'Use projected local incidence angle from DEM')
    parameters.put('auxFile', 'Latest Auxiliary File')

    terrain_correction = GPF.createProduct('Terrain-Correction', parameters,
                                           multilook)
    print('Terrain-Correction done')

    ## Linear to dB
    parameters = HashMap()

    parameters.put('sourceBands', 'Gamma0_{}'.format(polarisation))
    parameters.put('outputImageScaleInDb', True)
    lintodB = GPF.createProduct('LinearToFromdB', parameters,
                                terrain_correction)
    print('Linear to dB done')
    final_product = lintodB

    #return final_product

    ## Save the result

    #ProductIO.writeProduct(terrain_correction, s1_identifier + '.tif', 'GeoTIFF')
    #ProductIO.writeProduct(terrain_correction, s1_identifier + '.tif', "GeoTIFF-BigTIFF")
    #ProductIO.writeProduct(terrain_correction, s1_identifier + '.dim', 'BEAM-DIMAP')
    #print('Product saved as {}.dim'.format(s1_identifier))

    # ## Plot the result

    def plotBand(s1_identifier, product, rband, vmin, vmax):

        band = product.getBand(rband)

        w = band.getRasterWidth()
        h = band.getRasterHeight()

        band_data = np.zeros(w * h, np.float32)
        band.readPixels(0, 0, w, h, band_data)

        band_data.shape = h, w

        width = 12
        height = 12
        plt.figure(figsize=(width, height))
        imgplot = plt.imshow(band_data,
                             cmap=plt.cm.binary,
                             vmin=vmin,
                             vmax=vmax)
        plt.axis('off')
        #plt.imshow(band_data, cmap=plt.cm.RdGy, vmin=vmin, vmax=vmax)

        plt.savefig(s1_identifier, bbox_inches='tight')
        #return imgplot

    #plotBand(terrain_correction, 'Gamma0_{}'.format(polarisation), 0, 0.3)
    plotBand(s1_identifier, terrain_correction,
             'Gamma0_{}'.format(polarisation), 0, 6)
    print('Plot saved as {}.png'.format(s1_identifier))
    return final_product  #totally wrong hier move out plotter
예제 #8
0
#list to store all products
products = []
pols = ["VH","VV"]
#name, sensing_mode, product_type, polarization, height, width, band_names = ([] for i in range(7))
#dates = []
for folder in os.listdir(path):
    gc.enable()
    print("folder",folder)
    output = path + folder
    timestamp = folder.split("_")[4]
    date = timestamp[:8]
    dates.append(date)
    s1prd = "%s/%s/%s.SAFE/%s" % (path, folder,folder, s1meta)
    print ("output:", output)
    print("s1prd:", s1prd)
    reader_p = ProductIO.getProductReader("SENTINEL-1")
    product_p = reader_p.readProductNodes(s1prd, None)
    products.append(product_p)
    print("Spath:",product_p)

    #Set target foledr and extract metadata
    ##THIS IS CRITICAL -DONOT FORGET WHOLE PATH, ENTIRE DIRECTORY (PATH) PLUS S1 DIRECT
    #Extract information about the Sentinel-1 GRD products:

for product in products:

    width = product.getSceneRasterWidth()
    height = product.getSceneRasterHeight()
    name = product.getName()
    band_names = product.getBandNames()
    print("Product: %s, %d x %d pixels" % (name, width, height))