예제 #1
0
def mt_speckle_filter(in_stack, out_stack, logfile):
    '''
    '''

    # get gpt file
    gpt_file = h.gpt_path()

    # get path to graph
    rootpath = imp.find_module('ost')[1]
    graph = opj(rootpath, 'graphs', 'S1_TS', '2_MT_Speckle.xml')

    logger.debug("INFO: Applying the multi-temporal speckle-filtering")
    command = '{} {} -x -q {} -Pinput={} \
                   -Poutput={}'.format(gpt_file, graph, 2 * os.cpu_count(),
                                       in_stack, out_stack)

    return_code = h.run_command(command, logfile)

    if return_code == 0:
        logger.debug(
            'INFO: Succesfully applied multi-temporal speckle filtering')
    else:
        logger.debug(
            'ERROR: Multi-temporal speckle filtering exited with an error. \
                See {} for Snap Error output'.format(logfile))
        sys.exit(202)

    return return_code
예제 #2
0
def _grd_to_db(infile, outfile, logfile):
    '''A wrapper around SNAP's linear to db routine

    This function takes an OST calibrated Sentinel-1 product
    and converts it to dB.

    Args:
        infile: string or os.path object for
                an OST imported frame in BEAM-Dimap format (i.e. *.dim)
        outfile: string or os.path object for the output
                 file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to
    '''

    # get path to SNAP's command line executable gpt
    gpt_file = h.gpt_path()

    logger.debug('INFO: Converting the image to dB-scale.')
    # construct command string
    command = '{} LinearToFromdB -x -q {} -t \'{}\' {}'.format(
        gpt_file, 2 * os.cpu_count(), outfile, infile)

    # run command and get return code
    return_code = h.run_command(command, logfile)

    # handle errors and logs
    if return_code == 0:
        logger.debug('INFO: Succesfully converted product to dB-scale.')
    else:
        logger.debug('ERROR: Linear to dB conversion exited with an error. \
                See {} for Snap Error output'.format(logfile))
        sys.exit(113)

    return return_code
예제 #3
0
def _coherence(infile, outfile, logfile):
    '''A wrapper around SNAP's coherence routine

    This function takes a co-registered stack of 2 Sentinel-1 SLC products
    and calculates the coherence.

    Args:
        infile: string or os.path object for
                an OST imported frame in BEAM-Dimap format (i.e. *.dim)
        outfile: string or os.path object for the output
                 file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to

    '''
    # get gpt file
    gpt_file = h.gpt_path()

    graph = opj(OST_ROOT, 'graphs', 'S1_SLC2ARD', 'S1_SLC_Coh_Deb.xml')

    logger.debug('INFO: Coherence estimation')
    command = '{} {} -x -q {} -Pinput={} -Poutput={}' \
        .format(gpt_file, graph, 2, infile, outfile)

    return_code = h.run_command(command, logfile)

    if return_code == 0:
        logger.debug('INFO: Succesfully created coherence product.')
    else:
        logger.debug('ERROR: Coherence exited with an error. \
                See {} for Snap Error output'.format(logfile))
        # sys.exit(121)

    return return_code
예제 #4
0
def _grd_frame_import_subset(infile,
                             outfile,
                             georegion,
                             logfile,
                             polarisation='VV,VH,HH,HV'):
    '''A wrapper of SNAP import of a subset of single Sentinel-1 GRD product

    This function takes an original Sentinel-1 scene (either zip or
    SAFE format), updates the orbit information (does not fail if not
    available), removes the thermal noise, subsets it to the given georegion
    and stores it as a SNAP
    compatible BEAM-Dimap format.


    Args:
        infile: string or os.path object for
                an original Sentinel-1 GRD product in zip or SAFE format
        outfile: string or os.path object for the output
                 file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to
        polarisation (str): a string consisiting of the polarisation (comma separated)
                     e.g. 'VV,VH',
                     default value: 'VV,VH,HH,HV'
        georegion (str): a WKT style formatted POLYGON that bounds the
                         subset region
    '''

    logger.debug('INFO: Importing {} by applying precise orbit file and '
                 'removing thermal noise, as well as subsetting.'.format(
                     os.path.basename(infile)))

    # get path to SNAP's command line executable gpt
    gpt_file = h.gpt_path()

    # get path to ost package
    graph = opj(OST_ROOT, 'graphs', 'S1_GRD2ARD', '1_AO_TNR_SUB.xml')

    # construct command
    command = '{} {} -x -q {} -Pinput="{}" -Pregion="{}" -Ppolarisation={} \
                      -Poutput="{}"'.format(gpt_file, graph,
                                            2 * os.cpu_count(), infile,
                                            georegion, polarisation, outfile)

    # run command and get return code
    return_code = h.run_command(command, logfile)

    # handle errors and logs
    if return_code == 0:
        logger.debug('INFO: Succesfully imported product')
    else:
        raise RuntimeError('ERROR: Frame import exited with an error. \
                See {} for Snap Error output'.format(logfile))
    return return_code
