source: trunk/src/imadoc2struct.m @ 910

Last change on this file since 910 was 908, checked in by g7moreau, 9 years ago
  • Update Copyright to 2015
File size: 8.3 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-2015, LEGI UMR 5519 / CNRS UJF 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    s.Time=[zeros(size(s.Time,1),1) s.Time]; %insert a vertical line of zeros (to deal with zero file indices)
129    s.Time=[zeros(1,size(s.Time,2)); s.Time]; %insert a horizontal line of zeros
130end
131
132function [s,errormsg]=read_subtree(subt,Data,NbOccur,NumTest)
133%--------------------------------------------------
134s=[];%default
135errormsg='';
136head_element=get(subt,1,'name');
137    cont=get(subt,1,'contents');
138    if ~isempty(cont)
139        for ilist=1:length(Data)
140            uid_key=find(subt,[head_element '/' Data{ilist}]);
141            if ~isequal(length(uid_key),NbOccur(ilist))
142                errormsg=['wrong number of occurence for ' Data{ilist}];
143                return
144            end
145            for ival=1:length(uid_key)
146                val=get(subt,children(subt,uid_key(ival)),'value');
147                if ~NumTest(ilist)
148                    eval(['s.' Data{ilist} '=val;']);
149                else
150                    eval(['s.' Data{ilist} '=str2double(val);'])
151                end
152            end
153        end
154    end
155
156
157%--------------------------------------------------
158%  read an xml element
159function val=get_value(t,label,default)
160%--------------------------------------------------
161val=default;
162uid=find(t,label);%find the element iud(s)
163if ~isempty(uid) %if the element named label exists
164   uid_child=children(t,uid);%find the children
165   if ~isempty(uid_child)
166       data=get(t,uid_child,'type');%get the type of child
167       if iscell(data)% case of multiple element
168           for icell=1:numel(data)
169               val_read=str2num(get(t,uid_child(icell),'value'));
170               if ~isempty(val_read)
171                   val(icell,:)=val_read;
172               end
173           end
174%           val=val';
175       else % case of unique element value
176           val_read=str2num(get(t,uid_child,'value'));
177           if ~isempty(val_read)
178               val=val_read;
179           else
180              val=get(t,uid_child,'value');%char string data
181           end
182       end
183   end
184end
185
186%------------------------------------------------------------------------
187%'read_imatext': reads the .civ file for image documentation (obsolete)
188% fileinput: name of the documentation file
189% time: matrix of times for the set of images
190%pxcmx: scale along x in pixels/cm
191%pxcmy: scale along y in pixels/cm
192function [error,time,TimeUnit,mode,npx,npy,GeometryCalib]=read_imatext(fileinput)
193%------------------------------------------------------------------------
194error='';%default
195time=[]; %default
196TimeUnit='s';
197mode='pairs';
198npx=[]; %default
199npy=[]; %default
200GeometryCalib=[];
201if ~exist(fileinput,'file'), error=['image doc file ' fileinput ' does not exist']; return;end;%input file does not exist
202dotciv=textread(fileinput);
203sizdot=size(dotciv);
204if ~isequal(sizdot(1)-8,dotciv(1,1));
205    error=1; %inconsistent number of bursts
206end
207nbfield=sizdot(1)-8;
208npx=(dotciv(2,1));
209npy=(dotciv(2,2));
210pxcmx=(dotciv(6,1));% pixels/cm in the .civ file
211pxcmy=(dotciv(6,2));
212% nburst=dotciv(3,1); % nbre of bursts
213abs_time1=dotciv([9:nbfield+8],2);
214dtime=dotciv(5,1)*(dotciv([9:nbfield+8],[3:end-1])+1);
215timeshift=[abs_time1 dtime];
216time=cumsum(timeshift,2);
217GeometryCalib.CalibrationType='rescale';
218GeometryCalib.R=[pxcmx 0 0; 0 pxcmy 0;0 0 0];
219GeometryCalib.Tx=0;
220GeometryCalib.Ty=0;
221GeometryCalib.Tz=1;
222GeometryCalib.dpx=1;
223GeometryCalib.dpy=1;
224GeometryCalib.sx=1;
225GeometryCalib.Cx=0;
226GeometryCalib.Cy=0;
227GeometryCalib.f=1;
228GeometryCalib.kappa1=0;
229GeometryCalib.CoordUnit='cm';
Note: See TracBrowser for help on using the repository browser.