source: trunk/src/imadoc2struct.m @ 643

Last change on this file since 643 was 611, checked in by sommeria, 11 years ago

problem of time display repaired

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