def toa_reflectance(infiles, outfile, mtdfile, bands): """Calculate TOA reflectance Parameters ---------- infiles : list of str or str paths to Landsat 8 band files or URIs for members in TAR file or path to TAR file outfile : str path to save output to mtdfile : str path to metadata file bands : list of int bands to extract from TAR file or bands that the URIs correspond to """ bandfiles = l8utils.get_bandfiles(infiles, bands) reflectance.calculate_landsat_reflectance(src_path=bandfiles, src_mtl=mtdfile, dst_path=outfile, rescale_factor=None, creation_options={}, bands=bands, dst_dtype='float32', processes=1, pixel_sunangle=True)
def create_toa(self, image_path, dst_path, mtl): # In this function we use rasterio library to calculate Landsat0 Top of atmosphere images creation_options = {'nodata': 0, 'compress': 'deflate', 'predict': 2} try: #define which band bandnum = _parse_bands_from_filename([image_path], '.*/LC08.*\_B{b}.TIF') print(bandnum) calculate_landsat_reflectance( [image_path], mtl, dst_path, rescale_factor=55000, creation_options=creation_options, bands=bandnum, dst_dtype='uint16', processes=4, pixel_sunangle=True, ) return dst_path except ValueError: pass
def reflectance(ctx, src_paths, src_mtl, dst_path, dst_dtype, rescale_factor, clip, readtemplate, workers, l8_bidx, verbose, creation_options, pixel_sunangle): """Calculates Landsat8 Top of Atmosphere Reflectance """ if verbose: logger.setLevel(logging.DEBUG) if l8_bidx == 0: l8_bidx = _parse_bands_from_filename(list(src_paths), readtemplate) calculate_landsat_reflectance(list(src_paths), src_mtl, dst_path, rescale_factor, creation_options, list(l8_bidx), dst_dtype, workers, pixel_sunangle, clip)
def test_calculate_landsat_reflectance(test_var, capfd): src_path, src_mtl = test_var[0], test_var[3] dst_path = '/tmp/ref1.TIF' rescale_factor = 1.0 creation_options = {} band = 5 dst_dtype = 'uint8' processes = 1 pixel_sunangle = False reflectance.calculate_landsat_reflectance([src_path], src_mtl, dst_path, rescale_factor, creation_options, [band], dst_dtype, processes, pixel_sunangle) out, err = capfd.readouterr() assert os.path.exists(dst_path)
def reflectance(ctx, src_path, src_mtl, dst_path, verbose, creation_options, l8_bidx, dst_dtype, workers): """Calculates Landsat8 Surface Reflectance """ if verbose: logger.setLevel(logging.DEBUG) if l8_bidx == 0: template = '.*\LC8.*_B{b}.TIF' l8_bidx = _parse_bands_from_filename([src_path], template)[0] elif not isinstance(l8_bidx, int): raise ValueError("%s is not a valid integer" % l8_bidx) calculate_landsat_reflectance(src_path, src_mtl, dst_path, creation_options, l8_bidx, dst_dtype, workers)
def test_calculate_landsat_reflectance_single_pixel(test_var, capfd): src_path, src_mtl = test_var[0], test_var[3] dst_path = '/tmp/ref1.tif' expected_path = 'tests/expected/ref1.tif' rescale_factor = 1.0 creation_options = {} band = 5 dst_dtype = 'uint16' processes = 1 pixel_sunangle = True reflectance.calculate_landsat_reflectance([src_path], src_mtl, dst_path, rescale_factor, creation_options, [band], dst_dtype, processes, pixel_sunangle) out, err = capfd.readouterr() with rio.open(dst_path) as created: with rio.open(expected_path) as expected: assert flex_compare(created.read(), expected.read())
def apply_srem(input_raster_file: str, metadata_file: str, angle_file: str, output_raster_file: str) -> None: out_dir = os.path.join(os.path.dirname(output_raster_file)) os.makedirs(out_dir, exist_ok=True) band_id = utils.get_band_id(input_raster_file) wavelength = OLI_WAVELENGTHS[band_id] with tempfile.TemporaryDirectory() as tmp_dir: toa_file = os.path.join(tmp_dir, 'toa.tif') reflectance.calculate_landsat_reflectance( [input_raster_file], metadata_file, toa_file, rescale_factor=1.0, creation_options={}, bands=[band_id], dst_dtype='float32', processes=1, pixel_sunangle=True, clip=True) solar_angle_file, sensor_angle_file = \ utils.get_pixel_angle_files(angle_file, band_id, tmp_dir) with rasterio.open(input_raster_file) as src: options = src.profile options.update(nodata=0) with riomucho.RioMucho( [toa_file, solar_angle_file, sensor_angle_file], output_raster_file, utils.srem_worker, mode='array_read', global_args={ 'wavelength': wavelength, 'dtype': options['dtype']}, options=options) as rios: rios.run(4)
def test_calculate_landsat_reflectance_stack_pixel(test_var, test_data, capfd): src_path, src_mtl, tif_output_stack = \ test_var[:3], test_var[3], test_data[-3] dst_path = '/tmp/ref2.tif' expected_path = 'tests/expected/ref2.tif' creation_options = {} dst_dtype = 'uint8' rescale_factor = 215 processes = 1 pixel_sunangle = True reflectance.calculate_landsat_reflectance(list(src_path), src_mtl, dst_path, rescale_factor, creation_options, [4, 3, 2], dst_dtype, processes, pixel_sunangle) out, err = capfd.readouterr() assert os.path.exists(dst_path) with rio.open(dst_path) as created: with rio.open(expected_path) as expected: assert flex_compare(created.read(), expected.read())
write_cloud_mask(qa_raster.read(1), profile, os.path.join(dst_folder, 'cloudmask.TIF')) # Set up the second loop. For bands 1 to 9 in this image. pbar_2nd_loop = tqdm_notebook(range(1, 10), desc='Band') # Iterate bands 1 to 0 using the tqdm_notebook object defined above for band in pbar_2nd_loop: # Use glob to find the current band GeoTiff in this image. src_path = glob(os.path.join(src_folder, '*B{}.TIF'.format(band))) dst_path = os.path.join(dst_folder, 'TOA_B{}.TIF'.format(band)) # Writing reflectance takes a bit to process, so if it crashes during the processing, # this code skips the ones that were already processed. if not os.path.exists(dst_path): # Use the `rio-toa` module for reflectance. reflectance.calculate_landsat_reflectance(src_path, src_mtl,dst_path, rescale_factor=rescale_factor, creation_options=creation_options,bands=[band], dst_dtype=dtype,processes=processes, pixel_sunangle=True) # Just copy the metadata from source to destination in case we need it in the future. shutil.copy(src_mtl, os.path.join(dst_folder, 'MTL.txt')) from matplotlib.cm import viridis as cmap import matplotlib.patheffects as pe bounds = gpd.read_file('./data/area_of_study_bounds.gpkg') xmin, xmax, ymin, ymax = [], [], [], [] for image_path in glob(os.path.join(DST_LANDSAT_FOLDER, '*/*B5.TIF')): with rasterio.open(image_path) as src_raster: xmin.append(src_raster.bounds.left) xmax.append(src_raster.bounds.right)