def apply_landsat_harmonization(services, url, band, angle_bucket_dir=None, quality_band=False): """Generate Landsat NBAR. Args: url (str) - link scene band (str) - band name angle_bucket_dir Optional[str] - path to directory/bucket containing angle bands. Returns: str: full path result images. """ try: scene_id = Path(url).stem.replace(f'_{band}', '') # download scene to temp directory source_dir = f'/tmp/processing/{scene_id}' if not quality_band else '/tmp/processing/quality' Path(source_dir).mkdir(parents=True, exist_ok=True) _ = download_raster_aws(services, url, dst_path=f'{source_dir}/{Path(url).name}', requester_pays=True) if quality_band: return f'{source_dir}/{Path(url).name}' # download angs to temp directory url_parts = url.replace('s3://', '').split('/') angle_folder = '/'.join(url_parts[2:-1]) for angle_key in [ 'sensor_azimuth', 'sensor_zenith', 'solar_azimuth', 'solar_zenith' ]: angle_file = f'{scene_id}_{angle_key}_B04.tif' path = f'{angle_bucket_dir}/{angle_folder}/{angle_file}' _ = download_raster_aws(services, path, dst_path=f'{source_dir}/{angle_file}', requester_pays=True) target_dir = '/tmp/processing/result' result_path = url _, result_paths = landsat_harmonize(scene_id, source_dir, target_dir, bands=[band], cp_quality_band=False) for r in result_paths: if r.get(band, None): result_path = r[band] shutil.rmtree(source_dir) return str(result_path) except Exception: raise
# # This file is part of Sensor Harmonization. # Copyright (C) 2020-2021 INPE. # # Sensor Harmonization (Landsat-8 and Sentinel-2) is free software; you can redistribute it and/or modify it # under the terms of the MIT License; see LICENSE file for more details. # """Define a minimal example Landsat-7 harmonization.""" # Python Native import time # 3rdparty from sensor_harm.landsat import landsat_harmonize start = time.time() sr_dir = '/path/to/L7/SR/images/' target_dir = '/path/to/output/NBAR/' scene_id = 'THE_SCENE_ID' landsat_harmonize('LE7', scene_id, sr_dir, target_dir) end = time.time() print(f'Duration time: {end - start}')
start = time.time() try: sceneid = sys.argv[1] entry = os.path.join('/mnt/input-dir/', sceneid) target_dir = '/mnt/output-dir/' if sceneid.startswith('S2'): # Sentinel print(f'Harmonizing Sentinel-2 scene: {sceneid}') sentinel_harmonize(entry, target_dir, apply_bandpass=True) elif sceneid.startswith(('LT04', 'LT05', 'LE07', 'LC08')): # Landsat angle_dir = os.path.join('/mnt/angles-dir/', sceneid) landsat_harmonize(sceneid, entry, target_dir, angle_dir=angle_dir) else: raise except: print("""Use Sentinel-2 or Landsat Data Surface Reflectance as input. Usage: docker run --rm -v /path/to/input/:/mnt/input-dir:ro -v /path/to/angles:/mnt/angles-dir:ro -v /path/to/output:/mnt/output-dir:rw -t sensor-harm '<LANDSAT Sceneid or SENTINEL-2.SAFE>""") sys.exit() end = time.time() print(f'Harmonization duration time: {end - start}')