예제 #1
0
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()
예제 #2
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')
예제 #3
0
def match_up(filename, file, stations):
    print('match_up...')
    #    file = ProductIO.readProduct(filepath+filename)
    geo = file.getSceneGeoCoding()
    size = file.getSceneRasterSize()
    if geo.canGetGeoPos():
        for station in stations:
            p1 = geo.getPixelPos(geopos(station['Lat'], station['Lon']), None)
            if p1.getX() == 'non':
                continue
            x = p1.getX() - Height / 2
            y = p1.getY() - Width / 2
            if file.containsPixel(p1):
                flist = filename.split('_')
                wdir, wspeed = wind_filed_from_ndbc(station['name'], flist[-5])
                if wdir == 0 and wspeed == 0:
                    continue
                else:
                    print(flist[-1].split('.')[0])
                    print(station)
                    print(x, y)
                    print(size.getHeight(), size.getWidth())
                    if size.getHeight() > 400 and size.getWidth > 400:
                        subfile = subset(file, int(x), int(y))
                        if subfile is False:
                            print('cut exception')
                            continue
                        print('write...')
                        print(filename, station['name'])
                        try:
                            ProductIO.writeProduct(
                                subfile,
                                '/users/yangchao/GitHub/wind/snap/match_data1/'
                                + station['name'] + '_subset_' + wdir + '_' +
                                wspeed + '_' + station['height'] + '_' +
                                filename.split('.')[0], 'BEAM-DIMAP')
                        except Exception:
                            print('exception')
                            subfile.dispose()
                            continue
                        subfile.dispose()
                        del subfile
                    else:
                        print('copy...')
                        try:
                            ProductIO.writeProduct(
                                file,
                                '/users/yangchao/GitHub/wind/snap/match_data1/'
                                + station['name'] + '_subset_' + wdir + '_' +
                                wspeed + '_' + station['height'] + '_' +
                                filename.split('.')[0], 'BEAM-DIMAP')
                        except Exception:
                            print('exception')
                            continue
    else:
        print(filename + " can't get geo pos")

    del size
    del geo
    file.dispose()
예제 #4
0
    def apply_orbit_file(self,
                         write_intermediate=False):

        parameters = self.HashMap()

        dataproduct = GPF.createProduct("Apply-Orbit-File", parameters, self.dataproduct_r)

        self.operations_list.append('ApplyOrbit')

        SF_filepath = ""

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

        if write_intermediate:
            # Write data product with BEAM-DIM format to given file path
            # ProductIO.writeProduct(Product product, String filePath, String formatName)
            SF_filepath = Path(self.working_dir, self.name_with_safe + "_ORB")
            print('Writing out Orbit File corrected product')

            ProductIO.writeProduct(dataproduct, str(SF_filepath), 'BEAM-DIMAP', monitor)
             # Set start time and loop counter
            finish_time = datetime.datetime.now()  # for calculation
            elapsed_time = finish_time - self.start_time
            print(elapsed_time.strftime("%H:%M:%S"))


        if os.path.exists(SF_filepath + ".dim"):
            print("Completed applying orbit file:", SF_filepath)
        else:
            print("Completed applying orbit file: data product saved to in-memory")

        self.intermediate_product = dataproduct