예제 #5
0
def _terrain_correction(infile,
                        outfile,
                        logfile,
                        resolution,
                        dem='SRTM 1sec HGT'):
    '''A wrapper around SNAP's Terrain Correction routine

    This function takes an OST calibrated Sentinel-1 product and
    does the geocodification.

    Args:
        infile: string or os.path object for
                an OST imported frame in BEAM-Dimap format (i.e. *.dim)
        outfile: string or os.path object for the output
                 file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to
        resolution (int): the resolution of the output product in meters
        dem (str): A Snap compliant string for the dem to use.
                   Possible choices are:
                       'SRTM 1sec HGT'(default)
                       'SRTM 3sec'
                       'ASTER 1sec GDEM'
                       'ACE30'

    '''

    # get gpt file
    gpt_file = h.gpt_path()

    logger.debug("INFO: Geocoding input scene")

    command = '{} Terrain-Correction -x -q {} \
              -PdemResamplingMethod=\'BILINEAR_INTERPOLATION\'\
              -PimgResamplingMethod=\'BILINEAR_INTERPOLATION\'\
              -PnodataValueAtSea=\'false\'\
              -PpixelSpacingInMeter=\'{}\'\
              -PdemName=\'{}\'\
              -t {} {}'\
              .format(gpt_file, 2, resolution, dem,
                      outfile, infile)

    return_code = h.run_command(command, logfile)

    if return_code == 0:
        logger.debug('INFO: Succesfully orthorectified product.')
    else:
        logger.debug('ERROR: Geocoding exited with an error. \
                See {} for Snap Error output'.format(logfile))
        # sys.exit(122)

    return return_code
예제 #6
0
def _coreg2(master,
            slave,
            outfile,
            logfile,
            dem='SRTM 1sec HGT',
            master_burst_poly=''):
    '''A wrapper around SNAP's back-geocoding co-registration routine

    This function takes a list of 2 OST imported Sentinel-1 SLC products
    and co-registers them properly. This routine is sufficient for coherence
    estimation, but not for InSAR, since the ESD refinement is not applied.

    Args:
        infile: string or os.path object for
                an OST imported frame in BEAM-Dimap format (i.e. *.dim)
        outfile: string or os.path object for the output
                 file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to
        dem (str): A Snap compliant string for the dem to use.
                   Possible choices are:
                       'SRTM 1sec HGT'(default)
                       'SRTM 3sec'
                       'ASTER 1sec GDEM'
                       'ACE30'

    '''
    # get gpt file
    gpt_file = h.gpt_path()

    graph = opj(OST_ROOT, 'graphs', 'S1_SLC2ARD', 'S1_SLC_Coreg.xml')

    logger.debug('INFO: Co-registering {} and {}'.format(master, slave))
    command = '{} {} -x -q {} -Pmaster={} -Pslave={} -Poutput={} ' \
              '-Pdem=\'{}\' -Pregion="{}"' \
        .format(gpt_file, graph, 2, master, slave,
                outfile, dem, master_burst_poly
                )

    return_code = h.run_command(command, logfile)

    if return_code == 0:
        logger.debug('INFO: Succesfully coregistered product.')
    else:
        logger.debug('ERROR: Co-registration exited with an error. \
                See {} for Snap Error output'.format(logfile))
        # sys.exit(112)

    return return_code
예제 #7
0
def _import(infile, out_prefix, logfile, swath, burst, polar='VV,VH,HH,HV'):
    '''A wrapper of SNAP import of a single Sentinel-1 SLC burst

    This function takes an original Sentinel-1 scene (either zip or
    SAFE format), updates the orbit information (does not fail if not
    available), and extracts a single burst based on the
    given input parameters.

    Args:
        infile: string or os.path object for
                an original Sentinel-1 GRD product in zip or SAFE format
        out_prefix: string or os.path object for the output
                    file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to
        swath (str): the corresponding IW subswath of the burst
        burst (str): the burst number as in the Sentinel-1 annotation file
        polar (str): a string consisiting of the polarisation (comma separated)
                     e.g. 'VV,VH',
                     default value: 'VV,VH,HH,HV'

    '''

    # get gpt file
    gpt_file = h.gpt_path()

    graph = opj(OST_ROOT, 'graphs', 'S1_SLC2ARD', 'S1_SLC_BurstSplit_AO.xml')

    logger.debug('INFO: Importing Burst {} from Swath {} '
                 'from scene {}'.format(burst, swath,
                                        os.path.basename(infile)))
    command = '{} {} -x -q {} -Pinput={} -Ppolar={} -Pswath={}\
                      -Pburst={} -Poutput={}'\
        .format(gpt_file, graph, 2, infile, polar, swath,
                burst, out_prefix)
    return_code = h.run_command(command, logfile)

    if return_code == 0:
        logger.debug('INFO: Succesfully imported product')
    else:
        logger.debug('ERROR: Frame import exited with an error. \
                See {} for Snap Error output'.format(logfile))
        # sys.exit(119)

    return return_code
