source: trunk/src/find_file_series.m @ 962

Last change on this file since 962 was 961, checked in by sommeria, 5 years ago

NomType? level introduced

File size: 18.3 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-2016, 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    i1_series=zeros(1,1,1);
59    i2_series=zeros(1,1,1);
60    j1_series=zeros(1,1,1);
61    j2_series=zeros(1,1,1);
62    checkfileindexing=1;
63else % no file indexing
64    [PathDir,RootFile]=fileparts(fullfileinput);
65    [RootPath,SubDir,DirExt]=fileparts(PathDir);
66    SubDir=[SubDir DirExt];% include part after . in the name (considered as a file extension)
67    NomType='*';
68    i1_series=[];i2_series=[];j1_series=[];j2_series=[];
69    i1_input=1;i2_input=[];j1_input=[];j2_input=[];
70    if exist(fullfileinput,'file')~=2
71        RootFile='';
72        return
73    end
74end
75if ~exist(FilePath,'dir')
76    return % don't go further if the dir path does not exist
77end
78if checkfileindexing
79    NomTypePref='';
80    if isempty(NomType)||strcmp(NomType,'*')
81        if exist(fullfileinput,'file')
82            [tild,RootFile]=fileparts(fileinput);% case of constant name (no indexing), get the filename without its extension
83        else
84            RootFile='';
85        end
86    else
87        %% if checkxml=1, possibly include the first index in the root name, if there exists a corresponding xml file
88        if ~exist('checkxml','var')||checkxml
89            r=regexp(NomType,'^(?<tiretnum>_|\d+)','names');%look for a number or _1 at the beginning of NomType
90            if ~isempty(r) %if NomType begins by a number or _1
91                fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
92                if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
93                    rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
94                else% if a separator '_' is  detected
95                    rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
96                end
97                if ~isempty(rr)
98                    RootFile_i=[RootFile rr.i1];% new root file
99                    %look for an xml file correspoonding to the new root name
100                    if exist(fullfile(RootPath,SubDir,[RootFile_i '.xml']),'file') || (strcmp(FileExt,'.nc') && exist(fullfile(RootPath,[RootFile_i '.xml']),'file'))
101                        RootFile=RootFile_i;
102                        NomTypePref=r.tiretnum;
103                        NomType=regexprep(NomType,['^'  NomTypePref],'');
104                        i1_input=j1_input;
105                        i2_input=j2_input;
106                        j1_input=[];
107                        j2_input=[];
108                    elseif exist([RootPath '.xml'],'file')% new convention with j indices in sub-folders level0, 1...
109                        rj=regexp(SubDir,'^level(?<j1>\d+)$','names');
110                        if ~isempty(rj)
111                            j1_input=rj.j1;
112                            NomType='level';
113                            [RootPath,SubDir]=fileparts(RootPath);
114                        end
115                    end
116                end
117            end
118        end
119       
120        %% analyse the list of existing files when relevant
121        sep1='';
122        sep2='';
123        i1_str='(?<i1>)';%will set i1=[];
124        i1_star='';
125        i2_str='(?<i2>)';%will set i2=[];
126        i2_star='';
127        j1_str='(?<j1>)';%will set j1=[];
128        j1_star='';
129        j2_str='(?<j2>)';%will set j2=[];
130        j2_star='';
131        %Look for cases with letter indexing for the second index
132        r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<sep2>_?)(?<j1>[a|A])(?<j2>[b|B]?)$','names');
133        if ~isempty(r)
134            sep1=r.sep1;
135            sep2=r.sep2;
136            i1_str='(?<i1>\d+)';
137            i1_star='*';
138            if strcmp(lower(r.j1),r.j1)% lower case index
139                j1_str='(?<j1>[a-z])';
140            else
141                j1_str='(?<j1>[A-Z])'; % upper case index
142            end
143            j1_star='*';
144            if ~isempty(r.j2)
145                if strcmp(lower(r.j1),r.j1)
146                    j2_str='(?<j2>[a-z])';
147                else
148                    j2_str='(?<j2>[A-Z])';
149                end
150                j2_star='*';
151            end
152        else %numerical indexing
153            r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<i2>(-\d+)?)(?<j1>(_\d+)?)(?<j2>(-\d+)?)$','names');
154            if ~isempty(r)
155                sep1=r.sep1;
156                i1_str='(?<i1>\d+)';
157                i1_star='*';
158                if ~isempty(r.i2)
159                    i2_str='(?<i2>-\d+)';
160                    i2_star='-*';
161                end
162                if ~isempty(r.j1)
163                    j1_str='(?<j1>_\d+)';
164                    j1_star='_*';
165                end
166                if ~isempty(r.j2)
167                    j2_str='(?<j2>-\d+)';
168                    j2_star='-*';
169                end
170            end
171        end
172        if strcmp(NomType,'level')
173            star_string=[RootFile '*' FileExt];
174            detect_string=['^' RootFile '(?<i1>\d+)' FileExt '$'];%string used in regexp to detect file indices
175             wd=pwd;%current working directory
176            cd (FilePath)% move to the local dir to save time in the operation dir.
177            dirpair=dir(star_string);% look for relevant files in the file directory
178            cd(wd)
179            nbpair=numel(dirpair);
180            i1_series=zeros(1,nbpair);
181            if nbpair==0% no detected file
182                RootFile='';
183            end
184            % scan the list of relevant files, extract the indices
185            for ifile=1:nbpair
186                rr=regexp(dirpair(ifile).name,detect_string,'names');
187                if ~isempty(rr)
188                    i1_series(ifile)=str2num(rr.i1);                 
189                end
190            end
191            % look for the list of subfolders level#
192            cd (fullfile(RootPath,SubDir))% move to the local dir to save time in the operation dir.
193            dirpair=dir('level*');% look for relevant files in the file directory
194            cd(wd)
195            nbpair=numel(dirpair);
196            jfile=0;
197            for ifile=1:nbpair
198                rr=regexp(dirpair(ifile).name,'^level(?<i1>\d+)$','names');
199                if ~isempty(rr)
200                    jfile=jfile+1;
201                    j1_series(jfile)=str2num(rr.i1);             
202                end
203            end
204            [i1_series,j1_series]=meshgrid(i1_series,j1_series);
205            i1_series=reshape(i1_series,1,[]);
206            j1_series=reshape(j1_series,1,[]);
207        else
208            detect_string=['^' RootFile sep1 i1_str i2_str sep2 j1_str j2_str FileExt '$'];%string used in regexp to detect file indices
209            %find the string used to extract the relevant files with the command dir
210            star_string=[RootFile sep1 i1_star i2_star sep2 j1_star j2_star FileExt];
211            wd=pwd;%current working directory
212            cd (FilePath)% move to the local dir to save time in the operation dir.
213            dirpair=dir(star_string);% look for relevant files in the file directory
214            cd(wd)
215            nbpair=numel(dirpair);
216            ref_i_list=zeros(1,nbpair);
217            ref_j_list=zeros(1,nbpair);
218            if nbpair==0% no detected file
219                RootFile='';
220            end
221            % scan the list of relevant files, extract the indices
222            for ifile=1:nbpair
223                rr=regexp(dirpair(ifile).name,detect_string,'names');
224                if ~isempty(rr)
225                    i1=str2num(rr.i1);
226                    i2=str2num(regexprep(rr.i2,'^-',''));
227                    j1=stra2num(regexprep(rr.j1,'^_',''));
228                    j2=stra2num(regexprep(rr.j2,'^-',''));
229                    ref_i=i1;
230                    if isempty(i2_input)
231                        if ~isempty(i2)% invalid file name if i2 does not exist in the input file
232                            break
233                        end
234                    else
235                        ref_i=floor((i1+i2)/2);
236                    end
237                    ref_j=1;
238                    if isempty(j1_input)
239                        if  ~isempty(j1)% invalid file name if j1 does not exist in the input file
240                            break
241                        end
242                    else %j1_input is not empty
243                        if isempty(j1)% the detected name does not fit with the input
244                            break
245                        else
246                            ref_j=j1;
247                            if isempty(j2_input)
248                                if  ~isempty(j2)% invalid file name if j2 does not exist in the input file
249                                    break
250                                end
251                            else
252                                ref_j=floor((j1+j2)/2);
253                            end
254                        end
255                    end
256                    % update the detected index series
257                    if ~isempty(ref_i)&&~isempty(ref_j)
258                        ref_i_list(ifile)=ref_i;
259                        ref_j_list(ifile)=ref_j;
260                        nb_pairs=0;
261                        if ~isempty(i2_input)|| ~isempty(j2_input) %deals with  pairs
262                            if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1
263                                nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0));
264                            end
265                        end
266                        if i1==0
267                            i1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
268                        end
269                        if j1==0
270                            j1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
271                        end
272                        i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1;
273                        if ~isempty(i2_input)
274                            i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2;
275                        end
276                        if ~isempty(j1_input)
277                            j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
278                        end
279                        if ~isempty(j2_input)
280                            j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
281                            j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2;
282                        end
283                    end
284                end
285            end
286            % look for the numerical string of the first files to update the NomType (take into account the 0 before the number)
287            max_j=max(ref_j_list);
288            if isempty(max_j)
289                ref_ij=ref_i_list;
290            else
291                ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
292            end
293            ind_select=find(ref_ij>0);
294           
295            if ~isempty(ind_select)
296                [tild,ifile_min]=min(ref_ij(ind_select));
297                [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)
298                NomType=regexprep(NomType,['^' NomTypePref],'');
299                %% update the file type if the input file does not exist (pb of 0001)
300                if isempty(FileInfo.FileType)
301                    [FileInfo,MovieObject]=get_file_info(fullfile(FilePath,dirpair(ifile_min).name));
302                end
303            end
304        end
305    end
306
307%% set to empty array the irrelevant index series
308if isequal(i1_series,0), i1_series=[]; end
309if isequal(i2_series,0), i2_series=[]; end
310if isequal(j1_series,0), j1_series=[]; end
311if isequal(j2_series,0), j2_series=[]; end
312end
313% %% detect rdvision format
314% if strcmp(FileExt,'.bin')
315%     if exist(fullfile(RootPath,SubDir,[RootFile '.seq']),'file')
316%         FileInfo.FileType='rdvision';
317%         FileInfo.SeqFile=[RootFile '.seq'];
318%     end
319% end
320
321%% introduce the frame index in case of movies or multimage type
322if isfield(FileInfo,'NumberOfFrames') && FileInfo.NumberOfFrames >1
323    if isempty(i1_series)% if there is no file index, i denotes the frame index
324        i1_series=zeros(FileInfo.NumberOfFrames+1,2);% first column =0
325        i1_series(:,2)=(0:FileInfo.NumberOfFrames)'; % second column=frame index -1
326        i1_input=1;
327        NomType='*';
328    else  % if there is a file index, j denotes the frame index while i denotes the file index
329        if ~isempty(regexp(NomType,'ab$', 'once'))% recognized as a pair
330            RootFile=fullfile_uvmat('','',RootFile,'',NomType,i1_input,i2_input,j1_input,j2_input);% restitute the root name without the detected indices       
331            i1_series=zeros(FileInfo.NumberOfFrames+1,2);% first column =0
332            i1_series(:,2)=(0:FileInfo.NumberOfFrames)'; % second column=frame index -1
333            j1_series=[];
334            i1_input=1;
335            NomType='*';
336        else
337            i1_series=i1_series(:,2)*ones(1,FileInfo.NumberOfFrames);%
338            i1_series=[zeros(size(i1_series,1),1) i1_series];
339            j1_series=ones(size(i1_series,1),1)*(1:FileInfo.NumberOfFrames);%
340            j1_series=[zeros(size(i1_series,1),1) j1_series];
341            %  include the first index in the root name
342            r=regexp(NomType,'^(?<tiretnum>_?\d+)','names');%look for a number or _1 at the beginning of NomType
343            if ~isempty(r)
344                fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
345                if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
346                    rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
347                else% if a separator '_' is  detected
348                    rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
349                end
350                if ~isempty(rr)
351                    j1_input=1;
352                    j2_input=[];
353                end
354            end
355        end
356    end
357end
358
359%% sort pairs by decreasing index differences in case of multiple pairs at the same reference index
360if size(i2_series,3)>1 %pairs i1 -i2
361    diff_index=abs(i2_series-i1_series);
362    [tild,ind_pair]=sort(diff_index,3,'descend');
363    for ref_i=1:size(i1_series,1)
364        for ref_j=1:size(j1_series,2)
365            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
366            i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
367            if ~isempty(j1_series)
368                j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
369            end
370        end
371    end
372elseif size(j2_series,3)>1 %pairs j1 -j2
373    diff_index=abs(j2_series-j1_series);
374    [tild,ind_pair]=sort(diff_index,3,'descend');
375    for ref_i=1:size(i1_series,1)
376        for ref_j=1:size(j1_series,2)
377            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
378            if ~isempty(i2_series)
379                i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
380            end
381            j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
382            j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
383        end
384    end
385end
386i1_series=permute(i1_series,[3 2 1]);% permute dimensions
387i2_series=permute(i2_series,[3 2 1]);% permute dimensions
388j1_series=permute(j1_series,[3 2 1]);% permute dimensions
389j2_series=permute(j2_series,[3 2 1]);% permute dimensions
Note: See TracBrowser for help on using the repository browser.