예제 #5
0
def main():
    ## All Sentinel-2 data subfolders are located within a super folder (make sure data is already unzipped and each sub folder name ends with .SAFE)
    path = r'D:\Sentinel\test'
    outpath = r'D:\Sentinel\out'
    if not os.path.exists(outpath):
        os.makedirs(outpath)
    
    for folder in os.listdir(path):
        gc.enable()
        gc.collect()
        print ("Filname: ", path + "\\" + folder + "\\manifest.safe")
        ##sentinel2 = path + "\\" + folder + "\\MTD_MSIL1C.xml"
        sentinel2 = ProductIO.readProduct(path + "\\" + folder + "\\MTD_MSIL1C.xml")

        loopstarttime = str(datetime.datetime.now())
        print ('Start time: ', loopstarttime)
        start_time = time.time()

        ## Extract mode, product type
        modestamp = folder.split("_")[1]
        productstamp = folder.split("_")[2]
        
        ## Start preprocessing 
        resample = do_resampling(sentinel2)
        biOp = do_biophysical_parameter(resample)
        ##ndvi = do_vegetation_indices(sentinel2)
        print ("outFilename: ", outpath + "\\" + folder.split(".SAFE")[0] + '_biOp' + '.dim')
        ProductIO.writeProduct(biOp, outpath + "\\" + folder.split(".SAFE")[0] + '_biOp' + '.dim', "BEAM-DIMAP")
def guardarArchivo():
    global flood_mask
    #Crear la imagen a partir de la mascara
    ProductIO.writeProduct(flood_mask, "C:/CTE_334/Actividad09/final_mask",
                           'GeoTIFF')
    os.path.exists("C:/CTE_334/Actividad09/final_mask.tif")
    print("IMAGEN CREADA EXITOSAMENTE A PARTIR DE MASCARA")
def guardarArchivo():
    global flood_mask
    #Crear la imagen a partir de la mascara
    ProductIO.writeProduct(
        flood_mask,
        "C:/Users/Usuario/Desktop/Actvidades_CTE_334/Examen_unidad_2/final_mask",
        'GeoTIFF')
    print("IMAGEN CREADA EXITOSAMENTE A PARTIR DE MASCARA")
예제 #8
0
def topo_removal(file):
    image = ProductIO.readProduct(file)
    p = HashMap()
    p.put('demName', 'SRTM 1Sec HGT')
    result = GPF.createProduct('TopoPhaseRemoval', p, image)
    # Write to temporary file
    outfile = file.split('.dim')[0] + '_noSRTM.dim'
    ProductIO.writeProduct(result, outfile, 'BEAM-DIMAP')
예제 #9
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
예제 #10
0
def classify(prepros_folder):
    for file in os.listdir(prepros_folder):

        ### Folder, date & timestamp
        classification = rootpath + "\\classification"
        scene_name = str(prepros_folder.split("\\")[X])[:32]  # [X] element of filepath, change this to match scene name. [:32] first caracters of element
        print("scene_name:", scene_name)
        output_folder = os.path.join(classification, scene_name)
        print("output_folder:", output_folder)
        polarization = str(file)[46:48] # polarization from filename

        if not os.path.exists(output_folder):
            os.mkdir(output_folder)

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

        sentinel_1 = ProductIO.readProduct(os.path.join(prepros_folder, file))

        res = [20, 50]  # parameters for CFAR
        for r in res:
            resolution = r

            gd_window = resolution * 12.0
            bg_window = resolution * 37.0

            # AdaptiveThresholding (Constant False Alarm Rate CFAR)
            parameters = HashMap()
            parameters.put('targetWindowSizeInMeter', resolution)
            parameters.put('guardWindowSizeInMeter', gd_window)
            parameters.put('backgroundWindowSizeInMeter', bg_window)
            parameters.put('pfa', 6.0)

            target_1 = GPF.createProduct("AdaptiveThresholding", parameters, sentinel_1)
            parameters = None

            # Subset (extracting classification band)
            parameters = HashMap()
            parameters.put('bandNames', 'Sigma0_' + polarization + '_ship_bit_msk')

            outfile = output_folder + "\\" + scene_name + "_" + polarization + "_cfar_" + str(resolution) + "m"
            target_2 = GPF.createProduct("Subset", parameters, target_1)
            ProductIO.writeProduct(target_2, outfile, 'GeoTIFF-BigTIFF', pm)

            # Classification to vector
            ds = gdal.Open(str(outfile + ".tif"))

            rasterband = ds.GetRasterBand(1)

            dst_layername = outfile + "_Iceberg_outline"
            drv = ogr.GetDriverByName("GeoJSON")
            dst_ds = drv.CreateDataSource(dst_layername + ".geojson")
            dest_srs = ogr.osr.SpatialReference()
            dest_srs.ImportFromEPSG(4326)
            dst_layer = dst_ds.CreateLayer(dst_layername, dest_srs)

            gdal.Polygonize(rasterband, rasterband, dst_layer, -1, [], callback=None) # (input, mask, dest_layer,,,) 