예제 #8
0
def _ls_mask(infile, outfile, logfile, resolution, dem='SRTM 1sec HGT'):
    '''A wrapper around SNAP's Layover/Shadow mask routine

    This function takes OST imported Sentinel-1 product and calculates
    the Layover/Shadow mask.

    Args:
        infile: string or os.path object for
                an OST imported frame in BEAM-Dimap format (i.e. *.dim)
        outfile: string or os.path object for the output
                 file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to
        resolution (int): the resolution of the output product in meters
        dem (str): A Snap compliant string for the dem to use.
                   Possible choices are:
                       'SRTM 1sec HGT'(default)
                       'SRTM 3sec'
                       'ASTER 1sec GDEM'
                       'ACE30'

    '''

    # get gpt file
    gpt_file = h.gpt_path()

    graph = opj(OST_ROOT, 'graphs', 'S1_SLC2ARD', 'S1_SLC_LS_TC.xml')

    logger.debug("INFO: Compute Layover/Shadow mask")
    command = '{} {} -x -q {} -Pinput={} -Presol={} -Poutput={} -Pdem=\'{}\''\
        .format(gpt_file, graph, 2, infile, resolution,
                outfile, dem)

    return_code = h.run_command(command, logfile)

    if return_code == 0:
        logger.debug('INFO: Succesfully created Layover/Shadow mask')
    else:
        logger.debug(
            'ERROR: Layover/Shadow mask creation exited with an error. \
                See {} for Snap Error output'.format(logfile))
        # sys.exit(121)

    return return_code
예제 #9
0
def create_grd_stack(filelist,
                     out_stack,
                     logfile,
                     polarisation='VV,VH',
                     pattern=None):
    '''

    :param filelist: list of single Files (space separated)
    :param outfile: the stack that is generated
    :return:
    '''

    # get gpt file
    gpt_file = h.gpt_path()

    # get path to graph
    rootpath = imp.find_module('ost')[1]

    logger.debug("INFO: Creating multi-temporal stack of images")
    if isinstance(filelist, list):
        filelist = ','.join(filelist)
    if pattern:
        graph = opj(rootpath, 'graphs', 'S1_TS', '1_BS_Stacking_HAalpha.xml')
        command = '{} {} -x -q {} -Pfilelist={} -PbandPattern=\'{}.*\'\
               -Poutput={}'.format(gpt_file, graph, 2 * os.cpu_count(),
                                   filelist, pattern, out_stack)
    else:
        graph = opj(rootpath, 'graphs', 'S1_TS', '1_BS_Stacking.xml')
        command = '{} {} -x -q {} -Pfilelist={} -Ppol={} \
               -Poutput={}'.format(gpt_file, graph, 2 * os.cpu_count(),
                                   filelist, polarisation, out_stack)

    return_code = h.run_command(command, logfile)

    if return_code == 0:
        logger.debug('INFO: Succesfully created multi-temporal stack')
    else:
        logger.debug('ERROR: Stack creation exited with an error.'
                     'See {} for Snap Error output'.format(logfile))
        sys.exit(201)

    return return_code
예제 #10
0
def _ha_alpha(infile, outfile, logfile, pol_speckle_filter=False):
    '''A wrapper of SNAP H-A-alpha polarimetric decomposition

    This function takes an OST imported Sentinel-1 scene/burst
    and calulates the polarimetric decomposition parameters for
    the H-A-alpha decomposition.

    Args:
        infile: string or os.path object for
                an original Sentinel-1 GRD product in zip or SAFE format
        out_prefix: string or os.path object for the output
                    file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to
        pol_speckle_filter (bool): wether or not to apply the
                                   polarimetric speckle filter

    '''

    # get gpt file
    gpt_file = h.gpt_path()

    if pol_speckle_filter:
        graph = opj(OST_ROOT, 'graphs', 'S1_SLC2ARD',
                    'S1_SLC_Deb_Spk_Halpha.xml')
    else:
        graph = opj(OST_ROOT, 'graphs', 'S1_SLC2ARD', 'S1_SLC_Deb_Halpha.xml')

    logger.debug("INFO: Calculating the H-alpha dual polarisation")
    command = '{} {} -x -q {} -Pinput={} -Poutput={}'\
        .format(gpt_file, graph, 2, infile, outfile)

    return_code = h.run_command(command, logfile)

    if return_code == 0:
        logger.debug('INFO: Succesfully created H/Alpha product')
    else:
        logger.debug('ERROR: H/Alpha exited with an error. \
                See {} for Snap Error output'.format(logfile))
        # sys.exit(121)

    return return_code
