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 |
|
---|
17 | function [s,errormsg]=imadoc2struct(ImaDoc,varargin)
|
---|
18 | %% default input and output
|
---|
19 | errormsg='';%default
|
---|
20 | s=[];
|
---|
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)
|
---|
30 | if strcmp(FileExt,'.civ')
|
---|
31 | [errormsg,time,TimeUnit,mode,npx,npy,s.GeometryCalib]=read_imatext(ImaDoc);
|
---|
32 | return
|
---|
33 | end
|
---|
34 |
|
---|
35 | %% case of xml files
|
---|
36 | if nargin ==1
|
---|
37 | [s,Heading,errormsg]=xml2struct(ImaDoc);% convert the whole xml file in a structure s
|
---|
38 | elseif 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
|
---|
40 | else %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
|
---|
42 | end
|
---|
43 | if ~isempty(errormsg)
|
---|
44 | errormsg=['error in reading ImaDoc xml file: ' errormsg];
|
---|
45 | return
|
---|
46 | end
|
---|
47 | if ~strcmp(Heading,'ImaDoc')
|
---|
48 | errormsg='imadoc2struct/the input xml file is not ImaDoc';
|
---|
49 | return
|
---|
50 | end
|
---|
51 | %% reading timing
|
---|
52 | if 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
|
---|
112 | end
|
---|
113 |
|
---|
114 | function [s,errormsg]=read_subtree(subt,Data,NbOccur,NumTest)
|
---|
115 | %--------------------------------------------------
|
---|
116 | s=[];%default
|
---|
117 | errormsg='';
|
---|
118 | head_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
|
---|
141 | function val=get_value(t,label,default)
|
---|
142 | %--------------------------------------------------
|
---|
143 | val=default;
|
---|
144 | uid=find(t,label);%find the element iud(s)
|
---|
145 | if ~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
|
---|
166 | end
|
---|
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
|
---|
174 | function [error,time,TimeUnit,mode,npx,npy,GeometryCalib]=read_imatext(fileinput)
|
---|
175 | %------------------------------------------------------------------------
|
---|
176 | error='';%default
|
---|
177 | time=[]; %default
|
---|
178 | TimeUnit='s';
|
---|
179 | mode='pairs';
|
---|
180 | npx=[]; %default
|
---|
181 | npy=[]; %default
|
---|
182 | GeometryCalib=[];
|
---|
183 | if ~exist(fileinput,'file'), error=['image doc file ' fileinput ' does not exist']; return;end;%input file does not exist
|
---|
184 | dotciv=textread(fileinput);
|
---|
185 | sizdot=size(dotciv);
|
---|
186 | if ~isequal(sizdot(1)-8,dotciv(1,1));
|
---|
187 | error=1; %inconsistent number of bursts
|
---|
188 | end
|
---|
189 | nbfield=sizdot(1)-8;
|
---|
190 | npx=(dotciv(2,1));
|
---|
191 | npy=(dotciv(2,2));
|
---|
192 | pxcmx=(dotciv(6,1));% pixels/cm in the .civ file
|
---|
193 | pxcmy=(dotciv(6,2));
|
---|
194 | % nburst=dotciv(3,1); % nbre of bursts
|
---|
195 | abs_time1=dotciv([9:nbfield+8],2);
|
---|
196 | dtime=dotciv(5,1)*(dotciv([9:nbfield+8],[3:end-1])+1);
|
---|
197 | timeshift=[abs_time1 dtime];
|
---|
198 | time=cumsum(timeshift,2);
|
---|
199 | GeometryCalib.CalibrationType='rescale';
|
---|
200 | GeometryCalib.R=[pxcmx 0 0; 0 pxcmy 0;0 0 0];
|
---|
201 | GeometryCalib.Tx=0;
|
---|
202 | GeometryCalib.Ty=0;
|
---|
203 | GeometryCalib.Tz=1;
|
---|
204 | GeometryCalib.dpx=1;
|
---|
205 | GeometryCalib.dpy=1;
|
---|
206 | GeometryCalib.sx=1;
|
---|
207 | GeometryCalib.Cx=0;
|
---|
208 | GeometryCalib.Cy=0;
|
---|
209 | GeometryCalib.f=1;
|
---|
210 | GeometryCalib.kappa1=0;
|
---|
211 | GeometryCalib.CoordUnit='cm'; |
---|