def parse(self): ''' Opens the metadata file and creates a json representation of it. ''' self.metadata = {} document = dom.parse(self.filepath) stack = [] self.metadata = {} _xml_to_json(document.documentElement, stack, self.metadata)
def parse(self): ''' The parse method for the landsat instance needs two different sources of metadata. The first source is the usual local file, using this, further information is obtained, and a request to the usgs server is created. The response is then parsed into a new dictionary. ''' metadata = open(self.filepath, 'r') group_dictionary = {} groups = {} stack = [] LOGGER.debug('File: %s will be parsed as Landsat metadata file.', self.filepath) for line in metadata.readlines(): if '=' in line: key, value = line.split('=') if key.lower().strip() == 'group': stack.append(value.strip()) # put_in_dictionary(groups, stack, {}) elif key.lower().strip() == 'end_group': if group_dictionary: put_in_dictionary(groups, stack, group_dictionary) stack.pop() group_dictionary = {} else: group_dictionary[key.strip()] = parse_value(value.strip()) metadata.close() self.metadata = groups LOGGER.debug('File metadata has been parsed.') path = self.get_attribute([ 'L1_METADATA_FILE', 'PRODUCT_METADATA', 'WRS_PATH' ]) row = self.get_attribute([ 'L1_METADATA_FILE', 'PRODUCT_METADATA', 'WRS_ROW' ]) sensor = self.get_attribute([ 'L1_METADATA_FILE', 'PRODUCT_METADATA', 'SENSOR_ID' ]) acquisition_date = self.get_attribute([ 'L1_METADATA_FILE', 'PRODUCT_METADATA', 'DATE_ACQUIRED'] ) self.usgs_metadata = {} try: request = _get_usgs_metadata(path, row, sensor, acquisition_date) document = dom.parseString(request.text) stack = [] _xml_to_json(document.documentElement, stack, self.usgs_metadata) LOGGER.debug('USGS metadata has been parsed.') except: LOGGER.error('USGS metadata was not received.')
def parse(self): ''' The parse method for the landsat instance needs two different sources of metadata. The first source is the usual local file, using this, further information is obtained, and a request to the usgs server is created. The response is then parsed into a new dictionary. ''' metadata = open(self.filepath, 'r') group_dictionary = {} groups = {} stack = [] LOGGER.debug('File: %s will be parsed as Landsat metadata file.', self.filepath) for line in metadata.readlines(): if "=" in line: key, value = line.split('=') if key.lower().strip() == 'group': stack.append(value.strip()) # put_in_dictionary(groups, stack, {}) elif key.lower().strip() == 'end_group': if group_dictionary: put_in_dictionary(groups, stack, group_dictionary) stack.pop() group_dictionary = {} else: group_dictionary[key.strip()] = parse_value(value.strip()) metadata.close() self.metadata = groups LOGGER.debug('File metadata has been parsed.') path = self.get_attribute([ 'L1_METADATA_FILE', 'PRODUCT_METADATA', 'WRS_PATH' ]) row = self.get_attribute([ 'L1_METADATA_FILE', 'PRODUCT_METADATA', 'WRS_ROW' ]) sensor = self.get_attribute([ 'L1_METADATA_FILE', 'PRODUCT_METADATA', 'SENSOR_ID' ]) acquisition_date = self.get_attribute([ 'L1_METADATA_FILE', 'PRODUCT_METADATA', 'DATE_ACQUIRED'] ) request = _get_usgs_metadata(path, row, sensor, acquisition_date) document = dom.parseString(request.text) stack = [] self.usgs_metadata = {} _xml_to_json(document.documentElement, stack, self.usgs_metadata) LOGGER.debug('USGS metadata has been parsed.')
def parse(self): document = dom.parse(self.filepath) stack = [] self.metadata = {} _xml_to_json(document.documentElement, stack, self.metadata)