From 2e1d88410086199cf017bdd360153c470d0bb40f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C4=81ris=20Narti=C5=A1s?= Date: Fri, 22 Oct 2021 11:03:12 +0300 Subject: [PATCH] Band references -> semantic labels: try to fix handling of free-form semnatic labels --- python/grass/semantic_label/reader.py | 44 +++++++++++---------------- scripts/g.bands/g.bands.py | 7 +---- 2 files changed, 18 insertions(+), 33 deletions(-) diff --git a/python/grass/semantic_label/reader.py b/python/grass/semantic_label/reader.py index b6671346c26..73bf70d7359 100644 --- a/python/grass/semantic_label/reader.py +++ b/python/grass/semantic_label/reader.py @@ -87,14 +87,8 @@ def print_kv(k, v, indent): for k, v in item[label].items(): print_kv(k, v, indent) - def _print_label(self, shortcut=None, band=None, semantic_label=None, tag=None): - if semantic_label: - try: - shortcut, band = semantic_label.split("_") - except ValueError: - shortcut = "unknown" - band = semantic_label - sys.stdout.write(self._label_identifier(semantic_label)) + def _print_label(self, semantic_label=None, tag=None): + sys.stdout.write(semantic_label) if tag: sys.stdout.write(" {}".format(tag)) sys.stdout.write(os.linesep) @@ -113,8 +107,8 @@ def print_info(self, shortcut=None, band=None, semantic_label=None, extended=Fal try: shortcut, band = semantic_label.split("_") except ValueError: - shortcut = "unknown" - band = semantic_label + shortcut = semantic_label + band = None found = False for root in self.config.values(): for item in root.values(): @@ -148,21 +142,21 @@ def print_info(self, shortcut=None, band=None, semantic_label=None, extended=Fal # basic information only if band: self._print_label( - shorcut=item["shortcut"], - band=band, + semantic_label=item["shortcut"], tag=item["bands"][band].get("tag"), ) else: for iband in item["bands"]: self._print_label( - shorcut=item["shortcut"], - band=iband, + semantic_label=item["shortcut"], tag=item["bands"][iband].get("tag"), ) # print warning when defined shortcut not found - if shortcut and not found: - gs.warning("Metadata for semantic label <{}> not found".format(shortcut)) + if not found: + gs.warning( + "Metadata for semantic label <{}> not found".format(semantic_label) + ) def find_file(self, semantic_label): """Find file by semantic label. @@ -179,15 +173,15 @@ def find_file(self, semantic_label): # raise SemanticLabelReaderError("Invalid band identifier <{}>".format( # semantic_label # )) - shortcut = "unknown" - band = semantic_label + shortcut = None for filename, config in self.config.items(): for root in config.keys(): - if config[root][ - "shortcut" - ].upper() == shortcut.upper() and band.upper() in map( - lambda x: x.upper(), config[root]["bands"].keys() + if ( + shortcut + and config[root]["shortcut"].upper() == shortcut.upper() + and band.upper() + in map(lambda x: x.upper(), config[root]["bands"].keys()) ): return filename @@ -202,9 +196,5 @@ def get_bands(self): for root in self.config.values(): for item in root.values(): for band in item["bands"]: - bands.append(self._band_identifier(item["shortcut"], band)) + bands.append("{}_{}".format(item["shortcut"], band)) return bands - - @staticmethod - def _band_identifier(shortcut, band): - return "{}_{}".format(shortcut, band) diff --git a/scripts/g.bands/g.bands.py b/scripts/g.bands/g.bands.py index 0f857aeb557..1552c2eca4e 100644 --- a/scripts/g.bands/g.bands.py +++ b/scripts/g.bands/g.bands.py @@ -54,12 +54,7 @@ def main(): kwargs = {} if "," in options["pattern"]: gs.fatal("Multiple values not supported") - if "_" in options["pattern"]: - # full band identifier specified - kwargs["shortcut"], kwargs["band"] = options["pattern"].split("_") - else: - # pattern - kwargs["shortcut"] = options["pattern"] + kwargs["semantic_label"] = options["pattern"] kwargs["extended"] = flags["e"] if options["operation"] == "print":