예제 #11
0
 def write(self, file_path):
     """
     Write the processed product to a file with a file type and close the product.
     """
     print("\tWrite, file_path={}".format(file_path))
     ProductIO.writeProduct(self.product, file_path, 'GeoTIFF')
     self.product.closeIO()
     self.product = None
     return self.product
예제 #12
0
    def speckle_filter(self,
                       filter="Gamma Map",
                       estimateENL=True,
                       filterSize=11,
                       numLooksStr="1",
                       targetWindowStr="3x3",
                       windowSize="11x11",
                       write_intermediate=False):
        print("-------------")

        # Apply SPECKLE FILTER
        print("Applying speckle-filter:", self.name)

        # Define object parameterisation for filtering
        parameters = self.HashMap()
        parameters.put('filter', filter)
        parameters.put('estimateENL', estimateENL)
        parameters.put('filterSizeX', filterSize)
        parameters.put('filterSizeY', filterSize)
        parameters.put('numLooksStr', numLooksStr)
        parameters.put('sourceBands', ",".join(self.srcbands))
        parameters.put('targetWindowStr', targetWindowStr)
        parameters.put('windowSize', windowSize)

        print("Speckle filtering parameters:", parameters)

        # Create speckle filter data product using 'Speckle-Filter' operator/parameters and raw data product as source product
        # Speckle noise reduction can be applied either by spatial filtering or multilook processing
        # Filtered product contains 4 real bands: 2 amplitude and intensity bands for each polarizations
        # createProduct(String operatorName, Map<String,Object> parameters, Map<String,Product> sourceProducts)
        dataproduct = GPF.createProduct("Speckle-Filter", parameters, self.dataproduct_r)

        self.operations_list.append('SpeckleFilter')

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

        if write_intermediate:
            # Write data product with BEAM-DIM format to given file path
            # ProductIO.writeProduct(Product product, String filePath, String formatName)
            SF_filepath = Path(self.working_dir, self.name_with_safe + "_SF")
            print('Writing out SpeckleFilter Product')


            ProductIO.writeProduct(dataproduct, str(SF_filepath), 'BEAM-DIMAP', monitor)
            finish_time = datetime.datetime.now()  # for calculation
            elapsed_time = finish_time - self.start_time
            print(elapsed_time.strftime("%H:%M:%S"))


        if os.path.exists(SF_filepath + ".dim"):
            print("Completed speckle-filtering:", SF_filepath)
        else:
            print("Completed speckle-filtering: data product saved to in-memory")

        self.intermediate_product = dataproduct
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
예제 #14
0
def collocate_all(input_imgs, write_product):
    if len(input_imgs) < 2:
        raise ValueError('Error, len(input_imgs)={}'.format(len(input_imgs)))
    col = collocate(input_imgs[0],input_imgs[1])
    for i in range(2,len(input_imgs)):
        col = collocate(col,input_imgs[i])
    if write_product:
        collocation = "collocation_all.tif"
        ProductIO.writeProduct(col, collocation, 'GeoTiff')
    return col
예제 #15
0
def snaphu_unwrapping(product, target_Product_File, outFolder, filename):
    parameters = HashMap()
    parameters.put('targetProductFile',
                   target_Product_File)  # from SNAPHU_export
    parameters.put('outputFolder', outFolder)
    parameters.put('copyOutputAndDelete', 'Snaphu-unwrapping-after.vm')
    parameters.put('copyFilesTemplate', 'Snaphu-unwrapping-before.vm')
    product = GPF.createProduct('snaphu-unwrapping', parameters, product)
    ProductIO.writeProduct(product, filename + '.dim', 'BEAM-DIMAP')
    print('Phase unwrapping performed successfully …')
