2828
2929import geopandas as gpd
3030import numpy as np
31+ import rasterio
3132import xarray as xr
3233from cloudpathlib import CloudPath
3334from lxml import etree
@@ -200,7 +201,9 @@ def _post_init(self, **kwargs) -> None:
200201 (setting sensor type, band names and so on)
201202 """
202203 # Manage Raw unit
203- band_name = files .get_filename (self .get_default_band_path ()).upper ().split ("_" )
204+ band_name = (
205+ files .get_filename (self ._get_stack_path (as_list = False )).upper ().split ("_" )
206+ )
204207 if "SR" in band_name :
205208 self ._raw_units = RawUnits .REFL
206209 elif "DN" in band_name :
@@ -389,6 +392,48 @@ def footprint(self) -> gpd.GeoDataFrame:
389392
390393 return gpd .GeoDataFrame (geometry = footprint .geometry , crs = footprint .crs )
391394
395+ def get_band_paths (
396+ self , band_list : list , pixel_size : float = None , ** kwargs
397+ ) -> dict :
398+ """
399+ Return the paths of required bands.
400+
401+ .. code-block:: python
402+
403+ >>> from eoreader.reader import Reader
404+ >>> from eoreader.bands import *
405+ >>> path = r"SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2"
406+ >>> prod = Reader().open(path)
407+ >>> prod.get_band_paths([GREEN, RED])
408+ {
409+ <SpectralBandNames.GREEN: 'GREEN'>:
410+ 'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B3.tif',
411+ <SpectralBandNames.RED: 'RED'>:
412+ 'SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2/SENTINEL2A_20190625-105728-756_L2A_T31UEQ_C_V2-2_FRE_B4.tif'
413+ }
414+
415+ Args:
416+ band_list (list): List of the wanted bands
417+ pixel_size (float): Band pixel size
418+ kwargs: Other arguments used to load bands
419+
420+ Returns:
421+ dict: Dictionary containing the path of each queried band
422+ """
423+ band_paths = {}
424+ path = self ._get_stack_path (as_list = False )
425+ for band in band_list :
426+ # Get clean band path
427+ clean_band = self ._get_clean_band_path (
428+ band , pixel_size = pixel_size , ** kwargs
429+ )
430+ if clean_band .is_file ():
431+ band_paths [band ] = clean_band
432+ else :
433+ band_paths [band ] = path
434+
435+ return band_paths
436+
392437 def _read_band (
393438 self ,
394439 path : Union [CloudPath , Path ],
@@ -413,15 +458,32 @@ def _read_band(
413458 xr.DataArray: Band xarray
414459
415460 """
416- # Read band
417- band_arr = utils .read (
418- path ,
419- pixel_size = pixel_size ,
420- size = size ,
421- resampling = Resampling .bilinear ,
422- indexes = [self .bands [band ].id ],
423- ** kwargs ,
424- )
461+ with rasterio .open (str (path )) as dst :
462+ # Manage the case if we open a simple band (EOReader processed bands)
463+ if dst .count == 1 :
464+ # Read band
465+ band_arr = utils .read (
466+ path ,
467+ pixel_size = pixel_size ,
468+ size = size ,
469+ resampling = Resampling .bilinear ,
470+ ** kwargs ,
471+ )
472+
473+ # Manage the case if we open a stack (native DIMAP bands)
474+ else :
475+ band_arr = utils .read (
476+ path ,
477+ pixel_size = pixel_size ,
478+ size = size ,
479+ resampling = Resampling .bilinear ,
480+ indexes = [self .bands [band ].id ],
481+ ** kwargs ,
482+ )
483+
484+ # Pop useless long name
485+ if "long_name" in band_arr .attrs :
486+ band_arr .attrs .pop ("long_name" )
425487
426488 # To float32
427489 if band_arr .dtype != np .float32 :
@@ -526,7 +588,7 @@ def _load_bands(
526588 return {}
527589
528590 # Get band paths
529- band_paths = self .get_band_paths (bands , ** kwargs )
591+ band_paths = self .get_band_paths (bands , pixel_size , ** kwargs )
530592
531593 # Open bands and get array (resampled if needed)
532594 band_arrays = self ._open_bands (
0 commit comments