예제 #11
0
def mt_speckle_filter(in_stack, out_stack, logfile, speckle_dict=None):
    '''
    '''
    # get gpt file
    gpt_file = h.gpt_path()
    if speckle_dict is None:
        speckle_dict = DEFAULT_MT_SPECKLE_DICT

    logger.debug(' INFO: Applying multi-temporal speckle filtering.')
    # contrcut command string
    command = ('{} Multi-Temporal-Speckle-Filter -x -q {}'
               ' -PestimateENL={}'
               ' -PanSize={}'
               ' -PdampingFactor={}'
               ' -Penl={}'
               ' -Pfilter="{}"'
               ' -PfilterSizeX={}'
               ' -PfilterSizeY={}'
               ' -PnumLooksStr={}'
               ' -PsigmaStr={}'
               ' -PtargetWindowSizeStr={}'
               ' -PwindowSize={}'
               ' -t "{}" "{}"'.format(
                   gpt_file, 2 * os.cpu_count(), speckle_dict['estimate_ENL'],
                   speckle_dict['pan_size'], speckle_dict['damping'],
                   speckle_dict['ENL'], speckle_dict['filter'],
                   speckle_dict['filter_x_size'],
                   speckle_dict['filter_y_size'], speckle_dict['num_of_looks'],
                   speckle_dict['sigma'], speckle_dict['target_window_size'],
                   speckle_dict['window_size'], out_stack, in_stack))
    return_code = h.run_command(command, logfile)

    if return_code == 0:
        logger.debug(
            ' INFO: Succesfully applied multi-temporal speckle filtering')
    else:
        raise RuntimeError(
            'Multi-temporal speckle filtering exited with an error. \
                See {} for Snap Error output'.format(logfile))

    return return_code
예제 #12
0
def _grd_speckle_filter(infile, outfile, logfile):
    '''A wrapper around SNAP's Refined Lee Speckle Filter

    This function takes OST imported Sentinel-1 product and applies
    a standardised version of the Lee-Sigma Speckle Filter with
    SNAP's defaut values.

    Args:
        infile: string or os.path object for
                an OST imported frame in BEAM-Dimap format (i.e. *.dim)
        outfile: string or os.path object for the output
                 file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to
    '''

    # get path to SNAP's command line executable gpt
    gpt_file = h.gpt_path()

    logger.debug('INFO: Applying the Refined-Lee Speckle Filter')
    # contrcut command string
    command = '{} Speckle-Filter -x -q {} -PestimateENL=true ' \
              '-Pfilter=\'Refined Lee\' -t \'{}\' ' \
              '\'{}\''.format(gpt_file, 2 * os.cpu_count(),
                              outfile, infile
                              )

    # run command and get return code
    return_code = h.run_command(command, logfile)

    # hadle errors and logs
    if return_code == 0:
        logger.debug('INFO: Succesfully imported product')
    else:
        logger.debug('ERROR: Speckle Filtering exited with an error. \
                See {} for Snap Error output'.format(logfile))
        sys.exit(111)

    return return_code
예제 #13
0
def _grd_subset(infile, outfile, logfile, region):
    '''A wrapper around SNAP's subset routine

    This function takes an OST imported frame and subsets it according to
    the coordinates given in the region

    Args:
        infile: string or os.path object for
                an OST imported frame in BEAM-Dimap format (i.e. *.dim)
        outfile: string or os.path object for the output
                 file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to
        region (str): a list of image coordinates that bound the subset region
    '''

    # get Snap's gpt file
    gpt_file = h.gpt_path()

    # format region string
    region = ','.join([str(int(x)) for x in region])

    # construct command
    command = '{} Subset -x -q {} -Pregion={} -t \'{}\' \'{}\''.format(
        gpt_file, 2 * os.cpu_count(), region, outfile, infile)

    # run command and get return code
    return_code = h.run_command(command, logfile)

    # handle errors and logs
    if return_code == 0:
        logger.debug('INFO: Succesfully subsetted product')
    else:
        logger.debug('ERROR: Subsetting exited with an error. \
                See {} for Snap Error output'.format(logfile))
        sys.exit(107)

    return return_code
예제 #14
0
def _grd_subset_georegion(infile, outfile, logfile, georegion):
    '''A wrapper around SNAP's subset routine

    This function takes an OST imported frame and subsets it according to
    the coordinates given in the region

    Args:
        infile: string or os.path object for
                an OST imported frame in BEAM-Dimap format (i.e. *.dim)
        outfile: string or os.path object for the output
                 file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to
        georegion (str): a WKT style formatted POLYGON that bounds the
                   subset region
    '''

    logger.debug('INFO: Subsetting imported imagery.')
    # get Snap's gpt file
    gpt_file = h.gpt_path()

    # extract window from scene
    command = '{} Subset -x -q {} -Ssource=\'{}\' -t "{}" \
                 -PcopyMetadata=true -PgeoRegion="{}"'.format(
        gpt_file, 2 * os.cpu_count(), infile, outfile, georegion)

    # run command and get return code
    return_code = h.run_command(command, logfile)

    # handle errors and logs
    if return_code == 0:
        logger.debug('INFO: Succesfully subsetted product.')
    else:
        logger.debug('ERROR: Subsetting exited with an error. \
                See {} for Snap Error output'.format(logfile))
        sys.exit(107)

    return return_code
