source: trunk/src/imadoc2struct.m @ 491

Last change on this file since 491 was 460, checked in by sommeria, 12 years ago

cleaning and bugs corrected

File size: 13.4 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
30[tild,tild,FileExt]=fileparts(ImaDoc);
31if strcmp(FileExt,'.civ')
32    [errormsg,time,TimeUnit,mode,npx,npy,s.GeometryCalib]=read_imatext(ImaDoc);
33    return
34end
35try
36    t=xmltree(ImaDoc);
37catch ME
38    errormsg={['error reading ' ImaDoc ': ']; ME.message};
39    display(errormsg);
40    return
41end
42uid_root=find(t,'/ImaDoc');
43if isempty(uid_root), return; end;%not an ImaDoc .xml file
44
45%% Heading
46uid_Heading=find(t,'/ImaDoc/Heading');
47if ~isempty(uid_Heading),
48    uid_Campaign=find(t,'/ImaDoc/Heading/Campaign');
49    uid_Exp=find(t,'/ImaDoc/Heading/Experiment');
50    uid_Device=find(t,'/ImaDoc/Heading/Device');
51    uid_Record=find(t,'/ImaDoc/Heading/Record');
52    uid_FirstImage=find(t,'/ImaDoc/Heading/ImageName');
53    s.Heading.Campaign=get(t,children(t,uid_Campaign),'value');
54    s.Heading.Experiment=get(t,children(t,uid_Exp),'value');
55    s.Heading.Device=get(t,children(t,uid_Device),'value');
56    if ~isempty(uid_Record)
57        s.Heading.Record=get(t,children(t,uid_Record),'value');
58    end
59    s.Heading.ImageName=get(t,children(t,uid_FirstImage),'value');
60end
61
62%% Camera  and timing
63if strcmp(option,'*') || strcmp(option,'Camera')
64    uid_Camera=find(t,'/ImaDoc/Camera');
65    if ~isempty(uid_Camera)
66        uid_ImageSize=find(t,'/ImaDoc/Camera/ImageSize');
67        if ~isempty(uid_ImageSize);
68            ImageSize=get(t,children(t,uid_ImageSize),'value');
69            xindex=findstr(ImageSize,'x');
70            if length(xindex)>=2
71                s.Npx=str2double(ImageSize(1:xindex(1)-1));
72                s.Npy=str2double(ImageSize(xindex(1)+1:xindex(2)-1));
73            end
74        end
75        uid_TimeUnit=find(t,'/ImaDoc/Camera/TimeUnit');
76        if ~isempty(uid_TimeUnit)
77            s.TimeUnit=get(t,children(t,uid_TimeUnit),'value');
78        end
79        uid_BurstTiming=find(t,'/ImaDoc/Camera/BurstTiming');
80        if ~isempty(uid_BurstTiming)
81            for k=1:length(uid_BurstTiming)
82                subt=branch(t,uid_BurstTiming(k));%subtree under BurstTiming
83                % reading Dtk
84                Frequency=get_value(subt,'/BurstTiming/FrameFrequency',1);
85                Dtj=get_value(subt,'/BurstTiming/Dtj',[]);
86                Dtj=Dtj/Frequency;%Dtj converted from frame unit to TimeUnit (e.g. 's')
87                NbDtj=get_value(subt,'/BurstTiming/NbDtj',1);
88                Dti=get_value(subt,'/BurstTiming/Dti',[]);
89                Dti=Dti/Frequency;%Dtj converted from frame unit to TimeUnit (e.g. 's')
90                NbDti=get_value(subt,'/BurstTiming/NbDti',1);
91                Time_val=get_value(subt,'/BurstTiming/Time',0);%time in TimeUnit
92                if ~isempty(Dti)
93                    Dti=reshape(Dti'*ones(1,NbDti),NbDti*numel(Dti),1); %concatene Dti vector NbDti times
94                    Time_val=[Time_val;Time_val(end)+cumsum(Dti)];%append the times defined by the intervals  Dti
95                end
96                if ~isempty(Dtj)
97                    Dtj=reshape(Dtj'*ones(1,NbDtj),1,NbDtj*numel(Dtj)); %concatene Dtj vector NbDtj times
98                    Dtj=[0 Dtj];
99                    Time_val=Time_val*ones(1,numel(Dtj))+ones(numel(Time_val),1)*cumsum(Dtj);% produce a time matrix with Dtj
100                end
101                % reading Dtk
102                Dtk=get_value(subt,'/BurstTiming/Dtk',[]);
103                NbDtk=get_value(subt,'/BurstTiming/NbDtk',1);
104                if isempty(Dtk)
105                    s.Time=[s.Time;Time_val];
106                else
107                    for kblock=1:NbDtk+1
108                        Time_val_k=Time_val+(kblock-1)*Dtk;
109                        s.Time=[s.Time;Time_val_k];
110                    end
111                end
112            end
113        end
114    end
115end
116
117%% motor
118if strcmp(option,'*') || strcmp(option,'GeometryCalib')
119    uid_subtree=find(t,'/ImaDoc/TranslationMotor');
120    if length(uid_subtree)==1
121        subt=branch(t,uid_subtree);%subtree under GeometryCalib
122       [s.TranslationMotor,errormsg]=read_subtree(subt,{'Nbslice','ZStart','ZEnd'},[1 1 1],[1 1 1]);
123    end
124end
125%%  geometric calibration
126if strcmp(option,'*') || strcmp(option,'GeometryCalib')
127    uid_GeometryCalib=find(t,'/ImaDoc/GeometryCalib');
128    if ~isempty(uid_GeometryCalib)
129        if length(uid_GeometryCalib)>1
130            errormsg=['More than one GeometryCalib in ' filecivxml];
131            return
132        end
133        subt=branch(t,uid_GeometryCalib);%subtree under GeometryCalib
134        cont=get(subt,1,'contents');
135        if ~isempty(cont)
136            uid_CalibrationType=find(subt,'/GeometryCalib/CalibrationType');
137            if isequal(length(uid_CalibrationType),1)
138                tsai.CalibrationType=get(subt,children(subt,uid_CalibrationType),'value');
139            end
140            uid_CoordUnit=find(subt,'/GeometryCalib/CoordUnit');
141            if isequal(length(uid_CoordUnit),1)
142                tsai.CoordUnit=get(subt,children(subt,uid_CoordUnit),'value');
143            end
144            uid_fx_fy=find(subt,'/GeometryCalib/fx_fy');
145            focal=[];%default fro old convention (Reg Wilson)
146            if isequal(length(uid_fx_fy),1)
147                tsai.fx_fy=str2num(get(subt,children(subt,uid_fx_fy),'value'));
148            else %old convention (Reg Wilson)
149                uid_focal=find(subt,'/GeometryCalib/focal');
150                uid_dpx_dpy=find(subt,'/GeometryCalib/dpx_dpy');
151                uid_sx=find(subt,'/GeometryCalib/sx');
152                if ~isempty(uid_focal) && ~isempty(uid_dpx_dpy) && ~isempty(uid_sx)
153                    dpx_dpy=str2num(get(subt,children(subt,uid_dpx_dpy),'value'));
154                    sx=str2num(get(subt,children(subt,uid_sx),'value'));
155                    focal=str2num(get(subt,children(subt,uid_focal),'value'));
156                    tsai.fx_fy(1)=sx*focal/dpx_dpy(1);
157                    tsai.fx_fy(2)=focal/dpx_dpy(2);
158                end
159            end
160            uid_Cx_Cy=find(subt,'/GeometryCalib/Cx_Cy');
161            if ~isempty(uid_Cx_Cy)
162                tsai.Cx_Cy=str2num(get(subt,children(subt,uid_Cx_Cy),'value'));
163            end
164            uid_kc=find(subt,'/GeometryCalib/kc');
165            if ~isempty(uid_kc)
166                tsai.kc=str2double(get(subt,children(subt,uid_kc),'value'));
167            else %old convention (Reg Wilson)
168                uid_kappa1=find(subt,'/GeometryCalib/kappa1');
169                if ~isempty(uid_kappa1)&& ~isempty(focal)
170                    kappa1=str2double(get(subt,children(subt,uid_kappa1),'value'));
171                    tsai.kc=-kappa1*focal*focal;
172                end
173            end
174            uid_Tx_Ty_Tz=find(subt,'/GeometryCalib/Tx_Ty_Tz');
175            if ~isempty(uid_Tx_Ty_Tz)
176                tsai.Tx_Ty_Tz=str2num(get(subt,children(subt,uid_Tx_Ty_Tz),'value'));
177            end
178            uid_R=find(subt,'/GeometryCalib/R');
179            if ~isempty(uid_R)
180                RR=get(subt,children(subt,uid_R),'value');
181                if length(RR)==3
182                    tsai.R=[str2num(RR{1});str2num(RR{2});str2num(RR{3})];
183                end
184            end
185           
186            %look for laser plane definitions
187            uid_Angle=find(subt,'/GeometryCalib/PlaneAngle');
188            uid_Pos=find(subt,'/GeometryCalib/SliceCoord');
189            if isempty(uid_Pos)
190                uid_Pos=find(subt,'/GeometryCalib/PlanePos');%old convention
191            end
192            if ~isempty(uid_Angle)
193                tsai.PlaneAngle=str2num(get(subt,children(subt,uid_Angle),'value'));
194            end
195            if ~isempty(uid_Pos)
196                for j=1:length(uid_Pos)
197                    tsai.SliceCoord(j,:)=str2num(get(subt,children(subt,uid_Pos(j)),'value'));
198                end
199                uid_DZ=find(subt,'/GeometryCalib/SliceDZ');
200                uid_NbSlice=find(subt,'/GeometryCalib/NbSlice');
201                if ~isempty(uid_DZ) && ~isempty(uid_NbSlice)
202                    DZ=str2double(get(subt,children(subt,uid_DZ),'value'));
203                    NbSlice=get(subt,children(subt,uid_NbSlice),'value');
204                    if isequal(NbSlice,'volume')
205                        tsai.NbSlice='volume';
206                        NbSlice=NbDtj+1;
207                    else
208                        tsai.NbSlice=str2double(NbSlice);
209                    end
210                    tsai.SliceCoord=ones(NbSlice,1)*tsai.SliceCoord+DZ*(0:NbSlice-1)'*[0 0 1];
211                end
212            end   
213            tsai.SliceAngle=get_value(subt,'/GeometryCalib/SliceAngle',[0 0 0]);
214            tsai.VolumeScan=get_value(subt,'/GeometryCalib/VolumeScan','n');
215            tsai.InterfaceCoord=get_value(subt,'/GeometryCalib/InterfaceCoord',[0 0 0]);
216            tsai.RefractionIndex=get_value(subt,'/GeometryCalib/RefractionIndex',1);
217           
218            if strcmp(option,'GeometryCalib')
219                tsai.PointCoord=get_value(subt,'/GeometryCalib/SourceCalib/PointCoord',[0 0 0 0 0]);
220            end
221            s.GeometryCalib=tsai;
222        end
223    end
224end
225
226%--------------------------------------------------
227%  read a subtree
228% INPUT:
229% t: xltree
230% head_element: head elelemnt of the subtree
231% Data, structure containing
232%    .Key: element name
233%    .Type: type of element ('charg', 'float'....)
234%    .NbOccur: nbre of occurrence, NaN for un specified number
235function [s,errormsg]=read_subtree(subt,Data,NbOccur,NumTest)
236%--------------------------------------------------
237s=[];%default
238errormsg='';
239head_element=get(subt,1,'name');
240    cont=get(subt,1,'contents');
241    if ~isempty(cont)
242        for ilist=1:length(Data)
243            uid_key=find(subt,[head_element '/' Data{ilist}]);
244            if ~isequal(length(uid_key),NbOccur(ilist))
245                errormsg=['wrong number of occurence for ' Data{ilist}];
246                return
247            end
248            for ival=1:length(uid_key)
249                val=get(subt,children(subt,uid_key(ival)),'value');
250                if ~NumTest(ilist)
251                    eval(['s.' Data{ilist} '=val;']);
252                else
253                    eval(['s.' Data{ilist} '=str2double(val);'])
254                end
255            end
256        end
257    end
258
259
260%--------------------------------------------------
261%  read an xml element
262function val=get_value(t,label,default)
263%--------------------------------------------------
264val=default;
265uid=find(t,label);%find the element iud(s)
266if ~isempty(uid) %if the element named label exists
267   uid_child=children(t,uid);%find the children
268   if ~isempty(uid_child)
269       data=get(t,uid_child,'type');%get the type of child
270       if iscell(data)% case of multiple element
271           for icell=1:numel(data)
272               val_read=str2num(get(t,uid_child(icell),'value'));
273               if ~isempty(val_read)
274                   val(icell,:)=val_read;
275               end
276           end
277%           val=val';
278       else % case of unique element value
279           val_read=str2num(get(t,uid_child,'value'));
280           if ~isempty(val_read)
281               val=val_read;
282           else
283              val=get(t,uid_child,'value');%char string data
284           end
285       end
286   end
287end
288
289%------------------------------------------------------------------------
290%'read_imatext': reads the .civ file for image documentation (obsolete)
291% fileinput: name of the documentation file
292% time: matrix of times for the set of images
293%pxcmx: scale along x in pixels/cm
294%pxcmy: scale along y in pixels/cm
295function [error,time,TimeUnit,mode,npx,npy,GeometryCalib]=read_imatext(fileinput)
296%------------------------------------------------------------------------
297error='';%default
298time=[]; %default
299TimeUnit='s';
300mode='pairs';
301npx=[]; %default
302npy=[]; %default
303GeometryCalib=[];
304if ~exist(fileinput,'file'), error=['image doc file ' fileinput ' does not exist']; return;end;%input file does not exist
305dotciv=textread(fileinput);
306sizdot=size(dotciv);
307if ~isequal(sizdot(1)-8,dotciv(1,1));
308    error=1; %inconsistent number of bursts
309end
310nbfield=sizdot(1)-8;
311npx=(dotciv(2,1));
312npy=(dotciv(2,2));
313pxcmx=(dotciv(6,1));% pixels/cm in the .civ file
314pxcmy=(dotciv(6,2));
315% nburst=dotciv(3,1); % nbre of bursts
316abs_time1=dotciv([9:nbfield+8],2);
317dtime=dotciv(5,1)*(dotciv([9:nbfield+8],[3:end-1])+1);
318timeshift=[abs_time1 dtime];
319time=cumsum(timeshift,2);
320
321GeometryCalib.R=[pxcmx 0 0; 0 pxcmy 0;0 0 0];
322GeometryCalib.Tx=0;
323GeometryCalib.Ty=0;
324GeometryCalib.Tz=1;
325GeometryCalib.dpx=1;
326GeometryCalib.dpy=1;
327GeometryCalib.sx=1;
328GeometryCalib.Cx=0;
329GeometryCalib.Cy=0;
330GeometryCalib.f=1;
331GeometryCalib.kappa1=0;
332GeometryCalib.CoordUnit='cm';
Note: See TracBrowser for help on using the repository browser.