source: trunk/src/imadoc2struct.m @ 685

Last change on this file since 685 was 685, checked in by sommeria, 10 years ago

error message improved for reading xml files

File size: 7.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, 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,errormsg]=xml2struct(ImaDoc);% convert the whole xml file in a structure s
38elseif nargin ==2
39    [s,Heading,errormsg]=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,errormsg]=xml2struct(ImaDoc,varargin{1},varargin{2});% convert the xml file in a structure s, keeping only the subtree defined in input
42end
43if ~isempty(errormsg)
44    errormsg=['imadoc2struct/xml2struct/' errormsg];
45    return
46end
47if ~strcmp(Heading,'ImaDoc')
48    errormsg='imadoc2struct/the input xml file is not ImaDoc';
49    return
50end
51%% reading timing
52if isfield(s,'Camera')
53    if isfield(s.Camera,'TimeUnit')
54        s.TimeUnit=s.Camera.TimeUnit;
55    end
56    Timing=s.Camera.BurstTiming;
57    if ~iscell(Timing)
58        Timing={Timing};
59    end
60    s.Time=[];
61    for k=1:length(Timing)
62        Frequency=1;
63        if isfield(Timing{k},'FrameFrequency')
64            Frequency=Timing{k}.FrameFrequency;
65        end
66        Dtj=[];
67        if isfield(Timing{k},'Dtj')
68            Dtj=Timing{k}.Dtj/Frequency;%Dtj converted from frame unit to TimeUnit (e.g. 's');
69        end
70        NbDtj=1;
71        if isfield(Timing{k},'NbDtj')&&~isempty(Timing{k}.NbDtj)
72            NbDtj=Timing{k}.NbDtj;
73        end
74        Dti=[];
75        if isfield(Timing{k},'Dti')
76            Dti=Timing{k}.Dti/Frequency;%Dti converted from frame unit to TimeUnit (e.g. 's');
77        end
78        NbDti=1;
79        if isfield(Timing{k},'NbDti')&&~isempty(Timing{k}.NbDti)
80            NbDti=Timing{k}.NbDti;
81        end
82        Time_val=Timing{k}.Time;%time in TimeUnit
83        if ~isempty(Dti)
84            Dti=reshape(Dti'*ones(1,NbDti),NbDti*numel(Dti),1); %concatene Dti vector NbDti times
85            Time_val=[Time_val;Time_val(end)+cumsum(Dti)];%append the times defined by the intervals  Dti
86        end
87        if ~isempty(Dtj)
88            Dtj=reshape(Dtj'*ones(1,NbDtj),1,NbDtj*numel(Dtj)); %concatene Dtj vector NbDtj times
89            Dtj=[0 Dtj];
90            Time_val=Time_val*ones(1,numel(Dtj))+ones(numel(Time_val),1)*cumsum(Dtj);% produce a time matrix with Dtj
91        end
92        % reading Dtk
93        Dtk=[];%default
94        NbDtk=1;%default
95        if isfield(Timing{k},'Dtk')
96            Dtk=Timing{k}.Dtk;
97        end
98        if isfield(Timing{k},'NbDtk')&&~isempty(Timing{k}.NbDtk)
99            NbDtk=Timing{k}.NbDtk;
100        end
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    s.Time=[zeros(size(s.Time,1),1) s.Time]; %insert a vertical line of zeros (to deal with zero file indices)
111    s.Time=[zeros(1,size(s.Time,2)); s.Time]; %insert a horizontal line of zeros
112end
113
114function [s,errormsg]=read_subtree(subt,Data,NbOccur,NumTest)
115%--------------------------------------------------
116s=[];%default
117errormsg='';
118head_element=get(subt,1,'name');
119    cont=get(subt,1,'contents');
120    if ~isempty(cont)
121        for ilist=1:length(Data)
122            uid_key=find(subt,[head_element '/' Data{ilist}]);
123            if ~isequal(length(uid_key),NbOccur(ilist))
124                errormsg=['wrong number of occurence for ' Data{ilist}];
125                return
126            end
127            for ival=1:length(uid_key)
128                val=get(subt,children(subt,uid_key(ival)),'value');
129                if ~NumTest(ilist)
130                    eval(['s.' Data{ilist} '=val;']);
131                else
132                    eval(['s.' Data{ilist} '=str2double(val);'])
133                end
134            end
135        end
136    end
137
138
139%--------------------------------------------------
140%  read an xml element
141function val=get_value(t,label,default)
142%--------------------------------------------------
143val=default;
144uid=find(t,label);%find the element iud(s)
145if ~isempty(uid) %if the element named label exists
146   uid_child=children(t,uid);%find the children
147   if ~isempty(uid_child)
148       data=get(t,uid_child,'type');%get the type of child
149       if iscell(data)% case of multiple element
150           for icell=1:numel(data)
151               val_read=str2num(get(t,uid_child(icell),'value'));
152               if ~isempty(val_read)
153                   val(icell,:)=val_read;
154               end
155           end
156%           val=val';
157       else % case of unique element value
158           val_read=str2num(get(t,uid_child,'value'));
159           if ~isempty(val_read)
160               val=val_read;
161           else
162              val=get(t,uid_child,'value');%char string data
163           end
164       end
165   end
166end
167
168%------------------------------------------------------------------------
169%'read_imatext': reads the .civ file for image documentation (obsolete)
170% fileinput: name of the documentation file
171% time: matrix of times for the set of images
172%pxcmx: scale along x in pixels/cm
173%pxcmy: scale along y in pixels/cm
174function [error,time,TimeUnit,mode,npx,npy,GeometryCalib]=read_imatext(fileinput)
175%------------------------------------------------------------------------
176error='';%default
177time=[]; %default
178TimeUnit='s';
179mode='pairs';
180npx=[]; %default
181npy=[]; %default
182GeometryCalib=[];
183if ~exist(fileinput,'file'), error=['image doc file ' fileinput ' does not exist']; return;end;%input file does not exist
184dotciv=textread(fileinput);
185sizdot=size(dotciv);
186if ~isequal(sizdot(1)-8,dotciv(1,1));
187    error=1; %inconsistent number of bursts
188end
189nbfield=sizdot(1)-8;
190npx=(dotciv(2,1));
191npy=(dotciv(2,2));
192pxcmx=(dotciv(6,1));% pixels/cm in the .civ file
193pxcmy=(dotciv(6,2));
194% nburst=dotciv(3,1); % nbre of bursts
195abs_time1=dotciv([9:nbfield+8],2);
196dtime=dotciv(5,1)*(dotciv([9:nbfield+8],[3:end-1])+1);
197timeshift=[abs_time1 dtime];
198time=cumsum(timeshift,2);
199GeometryCalib.CalibrationType='rescale';
200GeometryCalib.R=[pxcmx 0 0; 0 pxcmy 0;0 0 0];
201GeometryCalib.Tx=0;
202GeometryCalib.Ty=0;
203GeometryCalib.Tz=1;
204GeometryCalib.dpx=1;
205GeometryCalib.dpy=1;
206GeometryCalib.sx=1;
207GeometryCalib.Cx=0;
208GeometryCalib.Cy=0;
209GeometryCalib.f=1;
210GeometryCalib.kappa1=0;
211GeometryCalib.CoordUnit='cm';
Note: See TracBrowser for help on using the repository browser.