예제 #16
0
def do_ifg(path):
    """
    Takes filepath of TDX xml file
    Uses snappy to create interferogram 
    Add in parameters to p if required (see gpt Interferogram -h for details)
    """
    target = ifg_file(path)
    image = ProductIO.readProduct(path)
    p = HashMap()
    image = GPF.createProduct('Interferogram',p,image)
    ProductIO.writeProduct(image,target,'BEAM-DIMAP')
예제 #17
0
def createP(function, inputProduct, writeout=False, **kwargs):
    # pythonic version of GPF.createProduct()
    # function is string of SNAP operator
    # inputProduct is SNAP product object
    # kwargs contains any parameters to pass to the operator
    p = HashMap()
    for arg in kwargs:
        p.put(arg, kwargs.get(arg))
    result = GPF.createProduct(function, p, inputProduct)
    if writeout != False:
        ProductIO.writeProduct(result, writeout, 'BEAM-DIMAP')
    else:
        return result
예제 #18
0
    def write_out_result(self, format='BEAM-DIMAP'):

        if format == 'BEAM-DIMAP':
            # Write data product with BEAM-DIM format to given file path
            # ProductIO.writeProduct(Product product, String filePath, String formatName)
            SF_filepath = Path(self.working_dir, self.name_with_safe + "_FINAL")
            print('WRITING OUT PRODUCT')

            ProductIO.writeProduct(self.intermediate_product, str(SF_filepath), 'BEAM-DIMAP')

            finish_time = datetime.datetime.now()# for calculation
            elapsed_time = finish_time - self.start_time
            print(elapsed_time.strftime("%H:%M:%S"))


        elif format == 'GEOTIFF':

            print('WRITING OUT TO GEOTIFF!!')

            sigma0_ortho_bands = ["Sigma0_VH_use_local_inci_angle_from_dem",
                                 "Sigma0_VV_use_local_inci_angle_from_dem"]

            print(self.intermediate_product)
            print(self.intermediate_product.getBandNames())
            for band in self.intermediate_product.getBandNames():
                print(band)

            self.convertComputedBandToBand(self.intermediate_product.getBand(sigma0_ortho_bands[0]))
            self.convertComputedBandToBand(self.intermediate_product.getBand(sigma0_ortho_bands[1]))

            sub_parameters = self.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, self.intermediate_product)

            print(self.name_with_safe)

            final_output_name = Path(self.working_dir, self.name_with_safe + "_".join(self.operations_list))
            print('writing out final result')

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

            print('WRITING OUT PRODUCT')

            ProductIO.writeProduct(self.intermediate_product, str(final_output_name), 'GeoTIFF')

            finish_time = datetime.datetime.now() # for calculation
            elapsed_time = finish_time - self.start_time
            print(elapsed_time.strftime("%H:%M:%S"))
