def map_bands_to_indices(target_bands, satellite=8): band_map = defaultdict(list) for band in landsat_rasters()[satellite]: band_map[band] = [] for band in static_rasters(): band_map[band] = [] for band in climate_rasters(): band_map[band] = [] image_directory = '/home/thomas/share/image_data/train/37_28_2013/' extensions = (".tif", ".TIF") for dirpath, dirnames, filenames in os.walk(image_directory): for f in filenames: if any(ext in f for ext in extensions): for band in band_map: if f.endswith(band): band_map[band].append(os.path.join(dirpath, f)) for band in band_map: band_map[band] = sorted(band_map[band]) # ensures ordering within bands - sort by time. indices = [] i = 0 for feat in sorted(band_map.keys()): # ensures the stack is in the same order each time. feature_rasters = band_map[feat] for feature_raster in feature_rasters: for band in target_bands: if feature_raster.endswith(band): indices.append(i) i += 1 return indices
def paths_mapping_single_scene(landsat_directory): directories = [ os.path.join(landsat_directory, f) for f in os.listdir(landsat_directory) if os.path.isdir(os.path.join(landsat_directory, f)) ] climate_directory = os.path.join(landsat_directory, 'climate_rasters') other_rasters = [ os.path.join(landsat_directory, f) for f in os.listdir(landsat_directory) if not os.path.isdir(os.path.join(landsat_directory, f)) ] date_dict = dict() for d in directories: if 'climate' in d: continue pm = _landsat_band_map(d) date = _parse_landsat_capture_date(d) cm = _climate_band_map(climate_directory, pm, date) for raster in other_rasters: for band in static_rasters(): if raster.endswith(band): pm[band] = raster date_dict[date] = pm return date_dict
def _landsat_band_map(subdirectory, satellite=8): band_map = dict() for band in landsat_rasters()[satellite]: band_map[band] = None for band in static_rasters(): band_map[band] = None for band in climate_rasters(): band_map[band] = None extensions = (".tif", ".TIF") for dirpath, dirnames, filenames in os.walk(subdirectory): for f in filenames: if any(ext in f for ext in extensions): for band in band_map: if f.endswith(band): band_map[band] = os.path.join(dirpath, f) return band_map
def all_rasters(image_directory, satellite=8): ''' Recursively get all rasters in image_directory and its subdirectories, and adds them to band_map. ''' band_map = defaultdict(list) for band in landsat_rasters()[satellite]: band_map[band] = [] for band in static_rasters(): band_map[band] = [] for band in climate_rasters(): band_map[band] = [] extensions = (".tif", ".TIF") for dirpath, dirnames, filenames in os.walk(image_directory): for f in filenames: if any(ext in f for ext in extensions): for band in band_map: if f.endswith(band): band_map[band].append(os.path.join(dirpath, f)) for band in band_map: band_map[band] = sorted(band_map[band]) # ensures ordering within bands - sort by time. return band_map