source: trunk/src/imadoc2struct.m @ 191

Last change on this file since 191 was 191, checked in by sommeria, 13 years ago

introduce edit boxes to set axis limits. rationalisation of names FixScal?, FixLimits?....
introduce volume scan in calibration procedures

File size: 11.7 KB
Line 
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
17function [s,errormsg]=imadoc2struct(ImaDoc,option)
18%% default input and output
19if ~exist('option','var')
20    option='*';
21end
22errormsg=[];%default
23s.Heading=[];%default
24s.Time=[]; %default
25s.TimeUnit=[]; %default
26s.GeometryCalib=[];
27tsai=[];%default
28
29%% opening the xml file
30if exist(ImaDoc,'file')~=2, errormsg=[ ImaDoc ' does not exist']; return;end;%input file does not exist
31try
32    t=xmltree(ImaDoc);
33catch
34    errormsg={[ImaDoc ' is not a valid xml file']; lasterr};
35    display(errormsg);
36    return
37end
38uid_root=find(t,'/ImaDoc');
39if isempty(uid_root), errormsg=[ImaDoc ' is not an image documentation file ImaDoc']; return; end;%not an ImaDoc .xml file
40
41
42%% Heading
43uid_Heading=find(t,'/ImaDoc/Heading');
44if ~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');
57end
58
59%% Camera  and timing
60if 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
112end
113
114%% motor
115if 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
121end
122%%  geometric calibration
123if 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            if strcmp(option,'GeometryCalib')
215                tsai.PointCoord=get_value(subt,'/GeometryCalib/SourceCalib/PointCoord',[0 0 0 0 0]);
216            end
217            s.GeometryCalib=tsai;
218        end
219    end
220end
221
222%--------------------------------------------------
223%  read a subtree
224% INPUT:
225% t: xltree
226% head_element: head elelemnt of the subtree
227% Data, structure containing
228%    .Key: element name
229%    .Type: type of element ('charg', 'float'....)
230%    .NbOccur: nbre of occurrence, NaN for un specified number
231function [s,errormsg]=read_subtree(subt,Data,NbOccur,NumTest)
232%--------------------------------------------------
233s=[];%default
234errormsg='';
235head_element=get(subt,1,'name')
236    cont=get(subt,1,'contents');
237    if ~isempty(cont)
238        for ilist=1:length(Data)
239            uid_key=find(subt,[head_element '/' Data{ilist}])
240            if ~isequal(length(uid_key),NbOccur(ilist))
241                errormsg=['wrong number of occurence for ' Data{ilist}]
242                return
243            end
244            for ival=1:length(uid_key)
245                val=get(subt,children(subt,uid_key(ival)),'value')
246                if ~NumTest(ilist)
247                    eval(['s.' Data{ilist} '=val;']);
248                else
249                    eval(['s.' Data{ilist} '=str2double(val);'])
250                end
251            end
252        end
253    end
254
255
256%--------------------------------------------------
257%  read an xml element
258function val=get_value(t,label,default)
259%--------------------------------------------------
260val=default;
261uid=find(t,label);%find the element iud(s)
262if ~isempty(uid) %if the element named label exists
263   uid_child=children(t,uid);%find the children
264   if ~isempty(uid_child)
265       data=get(t,uid_child,'type');%get the type of child
266       if iscell(data)% case of multiple element
267           for icell=1:numel(data)
268               val_read=str2num(get(t,uid_child(icell),'value'));
269               if ~isempty(val_read)
270                   val(icell,:)=val_read;
271               end
272           end
273%           val=val';
274       else % case of unique element value
275           val_read=str2num(get(t,uid_child,'value'));
276           if ~isempty(val_read)
277               val=val_read;
278           end
279       end
280   end
281end
Note: See TracBrowser for help on using the repository browser.