예제 #19
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.")
예제 #20
0
    def range_doppler_to_sigma0(self, resolution=10.0, suffix="", window_size=11, write_intermediate=False):
           # APPLY Range Doppler terrain correction and ortho

        print('Applying applying R.Doppler terrain corr. and ortho...')

        rd_parameters = self.HashMap()
        rd_parameters.put('demName', "SRTM 1Sec HGT") # Should eventually look at using a local SRTM 1Sec DEM (instead of auto downloading)
        rd_parameters.put('imgResamplingMethod', "BILINEAR_INTERPOLATION")
        rd_parameters.put('pixelSpacingInMeter', resolution)
        rd_parameters.put('sourceBands', ",".join(self.srcbands))

        # rd_parameters.put('mapProjection', projection) # Defined above, should be auto set to best UTM zone

        # APPLY RADIOMETRIC NORMALIZATION to SIGMA0 (not clear if necessary) only do if sigma0 NOT done above
        rd_parameters.put('applyRadiometricNormalization', True)
        rd_parameters.put('incidenceAngleForSigma0', "Use local incidence angle from DEM")
        rd_parameters.put('saveSigmaNought', True)
        rd_parameters.put('saveLocalIncidenceAngle', True)

        rd_corrected = GPF.createProduct("Terrain-Correction", rd_parameters, self.intermediate_product)

        self.operations_list.append("RangeDopplerOrthoConversionToSigma0")

        print(self.name_with_safe)
        final_output_name = self.name_with_safe + "_{}_{}x{}_{}m".format(suffix, window_size, window_size, resolution)
        print('writing out final result')

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

        SF_filepath = ""

        if write_intermediate:
            # Write data product with BEAM-DIM format to given file path
            # ProductIO.writeProduct(Product product, String filePath, String formatName)
            SF_filepath = Path(self.working_dir, self.name_with_safe + "_ORTHO")
            print('Writing out Terrain-Correction Product')
            ProductIO.writeProduct(rd_corrected, str(SF_filepath), 'BEAM-DIMAP', monitor)
            finish_time = datetime.datetime.now()  # for calculation
            elapsed_time = finish_time - self.start_time
            print(elapsed_time.strftime("%H:%M:%S"))


        if os.path.exists(SF_filepath + ".dim"):
            print("Completed speckle-filtering:", SF_filepath)
        else:
            print("Completed speckle-filtering: data product saved to in-memory")

        self.intermediate_product = rd_corrected
예제 #21
0
    def nbr(self):
        if hasattr(self, 'path_post'):
            name = 'NBR'
            exprss = '(B8 - B12) / (B8 + B12)'
            product_pre = self._band_math(self.product_pre, name, exprss)
            product_post = self._band_math(self.product_post, name, exprss)
            print(list(product_pre.getBandNames()))
            print(list(product_post.getBandNames()))

            name = 'difNBR'
            exprss = '$p1.NBR - $p2.NBR'.format(product_pre, product_post)
            products = [product_pre, product_post]
            product = self._band_math(products, name, exprss)
            out = "/".join([self.path, 'NBR.tif'])
            ProductIO.writeProduct(product, out, "GeoTIFF-BigTIFF")
예제 #22
0
def read_ref_raster(region, data_server):
    """
    Checks if reference raster for region exists, if not, creates it using B1 of a Sentinel-2 L1C product.
    """

    # Requires snappy, currently imported in sentinel1_pre/sentinel2_pre
    loc_raster = os.path.join(os.environ[data_server], 'spatial_ref',
                              region + '.dim')
    if os.path.isfile(loc_raster):
        return loc_raster
    else:
        # Import required packages
        import snappy
        from snappy import ProductIO, HashMap, GPF, jpy
        GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
        HashMap = snappy.jpy.get_type('java.util.HashMap')
        WKTReader = snappy.jpy.get_type('com.vividsolutions.jts.io.WKTReader')
        #Construct ref_raster

        # Get any s2 product available ## CHANGED TO L2A for Europe, change back to L1C for others
        prdlist = filter(
            re.compile(r'^S2.*L2A.*SAFE$').search,
            os.listdir(set_data_dir(region, 'S2', data_server)))
        first_product = prdlist[0]

        # Read reference product
        ref_product = ProductIO.readProduct(
            os.path.join(set_data_dir(region, 'S2', data_server),
                         first_product))

        # Resample all bands to 10m resolution
        resample_subset = HashMap()
        resample_subset.put('targetResolution', 10)
        resampled = GPF.createProduct('Resample', resample_subset, ref_product)

        # Subset to area of interest
        param_subset = HashMap()
        param_subset.put('geoRegion', read_aoi(region, data_server))
        param_subset.put('outputImageScaleInDb', False)
        param_subset.put('bandNames', 'B1')
        subset = GPF.createProduct("Subset", param_subset, resampled)

        # Write file
        ProductIO.writeProduct(subset, loc_raster, 'BEAM-DIMAP')

        return loc_raster
