1 | %'imadoc2struct': reads the xml file for image documentation
|
---|
2 | %------------------------------------------------------------------------
|
---|
3 | % function [s,errormsg]=imadoc2struct(ImaDoc,option)
|
---|
4 | %
|
---|
5 | % OUTPUT:
|
---|
6 | % s: structure representing ImaDoc
|
---|
7 | % s.Heading: information about the data hierarchical structure
|
---|
8 | % s.Time: matrix of times
|
---|
9 | % s.TimeUnit
|
---|
10 | % s.GeometryCalib: substructure containing the parameters for geometric calibration
|
---|
11 | % errormsg: error message
|
---|
12 | %
|
---|
13 | % INPUT:
|
---|
14 | % ImaDoc: full name of the xml input file with head key ImaDoc
|
---|
15 | % option: ='GeometryCalib': read the data of GeometryCalib, including source point coordinates
|
---|
16 |
|
---|
17 | function [s,errormsg]=imadoc2struct(ImaDoc,option)
|
---|
18 | %% default input and output
|
---|
19 | if ~exist('option','var')
|
---|
20 | option='*';
|
---|
21 | end
|
---|
22 | errormsg=[];%default
|
---|
23 | s.Heading=[];%default
|
---|
24 | s.Time=[]; %default
|
---|
25 | s.TimeUnit=[]; %default
|
---|
26 | s.GeometryCalib=[];
|
---|
27 | tsai=[];%default
|
---|
28 |
|
---|
29 | %% opening the xml file
|
---|
30 | if exist(ImaDoc,'file')~=2, errormsg=[ ImaDoc ' does not exist']; return;end;%input file does not exist
|
---|
31 | try
|
---|
32 | t=xmltree(ImaDoc);
|
---|
33 | catch
|
---|
34 | errormsg={[ImaDoc ' is not a valid xml file']; lasterr};
|
---|
35 | display(errormsg);
|
---|
36 | return
|
---|
37 | end
|
---|
38 | uid_root=find(t,'/ImaDoc');
|
---|
39 | if isempty(uid_root), errormsg=[ImaDoc ' is not an image documentation file ImaDoc']; return; end;%not an ImaDoc .xml file
|
---|
40 |
|
---|
41 |
|
---|
42 | %% Heading
|
---|
43 | uid_Heading=find(t,'/ImaDoc/Heading');
|
---|
44 | if ~isempty(uid_Heading),
|
---|
45 | uid_Campaign=find(t,'/ImaDoc/Heading/Campaign');
|
---|
46 | uid_Exp=find(t,'/ImaDoc/Heading/Experiment');
|
---|
47 | uid_Device=find(t,'/ImaDoc/Heading/Device');
|
---|
48 | uid_Record=find(t,'/ImaDoc/Heading/Record');
|
---|
49 | uid_FirstImage=find(t,'/ImaDoc/Heading/ImageName');
|
---|
50 | s.Heading.Campaign=get(t,children(t,uid_Campaign),'value');
|
---|
51 | s.Heading.Experiment=get(t,children(t,uid_Exp),'value');
|
---|
52 | s.Heading.Device=get(t,children(t,uid_Device),'value');
|
---|
53 | if ~isempty(uid_Record)
|
---|
54 | s.Heading.Record=get(t,children(t,uid_Record),'value');
|
---|
55 | end
|
---|
56 | s.Heading.ImageName=get(t,children(t,uid_FirstImage),'value');
|
---|
57 | end
|
---|
58 |
|
---|
59 | %% Camera and timing
|
---|
60 | if strcmp(option,'*') || strcmp(option,'Camera')
|
---|
61 | uid_Camera=find(t,'/ImaDoc/Camera');
|
---|
62 | if ~isempty(uid_Camera)
|
---|
63 | uid_ImageSize=find(t,'/ImaDoc/Camera/ImageSize');
|
---|
64 | if ~isempty(uid_ImageSize);
|
---|
65 | ImageSize=get(t,children(t,uid_ImageSize),'value');
|
---|
66 | xindex=findstr(ImageSize,'x');
|
---|
67 | if length(xindex)>=2
|
---|
68 | s.Npx=str2double(ImageSize(1:xindex(1)-1));
|
---|
69 | s.Npy=str2double(ImageSize(xindex(1)+1:xindex(2)-1));
|
---|
70 | end
|
---|
71 | end
|
---|
72 | uid_TimeUnit=find(t,'/ImaDoc/Camera/TimeUnit');
|
---|
73 | if ~isempty(uid_TimeUnit)
|
---|
74 | s.TimeUnit=get(t,children(t,uid_TimeUnit),'value');
|
---|
75 | end
|
---|
76 | uid_BurstTiming=find(t,'/ImaDoc/Camera/BurstTiming');
|
---|
77 | if ~isempty(uid_BurstTiming)
|
---|
78 | for k=1:length(uid_BurstTiming)
|
---|
79 | subt=branch(t,uid_BurstTiming(k));%subtree under BurstTiming
|
---|
80 | % reading Dtk
|
---|
81 | Frequency=get_value(subt,'/BurstTiming/FrameFrequency',1);
|
---|
82 | Dtj=get_value(subt,'/BurstTiming/Dtj',[]);
|
---|
83 | Dtj=Dtj/Frequency;%Dtj converted from frame unit to TimeUnit (e.g. 's')
|
---|
84 | NbDtj=get_value(subt,'/BurstTiming/NbDtj',1);
|
---|
85 | Dti=get_value(subt,'/BurstTiming/Dti',[]);
|
---|
86 | Dti=Dti/Frequency;%Dtj converted from frame unit to TimeUnit (e.g. 's')
|
---|
87 | NbDti=get_value(subt,'/BurstTiming/NbDti',1);
|
---|
88 | Time_val=get_value(subt,'/BurstTiming/Time',0);%time in TimeUnit
|
---|
89 | if ~isempty(Dti)
|
---|
90 | Dti=reshape(Dti'*ones(1,NbDti),NbDti*numel(Dti),1); %concatene Dti vector NbDti times
|
---|
91 | Time_val=[Time_val;Time_val(end)+cumsum(Dti)];%append the times defined by the intervals Dti
|
---|
92 | end
|
---|
93 | if ~isempty(Dtj)
|
---|
94 | Dtj=reshape(Dtj'*ones(1,NbDtj),1,NbDtj*numel(Dtj)); %concatene Dti vector NbDti times
|
---|
95 | Dtj=[0 Dtj];
|
---|
96 | Time_val=Time_val*ones(1,numel(Dtj))+ones(numel(Time_val),1)*cumsum(Dtj);% produce a time matrix with Dtj
|
---|
97 | end
|
---|
98 | % reading Dtk
|
---|
99 | Dtk=get_value(subt,'/BurstTiming/Dtk',[]);
|
---|
100 | NbDtk=get_value(subt,'/BurstTiming/NbDtk',1);
|
---|
101 | if isempty(Dtk)
|
---|
102 | s.Time=[s.Time;Time_val];
|
---|
103 | else
|
---|
104 | for kblock=1:NbDtk+1
|
---|
105 | Time_val_k=Time_val+(kblock-1)*Dtk;
|
---|
106 | s.Time=[s.Time;Time_val_k];
|
---|
107 | end
|
---|
108 | end
|
---|
109 | end
|
---|
110 | end
|
---|
111 | end
|
---|
112 | end
|
---|
113 |
|
---|
114 | %% motor
|
---|
115 | if strcmp(option,'*') || strcmp(option,'GeometryCalib')
|
---|
116 | uid_subtree=find(t,'/ImaDoc/TranslationMotor');
|
---|
117 | if length(uid_subtree)==1
|
---|
118 | subt=branch(t,uid_subtree);%subtree under GeometryCalib
|
---|
119 | [s.TranslationMotor,errormsg]=read_subtree(subt,{'Nbslice','ZStart','ZEnd'},[1 1 1],[1 1 1])
|
---|
120 | end
|
---|
121 | end
|
---|
122 | %% geometric calibration
|
---|
123 | if strcmp(option,'*') || strcmp(option,'GeometryCalib')
|
---|
124 | uid_GeometryCalib=find(t,'/ImaDoc/GeometryCalib');
|
---|
125 | if ~isempty(uid_GeometryCalib)
|
---|
126 | if length(uid_GeometryCalib)>1
|
---|
127 | errormsg=['More than one GeometryCalib in ' filecivxml];
|
---|
128 | return
|
---|
129 | end
|
---|
130 | subt=branch(t,uid_GeometryCalib);%subtree under GeometryCalib
|
---|
131 | cont=get(subt,1,'contents');
|
---|
132 | if ~isempty(cont)
|
---|
133 | uid_CalibrationType=find(subt,'/GeometryCalib/CalibrationType');
|
---|
134 | if isequal(length(uid_CalibrationType),1)
|
---|
135 | tsai.CalibrationType=get(subt,children(subt,uid_CalibrationType),'value');
|
---|
136 | end
|
---|
137 | uid_CoordUnit=find(subt,'/GeometryCalib/CoordUnit');
|
---|
138 | if isequal(length(uid_CoordUnit),1)
|
---|
139 | tsai.CoordUnit=get(subt,children(subt,uid_CoordUnit),'value');
|
---|
140 | end
|
---|
141 | uid_fx_fy=find(subt,'/GeometryCalib/fx_fy');
|
---|
142 | focal=[];%default fro old convention (Reg Wilson)
|
---|
143 | if isequal(length(uid_fx_fy),1)
|
---|
144 | tsai.fx_fy=str2num(get(subt,children(subt,uid_fx_fy),'value'));
|
---|
145 | else %old convention (Reg Wilson)
|
---|
146 | uid_focal=find(subt,'/GeometryCalib/focal');
|
---|
147 | uid_dpx_dpy=find(subt,'/GeometryCalib/dpx_dpy');
|
---|
148 | uid_sx=find(subt,'/GeometryCalib/sx');
|
---|
149 | if ~isempty(uid_focal) && ~isempty(uid_dpx_dpy) && ~isempty(uid_sx)
|
---|
150 | dpx_dpy=str2num(get(subt,children(subt,uid_dpx_dpy),'value'));
|
---|
151 | sx=str2num(get(subt,children(subt,uid_sx),'value'));
|
---|
152 | focal=str2num(get(subt,children(subt,uid_focal),'value'));
|
---|
153 | tsai.fx_fy(1)=sx*focal/dpx_dpy(1);
|
---|
154 | tsai.fx_fy(2)=focal/dpx_dpy(2);
|
---|
155 | end
|
---|
156 | end
|
---|
157 | uid_Cx_Cy=find(subt,'/GeometryCalib/Cx_Cy');
|
---|
158 | if ~isempty(uid_Cx_Cy)
|
---|
159 | tsai.Cx_Cy=str2num(get(subt,children(subt,uid_Cx_Cy),'value'));
|
---|
160 | end
|
---|
161 | uid_kc=find(subt,'/GeometryCalib/kc');
|
---|
162 | if ~isempty(uid_kc)
|
---|
163 | tsai.kc=str2double(get(subt,children(subt,uid_kc),'value'));
|
---|
164 | else %old convention (Reg Wilson)
|
---|
165 | uid_kappa1=find(subt,'/GeometryCalib/kappa1');
|
---|
166 | if ~isempty(uid_kappa1)&& ~isempty(focal)
|
---|
167 | kappa1=str2double(get(subt,children(subt,uid_kappa1),'value'));
|
---|
168 | tsai.kc=-kappa1*focal*focal;
|
---|
169 | end
|
---|
170 | end
|
---|
171 | uid_Tx_Ty_Tz=find(subt,'/GeometryCalib/Tx_Ty_Tz');
|
---|
172 | if ~isempty(uid_Tx_Ty_Tz)
|
---|
173 | tsai.Tx_Ty_Tz=str2num(get(subt,children(subt,uid_Tx_Ty_Tz),'value'));
|
---|
174 | end
|
---|
175 | uid_R=find(subt,'/GeometryCalib/R');
|
---|
176 | if ~isempty(uid_R)
|
---|
177 | RR=get(subt,children(subt,uid_R),'value');
|
---|
178 | if length(RR)==3
|
---|
179 | tsai.R=[str2num(RR{1});str2num(RR{2});str2num(RR{3})];
|
---|
180 | end
|
---|
181 | end
|
---|
182 |
|
---|
183 | %look for laser plane definitions
|
---|
184 | uid_Angle=find(subt,'/GeometryCalib/PlaneAngle');
|
---|
185 | uid_Pos=find(subt,'/GeometryCalib/SliceCoord');
|
---|
186 | if isempty(uid_Pos)
|
---|
187 | uid_Pos=find(subt,'/GeometryCalib/PlanePos');%old convention
|
---|
188 | end
|
---|
189 | if ~isempty(uid_Angle)
|
---|
190 | tsai.PlaneAngle=str2num(get(subt,children(subt,uid_Angle),'value'));
|
---|
191 | end
|
---|
192 | if ~isempty(uid_Pos)
|
---|
193 | for j=1:length(uid_Pos)
|
---|
194 | tsai.SliceCoord(j,:)=str2num(get(subt,children(subt,uid_Pos(j)),'value'));
|
---|
195 | end
|
---|
196 | uid_DZ=find(subt,'/GeometryCalib/SliceDZ');
|
---|
197 | uid_NbSlice=find(subt,'/GeometryCalib/NbSlice');
|
---|
198 | if ~isempty(uid_DZ) && ~isempty(uid_NbSlice)
|
---|
199 | DZ=str2double(get(subt,children(subt,uid_DZ),'value'));
|
---|
200 | NbSlice=get(subt,children(subt,uid_NbSlice),'value');
|
---|
201 | if isequal(NbSlice,'volume')
|
---|
202 | tsai.NbSlice='volume';
|
---|
203 | NbSlice=NbDtj+1;
|
---|
204 | else
|
---|
205 | tsai.NbSlice=str2double(NbSlice);
|
---|
206 | end
|
---|
207 | tsai.SliceCoord=ones(NbSlice,1)*tsai.SliceCoord+DZ*(0:NbSlice-1)'*[0 0 1];
|
---|
208 | end
|
---|
209 | end
|
---|
210 | uid_VolumeScan=find(subt,'/GeometryCalib/VolumeScan');
|
---|
211 | if ~isempty(uid_VolumeScan)
|
---|
212 | tsai.VolumeScan=get(subt,children(subt,uid_VolumeScan),'value');
|
---|
213 | end
|
---|
214 | tsai.InterfaceCoord=get_value(subt,'/GeometryCalib/InterfaceCoord',[0 0 0])
|
---|
215 | tsai.RefractionIndex=get_value(subt,'/GeometryCalib/RefractionIndex',1)
|
---|
216 |
|
---|
217 | if strcmp(option,'GeometryCalib')
|
---|
218 | tsai.PointCoord=get_value(subt,'/GeometryCalib/SourceCalib/PointCoord',[0 0 0 0 0]);
|
---|
219 | end
|
---|
220 | s.GeometryCalib=tsai;
|
---|
221 | end
|
---|
222 | end
|
---|
223 | end
|
---|
224 |
|
---|
225 | %--------------------------------------------------
|
---|
226 | % read a subtree
|
---|
227 | % INPUT:
|
---|
228 | % t: xltree
|
---|
229 | % head_element: head elelemnt of the subtree
|
---|
230 | % Data, structure containing
|
---|
231 | % .Key: element name
|
---|
232 | % .Type: type of element ('charg', 'float'....)
|
---|
233 | % .NbOccur: nbre of occurrence, NaN for un specified number
|
---|
234 | function [s,errormsg]=read_subtree(subt,Data,NbOccur,NumTest)
|
---|
235 | %--------------------------------------------------
|
---|
236 | s=[];%default
|
---|
237 | errormsg='';
|
---|
238 | head_element=get(subt,1,'name')
|
---|
239 | cont=get(subt,1,'contents');
|
---|
240 | if ~isempty(cont)
|
---|
241 | for ilist=1:length(Data)
|
---|
242 | uid_key=find(subt,[head_element '/' Data{ilist}])
|
---|
243 | if ~isequal(length(uid_key),NbOccur(ilist))
|
---|
244 | errormsg=['wrong number of occurence for ' Data{ilist}]
|
---|
245 | return
|
---|
246 | end
|
---|
247 | for ival=1:length(uid_key)
|
---|
248 | val=get(subt,children(subt,uid_key(ival)),'value')
|
---|
249 | if ~NumTest(ilist)
|
---|
250 | eval(['s.' Data{ilist} '=val;']);
|
---|
251 | else
|
---|
252 | eval(['s.' Data{ilist} '=str2double(val);'])
|
---|
253 | end
|
---|
254 | end
|
---|
255 | end
|
---|
256 | end
|
---|
257 |
|
---|
258 |
|
---|
259 | %--------------------------------------------------
|
---|
260 | % read an xml element
|
---|
261 | function val=get_value(t,label,default)
|
---|
262 | %--------------------------------------------------
|
---|
263 | val=default;
|
---|
264 | uid=find(t,label);%find the element iud(s)
|
---|
265 | if ~isempty(uid) %if the element named label exists
|
---|
266 | uid_child=children(t,uid);%find the children
|
---|
267 | if ~isempty(uid_child)
|
---|
268 | data=get(t,uid_child,'type');%get the type of child
|
---|
269 | if iscell(data)% case of multiple element
|
---|
270 | for icell=1:numel(data)
|
---|
271 | val_read=str2num(get(t,uid_child(icell),'value'));
|
---|
272 | if ~isempty(val_read)
|
---|
273 | val(icell,:)=val_read;
|
---|
274 | end
|
---|
275 | end
|
---|
276 | % val=val';
|
---|
277 | else % case of unique element value
|
---|
278 | val_read=str2num(get(t,uid_child,'value'));
|
---|
279 | if ~isempty(val_read)
|
---|
280 | val=val_read;
|
---|
281 | end
|
---|
282 | end
|
---|
283 | end
|
---|
284 | end
|
---|