reader.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. import os
  2. import sys
  3. import json
  4. import glob
  5. import re
  6. from collections import OrderedDict
  7. # band reference should be required to have the format
  8. # <shortcut>_<band>
  9. # instead, the sensor name should be stored somewhere else,
  10. # and band names should be STAC common names, see
  11. # https://stacspec.org/
  12. # https://github.com/radiantearth/stac-spec/blob/master/extensions/eo/README.md#band-object
  13. # custom names must be possible
  14. class BandReferenceReaderError(Exception):
  15. pass
  16. class BandReferenceReader:
  17. """Band references reader"""
  18. def __init__(self):
  19. self._json_files = glob.glob(
  20. os.path.join(os.environ['GISBASE'], 'etc', 'g.bands', '*.json')
  21. )
  22. if not self._json_files:
  23. raise BandReferenceReaderError("No band definitions found")
  24. self._read_config()
  25. def _read_config(self):
  26. """Read configuration"""
  27. self.config = dict()
  28. for json_file in self._json_files:
  29. try:
  30. with open(json_file) as fd:
  31. config = json.load(
  32. fd,
  33. object_pairs_hook=OrderedDict
  34. )
  35. except json.decoder.JSONDecodeError as e:
  36. raise BandReferenceReaderError(
  37. "Unable to parse '{}': {}".format(
  38. json_file, e
  39. ))
  40. # check if configuration is valid
  41. self._check_config(config)
  42. self.config[os.path.basename(json_file)] = config
  43. @staticmethod
  44. def _check_config(config):
  45. """Check if config is valid
  46. :todo: check shortcut uniqueness
  47. :param dict config: configuration to be validated
  48. """
  49. for items in config.values():
  50. for item in ('shortcut', 'bands'):
  51. if item not in items.keys():
  52. raise BandReferenceReaderError(
  53. "Invalid band definition: <{}> is missing".format(item
  54. ))
  55. if len(items['bands']) < 1:
  56. raise BandReferenceReaderError(
  57. "Invalid band definition: no bands defined"
  58. )
  59. @staticmethod
  60. def _print_band_extended(band, item):
  61. """Print band-specific metadata
  62. :param str band: band identifier
  63. :param str item: items to be printed out
  64. """
  65. def print_kv(k, v, indent):
  66. if isinstance(v, OrderedDict):
  67. print ('{}{}:'.format(' ' * indent * 2, k))
  68. for ki, vi in v.items():
  69. print_kv(ki, vi, indent * 2)
  70. else:
  71. print ('{}{}: {}'.format(' ' * indent * 2, k, v))
  72. indent = 4
  73. print ('{}band: {}'.format(
  74. ' ' * indent, band
  75. ))
  76. for k, v in item[band].items():
  77. print_kv(k, v, indent)
  78. def _print_band(self, shortcut, band, tag=None):
  79. sys.stdout.write(self._band_identifier(shortcut, band))
  80. if tag:
  81. sys.stdout.write(' {}'.format(tag))
  82. sys.stdout.write(os.linesep)
  83. def print_info(self, shortcut=None, band=None, extended=False):
  84. """Prints band reference information to stdout.
  85. Can be filtered by shortcut or band identifier.
  86. :param str shortcut: shortcut to filter (eg. S2) or None
  87. :param str band: band (eg. 1) or None
  88. :param bool extended: print also extended metadata
  89. """
  90. found = False
  91. for root in self.config.values():
  92. for item in root.values():
  93. try:
  94. if shortcut and re.match(shortcut, item['shortcut']) is None:
  95. continue
  96. except re.error as e:
  97. raise BandReferenceReaderError(
  98. "Invalid pattern: {}".format(e)
  99. )
  100. found = True
  101. if band and band not in item['bands']:
  102. raise BandReferenceReaderError(
  103. "Band <{}> not found in <{}>".format(
  104. band, shortcut
  105. ))
  106. # print generic information
  107. if extended:
  108. for subitem in item.keys():
  109. if subitem == 'bands':
  110. # bands item is processed bellow
  111. continue
  112. print ('{}: {}'.format(
  113. subitem, item[subitem]
  114. ))
  115. # print detailed band information
  116. if band:
  117. self._print_band_extended(band, item['bands'])
  118. else:
  119. for iband in item['bands']:
  120. self._print_band_extended(iband, item['bands'])
  121. else:
  122. # basic information only
  123. if band:
  124. self._print_band(
  125. item['shortcut'], band,
  126. item['bands'][band].get('tag')
  127. )
  128. else:
  129. for iband in item['bands']:
  130. self._print_band(
  131. item['shortcut'], iband,
  132. item['bands'][iband].get('tag')
  133. )
  134. # raise error when defined shortcut not found
  135. if shortcut and not found:
  136. raise BandReferenceReaderError(
  137. "Band reference <{}> not found".format(shortcut)
  138. )
  139. def find_file(self, band_reference):
  140. """Find file by band reference.
  141. Match is case-insensitive.
  142. :param str band_reference: band reference identifier to search for (eg. S2_1)
  143. :return str: file basename if found or None
  144. """
  145. try:
  146. shortcut, band = band_reference.split('_')
  147. except ValueError:
  148. # raise BandReferenceReaderError("Invalid band identifier <{}>".format(
  149. # band_reference
  150. # ))
  151. shortcut = "unknown"
  152. band = band_reference
  153. for filename, config in self.config.items():
  154. for root in config.keys():
  155. if config[root]['shortcut'].upper() == shortcut.upper() and \
  156. band.upper() in map(lambda x: x.upper(), config[root]['bands'].keys()):
  157. return filename
  158. return None
  159. def get_bands(self):
  160. """Get list of band identifiers.
  161. :return list: list of valid band identifiers
  162. """
  163. bands = []
  164. for root in self.config.values():
  165. for item in root.values():
  166. for band in item['bands']:
  167. bands.append(
  168. self._band_identifier(item['shortcut'], band)
  169. )
  170. return bands
  171. @staticmethod
  172. def _band_identifier(shortcut, band):
  173. return '{}_{}'.format(shortcut, band)