source: trunk/src/imadoc2struct.m @ 185

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

bug repqir for reqding times zith Dtk, coord units displyed in uv,qt

File size: 10.0 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%%  geometric calibration
115if strcmp(option,'*') || strcmp(option,'GeometryCalib')
116    uid_GeometryCalib=find(t,'/ImaDoc/GeometryCalib');
117    if ~isempty(uid_GeometryCalib)
118        if length(uid_GeometryCalib)>1
119            errormsg=['More than one GeometryCalib in ' filecivxml];
120            return
121        end
122        subt=branch(t,uid_GeometryCalib);%subtree under GeometryCalib
123        cont=get(subt,1,'contents');
124        if ~isempty(cont)
125            uid_CalibrationType=find(subt,'/GeometryCalib/CalibrationType');
126            if isequal(length(uid_CalibrationType),1)
127                tsai.CalibrationType=get(subt,children(subt,uid_CalibrationType),'value');
128            end
129            uid_CoordUnit=find(subt,'/GeometryCalib/CoordUnit');
130            if isequal(length(uid_CoordUnit),1)
131                tsai.CoordUnit=get(subt,children(subt,uid_CoordUnit),'value');
132            end
133            uid_fx_fy=find(subt,'/GeometryCalib/fx_fy');
134            focal=[];%default fro old convention (Reg Wilson)
135            if isequal(length(uid_fx_fy),1)
136                tsai.fx_fy=str2num(get(subt,children(subt,uid_fx_fy),'value'));
137            else %old convention (Reg Wilson)
138                uid_focal=find(subt,'/GeometryCalib/focal');
139                uid_dpx_dpy=find(subt,'/GeometryCalib/dpx_dpy');
140                uid_sx=find(subt,'/GeometryCalib/sx');
141                if ~isempty(uid_focal) && ~isempty(uid_dpx_dpy) && ~isempty(uid_sx)
142                    dpx_dpy=str2num(get(subt,children(subt,uid_dpx_dpy),'value'));
143                    sx=str2num(get(subt,children(subt,uid_sx),'value'));
144                    focal=str2num(get(subt,children(subt,uid_focal),'value'));
145                    tsai.fx_fy(1)=sx*focal/dpx_dpy(1);
146                    tsai.fx_fy(2)=focal/dpx_dpy(2);
147                end
148            end
149            uid_Cx_Cy=find(subt,'/GeometryCalib/Cx_Cy');
150            if ~isempty(uid_Cx_Cy)
151                tsai.Cx_Cy=str2num(get(subt,children(subt,uid_Cx_Cy),'value'));
152            end
153            uid_kc=find(subt,'/GeometryCalib/kc');
154            if ~isempty(uid_kc)
155                tsai.kc=str2double(get(subt,children(subt,uid_kc),'value'));
156            else %old convention (Reg Wilson)
157                uid_kappa1=find(subt,'/GeometryCalib/kappa1');
158                if ~isempty(uid_kappa1)&& ~isempty(focal)
159                    kappa1=str2double(get(subt,children(subt,uid_kappa1),'value'));
160                    tsai.kc=-kappa1*focal*focal;
161                end
162            end
163            uid_Tx_Ty_Tz=find(subt,'/GeometryCalib/Tx_Ty_Tz');
164            if ~isempty(uid_Tx_Ty_Tz)
165                tsai.Tx_Ty_Tz=str2num(get(subt,children(subt,uid_Tx_Ty_Tz),'value'));
166            end
167            uid_R=find(subt,'/GeometryCalib/R');
168            if ~isempty(uid_R)
169                RR=get(subt,children(subt,uid_R),'value');
170                if length(RR)==3
171                    tsai.R=[str2num(RR{1});str2num(RR{2});str2num(RR{3})];
172                end
173            end
174           
175            %look for laser plane definitions
176            uid_Angle=find(subt,'/GeometryCalib/PlaneAngle');
177            uid_Pos=find(subt,'/GeometryCalib/SliceCoord');
178            if isempty(uid_Pos)
179                uid_Pos=find(subt,'/GeometryCalib/PlanePos');%old convention
180            end
181            if ~isempty(uid_Angle)
182                tsai.PlaneAngle=str2num(get(subt,children(subt,uid_Angle),'value'));
183            end
184            if ~isempty(uid_Pos)
185                for j=1:length(uid_Pos)
186                    tsai.SliceCoord(j,:)=str2num(get(subt,children(subt,uid_Pos(j)),'value'));
187                end
188                uid_DZ=find(subt,'/GeometryCalib/SliceDZ');
189                uid_NbSlice=find(subt,'/GeometryCalib/NbSlice');
190                if ~isempty(uid_DZ) && ~isempty(uid_NbSlice)
191                    DZ=str2double(get(subt,children(subt,uid_DZ),'value'));
192                    NbSlice=get(subt,children(subt,uid_NbSlice),'value');
193                    if isequal(NbSlice,'volume')
194                        tsai.NbSlice='volume';
195                        NbSlice=NbDtj+1;
196                    else
197                        tsai.NbSlice=str2double(NbSlice);
198                    end
199                    tsai.SliceCoord=ones(NbSlice,1)*tsai.SliceCoord+DZ*(0:NbSlice-1)'*[0 0 1];
200                end
201            end
202            if strcmp(option,'GeometryCalib')
203                tsai.PointCoord=get_value(subt,'/GeometryCalib/SourceCalib/PointCoord',[0 0 0 0 0]);
204            end
205            s.GeometryCalib=tsai;
206        end
207    end
208end
209
210%--------------------------------------------------
211%  read an xml element
212function val=get_value(t,label,default)
213%--------------------------------------------------
214val=default;
215uid=find(t,label);%find the element iud(s)
216if ~isempty(uid) %if the element named label exists
217   uid_child=children(t,uid);%find the children
218   if ~isempty(uid_child)
219       data=get(t,uid_child,'type');%get the type of child
220       if iscell(data)% case of multiple element
221           for icell=1:numel(data)
222               val_read=str2num(get(t,uid_child(icell),'value'));
223               if ~isempty(val_read)
224                   val(icell,:)=val_read;
225               end
226           end
227%           val=val';
228       else % case of unique element value
229           val_read=str2num(get(t,uid_child,'value'));
230           if ~isempty(val_read)
231               val=val_read;
232           end
233       end
234   end
235end
Note: See TracBrowser for help on using the repository browser.