예제 #15
0
def _slice_assembly(filelist, outfile, logfile, polarisation='VV,VH,HH,HV'):
    '''A wrapper of SNAP's slice assembly routine

    This function assembles consecutive frames acquired at the same date.
    Can be either GRD or SLC products

    Args:
        filelist (str): a string of a space separated list of OST imported
                        Sentinel-1 product slices to be assembled
        outfile: string or os.path object for the output
                 file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to
    '''

    logger.debug('INFO: Assembling consecutive frames:')

    # get path to SNAP's command line executable gpt
    gpt_file = h.gpt_path()

    # construct command
    command = '{} SliceAssembly -x -q {} -PselectedPolarisations={} \
               -t \'{}\' {}'.format(gpt_file, 2 * os.cpu_count(), polarisation,
                                    outfile, filelist)

    # run command and get return code
    return_code = h.run_command(command, logfile)

    # handle errors and logs
    if return_code == 0:
        logger.debug('INFO: Succesfully assembled products')
    else:
        logger.debug('ERROR: Slice Assembly exited with an error. \
                See {} for Snap Error output'.format(logfile))
        sys.exit(101)

    return return_code
예제 #16
0
def create_stack(filelist,
                 out_stack,
                 logfile,
                 polarisation=None,
                 pattern=None):
    '''
    :param filelist: list of single Files (space separated)
    :param outfile: the stack that is generated
    :return:
    '''

    # get gpt file
    gpt_file = h.gpt_path()

    # get path to graph
    rootpath = importlib.util.find_spec('ost').submodule_search_locations[0]
    if pattern:
        graph = opj(rootpath, 'graphs', 'S1_TS', '1_BS_Stacking_HAalpha.xml')
        command = '{} {} -x -q {} -Pfilelist={} -PbandPattern=\'{}.*\' \
               -Poutput={}'.format(gpt_file, graph, 2 * os.cpu_count(),
                                   filelist, pattern, out_stack)
    else:
        graph = opj(rootpath, 'graphs', 'S1_TS', '1_BS_Stacking.xml')
        command = '{} {} -x -q {} -Pfilelist={} -Ppol={} \
               -Poutput={}'.format(gpt_file, graph, 2 * os.cpu_count(),
                                   filelist, polarisation, out_stack)

    return_code = h.run_command(command, logfile)

    if return_code == 0:
        logger.debug(' INFO: Succesfully created multi-temporal stack')
    else:
        logger.debug('ERROR: Stack creation exited with an error.'
                     ' See {} for Snap Error output'.format(logfile))

    return return_code
예제 #17
0
def _calibration(infile,
                 outfile,
                 logfile,
                 product_type='GTCgamma',
                 dem='SRTM 1sec HGT',
                 resampling=SNAP_S1_RESAMPLING_METHODS[2],
                 dem_file='',
                 dem_nodata=0.0,
                 region=''):
    '''A wrapper around SNAP's radiometric calibration

    This function takes OST imported Sentinel-1 product and generates
    it to calibrated backscatter.

    3 different calibration modes are supported.
        - Radiometrically terrain corrected Gamma nought (RTC)
          NOTE: that the routine actually calibrates to bet0 and needs to
          be used together with _terrain_flattening routine
        - ellipsoid based Gamma nought (GTCgamma)
        - Sigma nought (GTCsigma).

    Args:
        infile: string or os.path object for
                an OST imported frame in BEAM-Dimap format (i.e. *.dim)
        outfile: string or os.path object for the output
                 file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to
        resolution (int): the resolution of the output product in meters
        product_type (str): the product type of the output product
                            i.e. RTC, GTCgamma or GTCsigma

    '''

    # get gpt file
    gpt_file = h.gpt_path()
    if product_type == 'RTC':
        logger.debug('INFO: Calibrating the product to a RTC product.')
        graph = opj(OST_ROOT, 'graphs', 'S1_SLC2ARD',
                    'S1_SLC_TNR_Calbeta_Deb_ML_TF_SUB.xml')
        command = '{} {} -x -q {} -Pdem=\'{}\' -Pdem_file="{}" ' \
                  '-Pdem_nodata={} -Presampling={} -Pregion="{}" -Pinput={} ' \
                  '-Poutput={}' \
            .format(gpt_file, graph, 2, dem, dem_file,
                    dem_nodata, resampling, region, infile, outfile)
    elif product_type == 'GTCgamma':
        logger.debug(
            'INFO: Calibrating the product to a GTC product (Gamma0).')
        graph = opj(OST_ROOT, 'graphs', 'S1_SLC2ARD',
                    'S1_SLC_TNR_CalGamma_Deb_SUB.xml')
        command = '{} {} -x -q {} -Pregion="{}" -Pinput={} -Poutput={}' \
            .format(gpt_file, graph, 2, region, infile, outfile)
    elif product_type == 'GTCsigma':
        logger.debug(
            'INFO: Calibrating the product to a GTC product (Sigma0).')
        graph = opj(OST_ROOT, 'graphs', 'S1_SLC2ARD',
                    'S1_SLC_TNR_CalSigma_Deb_SUB.xml')
        command = '{} {} -x -q {} -Pregion="{}" -Pinput={} -Poutput={}' \
            .format(gpt_file, graph, 2, region, infile, outfile)
    else:
        logger.debug('ERROR: Wrong product type selected.')
        sys.exit(121)

    logger.debug("INFO: Removing thermal noise, calibrating and debursting")
    return_code = h.run_command(command, logfile)

    if return_code == 0:
        logger.debug('INFO: Succesfully calibrated product')
    else:
        logger.debug('ERROR: Frame import exited with an error. \
                See {} for Snap Error output'.format(logfile))
        # sys.exit(121)

    return return_code