예제 #23
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
예제 #24
0
def resize(sarImgDir, imgDst=None, maxSize=400):
    '''
    Get water body of reservoir from a tif image
    :type sarImgDir: string
    :type imgDst: string
    :type maxSize: int

    :param sarImgDir: directory to sentinel-1 .tif image
    :param imgDst: directory to resized image. If none, use default name
    :param maxSize: maximum value of width and height after resizing
    
    :return: string - directory to resized image
    '''

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

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

    p = ProductIO.readProduct(sarImgDir)
    firstBand = p.getBands()[0]
    width = firstBand.getRasterWidth()
    height = firstBand.getRasterHeight()
    ratio = width / height

    parameters = HashMap()
    if ratio <= 1:
        parameters.put('targetHeight', maxSize)
        parameters.put('targetWidth', math.ceil(maxSize * ratio))
    else:
        parameters.put('targetWidth', maxSize)
        parameters.put('targetHeight', math.ceil(maxSize / ratio))
    product = GPF.createProduct('Resample', parameters, p)

    if imgDst is None:
        sourceName = imgDir.split('/')[:-4]
        imgDst = sourceName + '_resized.tif'
    ProductIO.writeProduct(product, imgDst, 'GeoTiff')
    del p, product
    print('Done.')
    return imgDst
예제 #25
0
def thermal_noise_removal_gpt(product):
    logger = logging.getLogger('root')
    fname = product.getName()
    logger.info("writing product for thermal noise removal")
    ProductIO.writeProduct(product, sarIn + "/" + fname + '.dim', "BEAM-DIMAP")
    logger.info("finished writing. proceeding with gpt ThermalNoiseRemoval")
    product.dispose()

    subprocess.call([
        '/users/stud09/martinsd/local/snap/bin/gpt', 'ThermalNoiseRemoval',
        '-SsourceProduct=' + sarIn + '/' + fname + '.dim',
        '-PselectedPolarisations=VV', '-PremoveThermalNoise=true', '-t',
        sarIn + '/' + fname + '.dim'
    ])

    result = ProductIO.readProduct(sarIn + "/" + fname + '.dim')
    logger.info("finished thermal noise removal")
    return (result)
예제 #26
0
def subset_s1(path, file, aoi):
    # Read Data________________________________________________________________
    print("SUBSET: Read Product...")
    sentinel = ProductIO.readProduct(path+file)
    print("SUBSET: Done reading!")
    name = sentinel.getName()
    print(name)
    # Get Polarisation and Name
    pols = ['VV'] # WISHLIST only VV works right now, should be changed to HH and VH
    for p in pols:
        print("SUBSET: calibration:   %s" % p)
        polarization = p
        # Preprocessing________________________________________________________
        # Calibration
        parameters = HashMap() 
        parameters.put('outputSigmaBand', True) 
        parameters.put('sourceBands', 'Intensity_' + polarization) 
        parameters.put('selectedPolarisations', polarization) 
        parameters.put('outputImageScaleInDb', False)      
        calib = path + file + "_calibrate_" + polarization 
        target_0 = GPF.createProduct("Calibration", parameters, sentinel) 
        ProductIO.writeProduct(target_0, calib, 'BEAM-DIMAP')
        calibration = ProductIO.readProduct(calib + ".dim")
        # Geojson to wkt
        with open(aoi) as f:
                gjson = json.load(f)       
        for feature in gjson['features']:
            polygon = (feature['geometry'])
        str_poly = json.dumps(polygon)
        gjson_poly = geojson.loads(str_poly)
        poly_shp = shape(gjson_poly)
        wkt = poly_shp.wkt
        # Subset
        geom = WKTReader().read(wkt)
        subsettings = HashMap()
        subsettings.put('geoRegion', geom)
        subsettings.put('outputImageScaleInDb', False)
        # Write Data_______________________________________________________            
        print("SUBSET: Writing subset.")  
        subset = path + file + "_subset_" + polarization
        target_1 = GPF.createProduct("Subset", subsettings, calibration)
        ProductIO.writeProduct(target_1, subset, 'BEAM-DIMAP')
        print("SUBSET: Done and saved in %s" % path)
