source: trunk/src/find_file_series.m @ 1010

Last change on this file since 1010 was 1009, checked in by sommeria, 7 years ago

various bugs fixed

File size: 18.8 KB
Line 
1%'find_file_series': check the content of an input file and find the corresponding file series
2%--------------------------------------------------------------------------
3% function [RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileInfo,Object,i1_input,i2_input,j1_input,j2_input]=find_file_series(FilePath,fileinput,checkxml)
4%
5% OUTPUT:
6% RootPath: path to the dir containing the input file
7% SubDir: data dir containing the input file series
8% RootFile: root file detected in fileinput, possibly modified for movies (indexing is then done on image view, not file)
9% i1_series(pair,ref_j+1, ref_i+1),i2_series,j1_series,j2_series: set of indices (i1,i2,j1,j2) sorted by ref index ref_i, ref_j, and pairindex in case of multiple pairs with the same ref
10%  (ref_i+1 is used to deal with the image index zero sometimes used)
11% NomType: nomenclature type corrected after checking the first file (problem of 0 before the number string)
12% FileInfo: structure containing info on the input files (assumed identical on the whole series)
13% FileInfo.FileType: type of file, =
14%       = 'image', usual image as recognised by Matlab
15%       = 'multimage', image series stored in a single file
16%       = 'civx', netcdf file with civx convention
17%       = 'civdata', civ data with new convention
18%       = 'netcdf' other netcdf files
19%       = 'video': movie recognised by VideoReader (e;g. avi)
20% MovieObject: video object (=[] otherwise
21% i1_input,i2_input,j1_input,j2_input: indices of the input file, or of the first file in the series if the input file does not exist
22%
23%INPUT
24% FilePath: path to the directory to be scanned
25% fileinput: name (without path) of the input file sample
26% checkxml: =1(default) take into account xml file existence to possibly include indexes in RootFile
27%           =0: do not take into account xml file existence
28
29%=======================================================================
30% Copyright 2008-2017, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
31%   http://www.legi.grenoble-inp.fr
32%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr
33%
34%     This file is part of the toolbox UVMAT.
35%
36%     UVMAT is free software; you can redistribute it and/or modify
37%     it under the terms of the GNU General Public License as published
38%     by the Free Software Foundation; either version 2 of the license,
39%     or (at your option) any later version.
40%
41%     UVMAT is distributed in the hope that it will be useful,
42%     but WITHOUT ANY WARRANTY; without even the implied warranty of
43%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
44%     GNU General Public License (see LICENSE.txt) for more details.
45%=======================================================================
46
47function [RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileInfo,MovieObject,i1_input,i2_input,j1_input,j2_input]=find_file_series(FilePath,fileinput,checkxml)
48%------------------------------------------------------------------------
49
50%% get input root name and nomenclature type
51fullfileinput=fullfile(FilePath,fileinput);% input file name with path
52[FileInfo,MovieObject]=get_file_info(fullfileinput);
53
54%% check for particular file types: images, movies, civ data
55if isfield(FileInfo,'FileIndexing') && strcmp(FileInfo.FileIndexing,'on')
56    [RootPath,SubDir,RootFile,i1_input,i2_input,j1_input,j2_input,FileExt,NomType]=fileparts_uvmat(fullfileinput);
57    %     if ~isempty(regexp(SubDir,'^level\d+$')) && exist([RootPath '.xml'],'file')
58    %         NomType='level';
59    %     end
60    i1_series=zeros(1,1,1);
61    i2_series=zeros(1,1,1);
62    j1_series=zeros(1,1,1);
63    j2_series=zeros(1,1,1);
64    checkfileindexing=1;
65else % no file indexing
66    [PathDir,RootFile]=fileparts(fullfileinput);
67    [RootPath,SubDir,DirExt]=fileparts(PathDir);
68    SubDir=[SubDir DirExt];% include part after . in the name (considered as a file extension)
69    NomType='*';
70    i1_series=[];i2_series=[];j1_series=[];j2_series=[];
71    i1_input=1;i2_input=[];j1_input=[];j2_input=[];
72    if exist(fullfileinput,'file')~=2
73        RootFile='';
74        return
75    end
76    checkfileindexing=0;
77end
78if ~exist(FilePath,'dir')
79    return % don't go further if the dir path does not exist
80end
81if checkfileindexing
82    NomTypePref='';
83    if isempty(NomType)||strcmp(NomType,'*')
84        if exist(fullfileinput,'file')
85            [tild,RootFile]=fileparts(fileinput);% case of constant name (no indexing), get the filename without its extension
86        else
87            RootFile='';
88        end
89    else
90        %% if checkxml=1, possibly include the first index in the root name, if there exists a corresponding xml file
91        if ~exist('checkxml','var')||checkxml
92            r=regexp(NomType,'^(?<tiretnum>_|\d+)','names');%look for a number or _1 at the beginning of NomType
93            if ~isempty(r) %if NomType begins by a number or _1
94                fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
95                if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
96                    rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
97                else% if a separator '_' is  detected
98                    rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
99                end
100                if ~isempty(rr)
101                    RootFile_i=[RootFile rr.i1];% new root file
102                    %look for an xml file correspoonding to the new root name
103                    if exist(fullfile(RootPath,SubDir,[RootFile_i '.xml']),'file') || (strcmp(FileExt,'.nc') && exist(fullfile(RootPath,[RootFile_i '.xml']),'file'))
104                        RootFile=RootFile_i;
105                        NomTypePref=r.tiretnum;
106                        NomType=regexprep(NomType,['^'  NomTypePref],'');
107                        i1_input=j1_input;
108                        i2_input=j2_input;
109                        j1_input=[];
110                        j2_input=[];
111                    elseif exist([RootPath '.xml'],'file')% new convention with j indices in sub-folders level0, 1...
112                        rj=regexp(SubDir,'^level(?<j1>\d+)$','names');
113                        if ~isempty(rj)
114                            j1_input=rj.j1;
115                            NomType='level';
116                            [RootPath,SubDir]=fileparts(RootPath);
117                        end
118                    end
119                end
120            end
121        end
122       
123        %% analyse the list of existing files when relevant
124        sep1='';
125        sep2='';
126        i1_str='(?<i1>)';%will set i1=[];
127        i1_star='';
128        i2_str='(?<i2>)';%will set i2=[];
129        i2_star='';
130        j1_str='(?<j1>)';%will set j1=[];
131        j1_star='';
132        j2_str='(?<j2>)';%will set j2=[];
133        j2_star='';
134        %Look for cases with letter indexing for the second index
135        r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<sep2>_?)(?<j1>[a|A])(?<j2>[b|B]?)$','names');
136        if ~isempty(r)
137            sep1=r.sep1;
138            sep2=r.sep2;
139            i1_str='(?<i1>\d+)';
140            i1_star='*';
141            if strcmp(lower(r.j1),r.j1)% lower case index
142                j1_str='(?<j1>[a-z])';
143            else
144                j1_str='(?<j1>[A-Z])'; % upper case index
145            end
146            j1_star='*';
147            if ~isempty(r.j2)
148                if strcmp(lower(r.j1),r.j1)
149                    j2_str='(?<j2>[a-z])';
150                else
151                    j2_str='(?<j2>[A-Z])';
152                end
153                j2_star='*';
154            end
155        else %numerical indexing
156            r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<i2>(-\d+)?)(?<j1>(_\d+)?)(?<j2>(-\d+)?)$','names');
157            if ~isempty(r)
158                sep1=r.sep1;
159                i1_str='(?<i1>\d+)';
160                i1_star='*';
161                if ~isempty(r.i2)
162                    i2_str='(?<i2>-\d+)';
163                    i2_star='-*';
164                end
165                if ~isempty(r.j1)
166                    j1_str='(?<j1>_\d+)';
167                    j1_star='_*';
168                end
169                if ~isempty(r.j2)
170                    j2_str='(?<j2>-\d+)';
171                    j2_star='-*';
172                end
173            end
174        end
175        %         if strcmp(NomType,'level')
176        %             star_string=[RootFile '*' FileExt];
177        %             detect_string=['^' RootFile '(?<i1>\d+)' FileExt '$'];%string used in regexp to detect file indices
178        %             wd=pwd;%current working directory
179        %             cd (FilePath)% move to the local dir to save time in the operation dir.
180        %             dirpair=dir(star_string);% look for relevant files in the file directory
181        %             cd(wd)% back to the working directory
182        %             nbpair=numel(dirpair);
183        %             i1_series=zeros(1,nbpair);
184        %             if nbpair==0% no detected file
185        %                 RootFile='';
186        %             end
187        %             % scan the list of relevant files, extract the indices
188        %             for ifile=1:nbpair
189        %                 rr=regexp(dirpair(ifile).name,detect_string,'names');
190        %                 if ~isempty(rr)
191        %                     i1_series(ifile)=str2num(rr.i1);
192        %                 end
193        %             end
194        %             % look for the list of subfolders level#
195        %             cd (RootPath)% move to the local dir to save time in the operation dir.
196        %             dirpair=dir('level*');% look for relevant subfolders named with leve#
197        %             cd(wd)
198        %             [RootPath,SubDir]=fileparts(RootPath);
199        %             nbpair=numel(dirpair);
200        %             jfile=0;
201        %             for ifile=1:nbpair
202        %                 rr=regexp(dirpair(ifile).name,'^level(?<i1>\d+)$','names');
203        %                 if ~isempty(rr)
204        %                     jfile=jfile+1;
205        %                     j1_series(jfile)=str2num(rr.i1);
206        %                 end
207        %             end
208        %             [j1_series,i1_series]=meshgrid(j1_series,i1_series);
209        %         else
210        detect_string=['^' RootFile sep1 i1_str i2_str sep2 j1_str j2_str FileExt '$'];%string used in regexp to detect file indices
211        %find the string used to extract the relevant files with the command dir
212        star_string=[RootFile sep1 i1_star i2_star sep2 j1_star j2_star FileExt];
213        wd=pwd;%current working directory
214        cd (FilePath)% move to the local dir to save time in the operation dir.
215        dirpair=dir(star_string);% look for relevant files in the file directory
216        cd(wd)
217        nbpair=numel(dirpair);
218        ref_i_list=zeros(1,nbpair);
219        ref_j_list=zeros(1,nbpair);
220        if nbpair==0% no detected file
221            RootFile='';
222        end
223        % scan the list of relevant files, extract the indices
224        for ifile=1:nbpair
225            rr=regexp(dirpair(ifile).name,detect_string,'names');
226            if ~isempty(rr)
227                i1=str2num(rr.i1);
228                i2=str2num(regexprep(rr.i2,'^-',''));
229                j1=stra2num(regexprep(rr.j1,'^_',''));
230                j2=stra2num(regexprep(rr.j2,'^-',''));
231                ref_i=i1;
232                if isempty(i2_input)
233                    if ~isempty(i2)% invalid file name if i2 does not exist in the input file
234                        break
235                    end
236                else
237                    ref_i=floor((i1+i2)/2);
238                end
239                ref_j=1;
240                if isempty(j1_input)
241                    if  ~isempty(j1)% invalid file name if j1 does not exist in the input file
242                        break
243                    end
244                else %j1_input is not empty
245                    if isempty(j1)% the detected name does not fit with the input
246                        break
247                    else
248                        ref_j=j1;
249                        if isempty(j2_input)
250                            if  ~isempty(j2)% invalid file name if j2 does not exist in the input file
251                                break
252                            end
253                        else
254                            ref_j=floor((j1+j2)/2);
255                        end
256                    end
257                end
258                % update the detected index series
259                if ~isempty(ref_i)&&~isempty(ref_j)
260                    ref_i_list(ifile)=ref_i;
261                    ref_j_list(ifile)=ref_j;
262                    nb_pairs=0;
263                    if ~isempty(i2_input)|| ~isempty(j2_input) %deals with  pairs
264                        if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1
265                            nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0));
266                        end
267                    end
268                    if i1==0
269                        i1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
270                    end
271                    if j1==0
272                        j1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
273                    end
274                    i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1;
275                    if ~isempty(i2_input)
276                        i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2;
277                    end
278                    if ~isempty(j1_input)
279                        j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
280                    end
281                    if ~isempty(j2_input)
282                        j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
283                        j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2;
284                    end
285                end
286            end
287        end
288        % look for the numerical string of the first files to update the NomType (take into account the 0 before the number)
289        max_j=max(ref_j_list);
290        if isempty(max_j)
291            ref_ij=ref_i_list;
292        else
293            ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
294        end
295        ind_select=find(ref_ij>0);
296       
297        if ~isempty(ind_select)
298            [tild,ifile_min]=min(ref_ij(ind_select));
299            [tild,tild,tild,tild,tild,tild,tild,tild,NomType]=fileparts_uvmat(dirpair(ind_select(ifile_min)).name);% update the representation of indices (number of 0 before the number)
300            NomType=regexprep(NomType,['^' NomTypePref],'');
301            %% update the file type if the input file does not exist (pb of 0001)
302            if isempty(FileInfo.FileType)
303                [FileInfo,MovieObject]=get_file_info(fullfile(FilePath,dirpair(ifile_min).name));
304            end
305        end
306        %         end
307    end
308   
309    %% set to empty array the irrelevant index series
310    if isequal(i1_series,0), i1_series=[]; end
311    if isequal(i2_series,0), i2_series=[]; end
312    if isequal(j1_series,0), j1_series=[]; end
313    if isequal(j2_series,0), j2_series=[]; end
314   
315    %% case of isolated input file, not member of an  indexed series
316    if isempty(i1_series)
317        [PathDir,RootFile]=fileparts(fullfileinput);
318        [RootPath,SubDir,DirExt]=fileparts(PathDir);
319        SubDir=[SubDir DirExt];% include part after . in the name (considered as a file extension)
320        NomType='*';
321        i2_series=[];j1_series=[];j2_series=[];
322%         i1_input=1;i2_input=[];j1_input=[];j2_input=[];
323        if ~exist(fullfileinput,'file')
324            RootFile='';
325            return
326        end
327    end
328end
329% %% detect rdvision format
330% if strcmp(FileExt,'.bin')
331%     if exist(fullfile(RootPath,SubDir,[RootFile '.seq']),'file')
332%         FileInfo.FileType='rdvision';
333%         FileInfo.SeqFile=[RootFile '.seq'];
334%     end
335% end
336
337%% introduce the frame index in case of movies or multimage type
338if isfield(FileInfo,'NumberOfFrames') && FileInfo.NumberOfFrames >1
339    if isempty(i1_series)% if there is no file index, i denotes the frame index
340        i1_series=zeros(FileInfo.NumberOfFrames+1,2);% first column =0
341        i1_series(:,2)=(0:FileInfo.NumberOfFrames)'; % second column=frame index -1
342        i1_input=1;
343        NomType='*';
344    else  % if there is a file index, j denotes the frame index while i denotes the file index
345        if ~isempty(regexp(NomType,'ab$', 'once'))% recognized as a pair
346            RootFile=fullfile_uvmat('','',RootFile,'',NomType,i1_input,i2_input,j1_input,j2_input);% restitute the root name without the detected indices
347            i1_series=zeros(FileInfo.NumberOfFrames+1,2);% first column =0
348            i1_series(:,2)=(0:FileInfo.NumberOfFrames)'; % second column=frame index -1
349            j1_series=[];
350            i1_input=1;
351            NomType='*';
352        else
353            i1_series=i1_series(:,2)*ones(1,FileInfo.NumberOfFrames);%
354            i1_series=[zeros(size(i1_series,1),1) i1_series];
355            j1_series=ones(size(i1_series,1),1)*(1:FileInfo.NumberOfFrames);%
356            j1_series=[zeros(size(i1_series,1),1) j1_series];
357            %  include the first index in the root name
358            r=regexp(NomType,'^(?<tiretnum>_?\d+)','names');%look for a number or _1 at the beginning of NomType
359            if ~isempty(r)
360                fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
361                if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
362                    rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
363                else% if a separator '_' is  detected
364                    rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
365                end
366                if ~isempty(rr)
367                    j1_input=1;
368                    j2_input=[];
369                end
370            end
371        end
372    end
373end
374
375%% sort pairs by decreasing index differences in case of multiple pairs at the same reference index
376if size(i2_series,3)>1 %pairs i1 -i2
377    diff_index=abs(i2_series-i1_series);
378    [tild,ind_pair]=sort(diff_index,3,'descend');
379    for ref_i=1:size(i1_series,1)
380        for ref_j=1:size(j1_series,2)
381            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
382            i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
383            if ~isempty(j1_series)
384                j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
385            end
386        end
387    end
388elseif size(j2_series,3)>1 %pairs j1 -j2
389    diff_index=abs(j2_series-j1_series);
390    [tild,ind_pair]=sort(diff_index,3,'descend');
391    for ref_i=1:size(i1_series,1)
392        for ref_j=1:size(j1_series,2)
393            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
394            if ~isempty(i2_series)
395                i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
396            end
397            j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
398            j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
399        end
400    end
401end
402i1_series=permute(i1_series,[3 2 1]);% permute dimensions
403i2_series=permute(i2_series,[3 2 1]);% permute dimensions
404j1_series=permute(j1_series,[3 2 1]);% permute dimensions
405j2_series=permute(j2_series,[3 2 1]);% permute dimensions
Note: See TracBrowser for help on using the repository browser.