source: trunk/src/imadoc2struct.m @ 1102

Last change on this file since 1102 was 1093, checked in by g7moreau, 4 years ago
  • Update Copyright to 2021
File size: 8.5 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
17%=======================================================================
18% Copyright 2008-2021, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
19%   http://www.legi.grenoble-inp.fr
20%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
21%
22%     This file is part of the toolbox UVMAT.
23%
24%     UVMAT is free software; you can redistribute it and/or modify
25%     it under the terms of the GNU General Public License as published
26%     by the Free Software Foundation; either version 2 of the license,
27%     or (at your option) any later version.
28%
29%     UVMAT is distributed in the hope that it will be useful,
30%     but WITHOUT ANY WARRANTY; without even the implied warranty of
31%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
32%     GNU General Public License (see LICENSE.txt) for more details.
33%=======================================================================
34
35function [s,errormsg]=imadoc2struct(ImaDoc,varargin)
36%% default input and output
37errormsg='';%default
38s=[];
39% s.Heading=[];%default
40% s.Time=[]; %default
41% s.TimeUnit=[]; %default
42% s.GeometryCalib=[];
43% tsai=[];%default
44
45%% opening the xml file
46[tild,tild,FileExt]=fileparts(ImaDoc);
47%% case of .civ files (obsolete)
48if strcmp(FileExt,'.civ')
49    [errormsg,time,TimeUnit,mode,npx,npy,s.GeometryCalib]=read_imatext(ImaDoc);
50    return
51end
52
53%% case of xml files
54if nargin ==1
55    [s,Heading,errormsg]=xml2struct(ImaDoc);% convert the whole xml file in a structure s
56elseif nargin ==2
57    [s,Heading,errormsg]=xml2struct(ImaDoc,varargin{1});% convert the xml file in a structure s, keeping only the subtree defined in input
58else %TODO: deal with more than two subtrees?
59    [s,Heading,errormsg]=xml2struct(ImaDoc,varargin{1},varargin{2});% convert the xml file in a structure s, keeping only the subtree defined in input
60end
61if ~isempty(errormsg)
62    errormsg=['error in reading ImaDoc xml file: ' errormsg];
63    return
64end
65if ~strcmp(Heading,'ImaDoc')
66    errormsg='imadoc2struct/the input xml file is not ImaDoc';
67    return
68end
69%% reading timing
70if isfield(s,'Camera')
71    if isfield(s.Camera,'TimeUnit')
72        s.TimeUnit=s.Camera.TimeUnit;
73    end
74    Timing=s.Camera.BurstTiming;
75    if ~iscell(Timing)
76        Timing={Timing};
77    end
78    s.Time=[];
79    for k=1:length(Timing)
80        Frequency=1;
81        if isfield(Timing{k},'FrameFrequency')
82            Frequency=Timing{k}.FrameFrequency;
83        end
84        Dtj=[];
85        if isfield(Timing{k},'Dtj')
86            Dtj=Timing{k}.Dtj/Frequency;%Dtj converted from frame unit to TimeUnit (e.g. 's');
87        end
88        NbDtj=1;
89        if isfield(Timing{k},'NbDtj')&&~isempty(Timing{k}.NbDtj)
90            NbDtj=Timing{k}.NbDtj;
91        end
92        Dti=[];
93        if isfield(Timing{k},'Dti')
94            Dti=Timing{k}.Dti/Frequency;%Dti converted from frame unit to TimeUnit (e.g. 's');
95        end
96        NbDti=1;
97        if isfield(Timing{k},'NbDti')&&~isempty(Timing{k}.NbDti)
98            NbDti=Timing{k}.NbDti;
99        end
100        Time_val=Timing{k}.Time;%time in TimeUnit
101        if ~isempty(Dti)
102            Dti=reshape(Dti'*ones(1,NbDti),NbDti*numel(Dti),1); %concatene Dti vector NbDti times
103            Time_val=[Time_val;Time_val(end)+cumsum(Dti)];%append the times defined by the intervals  Dti
104        end
105        if ~isempty(Dtj)
106            Dtj=reshape(Dtj'*ones(1,NbDtj),1,NbDtj*numel(Dtj)); %concatene Dtj vector NbDtj times
107            Dtj=[0 Dtj];
108            Time_val=Time_val*ones(1,numel(Dtj))+ones(numel(Time_val),1)*cumsum(Dtj);% produce a time matrix with Dtj
109        end
110        % reading Dtk
111        Dtk=[];%default
112        NbDtk=1;%default
113        if isfield(Timing{k},'Dtk')
114            Dtk=Timing{k}.Dtk;
115        end
116        if isfield(Timing{k},'NbDtk')&&~isempty(Timing{k}.NbDtk)
117            NbDtk=Timing{k}.NbDtk;
118        end
119        if isempty(Dtk)
120            s.Time=[s.Time;Time_val];
121        else
122            for kblock=1:NbDtk+1
123                Time_val_k=Time_val+(kblock-1)*Dtk;
124                s.Time=[s.Time;Time_val_k];
125            end
126        end
127    end
128    if ~isfield(s.Camera,'FirstFrameIndexI')
129        s.Camera.FirstFrameIndexI=1; %first index qssumed equl to 1 by default
130    end
131    s.Time=[zeros(size(s.Time,1),1) s.Time]; %insert a vertical line of zeros (to deal with zero file indices)
132    if s.Camera.FirstFrameIndexI~=0
133    s.Time=[zeros(s.Camera.FirstFrameIndexI,size(s.Time,2)); s.Time]; %insert a horizontal line of zeros
134    end
135end
136
137function [s,errormsg]=read_subtree(subt,Data,NbOccur,NumTest)
138%--------------------------------------------------
139s=[];%default
140errormsg='';
141head_element=get(subt,1,'name');
142    cont=get(subt,1,'contents');
143    if ~isempty(cont)
144        for ilist=1:length(Data)
145            uid_key=find(subt,[head_element '/' Data{ilist}]);
146            if ~isequal(length(uid_key),NbOccur(ilist))
147                errormsg=['wrong number of occurence for ' Data{ilist}];
148                return
149            end
150            for ival=1:length(uid_key)
151                val=get(subt,children(subt,uid_key(ival)),'value');
152                if ~NumTest(ilist)
153                    eval(['s.' Data{ilist} '=val;']);
154                else
155                    eval(['s.' Data{ilist} '=str2double(val);'])
156                end
157            end
158        end
159    end
160
161
162%--------------------------------------------------
163%  read an xml element
164function val=get_value(t,label,default)
165%--------------------------------------------------
166val=default;
167uid=find(t,label);%find the element iud(s)
168if ~isempty(uid) %if the element named label exists
169   uid_child=children(t,uid);%find the children
170   if ~isempty(uid_child)
171       data=get(t,uid_child,'type');%get the type of child
172       if iscell(data)% case of multiple element
173           for icell=1:numel(data)
174               val_read=str2num(get(t,uid_child(icell),'value'));
175               if ~isempty(val_read)
176                   val(icell,:)=val_read;
177               end
178           end
179%           val=val';
180       else % case of unique element value
181           val_read=str2num(get(t,uid_child,'value'));
182           if ~isempty(val_read)
183               val=val_read;
184           else
185              val=get(t,uid_child,'value');%char string data
186           end
187       end
188   end
189end
190
191%------------------------------------------------------------------------
192%'read_imatext': reads the .civ file for image documentation (obsolete)
193% fileinput: name of the documentation file
194% time: matrix of times for the set of images
195%pxcmx: scale along x in pixels/cm
196%pxcmy: scale along y in pixels/cm
197function [error,time,TimeUnit,mode,npx,npy,GeometryCalib]=read_imatext(fileinput)
198%------------------------------------------------------------------------
199error='';%default
200time=[]; %default
201TimeUnit='s';
202mode='pairs';
203npx=[]; %default
204npy=[]; %default
205GeometryCalib=[];
206if ~exist(fileinput,'file'), error=['image doc file ' fileinput ' does not exist']; return;end;%input file does not exist
207dotciv=textread(fileinput);
208sizdot=size(dotciv);
209if ~isequal(sizdot(1)-8,dotciv(1,1));
210    error=1; %inconsistent number of bursts
211end
212nbfield=sizdot(1)-8;
213npx=(dotciv(2,1));
214npy=(dotciv(2,2));
215pxcmx=(dotciv(6,1));% pixels/cm in the .civ file
216pxcmy=(dotciv(6,2));
217% nburst=dotciv(3,1); % nbre of bursts
218abs_time1=dotciv([9:nbfield+8],2);
219dtime=dotciv(5,1)*(dotciv([9:nbfield+8],[3:end-1])+1);
220timeshift=[abs_time1 dtime];
221time=cumsum(timeshift,2);
222GeometryCalib.CalibrationType='rescale';
223GeometryCalib.R=[pxcmx 0 0; 0 pxcmy 0;0 0 0];
224GeometryCalib.Tx=0;
225GeometryCalib.Ty=0;
226GeometryCalib.Tz=1;
227GeometryCalib.dpx=1;
228GeometryCalib.dpy=1;
229GeometryCalib.sx=1;
230GeometryCalib.Cx=0;
231GeometryCalib.Cy=0;
232GeometryCalib.f=1;
233GeometryCalib.kappa1=0;
234GeometryCalib.CoordUnit='cm';
Note: See TracBrowser for help on using the repository browser.