Skip to content

Commit

Permalink
Band references -> semantic labels: try to fix handling of free-form …
Browse files Browse the repository at this point in the history
…semnatic labels
  • Loading branch information
marisn committed Oct 22, 2021
1 parent a041ac2 commit 2e1d884
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 33 deletions.
44 changes: 17 additions & 27 deletions python/grass/semantic_label/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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():
Expand Down Expand Up @@ -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.
Expand All @@ -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

Expand All @@ -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)
7 changes: 1 addition & 6 deletions scripts/g.bands/g.bands.py
Original file line number Diff line number Diff line change
Expand Up @@ -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":
Expand Down

0 comments on commit 2e1d884

Please sign in to comment.