예제 #18
0
def create_rgb_jpeg(filelist,
                    outfile=None,
                    shrink_factor=1,
                    plot=False,
                    minimum_list=None,
                    maximum_list=None,
                    date=None):

    minimum_list = []
    maximum_list = []

    with rasterio.open(filelist[0]) as src:
        # get metadata
        out_meta = src.meta.copy()
        # !!!assure that dimensions match ####
        new_height = int(src.height / shrink_factor)
        new_width = int(src.width / shrink_factor)
        out_meta.update(height=new_height, width=new_width)
        count = 1

        layer1 = src.read(
            out_shape=(src.count, new_height, new_width),
            resampling=5  # 5 = average
        )[0]
        minimum_list.append(get_min(filelist[0]))
        maximum_list.append(get_max(filelist[0]))
        layer1[layer1 == 0] = np.nan

    if len(filelist) > 1:
        with rasterio.open(filelist[1]) as src:
            layer2 = src.read(
                out_shape=(src.count, new_height, new_width),
                resampling=5  # 5 = average
            )[0]
            minimum_list.append(get_min(filelist[1]))
            maximum_list.append(get_max(filelist[1]))
            layer2[layer2 == 0] = np.nan
            count = 3

    # that should be the BS ratio case
    if len(filelist) == 2:
        layer3 = np.subtract(layer1, layer2)
        minimum_list.append(1)
        maximum_list.append(15)
    elif len(filelist) >= 3:
        # that's the full 3layer case
        with rasterio.open(filelist[2]) as src:
            layer3 = src.read(
                out_shape=(src.count, new_height, new_width),
                resampling=5  # 5 = average
            )[0]
        minimum_list.append(get_min(filelist[2]))
        maximum_list.append(get_max(filelist[2]))
        layer3[layer3 == 0] = np.nan

    # create empty array
    arr = np.zeros(
        (int(out_meta['height']), int(out_meta['width']), int(count)))

    arr[:, :, 0] = scale_to_int(layer1, minimum_list[0], maximum_list[0],
                                'uint8')
    if len(filelist) > 1:
        arr[:, :, 1] = scale_to_int(layer2, minimum_list[1], maximum_list[1],
                                    'uint8')
        arr[:, :, 2] = scale_to_int(layer3, minimum_list[2], maximum_list[2],
                                    'uint8')
    # transpose array to gdal format
    arr = np.transpose(arr, [2, 0, 1])

    # update outfile's metadata
    out_meta.update({'driver': 'JPEG', 'dtype': 'uint8', 'count': count})
    # write array to disk
    if outfile:
        with rasterio.open(outfile, 'w', **out_meta) as out:
            out.write(arr.astype('uint8'))
        if date:
            label_height = np.floor(np.divide(int(out_meta['height']), 15))
            cmd = 'convert -background \'#0008\'-fill white -gravity center \
                  -size {}x{} caption:\"{}\"{} +swap -gravity north \
                  -composite {}'.format(out_meta['width'], label_height, date,
                                        outfile, outfile)
            h.run_command(cmd, '{}.log'.format(outfile), elapsed=False)
    if plot:
        plt.imshow(arr)
