source: trunk/src/imadoc2struct.m @ 687

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

further corrections in error messages fo xml2struct

File size: 7.4 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
[685]37    [s,Heading,errormsg]=xml2struct(ImaDoc);% convert the whole xml file in a structure s
[565]38elseif nargin ==2
[685]39    [s,Heading,errormsg]=xml2struct(ImaDoc,varargin{1});% convert the xml file in a structure s, keeping only the subtree defined in input
[565]40else %TODO: deal with more than two subtrees?
[685]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
[560]42end
[685]43if ~isempty(errormsg)
[687]44    errormsg=['error in reading ImaDoc xml file: ' errormsg];
[685]45    return
46end
[560]47if ~strcmp(Heading,'ImaDoc')
[685]48    errormsg='imadoc2struct/the input xml file is not ImaDoc';
[8]49    return
50end
[560]51%% reading timing
[565]52if isfield(s,'Camera')
[675]53    if isfield(s.Camera,'TimeUnit')
54        s.TimeUnit=s.Camera.TimeUnit;
55    end
[565]56    Timing=s.Camera.BurstTiming;
57    if ~iscell(Timing)
58        Timing={Timing};
[8]59    end
[565]60    s.Time=[];
61    for k=1:length(Timing)
62        Frequency=1;
[582]63        if isfield(Timing{k},'FrameFrequency')
[565]64            Frequency=Timing{k}.FrameFrequency;
[8]65        end
[565]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
[591]95        if isfield(Timing{k},'Dtk')
[565]96            Dtk=Timing{k}.Dtk;
97        end
[591]98        if isfield(Timing{k},'NbDtk')&&~isempty(Timing{k}.NbDtk)
[565]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
[8]109    end
[611]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
[8]112end
113
[191]114function [s,errormsg]=read_subtree(subt,Data,NbOccur,NumTest)
115%--------------------------------------------------
116s=[];%default
117errormsg='';
[207]118head_element=get(subt,1,'name');
[191]119    cont=get(subt,1,'contents');
120    if ~isempty(cont)
121        for ilist=1:length(Data)
[207]122            uid_key=find(subt,[head_element '/' Data{ilist}]);
[191]123            if ~isequal(length(uid_key),NbOccur(ilist))
[207]124                errormsg=['wrong number of occurence for ' Data{ilist}];
[191]125                return
126            end
127            for ival=1:length(uid_key)
[207]128                val=get(subt,children(subt,uid_key(ival)),'value');
[191]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%--------------------------------------------------
[8]140%  read an xml element
141function val=get_value(t,label,default)
142%--------------------------------------------------
143val=default;
144uid=find(t,label);%find the element iud(s)
[116]145if ~isempty(uid) %if the element named label exists
146   uid_child=children(t,uid);%find the children
[8]147   if ~isempty(uid_child)
[116]148       data=get(t,uid_child,'type');%get the type of child
149       if iscell(data)% case of multiple element
[8]150           for icell=1:numel(data)
151               val_read=str2num(get(t,uid_child(icell),'value'));
152               if ~isempty(val_read)
[109]153                   val(icell,:)=val_read;
[8]154               end
155           end
[116]156%           val=val';
157       else % case of unique element value
[8]158           val_read=str2num(get(t,uid_child,'value'));
159           if ~isempty(val_read)
160               val=val_read;
[207]161           else
162              val=get(t,uid_child,'value');%char string data
[8]163           end
164       end
165   end
166end
[456]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%------------------------------------------------------------------------
[460]176error='';%default
[456]177time=[]; %default
178TimeUnit='s';
179mode='pairs';
180npx=[]; %default
181npy=[]; %default
[460]182GeometryCalib=[];
183if ~exist(fileinput,'file'), error=['image doc file ' fileinput ' does not exist']; return;end;%input file does not exist
[456]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);
[559]199GeometryCalib.CalibrationType='rescale';
[456]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.