source: trunk/src/find_file_series.m @ 997

Last change on this file since 997 was 977, checked in by g7moreau, 8 years ago
  • Update Copyright 2008-2017 notice
File size: 18.5 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
55checkfileindexing=0;
56if isfield(FileInfo,'FileIndexing') && strcmp(FileInfo.FileIndexing,'on')
57    [RootPath,SubDir,RootFile,i1_input,i2_input,j1_input,j2_input,FileExt,NomType]=fileparts_uvmat(fullfileinput);
58    if ~isempty(regexp(SubDir,'^level\d+$')) && exist([RootPath '.xml'],'file')
59        NomType='level';
60    end
61    i1_series=zeros(1,1,1);
62    i2_series=zeros(1,1,1);
63    j1_series=zeros(1,1,1);
64    j2_series=zeros(1,1,1);
65    checkfileindexing=1;
66else % no file indexing
67    [PathDir,RootFile]=fileparts(fullfileinput);
68    [RootPath,SubDir,DirExt]=fileparts(PathDir);
69    SubDir=[SubDir DirExt];% include part after . in the name (considered as a file extension)
70    NomType='*';
71    i1_series=[];i2_series=[];j1_series=[];j2_series=[];
72    i1_input=1;i2_input=[];j1_input=[];j2_input=[];
73    if exist(fullfileinput,'file')~=2
74        RootFile='';
75        return
76    end
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%             i1_series=reshape(i1_series,1,[]);
210%             j1_series=reshape(j1_series,1,[]);
211        else
212            detect_string=['^' RootFile sep1 i1_str i2_str sep2 j1_str j2_str FileExt '$'];%string used in regexp to detect file indices
213            %find the string used to extract the relevant files with the command dir
214            star_string=[RootFile sep1 i1_star i2_star sep2 j1_star j2_star FileExt];
215            wd=pwd;%current working directory
216            cd (FilePath)% move to the local dir to save time in the operation dir.
217            dirpair=dir(star_string);% look for relevant files in the file directory
218            cd(wd)
219            nbpair=numel(dirpair);
220            ref_i_list=zeros(1,nbpair);
221            ref_j_list=zeros(1,nbpair);
222            if nbpair==0% no detected file
223                RootFile='';
224            end
225            % scan the list of relevant files, extract the indices
226            for ifile=1:nbpair
227                rr=regexp(dirpair(ifile).name,detect_string,'names');
228                if ~isempty(rr)
229                    i1=str2num(rr.i1);
230                    i2=str2num(regexprep(rr.i2,'^-',''));
231                    j1=stra2num(regexprep(rr.j1,'^_',''));
232                    j2=stra2num(regexprep(rr.j2,'^-',''));
233                    ref_i=i1;
234                    if isempty(i2_input)
235                        if ~isempty(i2)% invalid file name if i2 does not exist in the input file
236                            break
237                        end
238                    else
239                        ref_i=floor((i1+i2)/2);
240                    end
241                    ref_j=1;
242                    if isempty(j1_input)
243                        if  ~isempty(j1)% invalid file name if j1 does not exist in the input file
244                            break
245                        end
246                    else %j1_input is not empty
247                        if isempty(j1)% the detected name does not fit with the input
248                            break
249                        else
250                            ref_j=j1;
251                            if isempty(j2_input)
252                                if  ~isempty(j2)% invalid file name if j2 does not exist in the input file
253                                    break
254                                end
255                            else
256                                ref_j=floor((j1+j2)/2);
257                            end
258                        end
259                    end
260                    % update the detected index series
261                    if ~isempty(ref_i)&&~isempty(ref_j)
262                        ref_i_list(ifile)=ref_i;
263                        ref_j_list(ifile)=ref_j;
264                        nb_pairs=0;
265                        if ~isempty(i2_input)|| ~isempty(j2_input) %deals with  pairs
266                            if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1
267                                nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0));
268                            end
269                        end
270                        if i1==0
271                            i1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
272                        end
273                        if j1==0
274                            j1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
275                        end
276                        i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1;
277                        if ~isempty(i2_input)
278                            i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2;
279                        end
280                        if ~isempty(j1_input)
281                            j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
282                        end
283                        if ~isempty(j2_input)
284                            j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
285                            j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2;
286                        end
287                    end
288                end
289            end
290            % look for the numerical string of the first files to update the NomType (take into account the 0 before the number)
291            max_j=max(ref_j_list);
292            if isempty(max_j)
293                ref_ij=ref_i_list;
294            else
295                ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
296            end
297            ind_select=find(ref_ij>0);
298           
299            if ~isempty(ind_select)
300                [tild,ifile_min]=min(ref_ij(ind_select));
301                [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)
302                NomType=regexprep(NomType,['^' NomTypePref],'');
303                %% update the file type if the input file does not exist (pb of 0001)
304                if isempty(FileInfo.FileType)
305                    [FileInfo,MovieObject]=get_file_info(fullfile(FilePath,dirpair(ifile_min).name));
306                end
307            end
308        end
309    end
310
311%% set to empty array the irrelevant index series
312if isequal(i1_series,0), i1_series=[]; end
313if isequal(i2_series,0), i2_series=[]; end
314if isequal(j1_series,0), j1_series=[]; end
315if isequal(j2_series,0), j2_series=[]; end
316end
317% %% detect rdvision format
318% if strcmp(FileExt,'.bin')
319%     if exist(fullfile(RootPath,SubDir,[RootFile '.seq']),'file')
320%         FileInfo.FileType='rdvision';
321%         FileInfo.SeqFile=[RootFile '.seq'];
322%     end
323% end
324
325%% introduce the frame index in case of movies or multimage type
326if isfield(FileInfo,'NumberOfFrames') && FileInfo.NumberOfFrames >1
327    if isempty(i1_series)% if there is no file index, i denotes the frame index
328        i1_series=zeros(FileInfo.NumberOfFrames+1,2);% first column =0
329        i1_series(:,2)=(0:FileInfo.NumberOfFrames)'; % second column=frame index -1
330        i1_input=1;
331        NomType='*';
332    else  % if there is a file index, j denotes the frame index while i denotes the file index
333        if ~isempty(regexp(NomType,'ab$', 'once'))% recognized as a pair
334            RootFile=fullfile_uvmat('','',RootFile,'',NomType,i1_input,i2_input,j1_input,j2_input);% restitute the root name without the detected indices       
335            i1_series=zeros(FileInfo.NumberOfFrames+1,2);% first column =0
336            i1_series(:,2)=(0:FileInfo.NumberOfFrames)'; % second column=frame index -1
337            j1_series=[];
338            i1_input=1;
339            NomType='*';
340        else
341            i1_series=i1_series(:,2)*ones(1,FileInfo.NumberOfFrames);%
342            i1_series=[zeros(size(i1_series,1),1) i1_series];
343            j1_series=ones(size(i1_series,1),1)*(1:FileInfo.NumberOfFrames);%
344            j1_series=[zeros(size(i1_series,1),1) j1_series];
345            %  include the first index in the root name
346            r=regexp(NomType,'^(?<tiretnum>_?\d+)','names');%look for a number or _1 at the beginning of NomType
347            if ~isempty(r)
348                fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
349                if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
350                    rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
351                else% if a separator '_' is  detected
352                    rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
353                end
354                if ~isempty(rr)
355                    j1_input=1;
356                    j2_input=[];
357                end
358            end
359        end
360    end
361end
362
363%% sort pairs by decreasing index differences in case of multiple pairs at the same reference index
364if size(i2_series,3)>1 %pairs i1 -i2
365    diff_index=abs(i2_series-i1_series);
366    [tild,ind_pair]=sort(diff_index,3,'descend');
367    for ref_i=1:size(i1_series,1)
368        for ref_j=1:size(j1_series,2)
369            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
370            i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
371            if ~isempty(j1_series)
372                j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
373            end
374        end
375    end
376elseif size(j2_series,3)>1 %pairs j1 -j2
377    diff_index=abs(j2_series-j1_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            if ~isempty(i2_series)
383                i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
384            end
385            j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
386            j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
387        end
388    end
389end
390i1_series=permute(i1_series,[3 2 1]);% permute dimensions
391i2_series=permute(i2_series,[3 2 1]);% permute dimensions
392j1_series=permute(j1_series,[3 2 1]);% permute dimensions
393j2_series=permute(j2_series,[3 2 1]);% permute dimensions
Note: See TracBrowser for help on using the repository browser.