source: trunk/src/imadoc2struct.m @ 675

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

various bugs corrected

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