-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_setting.pro
More file actions
144 lines (125 loc) · 5.13 KB
/
Copy pathread_setting.pro
File metadata and controls
144 lines (125 loc) · 5.13 KB
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
;; Copyright 2018 Euratom/CCFE/TuE
;; Permission is hereby granted, free of charge, to any person obtaining a copy of this
;; software and associated documentation files (the "Software"), to deal in the Software
;; without restriction, including without limitation the rights to use, copy, modify,
;; merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
;; permit persons to whom the Software is furnished to do so, subject to the following
;; conditions:
;; The above copyright notice and this permission notice shall be included in all copies
;; or substantial portions of the Software.
;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
;; INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
;; PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
;; CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE
;; OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
function read_setting, file
;
; reads the xml-setting file and returns a nested structure.
; The 1st level fields are: equi, beam, integration, plot, ...
; Each of these fields is a structure that contains the parameters for
; the equilibrium, the beam, the integration, the plotting, ...
; get the full filename (xml-routines don't work with things like '~mdebock') and check if it exists
file = file_search(file,/fully_qualify_path)
print,file
file = file[0]
if file eq '' then begin
print, format='("ERROR: file <",A,"> not found!")',file
return, -1
endif
; load the xml-file
oDocument = obj_new('IDLffXMLDOMDocument', filename=file)
; get the top node. REMARK: this has to be called 'stark_settings'!
nodelist0 = oDocument->GetElementsByTagName('stark_settings')
topnode = nodelist0->Item(0)
; get the 1st level nodes
nodelist1 = topnode->GetChildNodes()
n_nodes1 = nodelist1->GetLength()
; and loop through them
k=0
for i=0,n_nodes1-1 do begin
; get the node name
node1 = nodelist1->Item(i)
name1 = node1->GetNodeName()
; if the node name is '#text', we ignore the node (i.e. text not
; embedded into the <> </> delimiters, most likely white space)
if strcmp(name1,'#text') then continue
; if the node name differs from '#text', we make up a list of its children
nodelist2 = node1->GetChildNodes()
n_nodes2 = nodelist2->GetLength()
l=0
for j=0,n_nodes2-1 do begin
; get the node name
node2 = nodelist2->Item(j)
name2 = node2->GetNodeName()
; again ignore the nodes named '#text'
if strcmp(name2,'#text') then continue
; get the node's attributes
attributes = node2->GetAttributes()
; the get attributes 'value' and 'type'
; ('description' and 'unit' are ignored. they are only present to
; indicate what value the user should put into the setting file)
valueNode = attributes->getNamedItem('value')
typeNode = attributes->getNamedItem('type')
; get string values of each these 2 attributes
valueStr = valueNode->getNodeValue()
typeStr = typeNode->getNodeValue()
; convert 'valueStr' based upon 'typeStr',
; we use strsplit for this, which returns a 'comma'-separated list into an array.
; if there's only one element in the array, the array is converted in a scalar.
; if the type is unknown, then it is considered to be a 'float'
typeStr = strlowcase(typeStr) ; avoid errors due to lower and uppercase differences
case typestr of
'string': begin
value = strtrim(strsplit(valueStr,',',/extract),2)
if n_elements(value) eq 1 then value=value[0]
end
'float': begin
value = float(strsplit(valueStr,',',/extract))
if n_elements(value) eq 1 then value=value[0]
end
'double': begin
value = double(strsplit(valueStr,',',/extract))
if n_elements(value) eq 1 then value=value[0]
end
'integer': begin
value = float(strsplit(valueStr,',',/extract))
if n_elements(value) eq 1 then value=value[0]
value = fix(value)
end
'long': begin
value = float(strsplit(valueStr,',',/extract))
if n_elements(value) eq 1 then value=value[0]
value = long(value)
end
'byte': begin
value = float(strsplit(valueStr,',',/extract))
if n_elements(value) eq 1 then value=value[0]
value = byte(value)
end
else: begin
value = float(strsplit(valueStr,',',/extract))
if n_elements(value) eq 1 then value=value[0]
end
endcase
; create the 1st level structure
if l eq 0 then begin
s1 = create_struct(name2,value)
endif else begin
s1 = create_struct(name2,value,s1)
endelse
l++
endfor
; create the setting structure
if k eq 0 then begin
setting = create_struct(name1,s1)
endif else begin
setting = create_struct(name1,s1,setting)
endelse
k++
endfor
; clean up all xml-objects (i.e. free memory)
obj_destroy, oDocument
; return the setting structure
return, setting
end