Esempio n. 1
0
    def _resample(self, imagein):

        # display
        log.info('Resampling to 30m: Start...')

        # resampling
        imageout = mgrs_framing.resample(imagein, 30, self._output_file)

        # display
        if config.getboolean('generate_intermediate_products'):
            imageout.write(DCmode=True)  # digital count
        log.info('Resampling to 30m: End')

        return imageout
Esempio n. 2
0
    def process(self, pd, image, band):
        """
        Write final product in the archive directory
        'archive_dir' is defined in config.ini file
        Naming convention from Design Document
        :param pd: instance of S2L_Product class
        :param image: input instance of S2L_ImageFile class
        :param band: band being processed
        :return: outpu  t instance of instance of S2L_ImageFile class
        """

        # TODO : add production date?
        # TODO : change L8 band numbers to S2 numbers convention?

        # /data/HLS_DATA/Archive/Site_Name/TILE_ID/S2L_DATEACQ_DATEPROD_SENSOR/S2L_DATEACQ_DATEPROD_SENSOR
        res = image.xRes
        outdir, tilecode = self.base_path(pd)
        outfile = "_".join([outdir, band, '{}m'.format(int(res))]) + '.TIF'
        tsdir = os.path.join(config.get('archive_dir'), tilecode)  # ts = temporal series
        newpath = os.path.join(tsdir, outdir, outfile)

        log.debug('New: ' + newpath)
        image.write(creation_options=['COMPRESS=LZW'], filepath=newpath)

        # declare output internally
        self.images[band] = image.filepath
        # declare output in config file
        config.set('imageout_dir', image.dirpath)
        config.set('imageout_' + band, image.filename)

        if config.getboolean('hlsplus'):
            res = 30
            outfile_30m = "_".join([outdir, band, '{}m'.format(int(res))]) + '.TIF'
            newpath_30m = os.path.join(tsdir, outdir, outfile_30m)
            if pd.sensor == 'S2':
                # create 30m band as well
                # resampling
                log.info('Resampling to 30m: Start...')
                image_30m = mgrs_framing.resample(S2L_ImageFile(newpath), res, newpath_30m)
                image_30m.write(creation_options=['COMPRESS=LZW'], DCmode=True)  # digital count
                log.info('Resampling to 30m: End')

            if pd.sensor == 'L8' and band in pd.image30m:
                # copy 30m band as well
                # write
                pd.image30m[band].write(creation_options=['COMPRESS=LZW'], filepath=newpath_30m)
                del pd.image30m[band]

        return image
Esempio n. 3
0
    def get_harmonized_product(product, band_s2, output_shape, plus):
        image_file = product.get_band_file(band_s2, plus=plus)

        if image_file is None:
            log.info('Resampling to 30m: Start...')
            band_file = product.get_band_file(band_s2, plus=True)
            match = re.search(r'_(\d{2})m', band_file.filename)
            if match:
                resampled_file = band_file.filename.replace(
                    match.group(1), '30')
            else:
                resampled_file = band_file.filename.replace(
                    '.', '_resampled_30m.')
            image_file = mgrs_framing.resample(
                band_file, 30, os.path.join(band_file.dirpath, resampled_file))
            log.info('Resampling to 30m: End')
        array = image_file.array.astype(np.float32) / 10000.
        if output_shape != array.shape:
            array = skit_resize(array.clip(min=-1.0, max=1.0),
                                output_shape).astype(np.float32)
        return array
Esempio n. 4
0
    def process(self, product, image, band):
        wd = os.path.join(config.get('wd'), product.name)
        self._output_file = self.output_file(product, band)
        self._tmp_stats = {}

        log.info('Start')

        # MGRS reframing for Landsat8
        if product.sensor == 'L8':
            log.debug('{} {}'.format(config.getfloat('dx'), config.getfloat('dy')))
            image = self._reframe(product, image, config.getfloat('dx'), config.getfloat('dy'))

        # Resampling to 30m for S2 (HLS)
        elif product.sensor == 'S2':
            if not config.getboolean('hlsplus'):
                image = self._resample(image)
            else:
                # refine geometry
                # if config.getfloat('dx') > 0.3 or config.getfloat('dy') > 0.3:
                log.debug("{} {}".format(config.getfloat('dx'), config.getfloat('dy')))
                image = self._reframe(product, image, config.getfloat('dx'), config.getfloat('dy'))

        # matching for having QA stats
        if config.get('refImage'):

            # try to adapt resolution, changing end of reference filename
            refImage_path = config.get('refImage')
            if not os.path.exists(refImage_path):
                return image

            # open image ref
            imageref = S2L_ImageFile(refImage_path)

            # if refImage resolution does not fit
            if imageref.xRes != image.xRes:
                # new refImage filepath
                refImage_noext = os.path.splitext(refImage_path)[0]
                if refImage_noext.endswith(f"_{int(imageref.xRes)}m"):
                    refImage_noext = refImage_noext[:-len(f"_{int(imageref.xRes)}m")]
                refImage_path = refImage_noext + f"_{int(image.xRes)}m.TIF"

                # compute (resample), or load if exists
                if not os.path.exists(refImage_path):
                    log.info("Resampling of the reference image")
                    # compute
                    imageref = mgrs_framing.resample(imageref, image.xRes, refImage_path)
                    # write for reuse
                    imageref.write(DCmode=True, creation_options=['COMPRESS=LZW'])
                else:
                    # or load if exists
                    log.info("Change reference image to:" + refImage_path)
                    imageref = S2L_ImageFile(refImage_path)

            # open mask
            mask = S2L_ImageFile(product.mtl.mask_filename)
            if config.getboolean('freeze_dx_dy'):
                # do Geometry Assessment only if required
                assess_geometry_bands = config.get('doAssessGeometry', default='').split(',')
                if product.sensor != 'S2':
                    assess_geometry_bands = [product.reverse_bands_mapping.get(band) for band in assess_geometry_bands]
                if assess_geometry_bands and band in assess_geometry_bands:
                    log.info("Geometry assessment for band %s" % band)
                    # Coarse resolution of correlation grid (only for stats)
                    self._matching(imageref, image, wd, mask)

            else:
                # Fine resolution of correlation grid (for accurate dx dy computation)
                dx, dy = self._matching(imageref, image, wd, mask)
                # save values for correction on bands
                config.set('dx', dx)
                config.set('dy', dy)
                log.info("Geometrical Offsets (DX/DY): {}m {}m".format(config.getfloat('dx'), config.getfloat('dy')))

            # Append bands name to keys
            for key, item in self._tmp_stats.items():
                if config.get('reference_band') != band:
                    self._tmp_stats[key+'_{}'.format(band)] = self._tmp_stats.pop(key)
            metadata.qi.update(self._tmp_stats)

        log.info('End')
        return image