source: trunk/src/find_file_series.m @ 1012

Last change on this file since 1012 was 1011, checked in by sommeria, 7 years ago

series REFRESH improved

File size: 17.0 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
55%if 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;
65%else % 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;
77%end
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 
176        detect_string=['^' RootFile sep1 i1_str i2_str sep2 j1_str j2_str FileExt '$'];%string used in regexp to detect file indices
177        %find the string used to extract the relevant files with the command dir
178        star_string=[RootFile sep1 i1_star i2_star sep2 j1_star j2_star FileExt];
179        wd=pwd;%current working directory
180        cd (FilePath)% move to the local dir to save time in the operation dir.
181        dirpair=dir(star_string);% look for relevant files in the file directory
182        cd(wd)
183        nbpair=numel(dirpair);
184        ref_i_list=zeros(1,nbpair);
185        ref_j_list=zeros(1,nbpair);
186        if nbpair==0% no detected file
187            RootFile='';
188        end
189        % scan the list of relevant files, extract the indices
190        for ifile=1:nbpair
191            rr=regexp(dirpair(ifile).name,detect_string,'names');
192            if ~isempty(rr)
193                i1=str2num(rr.i1);
194                i2=str2num(regexprep(rr.i2,'^-',''));
195                j1=stra2num(regexprep(rr.j1,'^_',''));
196                j2=stra2num(regexprep(rr.j2,'^-',''));
197                ref_i=i1;
198                if isempty(i2_input)
199                    if ~isempty(i2)% invalid file name if i2 does not exist in the input file
200                        break
201                    end
202                else
203                    ref_i=floor((i1+i2)/2);
204                end
205                ref_j=1;
206                if isempty(j1_input)
207                    if  ~isempty(j1)% invalid file name if j1 does not exist in the input file
208                        break
209                    end
210                else %j1_input is not empty
211                    if isempty(j1)% the detected name does not fit with the input
212                        break
213                    else
214                        ref_j=j1;
215                        if isempty(j2_input)
216                            if  ~isempty(j2)% invalid file name if j2 does not exist in the input file
217                                break
218                            end
219                        else
220                            ref_j=floor((j1+j2)/2);
221                        end
222                    end
223                end
224                % update the detected index series
225                if ~isempty(ref_i)&&~isempty(ref_j)
226                    ref_i_list(ifile)=ref_i;
227                    ref_j_list(ifile)=ref_j;
228                    nb_pairs=0;
229                    if ~isempty(i2_input)|| ~isempty(j2_input) %deals with  pairs
230                        if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1
231                            nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0));
232                        end
233                    end
234                    if i1==0
235                        i1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
236                    end
237                    if j1==0
238                        j1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
239                    end
240                    i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1;
241                    if ~isempty(i2_input)
242                        i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2;
243                    end
244                    if ~isempty(j1_input)
245                        j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
246                    end
247                    if ~isempty(j2_input)
248                        j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
249                        j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2;
250                    end
251                end
252            end
253        end
254        % look for the numerical string of the first files to update the NomType (take into account the 0 before the number)
255        max_j=max(ref_j_list);
256        if isempty(max_j)
257            ref_ij=ref_i_list;
258        else
259            ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
260        end
261        ind_select=find(ref_ij>0);
262       
263        if ~isempty(ind_select)
264            [tild,ifile_min]=min(ref_ij(ind_select));
265            [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)
266            NomType=regexprep(NomType,['^' NomTypePref],'');
267            %% update the file type if the input file does not exist (pb of 0001)
268            if isempty(FileInfo.FileType)
269                [FileInfo,MovieObject]=get_file_info(fullfile(FilePath,dirpair(ifile_min).name));
270            end
271        end
272        %         end
273    end
274   
275    %% set to empty array the irrelevant index series
276    if isequal(i1_series,0), i1_series=[]; end
277    if isequal(i2_series,0), i2_series=[]; end
278    if isequal(j1_series,0), j1_series=[]; end
279    if isequal(j2_series,0), j2_series=[]; end
280   
281    %% case of isolated input file, not member of an  indexed series
282    if isempty(i1_series)
283        [PathDir,RootFile]=fileparts(fullfileinput);
284        [RootPath,SubDir,DirExt]=fileparts(PathDir);
285        SubDir=[SubDir DirExt];% include part after . in the name (considered as a file extension)
286        NomType='*';
287        i2_series=[];j1_series=[];j2_series=[];
288%         i1_input=1;i2_input=[];j1_input=[];j2_input=[];
289        if ~exist(fullfileinput,'file')
290            RootFile='';
291            return
292        end
293    end
294end
295% %% detect rdvision format
296% if strcmp(FileExt,'.bin')
297%     if exist(fullfile(RootPath,SubDir,[RootFile '.seq']),'file')
298%         FileInfo.FileType='rdvision';
299%         FileInfo.SeqFile=[RootFile '.seq'];
300%     end
301% end
302
303%% introduce the frame index in case of movies or multimage type
304if isfield(FileInfo,'NumberOfFrames') && FileInfo.NumberOfFrames >1
305    if isempty(i1_series)% if there is no file index, i denotes the frame index
306        i1_series=zeros(FileInfo.NumberOfFrames+1,2);% first column =0
307        i1_series(:,2)=(0:FileInfo.NumberOfFrames)'; % second column=frame index -1
308        i1_input=1;
309        NomType='*';
310    else  % if there is a file index, j denotes the frame index while i denotes the file index
311        if ~isempty(regexp(NomType,'ab$', 'once'))% recognized as a pair
312            RootFile=fullfile_uvmat('','',RootFile,'',NomType,i1_input,i2_input,j1_input,j2_input);% restitute the root name without the detected indices
313            i1_series=zeros(FileInfo.NumberOfFrames+1,2);% first column =0
314            i1_series(:,2)=(0:FileInfo.NumberOfFrames)'; % second column=frame index -1
315            j1_series=[];
316            i1_input=1;
317            NomType='*';
318        else
319            i1_series=i1_series(:,2)*ones(1,FileInfo.NumberOfFrames);%
320            i1_series=[zeros(size(i1_series,1),1) i1_series];
321            j1_series=ones(size(i1_series,1),1)*(1:FileInfo.NumberOfFrames);%
322            j1_series=[zeros(size(i1_series,1),1) j1_series];
323            %  include the first index in the root name
324            r=regexp(NomType,'^(?<tiretnum>_?\d+)','names');%look for a number or _1 at the beginning of NomType
325            if ~isempty(r)
326                fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
327                if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
328                    rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
329                else% if a separator '_' is  detected
330                    rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
331                end
332                if ~isempty(rr)
333                    j1_input=1;
334                    j2_input=[];
335                end
336            end
337        end
338    end
339end
340
341%% sort pairs by decreasing index differences in case of multiple pairs at the same reference index
342if size(i2_series,3)>1 %pairs i1 -i2
343    diff_index=abs(i2_series-i1_series);
344    [tild,ind_pair]=sort(diff_index,3,'descend');
345    for ref_i=1:size(i1_series,1)
346        for ref_j=1:size(j1_series,2)
347            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
348            i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
349            if ~isempty(j1_series)
350                j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
351            end
352        end
353    end
354elseif size(j2_series,3)>1 %pairs j1 -j2
355    diff_index=abs(j2_series-j1_series);
356    [tild,ind_pair]=sort(diff_index,3,'descend');
357    for ref_i=1:size(i1_series,1)
358        for ref_j=1:size(j1_series,2)
359            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
360            if ~isempty(i2_series)
361                i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
362            end
363            j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
364            j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
365        end
366    end
367end
368i1_series=permute(i1_series,[3 2 1]);% permute dimensions
369i2_series=permute(i2_series,[3 2 1]);% permute dimensions
370j1_series=permute(j1_series,[3 2 1]);% permute dimensions
371j2_series=permute(j2_series,[3 2 1]);% permute dimensions
Note: See TracBrowser for help on using the repository browser.