예제 #19
0
def mosaic(filelist, outfile, cut_to_aoi=False):

    check_file = opj(os.path.dirname(outfile),
                     '.{}.processed'.format(os.path.basename(outfile)[:-4]))

    logfile = opj(os.path.dirname(outfile),
                  '{}.errLog'.format(os.path.basename(outfile)[:-4]))

    with rasterio.open(filelist.split(' ')[0]) as src:
        dtype = src.meta['dtype']
        dtype = 'float' if dtype == 'float32' else dtype
    with TemporaryDirectory() as temp_dir:
        if cut_to_aoi:
            tempfile = opj(temp_dir, os.path.basename(outfile))
        else:
            tempfile = outfile

        cmd = ('otbcli_Mosaic -ram 4096'
               ' -progress 1'
               ' -comp.feather large'
               ' -harmo.method band'
               ' -harmo.cost rmse'
               ' -temp_dir {}'
               ' -il {}'
               ' -out {} {}'.format(temp_dir, filelist, tempfile, dtype))

        return_code = h.run_command(cmd, logfile)
        if return_code != 0:
            if os.path.isfile(tempfile):
                os.remove(tempfile)

            return

    if cut_to_aoi:
        # get aoi ina way rasterio wants it
        features = vec.gdf_to_json_geometry(vec.wkt_to_gdf(cut_to_aoi))

        # import raster and mask
        with rasterio.open(tempfile) as src:
            out_image, out_transform = rasterio.mask.mask(src,
                                                          features,
                                                          crop=True)
            out_meta = src.meta.copy()
            ndv = src.nodata
            out_image = np.ma.masked_where(out_image == ndv, out_image)

        out_meta.update({
            'driver': 'GTiff',
            'height': out_image.shape[1],
            'width': out_image.shape[2],
            'transform': out_transform,
            'tiled': True,
            'blockxsize': 128,
            'blockysize': 128
        })

        with rasterio.open(outfile, 'w', **out_meta) as dest:
            dest.write(out_image.data)

        # remove intermediate file
        os.remove(tempfile)

    # check
    return_code = h.check_out_tiff(outfile)
    if return_code != 0:
        if os.path.isfile(outfile):
            os.remove(outfile)

    # write file, so we know this ts has been succesfully processed
    if return_code == 0:
        with open(str(check_file), 'w') as file:
            file.write('passed all tests \n')
예제 #20
0
def _grd_ls_mask(infile,
                 outfile,
                 logfile,
                 resolution,
                 dem='SRTM 1Sec HGT',
                 dem_file='',
                 resampling='BILINEAR_INTERPOLATION'):
    '''A wrapper around SNAP's Layover/Shadow mask routine

    This function takes OST imported Sentinel-1 product and calculates
    the Layover/Shadow mask.

    Args:
        infile: string or os.path object for
                an OST imported frame in BEAM-Dimap format (i.e. *.dim)
        outfile: string or os.path object for the output
                 file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to
        resolution (int): the resolution of the output product in meters
        dem (str): A Snap compliant string for the dem to use.
                   Possible choices are:
                       'SRTM 1sec HGT'(default)
                       'SRTM 3sec'
                       'ASTER 1sec GDEM'
                       'ACE30'

    '''

    # get path to SNAP's command line executable gpt
    gpt_file = h.gpt_path()

    logger.debug('INFO: Creating the Layover/Shadow mask')
    # get path to workflow xml
    graph = opj(OST_ROOT, 'graphs', 'S1_GRD2ARD', '3_LSmap.xml')

    if dem_file != '':
        with rasterio.open(dem_file, 'r') as dem_f:
            dem_nodata = dem_f.nodata
    else:
        dem_nodata = 0.0

    # construct command string
    command = '{} {} -x -q {} -Pinput=\'{}\' -Presol={} -Pdem=\'{}\' \
                 -Pdem_file=\'{}\' -Pdem_nodata={} -Presampling={} \
                 -Poutput=\'{}\''.format(gpt_file, graph, 2 * os.cpu_count(),
                                         infile, resolution, dem, dem_file,
                                         dem_nodata, resampling, outfile)

    # run command and get return code
    return_code = h.run_command(command, logfile)

    # handle errors and logs
    if return_code == 0:
        logger.debug('INFO: Succesfully create a Layover/Shadow mask')
    else:
        logger.debug(
            'ERROR: Layover/Shadow mask creation exited with an error. \
                See {} for Snap Error output'.format(logfile))
        raise RuntimeError
    return return_code
