def correction(self, scene):
        """Apply atmospheric correction on collection.

        Args:
            scene - Serialized Activity
        """
        scene['collection_id'] = 'LC8SR'
        scene['activity_type'] = 'correctionLC8'
        scene_id = scene['sceneid']

        # Create/Update activity
        execution = self.create_execution(scene)

        try:
            params = dict(
                app=scene['activity_type'],
                sceneid=scene['sceneid'],
                file=scene['args']['file']
            )

            pathrow = self.get_tile_id(scene_id)
            tile_date = self.get_tile_date(scene_id)
            yyyymm = tile_date.strftime('%Y-%m')
            date = tile_date.strftime('%Y%m%d')

            params['pathrow'] = pathrow

            # Send scene to the ESPA service
            req = resource_get('{}/espa'.format(Config.ESPA_URL), params=params)
            # Ensure the request has been successfully
            assert req.status_code == 200

            result = req.json()

            if result and result.get('status') == 'ERROR':
                raise RuntimeError('Error in espa-science execution - {}'.format(scene_id))

            # Product dir
            productdir = os.path.join(Config.DATA_DIR, 'Repository/Archive/{}/{}/{}'.format(scene['collection_id'], yyyymm, pathrow))

            logging.info('Checking for the ESPA generated files in {}'.format(productdir))

            if not LandsatTask.espa_done(productdir, pathrow, date):
                raise RuntimeError('Error in atmospheric correction')

            scene['args']['file'] = productdir

        except BaseException as e:
            logging.error('Error at correction Landsat {}, id={} - {}'.format(scene_id, execution.activity_id, str(e)))

            raise e

        scene['activity_type'] = 'publishLC8'

        return scene
def correction_sen2cor280(scene):
    """Dispatch sen2cor 2.8.0 execution."""
    safeL2Afull = scene['file'].replace('MSIL1C', 'MSIL2A')

    fragments = Path(safeL2Afull).name.split('_')
    fragments[3] = 'N9999'

    processing_date = datetime.strptime(fragments[-1].replace('.SAFE', ''),
                                        '%Y%m%dT%H%M%S')
    today = datetime.utcnow().timetuple()

    processing_date = processing_date.replace(year=today.tm_year,
                                              month=today.tm_mon,
                                              day=today.tm_mday)

    output_dir = '_'.join(fragments[:-1])
    sensing_date = datetime.strptime(fragments[2], '%Y%m%dT%H%M%S')
    output_dir = Config.DATA_DIR / Path(
        'Repository/Archive/S2_MSI/{}/{}_{}.SAFE'.format(
            sensing_date.strftime('%Y-%m'), output_dir,
            processing_date.strftime('%Y%m%dT%H%M%S')))

    os.makedirs(str(output_dir), exist_ok=True)
    output_dir.mkdir(parents=True, exist_ok=True)

    logging.info('Using outputdir {}'.format(str(output_dir)))
    scene['output_dir'] = str(output_dir)

    # Send scene to the sen2cor service
    req = resource_get('{}/sen2cor'.format(Config.SEN2COR_URL), params=scene)

    result = json_parser(req.content)

    if req.status_code != 200 and result and result.get('status') == 'ERROR':
        rmtree(str(output_dir))
        raise RuntimeError('Error in sen2cor execution')

    return str(output_dir)
def correction_sen2cor255(scene):
    """Dispatch sen2cor 2.5.5 execution."""
    safeL2Afull = scene['file'].replace('MSIL1C', 'MSIL2A')
    # TODO: check if file exists and validate SAFE
    valid = False
    if os.path.exists(safeL2Afull) and valid == True:
        return safeL2Afull
    if not os.path.exists(safeL2Afull) or valid == False:
        # Send scene to the sen2cor service

        req = resource_get('{}/sen2cor'.format(Config.SEN2COR_URL),
                           params=scene)
        # Ensure the request has been successfully
        assert req.status_code == 200

        result = json_parser(req.content)

        if result and result.get('status') == 'ERROR':
            if os.path.exists(safeL2Afull):
                shutil.rmtree(safeL2Afull, ignore_errors=True)
            raise RuntimeError('Error in sen2cor execution')

    return safeL2Afull