예제 #27
0
def pre_process(files, dest, nameFunc=defaultNaming):

    # List to keep track of processed image paths
    output = []

    # Iterates through list of files
    print('Starting...')
    for i in files:
        print('Reading product...')

        # Try reading the file path by itself
        try:
            product = ProductIO.readProduct(i)
        except:
            # Try reading a product.xml within the file
            try:
                print('Checking product.xml...')
                product = ProductIO.readProduct(os.path.join(i, 'product.xml'))
            except:
                print('Snap could not read product: %s' % i)
                continue

        print('Applying corrections...')

        # Apply preprocessing
        calibratedProduct = do_calibration(product)
        spkFilterProduct = do_speckle_filter(calibratedProduct)
        terCorrectedProduct = do_terrain_correction(spkFilterProduct)

        print('Creating product...')

        # Saving new product
        name = os.path.join(dest, nameFunc(i) + '.tif')
        ProductIO.writeProduct(terCorrectedProduct, name, 'GeoTiff')

        # Properly dispose in memory and close
        product.dispose()
        product.closeIO()

        output.append(name)

    return output
예제 #28
0
def BandMath(stack):
    product = ProductIO.readProduct(stack)
    band_names = product.getBandNames()
    print(list(band_names))
    band1 = input("Enter Band 1:")
    band2 = input("Enter Band 2 which will be subtracted:")
    GPF.getDefaultInstance().getOperatorSpiRegistry().loadOperatorSpis()
    ## Band Math
    targetBands = jpy.array(
        'org.esa.snap.core.gpf.common.BandMathsOp$BandDescriptor', 2)
    #print('{}_minus_{}'.format(bandlist[x], bandlist[y]))
    targetBand1 = BandDescriptor()
    targetBand1.name = '{}_minus_{}'.format(band1, band2)
    targetBand1.type = 'float32'
    #targetBand1.expression = '(({} - {})<(-2))? 255 : 0'.format(band1, band2)
    targetBand1.expression = '(({} - {})<(-2))? 255 : 0'.format(band1, band2)
    parameters = HashMap()
    parameters.put('targetBands', targetBands)
    result = GPF.createProduct('BandMaths', parameters, product)
    print("Writing...")
    ProductIO.writeProduct(result, '{}_minus_{}.dim'.format(band1, band2),
                           'BEAM-DIMAP')
    print('{}_minus_{}.dim Done.'.format(band1, band2))
예제 #29
0
 def write_product(self, src_data, trg_file_path, trg_file_format='BEAM-DIMAP'):
     # Parameter: - src_data <SAR Object>  : An object ('Product') of Sentinel SAR dataset
     #            - trg_file_path <string> : A string containing the path to the output Sentinel SAR dataset and it's name file.
     #            - trg_file_format <supported-format>: BEAM-DIMAP, GeoTIFF, NetCDF, ...
     # ------------------------------------------------------------------------------------------------------------------------------
     # ATTENTION, in case of:
     #                         RuntimeError: java.lang.OutOfMemoryError: Java heap space
     # ------------------------------------------------------------------------------------------------------------------------------
     # SOLUTION 1:
     # (http://forum.step.esa.int/t/snappy-error-productio-writeproduct/1102)
     #
     # 1. CHANGE <snappy>/jpyconfig.py:
     #                         jvm_maxmem = None    ---->      jvm_maxmem = '6G'
     #                                                         Increase RAM
     #
     # 2. CHANGE <snappy>/snappy.ini:
     #                         # java_max_mem: 4G   ---->      java_max_mem: 6G
     #                                                         Remove '#' and increase RAM
     # ------------------------------------------------------------------------------------------------------------------------------
     # SOLUTION 2:
     # If you swapped Latitude/Longitude in POLYGON(...) there is also a out-of-memory-error
     #
     ProductIO.writeProduct(src_data, trg_file_path, trg_file_format)
