source: trunk/src/series/rdvision_check_time.m @ 1179

Last change on this file since 1179 was 1179, checked in by sommeria, 4 weeks ago

a few bug repairs and cleaning

File size: 15.4 KB
Line 
1%'extract_rdvision': relabel an image series with two indices, and correct errors from the RDvision transfer program
2%------------------------------------------------------------------------
3% function ParamOut=extract_rdvision(Param)
4%------------------------------------------------------------------------
5%
6%%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %
7%%%%%%%%%%%%%%%%%%%%%%%%%%
8%
9%OUTPUT
10% ParamOut: sets options in the GUI series.fig needed for the function
11%
12%INPUT:
13% In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
14% In batch mode, Param is the name of the corresponding xml file containing the same information
15% when Param.Action.RUN=0 (as activated when the current Action is selected
16% in series), the function ouput paramOut set the activation of the needed GUI elements
17%
18% Param contains the elements:(use the menu bar command 'export/GUI config' in series to
19% see the current structure Param)
20%    .InputTable: cell of input file names, (several lines for multiple input)
21%                      each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
22%    .OutputSubDir: name of the subdirectory for data outputs
23%    .OutputDirExt: directory extension for data outputs
24%    .Action: .ActionName: name of the current activated function
25%             .ActionPath:   path of the current activated function
26%             .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled   Matlab fct
27%             .RUN =0 for GUI input, =1 for function activation
28%             .RunMode='local','background', 'cluster': type of function  use
29%             
30%    .IndexRange: set the file or frame indices on which the action must be performed
31%    .FieldTransform: .TransformName: name of the selected transform function
32%                     .TransformPath:   path  of the selected transform function
33%    .InputFields: sub structure describing the input fields withfields
34%              .FieldName: name(s) of the field
35%              .VelType: velocity type
36%              .FieldName_1: name of the second field in case of two input series
37%              .VelType_1: velocity type of the second field in case of two input series
38%              .Coord_y: name of y coordinate variable
39%              .Coord_x: name of x coordinate variable
40%    .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
41%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42
43%=======================================================================
44% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
45%   http://www.legi.grenoble-inp.fr
46%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
47%
48%     This file is part of the toolbox UVMAT.
49%
50%     UVMAT is free software; you can redistribute it and/or modify
51%     it under the terms of the GNU General Public License as published
52%     by the Free Software Foundation; either version 2 of the license,
53%     or (at your option) any later version.
54%
55%     UVMAT is distributed in the hope that it will be useful,
56%     but WITHOUT ANY WARRANTY; without even the implied warranty of
57%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
58%     GNU General Public License (see LICENSE.txt) for more details.
59%=======================================================================
60
61function ParamOut=extract_rdvision(Param) %default output=relabel_i_j(Param)
62
63%% set the input elements needed on the GUI series when the action is selected in the menu ActionName
64if isstruct(Param) && isequal(Param.Action.RUN,0)
65    ParamOut.AllowInputSort='off';...% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
66    ParamOut.WholeIndexRange='on';...% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
67    ParamOut.NbSlice=1; ...%nbre of slices, 1 prevents splitting in several processes, ('off' by default)
68    ParamOut.VelType='off';...% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
69    ParamOut.FieldName='off';...% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
70    ParamOut.FieldTransform = 'off';...%can use a transform function
71    ParamOut.ProjObject='off';...%can use projection object(option 'off'/'on',
72    ParamOut.Mask='off';...%can use mask option   (option 'off'/'on', 'off' by default)
73     ParamOut.OutputDirExt='.extract';%set the output dir extension
74    ParamOut.OutputSubDirMode='one'; %output folder given by the folder name of the first input line
75     % detect the set of image folder
76    RootPath=Param.InputTable{1,1};
77    ListStruct=dir(RootPath);   
78    ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray
79    check_bad=strcmp('.',ListCells(1,:))|strcmp('..',ListCells(1,:));%detect the dir '.' to exclude it
80    check_dir=cell2mat(ListCells(4,:));% =1 for directories, =0 for files
81    ListDir=ListCells(1,find(check_dir & ~check_bad));
82%     InputTable=cell(numel(ListDir),5);
83%     InputTable(:,2)=ListDir';
84    isel=0;
85    InputTable=Param.InputTable;
86    for ilist=1:numel(ListDir)
87        ListStructSub=dir(fullfile(RootPath,ListDir{ilist}));
88        ListCellSub=struct2cell(ListStructSub);% transform dir struct to a cell arrray
89        detect_seq=regexp(ListCellSub(1,:),'.seq$');
90        seq_index=find(~cellfun('isempty',detect_seq),1);
91        if ~isempty(seq_index)
92%             msgbox_uvmat('ERROR',['not seq file in ' ListDir{ilist} ': please check the input folders'])
93%         else
94           isel=isel+1;
95           InputTable{isel,1}=RootPath;
96           InputTable{isel,2}=ListDir{ilist};
97            RootFile=regexprep(ListCellSub{1,seq_index},'.seq$','');
98            InputTable{isel,3}=RootFile;
99        InputTable{isel,4}='*';
100        InputTable{isel,5}='.seq';
101    end
102    end
103    hseries=findobj(allchild(0),'Tag','series');% find the parent GUI 'series'
104    hhseries=guidata(hseries); %handles of the elements in 'series'
105    set(hhseries.InputTable,'Data',InputTable)
106    ParamOut.ActionInput.LogPath=RootPath;% indicate the path for the output info: 0_LOG ....
107return
108end
109
110ParamOut=[];
111%%%%%%%%%%%% STANDARD PART  %%%%%%%%%%%%
112%% read input parameters from an xml file if input is a file name (batch mode)
113checkrun=1;
114if ischar(Param)
115    Param=xml2struct(Param);% read Param as input file (batch case)
116    checkrun=0;
117end
118hseries=findobj(allchild(0),'Tag','series');
119RUNHandle=findobj(hseries,'Tag','RUN');%handle of RUN button in GUI series
120WaitbarHandle=findobj(hseries,'Tag','Waitbar');%handle of waitbar in GUI series
121
122%% root input file(s) and type
123RootPath=Param.InputTable{1,1};
124if ~isempty(find(~strcmp(RootPath,Param.InputTable(:,1))))% if the Rootpath for each camera are not identical
125    disp_uvmat('ERROR','Rootpath for all cameras must be identical',checkrun)
126    return
127end
128
129% get the set of input file names (cell array filecell), and the lists of
130% input file or frame indices i1_series,i2_series,j1_series,j2_series
131[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
132
133%OutputDir=[Param.OutputSubDir Param.OutputDirExt];
134 
135% numbers of slices and file indices
136nbfield_j=size(i1_series{1},1); %nb of fields for the j index (bursts or volume slices)
137nbfield_i=size(i1_series{1},2); %nb of fields for the i index
138nbfield=nbfield_j*nbfield_i; %total number of fields
139
140%determine the file type on each line from the first input file
141
142FileInfo=get_file_info(filecell{1,1});
143if strcmp(FileInfo.FileType,'rdvision')
144    if ~isequal(FileInfo.NumberOfFrames,nbfield)
145        disp_uvmat('WARNING',['the whole series of ' num2str(FileInfo.NumberOfFrames) ' images must be extracted at once'],checkrun)
146        %rmfield(OutputDir)
147%         return
148    end
149    %% interactive input of specific parameters (for RDvision system)
150    display('converting images from RDvision system...')
151else
152    disp_uvmat('ERROR','the input is not from rdvision: a .seq or .sqb file must be opened',checkrun)
153    return
154end
155%t=xmltree;
156
157%save(t,fullfile(RootPath,'Running.xml'))%create an xml file to indicate that processing takes place
158
159%% calibration data and timing: read the ImaDoc files
160mode=''; %default
161timecell={};
162itime=0;
163NbSlice_calib={};
164
165checkpreserve=0;% if =1, will npreserve the original images, else it erases them at the end
166for iview=1:size(Param.InputTable,1)
167    for iview_xml=1:size(Param.InputTable,1)% look for the xml files in the different data directories
168        filexml=[fullfile(RootPath,Param.InputTable{iview_xml,2},Param.InputTable{iview,3}) '.xml'];%new convention: xml at the level of the image folder
169        if exist(filexml,'file')
170            break
171        end
172    end
173    if ~exist(filexml,'file')
174        disp_uvmat('ERROR',[filexml ' missing'],checkrun)
175        return
176    end
177 
178    newxml=fullfile(RootPath,Param.InputTable{iview,3});
179    newxml=regexprep(newxml,'_Master_Dalsa_4M180$','');%suppress '_Master_Dalsa_4M180'
180    newxml=[newxml '.xml'];
181   
182    %% get the names of .seq and .sqb files
183    switch Param.InputTable{iview,5}
184        case {'.seq','.sqb'}
185            filename_seq=fullfile(RootPath,Param.InputTable{iview,2},[Param.InputTable{iview,3} '.seq']);
186            filename_sqb=fullfile(RootPath,Param.InputTable{iview,2},[Param.InputTable{iview,3} '.sqb']);
187            logdir=[Param.OutputSubDir Param.OutputDirExt];
188            [success,errormsg] = copyfile(filename_seq,[fullfile(RootPath,logdir,Param.InputTable{iview,3}) '.seq']); %copy the seq file in the upper folder
189            [success,errormsg] = copyfile(filename_sqb,[fullfile(RootPath,logdir,Param.InputTable{iview,3}) '.sqb']); %copy the sqb file in the upper folder
190        otherwise
191            errormsg='input file extension must be .seq or .sqb';
192    end
193    if ~exist(filename_seq,'file')
194        errormsg=[filename_seq ' does not exist'];
195    end
196    if ~isempty(errormsg)
197        disp_uvmat('ERRROR',errormsg,checkrun);
198        return
199    end
200   
201
202    %% get data from .seq file
203    s=ini2struct(filename_seq);
204    SeqData=s.sequenceSettings;
205    SeqData.width=str2double(SeqData.width);
206    SeqData.height=str2double(SeqData.height);
207    SeqData.bytesperpixel=str2double(SeqData.bytesperpixel);
208    SeqData.nb_frames=str2double(s.sequenceSettings.numberoffiles);
209    if isempty(SeqData.binrepertoire)%used when binrepertoire empty, strange feature of rdvision
210        SeqData.binrepertoire=regexprep(s.sequenceSettings.bindirectory,'\\$','');%tranform Windows notation to Linux
211        SeqData.binrepertoire=regexprep(SeqData.binrepertoire,'\','/');
212        [tild,binrepertoire,DirExt]=fileparts(SeqData.binrepertoire);
213        SeqData.binrepertoire=[SeqData.binrepertoire DirExt];
214    end
215   
216   
217   
218    %% reading the .sqb file
219    m = memmapfile(filename_sqb,'Format', { 'uint32' [1 1] 'offset'; ...
220        'uint32' [1 1] 'garbage1';...
221        'double' [1 1] 'timestamp';...
222        'uint32' [1 1] 'file_idx';...
223        'uint32' [1 1] 'garbage2' },'Repeat',SeqData.nb_frames);
224   
225    %%%%%%%BRICOLAGE in case of unreadable .sqb file: remplace lecture du fichier
226    %         ind=[111 114:211];%indices of bin files
227    %         w=1024;%w=width of images in pixels
228    %         h=1024;%h=height of images in pixels
229    %         bpp=2;% nbre of bytes per pixel
230    %         lengthimage=w*h*bpp;% lengthof an image record on the binary file
231    %         nbimages=32; %nbre of images of each camera in a bin file
232    %         for ii=1:32*numel(ind)
233    %             data(ii).offset=mod(ii-1,32)*2*lengthimage+lengthimage;%Dalsa_2
234    %             %data(ii).offset=mod(ii-1,32)*2*lengthimage;%Dalsa_1
235    %             data(ii).file_idx=ind(ceil(ii/32));
236    %             data(ii).timestamp=0.2*(ii-1);
237    %         end
238    %         m.Data=data;
239    %%%%%%%
240    timestamp=zeros(1,numel(m.Data));
241    for ii=1: numel(m.Data)
242        timestamp(ii)=m.Data(ii).timestamp;
243    end
244    copyfile_modif(filexml,timestamp,newxml)
245 
246    [XmlData,errormsg]=imadoc2struct(newxml);% check reading of the new xml file
247    if ~isempty(errormsg)
248        disp(errormsg)
249        return
250    end
251    difftime=XmlData.Time(2:end,2:end)-(reshape(timestamp,nbfield2,[]))';
252    disp(['time from xml and timestamp differ by ' num2str(max(max(abs(difftime))))])
253    if max(abs(difftime))>0.01
254        checkpreserve=1;% will not erase the initial files, possibility of error
255    end
256   
257        %% checking consistency with the xml file
258    if ~isequal(SeqData.nb_frames,numel(timestamp))
259        disp_uvmat('ERRROR',['inconsistent number of images ' num2str(SeqData.nb_frames) ' with respect to the xml file: ' num2str(numel(timestamp))] ,checkrun);
260        return
261    end   
262   
263    disp ('max time difference xml and timestamps (IN SEC.)')
264    max(abs(difftime))
265
266end
267   
268   
269 
270function [nbfield2,msg]=copyfile_modif(filexml,timestamp,newxml)
271msg='';
272t=xmltree(filexml);
273
274%% correct NbDtj
275uid_NbDtj=find(t,'ImaDoc/Camera/BurstTiming/NbDtj');
276uid_content=get(t,uid_NbDtj,'contents');
277t=set(t,uid_content,'value','1');% set NbDtj to 1 (correct error in the xml file)
278
279%% check Dtj
280uid_Dtj=find(t,'ImaDoc/Camera/BurstTiming/Dtj');
281uid_content=get(t,uid_Dtj,'contents');
282Dtjstring=get(t,uid_content,'value');
283if isempty(Dtjstring)
284    timestamp=timestamp';
285    nbfield2=1;
286else
287    Dtj=str2num(get(t,uid_content,'value'));
288    nbfield2=numel(Dtj)+1;
289    timestamp=(reshape(timestamp,nbfield2,[]))';
290    diff_Dtj=diff(timestamp(1,:))-Dtj;
291    if max(abs(diff_Dtj))>min(Dtj)/1000
292        disp(['Dtj from xml file differs from time stamp by ' num2str(max(abs(diff_Dtj))) ', '])%'
293    else
294        disp('Dtj OK');
295    end
296end
297%% correct NbDti
298NbDti=size(timestamp,1)-1; %default for series or burst
299uid_motor_nbslice=find(t,'ImaDoc/TranslationMotor/Nbslice');
300uid_NbDtk=find(t,'ImaDoc/Camera/BurstTiming/NbDtk');
301if ~isempty(uid_motor_nbslice)&& ~isempty(uid_NbDtk)
302    uid_content=get(t,uid_motor_nbslice,'contents');
303    NbSlice=str2num(get(t,uid_content,'value'));
304    NbDti=NbSlice-1;
305    uid_NbDti=find(t,'ImaDoc/Camera/BurstTiming/NbDti');
306    uid_content=get(t,uid_NbDti,'contents');
307    t=set(t,uid_content,'value',num2str(NbDti));
308end
309
310%% adjust Dti
311uid_Dti=find(t,'ImaDoc/Camera/BurstTiming/Dti');
312uid_content=get(t,uid_Dti,'contents');
313Dti=str2num(get(t,uid_content,'value'));
314Dti_stamp=(timestamp(1+NbDti,1)-timestamp(1,1))/NbDti;
315if abs(Dti_stamp-Dti)>Dti/1000
316    disp([msg 'Dti from xml file corrected by ' num2str(Dti_stamp-Dti) ', ']);%'
317else
318    disp('Dti OK')
319end
320t=set(t,uid_content,'value',num2str(Dti_stamp));
321
322%% adjust Dtk
323uid_Dtk=find(t,'ImaDoc/Camera/BurstTiming/Dtk');
324if ~isempty(uid_Dtk)
325    uid_content_Dtk=get(t,uid_Dtk,'contents');
326    Dtk=str2num(get(t,uid_content_Dtk,'value'));
327    uid_content_NbDtk=get(t,uid_NbDtk,'contents');
328    NbDtk=str2num(get(t,uid_content_NbDtk,'value'));
329    Dtk_stamp=(timestamp(end-NbDti,1)-timestamp(1,1))/NbDtk;
330    if abs(Dtk_stamp-Dtk)>Dtk/1000
331        disp(['Dtk from xml file corrected by ' num2str(Dtk_stamp-Dtk)]);
332    else
333        disp('Dtk OK')
334    end
335    t=set(t,uid_content_Dtk,'value',num2str(Dtk_stamp));
336end
337
338save(t,newxml)
339[success,errormsg] = fileattrib(newxml,'+w','g');% allow writing access for the group of users
340if success==0
341    disp({['warning: unable to set group write access to ' newxml ':']; errormsg});%error message for directory creation
342    msg=errormsg;
343end
344
345
346
Note: See TracBrowser for help on using the repository browser.