@@ -113,13 +113,15 @@ def rename_labels(self, target, source) -> Dimension:
113113class Band (NamedTuple ):
114114 """
115115 Simple container class for band metadata.
116- Based on https://github.com/stac-extensions/eo#band-object
116+ Based on STAC-1.1 common band object
117+ and https://github.com/stac-extensions/eo#band-object
117118 """
118119
119120 name : str
120121 common_name : Optional [str ] = None
121122 # wavelength in micrometer
122123 wavelength_um : Optional [float ] = None
124+ # Note: "aliases" is non-standard band metadata (probably just VITO-specific).
123125 aliases : Optional [List [str ]] = None
124126 # "openeo:gsd" field (https://github.com/Open-EO/openeo-stac-extensions#GSD-Object)
125127 gsd : Optional [dict ] = None
@@ -143,6 +145,16 @@ def band_aliases(self) -> List[List[str]]:
143145 def common_names (self ) -> List [str ]:
144146 return [b .common_name for b in self .bands ]
145147
148+ def _alias_match (self , name : str ) -> Union [Tuple [int , Band ], None ]:
149+ """Look up band by alias, return (index, Band) or None if not found."""
150+ matches = [(i , b ) for (i , b ) in enumerate (self .bands ) if b .aliases and name in b .aliases ]
151+ if len (matches ) == 0 :
152+ return None
153+ elif len (matches ) == 1 :
154+ return matches [0 ]
155+ else :
156+ raise ValueError (f"Multiple alias matches for band { name !r} : { [b .name for _ , b in matches ]} " )
157+
146158 def band_index (self , band : Union [int , str ]) -> int :
147159 """
148160 Resolve a given band (common) name/index to band index
@@ -161,9 +173,8 @@ def band_index(self, band: Union[int, str]) -> int:
161173 if band in band_names :
162174 return band_names .index (band )
163175 # Check band aliases to still support old band names
164- aliases = [True if aliases and band in aliases else False for aliases in self .band_aliases ]
165- if any (aliases ):
166- return aliases .index (True )
176+ if alias_match := self ._alias_match (name = band ):
177+ return alias_match [0 ]
167178 raise ValueError ("Invalid band name/index {b!r}. Valid names: {n!r}" .format (b = band , n = band_names ))
168179
169180 def band_name (self , band : Union [str , int ], allow_common = True ) -> str :
@@ -176,8 +187,8 @@ def band_name(self, band: Union[str, int], allow_common=True) -> str:
176187 return band
177188 else :
178189 return self .band_names [self .common_names .index (band )]
179- elif any ([ True if aliases and band in aliases else False for aliases in self .band_aliases ] ):
180- return self . band_names [ self . band_index ( band )]
190+ elif alias_match := self ._alias_match ( name = band ):
191+ return alias_match [ 1 ]. name
181192 elif isinstance (band , int ) and 0 <= band < len (self .bands ):
182193 return self .band_names [band ]
183194 raise ValueError ("Invalid band name/index {b!r}. Valid names: {n!r}" .format (b = band , n = self .band_names ))
0 commit comments