/
gamaxml2txt.py
147 lines (127 loc) · 4.87 KB
/
gamaxml2txt.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#!/usr/bin/python
'''
coordinate, precision, covariance matrix export from gama-local-adjustment
usage: ./gamaxml2txt.py < adjustment.xml > coordinates.txt
usage: ./gamaxml2txt.py --covmat covMatFile.txt < adjustment.xml > coordinates.txt
'''
## Author: Tomas Kubin
## License: GPL
from __future__ import print_function
import sys
import xml.parsers.expat
class gamaXMLParserError(Exception):
'Exception for gamaXMLParser'
def __init__(self, message):
self.message = message
def __str__(self):
return repr(self.message)
class gamaXMLParser:
'''Expat parser for gamaXMLParser 1.0 document'''
def __init__(self):
# create expat parser
self.__parser = xml.parsers.expat.ParserCreate()
self.__parser.StartElementHandler = self.__handle_start_element
self.__parser.EndElementHandler = self.__handle_end_element
self.__parser.CharacterDataHandler = self.__handle_char_data
# internal data
self.__characterData = ""
self.__attrs = []
self.__type = "#"
self.__readPoint = False # read tag <point> ?
self.__isFix = False # section <fixed>
self.__covmat = [] # covariance matrix
def parse_file(self, file):
'''parses file - file object'''
self.__parser.ParseFile(file)
def __handle_start_element(self, name, attrs):
'''start element handler'''
if name == "adjusted":
self.__readPoint = True
self.__characterData = ""
self.__type = "#"
self.__attrs = []
elif name == "fixed":
self.__readPoint = True
self.__isFix = True
self.__characterData = ""
elif name == "point":
self.__characterData = ""
elif name == "cov-mat":
self.__characterData = ""
def __handle_end_element(self, name):
'''end element handler'''
if name == "id":
self.__attrs.append(self.__characterData.strip())
self.__characterData = ""
elif name == "x" or name == "y" or name == "z" or\
name == "X" or name == "Y" or name == "Z":
self.__attrs.append(self.__characterData.strip())
self.__characterData = ""
self.__type += name
elif name == "point" and self.__readPoint is True:
if self.__isFix:
self.__type += "_fix"
self.__attrs.append(self.__type)
if len(self.__attrs) > 2:
print(" ".join(self.__attrs))
self.__type = "#"
self.__attrs = []
elif name == "fixed":
self.__readPoint = False
self.__isFix = False
elif name == "dim":
self.__dim = int(self.__characterData.strip())
self.__characterData = ""
elif name == "band":
self.__band = int(self.__characterData.strip())
self.__characterData = ""
elif name == "flt":
self.__covmat.append(self.__characterData.strip())
self.__characterData = ""
def __handle_char_data(self, data):
'''character data handler for method all and info'''
self.__characterData += data
def write_cov_mat(self, file):
dim = self.__dim
band = self.__band
#num = (dim + 1) * dim / 2 - (dim - band) * (dim - band - 1) / 2
ll = [] # list of rows of upper triangular part of covariance matrix
ll.append([])
radek = 0
sloupec = 0
for val in self.__covmat:
if sloupec - radek > band or sloupec > dim - 1:
ll.append([])
radek = radek + 1
sloupec = radek
ll[radek].append(val)
sloupec = sloupec + 1
for row in xrange(dim):
for col in xrange(dim):
if abs(col-row) > band:
print(" 0.0 ", file=file, end='')
continue
if col < row:
print(' ', file=file, end='')
print(ll[col][row-col], file=file, end='')
else:
print(' ', file=file, end='')
print(ll[row][col-row], file=file, end='')
#print((row, col), file=sys.stderr)
print('', file=file)
if __name__ == "__main__":
from optparse import OptionParser
parser = OptionParser()
parser.add_option("", "--covmat", dest="covmatFile",
help="Covariance matrix output to a FILE")
(options, args) = parser.parse_args()
parser = gamaXMLParser()
parser.parse_file(sys.stdin)
if options.covmatFile is not None:
try:
cfile = open(options.covmatFile, 'wt')
#print("Opening file %s" % options.covmatFile)
except Exception:
sys.exit('Cant open file %s for writing' % options.covmatFile)
parser.write_cov_mat(cfile)
cfile.close()