예제 #21
0
def _grd_terrain_correction_deg(infile,
                                outfile,
                                logfile,
                                resolution,
                                dem='SRTM 1Sec HGT',
                                dem_file='',
                                resampling='BILINEAR_INTERPOLATION'):
    '''A wrapper around SNAP's Terrain Correction routine

    This function takes an OST calibrated Sentinel-1 product and
    does the geocodification.

    Args:
        infile: string or os.path object for
                an OST imported frame in BEAM-Dimap format (i.e. *.dim)
        outfile: string or os.path object for the output
                 file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to
        resolution (int): the resolution of the output product in meters
        dem (str): A Snap compliant string for the dem to use.
                   Possible choices are:
                       'SRTM 1sec HGT'(default)
                       'SRTM 3sec'
                       'ASTER 1sec GDEM'
                       'ACE30'

    '''

    # get path to SNAP's command line executable gpt
    gpt_file = h.gpt_path()

    logger.debug('INFO: Geocoding the calibrated product')

    # calculate the multi-look factor
    # multilook_factor = int(int(resolution) / 10)
    multilook_factor = 1

    graph = opj(OST_ROOT, 'graphs', 'S1_GRD2ARD', '3_ML_TC_deg.xml')

    if dem_file != '':
        with rasterio.open(dem_file, 'r') as dem_f:
            dem_nodata = dem_f.nodata
    else:
        dem_nodata = 0.0

    # construct command string
    command = '{} {} -x -q {} -Pinput=\'{}\' -Presol={} -Pml={} -Pdem=\'{}\' \
                 -Pdem_file=\'{}\' -Pdem_nodata={} -Presampling={} \
                 -Poutput=\'{}\''.format(gpt_file, graph, 2 * os.cpu_count(),
                                         infile, resolution, multilook_factor,
                                         dem, dem_file, dem_nodata, resampling,
                                         outfile)

    # run command and get return code
    return_code = h.run_command(command, logfile)

    # handle errors and logs
    if return_code == 0:
        logger.debug('INFO: Succesfully imported product')
    else:
        logger.debug('ERROR: Terain Correction exited with an error. \
                See {} for Snap Error output'.format(logfile))
        sys.exit(112)

    return return_code
예제 #22
0
def _grd_backscatter(infile,
                     outfile,
                     logfile,
                     product_type='GTCgamma',
                     dem='SRTM 1Sec HGT',
                     dem_file='',
                     resampling='BILINEAR_INTERPOLATION'):
    '''A wrapper around SNAP's radiometric calibration

    This function takes OST imported Sentinel-1 product and generates
    it to calibrated backscatter.

    3 different calibration modes are supported.
        - Radiometrically terrain corrected Gamma nought (RTC)
        - ellipsoid based Gamma nought (GTCgamma)
        - Sigma nought (GTCsigma).

    Args:
        infile: string or os.path object for
                an OST imported frame in BEAM-Dimap format (i.e. *.dim)
        outfile: string or os.path object for the output
                 file written in BEAM-Dimap format
        logfile: string or os.path object for the file
                 where SNAP'S STDOUT/STDERR is written to
        resolution (int): the resolution of the output product in meters
        product_type (str): the product type of the output product
                            i.e. RTC, GTCgamma or GTCsigma
        dem (str): A Snap compliant string for the dem to use.
                   Possible choices are:
                       'SRTM 1sec HGT'(default)
                       'SRTM 3sec'
                       'ASTER 1sec GDEM'
                       'ACE30'

    '''

    # get path to SNAP's command line executable gpt
    gpt_file = h.gpt_path()

    # select xml according to product type
    if product_type == 'RTC':
        logger.debug('INFO: Calibrating the product to a RTC product.')
        graph = opj(OST_ROOT, 'graphs', 'S1_GRD2ARD', '2_CalBeta_TF.xml')
        if dem_file != '':
            with rasterio.open(dem_file, 'r') as dem_f:
                dem_nodata = dem_f.nodata
        else:
            dem_nodata = 0.0
    elif product_type == 'GTCgamma':
        logger.debug(
            'INFO: Calibrating the product to a GTC product (Gamma0).')
        graph = opj(OST_ROOT, 'graphs', 'S1_GRD2ARD', '2_CalGamma.xml')
    elif product_type == 'GTCsigma':
        logger.debug(
            'INFO: Calibrating the product to a GTC product (Sigma0).')
        graph = opj(OST_ROOT, 'graphs', 'S1_GRD2ARD', '2_CalSigma.xml')
    else:
        logger.debug('ERROR: Wrong product type selected.')
        sys.exit(103)

    # construct command sring
    if product_type == 'RTC':
        command = '{} {} -x -q {} -Pinput="{}" -Pdem=\'{}\' \
                   -Pdem_file=\'{}\' -Pdem_nodata={} -Presampling={} \
                   -Poutput=\'{}\''.format(gpt_file, graph, 2 * os.cpu_count(),
                                           infile, dem, dem_file, dem_nodata,
                                           resampling, outfile)
    else:
        command = '{} {} -x -q {} -Pinput=\'{}\' -Poutput=\'{}\''.format(
            gpt_file, graph, 2 * os.cpu_count(), infile, outfile)

    # run command and get return code
    return_code = h.run_command(command, logfile)

    # handle errors and logs
    if return_code == 0:
        logger.debug('INFO: Succesfully calibrated product')
    else:
        logger.debug('ERROR: Backscatter calibration exited with an error. \
                See {} for Snap Error output'.format(logfile))
        sys.exit(103)
    return return_code