예제 #30
0
def process(ziplist):
    product_set = []

    for f in ziplist:
        f = os.path.splitext(f)[
            0]  #pretty dirty works only with zip, because filenam is cut.
        f = _process(f)

        product_set.append(f)

    print("Creating Stack with", len(product_set), "bands")
    if len(product_set) <= 1:
        ##RuntimeError: org.esa.snap.core.gpf.OperatorException: Please select at least two source products
        parameters = HashMap()
        parameters.put('resamplingType', None)
        parameters.put('initialOffsetMethod', 'Orbit')
        parameters.put('extent', 'Master')
        create_stack = GPF.createProduct('CreateStack', parameters,
                                         product_set[0])

        #write the stack
        ProductIO.writeProduct(create_stack, 'create_stack.dim', 'BEAM-DIMAP')
        print('Single Product created')
    else:
        parameters = HashMap()
        parameters.put('resamplingType', None)
        parameters.put('initialOffsetMethod', 'Product Geolocation')
        parameters.put('extent', 'Master')
        ## Linear to dB
        create_stack = GPF.createProduct('CreateStack', parameters,
                                         product_set)

        #write the stack
        ProductIO.writeProduct(create_stack, 'create_stack.dim', 'BEAM-DIMAP')
        #ProductIO.writeProduct(terrain_correction, s1_identifier + '.tif', "GeoTIFF-BigTIFF")
        #ProductIO.writeProduct(terrain_correction, s1_identifier + '.dim', 'BEAM-DIMAP')
        print('Stack created')
예제 #31
0
    print 'Product file and band index required'
    sys.exit(1)

# 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()
예제 #32
0
import snappy
from snappy import ProductIO

SubsetOp = snappy.jpy.get_type('org.esa.snap.gpf.operators.standard.SubsetOp')
WKTReader = snappy.jpy.get_type('com.vividsolutions.jts.io.WKTReader')

if len(sys.argv) != 3:
    print("usage: %s <file> <geometry-wkt>" % sys.argv[0])
    print("       %s ./TEST.N1 \"POLYGON((15.786082 45.30223, 11.798364 46.118263, 10.878688 43.61961, 14.722727"
          "42.85818, 15.786082 45.30223))\"" % sys.argv[0])
    sys.exit(1)

file = sys.argv[1]
wkt = sys.argv[2]

geom = WKTReader().read(wkt)

print("Reading...")
product = ProductIO.readProduct(file)

op = SubsetOp()
op.setSourceProduct(product)
op.setGeoRegion(geom)

sub_product = op.getTargetProduct()

print("Writing...")
ProductIO.writeProduct(sub_product, "snappy_subset_output.dim", "BEAM-DIMAP")

print("Done.")
예제 #33
-1
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

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

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

print("Writing...")

ProductIO.writeProduct(result, 'snappy_bmaths_output.dim', 'BEAM-DIMAP')

print("Done.")


"""
   Please note: the next major version of snappy/jpy will be more pythonic in the sense that implicit data type
   conversions are performed. The 'parameters' from above variable could then be given as a Python dict object:

    parameters = {
        'targetBands': [
            {
                'name': 'band_1',
                'type': 'float32',
                'expression': '(radiance_10 - radiance_7) / (radiance_10 + radiance_7)'
            },