source: trunk/src/series/relabel_i_j.m @ 257

Last change on this file since 257 was 249, checked in by sommeria, 14 years ago

bug repaired in relabel_i_j (xml file no more corrupted)

File size: 18.4 KB
Line 
1%'relabel_i_j': relabel an image series with two indices, and correct errors from the RDvision transfer program
2%----------------------------------------------------------------------
3function GUI_input=relabel_i_j(num_i1,num_i2,num_j1,num_j2,Series)
4%requests for the visibility of input windows in the GUI series  (activated directly by the selection in the menu ACTION)
5
6GUI_input={};
7
8%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%enable waitbar
9hGUI=findobj(allchild(0),'name','series');
10hseries=guidata(hGUI);%handles of the GUI series
11WaitbarPos=get(hseries.waitbar_frame,'Position');
12%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13
14%% PARAMETERS (for RDvision system)
15display('RDvision system')
16first_label=0; %image numbers start from 0
17%errorfactor=1 %correct a factor of 2 in NbDk+1
18
19%% read imadoc
20RootPath=get(hseries.RootPath,'String');
21RootFile=get(hseries.RootFile,'String');
22if ~iscell(RootFile)
23    msgbox_uvmat('ERROR','please enter an input image series from RDVision system')%error message for xml file reading
24    return
25end
26basename=fullfile(RootPath{1},RootFile{1});
27[XmlData,warntext]=imadoc2struct([basename '.xml']);% read the xml file appended to the present function (containing bug corrections)
28if ~isempty(warntext)
29    msgbox_uvmat('ERROR',warntext)%error message for xml file reading
30end
31nbfield1=size(XmlData.Time,1);
32nbfield2=size(XmlData.Time,2);
33set(hseries.first_i,'String',num2str(first_label))% display the first image in the process
34set(hseries.last_i,'String',num2str(nbfield1*nbfield2-1+first_label))% display the last image in the process
35set(hseries.nb_field,'String',{num2str(nbfield1*nbfield2-1+first_label)})% display the total nbre of images
36SeriesData=get(hGUI,'UserData');
37if ~strcmp(SeriesData.NomType,'_000001')
38    msgbox_uvmat('WARNING','the input is not a file from RDvision: this function relabel_i_j has no action');%error message for directory creation
39    return
40else
41    answer=msgbox_uvmat('','this function will relabel the file series from RDvision and correct the xml file');%error message for directory creation
42    if ~strcmp(answer,'Yes')
43        return
44    end
45end
46
47%% stop program ther when it is selected in the menu (no run action)
48if ~exist('num_i1','var')
49    return
50end
51if nbfield2>=2
52answer=msgbox_uvmat('',[num2str(nbfield1) ' bursts containing ' num2str(nbfield2) ' images each']);%error message for directory creation
53nomtype='_i_j';
54else
55    answer=msgbox_uvmat('',['image series with ' num2str(nbfield1) ' images']);%error message for directory creation
56    nomtype='_i';
57end
58if ~strcmp(answer,'Yes')
59    return
60end
61
62%% copy and adapt the xml file
63if exist([basename '.xml'],'file')
64    try
65        copyfile([basename '.xml'],[basename '.xml~']);% backup the xml file
66    catch ME
67        msgbox_uvmat('ERROR',ME.message);
68        return
69    end
70    t=xmltree([basename '.xml']);
71   
72    %update information on the first image name in the series
73    uid_Heading=find(t,'ImaDoc/Heading');
74    if isempty(uid_Heading)
75        [t,uid_Heading]=add(t,1,'element','Heading');
76    end
77    uid_ImageName=find(t,'ImaDoc/Heading/ImageName');
78    ImageName=name_generator(basename,1,1,'.png','_i_j');
79    [pth,ImageName]=fileparts(ImageName);
80    ImageName=[ImageName '.png'];
81    if isempty(uid_ImageName)
82        [t,uid_ImageName]=add(t,uid_Heading,'element','ImageName');
83    end
84    uid_value=children(t,uid_ImageName);
85    if isempty(uid_value)
86        t=add(t,uid_ImageName,'chardata',ImageName);%indicate  name of the first image, with ;png extension
87    else
88        t=set(t,uid_value(1),'value',ImageName);%indicate  name of the first image, with ;png extension
89    end
90   
91    %%%% correction RDvision %%%%
92    if isfield(XmlData,'NbDtj')
93        uid_NbDtj=find(t,'ImaDoc/Camera/BurstTiming/NbDtj');
94        uid_value=children(t,uid_NbDtj);
95        if ~isempty(uid_value)
96            t=set(t,uid_value(1),'value',num2str(XmlData.NbDtj));
97        end
98    end
99    if isfield(XmlData,'NbDtk')
100        uid_NbDtk=find(t,'ImaDoc/Camera/BurstTiming/NbDtk');
101        uid_value=children(t,uid_NbDtk);
102        if ~isempty(uid_value)
103            t=set(t,uid_value(1),'value',num2str(XmlData.NbDtk));
104        end
105    end
106    if strcmp(nomtype,'_i') && isfield(XmlData,'NbDti')
107        uid_Dti=find(t,'ImaDoc/Camera/BurstTiming/Dti');
108        t=add(t,uid_Dti,'chardata',num2str(XmlData.Dti));
109        uid_NbDti=find(t,'ImaDoc/Camera/BurstTiming/NbDti');
110        t=add(t,uid_NbDti,'chardata',num2str(XmlData.NbDti));
111%         uid_value=children(t,uid_NbDti);
112%         if ~isempty(uid_value)
113%             t=set(t,uid_value(1),'value',num2str(XmlData.NbDti));
114%         end
115        uid_NbDtj=find(t,'ImaDoc/Camera/BurstTiming/NbDtj');
116        uid_NbDtk=find(t,'ImaDoc/Camera/BurstTiming/NbDtk');
117        t=delete(t,uid_NbDtj);
118        t=delete(t,uid_NbDtk);
119        uid_Dtj=find(t,'ImaDoc/Camera/BurstTiming/Dtj');
120        uid_Dtk=find(t,'ImaDoc/Camera/BurstTiming/Dtk');
121        t=delete(t,uid_Dtj);
122        t=delete(t,uid_Dtk);
123    end
124    %%%
125   
126    save(t,[basename '.xml'])
127end
128
129%% main loop
130
131for ifile=1:nbfield1*nbfield2
132    update_waitbar(hseries.waitbar,WaitbarPos,ifile/(nbfield1*nbfield2))
133    filename=name_generator(basename,ifile-1,1,Series.FileExt,Series.NomType);
134    num_j=mod(ifile-1+first_label,nbfield2)+1;
135    num_i=floor((ifile-1+first_label)/nbfield2)+1;
136    filename_new=name_generator(basename,num_i,num_j,'.png',nomtype);
137    try
138        movefile(filename,filename_new);
139        [s,errormsg] = fileattrib(filename_new,'-w','a'); %set images to read only '-w' for all users ('a')
140        if ~s
141            msgbox_uvmat('ERROR',errormsg);
142            return
143        end
144    catch ME
145        msgbox_uvmat('ERROR',ME.message);
146        return
147    end
148end
149
150
151%'imadoc2struct': reads the xml file for image documentation
152%------------------------------------------------------------------------
153% function [s,errormsg]=imadoc2struct(ImaDoc,option)
154%
155% OUTPUT:
156% s: structure representing ImaDoc
157%   s.Heading: information about the data hierarchical structure
158%   s.Time: matrix of times
159%   s.TimeUnit
160%  s.GeometryCalib: substructure containing the parameters for geometric calibration
161% errormsg: error message
162%
163% INPUT:
164% ImaDoc: full name of the xml input file with head key ImaDoc
165% option: ='GeometryCalib': read  the data of GeometryCalib, including source point coordinates
166
167function [s,errormsg]=imadoc2struct(ImaDoc,option)
168
169%% default input and output
170if ~exist('option','var')
171    option='*';
172end
173errormsg=[];%default
174s.Heading=[];%default
175s.Time=[]; %default
176s.TimeUnit=[]; %default
177s.GeometryCalib=[];
178tsai=[];%default
179
180%% opening the xml file
181if exist(ImaDoc,'file')~=2, errormsg=[ ImaDoc ' does not exist']; return;end;%input file does not exist
182try
183    t=xmltree(ImaDoc);
184catch
185    errormsg={[ImaDoc ' is not a valid xml file']; lasterr};
186    display(errormsg);
187    return
188end
189uid_root=find(t,'/ImaDoc');
190if isempty(uid_root), errormsg=[ImaDoc ' is not an image documentation file ImaDoc']; return; end;%not an ImaDoc .xml file
191
192
193%% Heading
194uid_Heading=find(t,'/ImaDoc/Heading');
195if ~isempty(uid_Heading),
196    uid_Campaign=find(t,'/ImaDoc/Heading/Campaign');
197    uid_Exp=find(t,'/ImaDoc/Heading/Experiment');
198    uid_Device=find(t,'/ImaDoc/Heading/Device');
199    uid_Record=find(t,'/ImaDoc/Heading/Record');
200    uid_FirstImage=find(t,'/ImaDoc/Heading/ImageName');
201    s.Heading.Campaign=get(t,children(t,uid_Campaign),'value');
202    s.Heading.Experiment=get(t,children(t,uid_Exp),'value');
203    s.Heading.Device=get(t,children(t,uid_Device),'value');
204    if ~isempty(uid_Record)
205        s.Heading.Record=get(t,children(t,uid_Record),'value');
206    end
207    s.Heading.ImageName=get(t,children(t,uid_FirstImage),'value');
208end
209
210%% Camera  and timing
211if strcmp(option,'*') || strcmp(option,'Camera')
212    uid_Camera=find(t,'/ImaDoc/Camera');
213    if ~isempty(uid_Camera)
214        uid_ImageSize=find(t,'/ImaDoc/Camera/ImageSize');
215        if ~isempty(uid_ImageSize);
216            ImageSize=get(t,children(t,uid_ImageSize),'value');
217            xindex=findstr(ImageSize,'x');
218            if length(xindex)>=2
219                s.Npx=str2double(ImageSize(1:xindex(1)-1));
220                s.Npy=str2double(ImageSize(xindex(1)+1:xindex(2)-1));
221            end
222        end
223        uid_TimeUnit=find(t,'/ImaDoc/Camera/TimeUnit');
224        if ~isempty(uid_TimeUnit)
225            s.TimeUnit=get(t,children(t,uid_TimeUnit),'value');
226        end
227        uid_BurstTiming=find(t,'/ImaDoc/Camera/BurstTiming');
228        if ~isempty(uid_BurstTiming)
229            for k=1:length(uid_BurstTiming)
230                subt=branch(t,uid_BurstTiming(k));%subtree under BurstTiming
231                % reading Dtk
232                Frequency=get_value(subt,'/BurstTiming/FrameFrequency',1);
233                Dtj=get_value(subt,'/BurstTiming/Dtj',[]);
234                Dtj=Dtj/Frequency;%Dtj converted from frame unit to TimeUnit (e.g. 's')
235                NbDtj=get_value(subt,'/BurstTiming/NbDtj',1);
236                %%%% correction RDvision %%%%
237%                 NbDtj=NbDtj/numel(Dtj);
238%                 s.NbDtj=NbDtj;
239%                 %%%%
240                Dti=get_value(subt,'/BurstTiming/Dti',[]);
241                NbDti=get_value(subt,'/BurstTiming/NbDti',1);
242                 %%%% correction RDvision %%%%
243                if isempty(Dti)% series
244                     Dti=Dtj;
245                      NbDti=NbDtj;
246                     Dtj=[];
247                     s.Dti=Dti;
248                else
249                     NbDtj=NbDtj/numel(Dtj);%bursts
250                    s.NbDtj=NbDtj;
251                end
252                %%%% %%%%
253                Dti=Dti/Frequency;%Dtj converted from frame unit to TimeUnit (e.g. 's')
254
255                Time_val=get_value(subt,'/BurstTiming/Time',0);%time in TimeUnit
256                if ~isempty(Dti)
257                    Dti=reshape(Dti'*ones(1,NbDti),NbDti*numel(Dti),1); %concatene Dti vector NbDti times
258                    Time_val=[Time_val;Time_val(end)+cumsum(Dti)];%append the times defined by the intervals  Dti
259                end
260                if ~isempty(Dtj)
261                    Dtj=reshape(Dtj'*ones(1,NbDtj),1,NbDtj*numel(Dtj)); %concatene Dtj vector NbDtj times
262                    Dtj=[0 Dtj];
263                    Time_val=Time_val*ones(1,numel(Dtj))+ones(numel(Time_val),1)*cumsum(Dtj);% produce a time matrix with Dtj
264                end
265                % reading Dtk
266                Dtk=get_value(subt,'/BurstTiming/Dtk',[]);
267                NbDtk=get_value(subt,'/BurstTiming/NbDtk',1);
268                %%%% correction RDvision %%%%
269                if ~isequal(NbDtk,1)
270                    NbDtk=-1+(NbDtk+1)/(NbDti+1);
271                end
272                s.NbDtk=NbDtk;
273                %%%%%
274                if isempty(Dtk)
275                    s.Time=[s.Time;Time_val];
276                else
277                    for kblock=1:NbDtk+1
278                        Time_val_k=Time_val+(kblock-1)*Dtk;
279                        s.Time=[s.Time;Time_val_k];
280                    end
281                end
282            end
283        end
284    end
285end
286
287%% motor
288if strcmp(option,'*') || strcmp(option,'GeometryCalib')
289    uid_subtree=find(t,'/ImaDoc/TranslationMotor');
290    if length(uid_subtree)==1
291        subt=branch(t,uid_subtree);%subtree under GeometryCalib
292       [s.TranslationMotor,errormsg]=read_subtree(subt,{'Nbslice','ZStart','ZEnd'},[1 1 1],[1 1 1]);
293    end
294end
295%%  geometric calibration
296if strcmp(option,'*') || strcmp(option,'GeometryCalib')
297    uid_GeometryCalib=find(t,'/ImaDoc/GeometryCalib');
298    if ~isempty(uid_GeometryCalib)
299        if length(uid_GeometryCalib)>1
300            errormsg=['More than one GeometryCalib in ' filecivxml];
301            return
302        end
303        subt=branch(t,uid_GeometryCalib);%subtree under GeometryCalib
304        cont=get(subt,1,'contents');
305        if ~isempty(cont)
306            uid_CalibrationType=find(subt,'/GeometryCalib/CalibrationType');
307            if isequal(length(uid_CalibrationType),1)
308                tsai.CalibrationType=get(subt,children(subt,uid_CalibrationType),'value');
309            end
310            uid_CoordUnit=find(subt,'/GeometryCalib/CoordUnit');
311            if isequal(length(uid_CoordUnit),1)
312                tsai.CoordUnit=get(subt,children(subt,uid_CoordUnit),'value');
313            end
314            uid_fx_fy=find(subt,'/GeometryCalib/fx_fy');
315            focal=[];%default fro old convention (Reg Wilson)
316            if isequal(length(uid_fx_fy),1)
317                tsai.fx_fy=str2num(get(subt,children(subt,uid_fx_fy),'value'));
318            else %old convention (Reg Wilson)
319                uid_focal=find(subt,'/GeometryCalib/focal');
320                uid_dpx_dpy=find(subt,'/GeometryCalib/dpx_dpy');
321                uid_sx=find(subt,'/GeometryCalib/sx');
322                if ~isempty(uid_focal) && ~isempty(uid_dpx_dpy) && ~isempty(uid_sx)
323                    dpx_dpy=str2num(get(subt,children(subt,uid_dpx_dpy),'value'));
324                    sx=str2num(get(subt,children(subt,uid_sx),'value'));
325                    focal=str2num(get(subt,children(subt,uid_focal),'value'));
326                    tsai.fx_fy(1)=sx*focal/dpx_dpy(1);
327                    tsai.fx_fy(2)=focal/dpx_dpy(2);
328                end
329            end
330            uid_Cx_Cy=find(subt,'/GeometryCalib/Cx_Cy');
331            if ~isempty(uid_Cx_Cy)
332                tsai.Cx_Cy=str2num(get(subt,children(subt,uid_Cx_Cy),'value'));
333            end
334            uid_kc=find(subt,'/GeometryCalib/kc');
335            if ~isempty(uid_kc)
336                tsai.kc=str2double(get(subt,children(subt,uid_kc),'value'));
337            else %old convention (Reg Wilson)
338                uid_kappa1=find(subt,'/GeometryCalib/kappa1');
339                if ~isempty(uid_kappa1)&& ~isempty(focal)
340                    kappa1=str2double(get(subt,children(subt,uid_kappa1),'value'));
341                    tsai.kc=-kappa1*focal*focal;
342                end
343            end
344            uid_Tx_Ty_Tz=find(subt,'/GeometryCalib/Tx_Ty_Tz');
345            if ~isempty(uid_Tx_Ty_Tz)
346                tsai.Tx_Ty_Tz=str2num(get(subt,children(subt,uid_Tx_Ty_Tz),'value'));
347            end
348            uid_R=find(subt,'/GeometryCalib/R');
349            if ~isempty(uid_R)
350                RR=get(subt,children(subt,uid_R),'value');
351                if length(RR)==3
352                    tsai.R=[str2num(RR{1});str2num(RR{2});str2num(RR{3})];
353                end
354            end
355           
356            %look for laser plane definitions
357            uid_Angle=find(subt,'/GeometryCalib/PlaneAngle');
358            uid_Pos=find(subt,'/GeometryCalib/SliceCoord');
359            if isempty(uid_Pos)
360                uid_Pos=find(subt,'/GeometryCalib/PlanePos');%old convention
361            end
362            if ~isempty(uid_Angle)
363                tsai.PlaneAngle=str2num(get(subt,children(subt,uid_Angle),'value'));
364            end
365            if ~isempty(uid_Pos)
366                for j=1:length(uid_Pos)
367                    tsai.SliceCoord(j,:)=str2num(get(subt,children(subt,uid_Pos(j)),'value'));
368                end
369                uid_DZ=find(subt,'/GeometryCalib/SliceDZ');
370                uid_NbSlice=find(subt,'/GeometryCalib/NbSlice');
371                if ~isempty(uid_DZ) && ~isempty(uid_NbSlice)
372                    DZ=str2double(get(subt,children(subt,uid_DZ),'value'));
373                    NbSlice=get(subt,children(subt,uid_NbSlice),'value');
374                    if isequal(NbSlice,'volume')
375                        tsai.NbSlice='volume';
376                        NbSlice=NbDtj+1;
377                    else
378                        tsai.NbSlice=str2double(NbSlice);
379                    end
380                    tsai.SliceCoord=ones(NbSlice,1)*tsai.SliceCoord+DZ*(0:NbSlice-1)'*[0 0 1];
381                end
382            end   
383            tsai.SliceAngle=get_value(subt,'/GeometryCalib/SliceAngle',[0 0 0]);
384            tsai.VolumeScan=get_value(subt,'/GeometryCalib/VolumeScan','n');
385            tsai.InterfaceCoord=get_value(subt,'/GeometryCalib/InterfaceCoord',[0 0 0]);
386            tsai.RefractionIndex=get_value(subt,'/GeometryCalib/RefractionIndex',1);
387           
388            if strcmp(option,'GeometryCalib')
389                tsai.PointCoord=get_value(subt,'/GeometryCalib/SourceCalib/PointCoord',[0 0 0 0 0]);
390            end
391            s.GeometryCalib=tsai;
392        end
393    end
394end
395
396%--------------------------------------------------
397%  read a subtree
398% INPUT:
399% t: xltree
400% head_element: head elelemnt of the subtree
401% Data, structure containing
402%    .Key: element name
403%    .Type: type of element ('charg', 'float'....)
404%    .NbOccur: nbre of occurrence, NaN for un specified number
405function [s,errormsg]=read_subtree(subt,Data,NbOccur,NumTest)
406%--------------------------------------------------
407s=[];%default
408errormsg='';
409head_element=get(subt,1,'name');
410    cont=get(subt,1,'contents');
411    if ~isempty(cont)
412        for ilist=1:length(Data)
413            uid_key=find(subt,[head_element '/' Data{ilist}]);
414            if ~isequal(length(uid_key),NbOccur(ilist))
415                errormsg=['wrong number of occurence for ' Data{ilist}];
416                return
417            end
418            for ival=1:length(uid_key)
419                val=get(subt,children(subt,uid_key(ival)),'value');
420                if ~NumTest(ilist)
421                    eval(['s.' Data{ilist} '=val;']);
422                else
423                    eval(['s.' Data{ilist} '=str2double(val);'])
424                end
425            end
426        end
427    end
428
429
430%--------------------------------------------------
431%  read an xml element
432function val=get_value(t,label,default)
433%--------------------------------------------------
434val=default;
435uid=find(t,label);%find the element iud(s)
436if ~isempty(uid) %if the element named label exists
437   uid_child=children(t,uid);%find the children
438   if ~isempty(uid_child)
439       data=get(t,uid_child,'type');%get the type of child
440       if iscell(data)% case of multiple element
441           for icell=1:numel(data)
442               val_read=str2num(get(t,uid_child(icell),'value'));
443               if ~isempty(val_read)
444                   val(icell,:)=val_read;
445               end
446           end
447%           val=val';
448       else % case of unique element value
449           val_read=str2num(get(t,uid_child,'value'));
450           if ~isempty(val_read)
451               val=val_read;
452           else
453              val=get(t,uid_child,'value');%char string data
454           end
455       end
456   end
457end
Note: See TracBrowser for help on using the repository browser.