搜索此博客

2018年9月10日星期一

Python Read XML Files

This code is used to extract noise value and rows and columns of sample points from auxiliary XML files of Sentinel-1 image source folders.The file the program reads is attached to the last.

Source Code:
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 10 10:43:16 2018
@author: Baikal
"""
import xml.etree.ElementTree
import numpy as np
from scipy import interpolate
## Read XML files.
noise_XML = r'G:\201606Sen\scihub.copernicus.eu\201511\S1A_EW_GRDH_1SDH_20151128T125054_20151128T125154_008805_00C8FF_BEC3.SAFE\annotation\calibration\noise-s1a-ew-grd-hh-20151128t125054-20151128t125154-008805-00c8ff-001.xml'
tree = xml.etree.ElementTree.parse( noise_XML ).getroot()
root = tree.find( 'noiseVectorList' )
## To get num_rows of sample points.
for name, value in root.attrib.items():
num_rows = int( value )
## New a matrix to store the datasets.
noise_value = [ ]
rows = [ ]
cols = [ ]
## To get lines(rows)
for node in root.findall( 'noiseVector' ):
## To get num_cols of sample points.
pixel_root = node.find( 'pixel' )
for name, value in pixel_root.attrib.items():
num_cols = int( value )
### To get concrete Cols position of sample points:
label = 0
if label == 0:
for child in node:
if child.tag in ( 'pixel' ):
number_col = child.text
label = 1
## To get concrete Rrows(lines) position of sample points:
for child in node:
if child.tag in ( 'line' ):
row_element = child.text
rows.append( row_element )
## To get noise value of sample points:
for child in node:
if child.tag in ( 'noiseLut' ):
noise_element = child.text
noise = noise + [ noise_element ]
for row in range( num_rows ):
noise_line = noise[ row ]
for col in range( num_cols ):
noise_single = noise_line.split( ' ' )
noise_value.append( noise_single )
# Reshape noise_value to a matrix of [ num_rows, num_cols ] size.
noise_value = np.asarray( noise_value ).reshape( num_rows, num_cols )
view raw gistfile1.txt hosted with ❤ by GitHub

Auxiliary XML Files Links:
https://mega.nz/#!dOIhyajZ!ymdjwhzSgoV1QOubBZpyuXo6WtpMP5t9yG3ydElFZdA


没有评论:

发表评论

LibSVM Chinese Brief Infroduction

Reference: [1]  https://blog.csdn.net/v_july_v/article/details/7624837 [2]  https://wenku.baidu.com/view/c402e983336c1eb91b375d37.html?fr...

  • Word (2)