source: trunk/src/find_file_series.m @ 1195

Last change on this file since 1195 was 1194, checked in by sommeria, 8 days ago

several bugs repaired

File size: 14.6 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)
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): set of indices i1 sorted by ref index ref_i, ref_j, and pair index 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% i2_series,j1_series,j2_series: same as i1_series but for the indices i2,j1,j2.
12
13% NomType: nomenclature type corrected after checking the first file (problem of 0 before the number string)
14% FileInfo: structure containing info on the input files (assumed identical on the whole series)
15% FileInfo.FileType: type of file, =
16%       = 'image', usual image as recognised by Matlab
17%       = 'multimage', image series stored in a single file
18%       = 'civx', netcdf file with civx convention
19%       = 'civdata', civ data with new convention
20%       = 'netcdf' other netcdf files
21%       = 'video': movie recognised by VideoReader (e;g. avi)
22% MovieObject: video object (=[] otherwise
23% 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
24%
25%INPUT
26% FilePath: path to the directory to be scanned
27% fileinput: name (without path) of the input file sample
28
29%=======================================================================
30% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
31%   http://www.legi.grenoble-inp.fr
32%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.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)
48%------------------------------------------------------------------------
49
50%% get input root name and info on the input file
51if isempty(regexp(FilePath,'^http://','once'))% case of usual file input
52fullfileinput=fullfile(FilePath,fileinput);% input file name with path
53else
54  fullfileinput=[FilePath '/' fileinput]; % case of web input
55end
56[FileInfo,MovieObject]=get_file_info(fullfileinput);
57
58%% check for particular file types: images, movies, civ data
59[RootPath,SubDir,RootFile,i1_input,i2_input,j1_input,j2_input,FileExt,NomType]=fileparts_uvmat(fullfileinput);
60i1_series=zeros(1,1,1);
61i2_series=zeros(1,1,1);
62j1_series=zeros(1,1,1);
63j2_series=zeros(1,1,1);
64checkfileindexing=1;
65if isempty(regexp(FilePath,'^http://', 'once')) && ~exist(FilePath,'dir')
66    return % don't go further if the dir path does not exist
67end
68if checkfileindexing
69    NomTypePref='';
70    if isempty(NomType)||strcmp(NomType,'*')
71        if exist(fullfileinput,'file')
72            [~,RootFile]=fileparts(fileinput);% case of constant name (no indexing), get the filename without its extension
73        else
74            RootFile='';
75        end
76    else
77     
78       
79        %% analyse the list of existing files when relevant
80        sep1='';
81        sep2='';
82        i1_str='(?<i1>)';%will set i1=[];
83        i2_str='(?<i2>)';%will set i2=[];
84        j1_str='(?<j1>)';%will set j1=[];
85        j2_str='(?<j2>)';%will set j2=[];
86
87        %Look for cases with letter indexing for the second index
88        r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<sep2>_?)(?<j1>[a|A])(?<j2>[b|B]?)$','names');
89        if ~isempty(r) %indexing image pair with letters
90            sep1=r.sep1;
91            sep2=r.sep2;
92            i1_str='(?<i1>\d+)';
93            if strcmp(lower(r.j1),r.j1)% lower case index
94                j1_str='(?<j1>[a-z])';
95            else
96                j1_str='(?<j1>[A-Z])'; % upper case index
97            end
98            if ~isempty(r.j2)
99                if strcmp(lower(r.j1),r.j1)
100                    j2_str='(?<j2>[a-z])';
101                else
102                    j2_str='(?<j2>[A-Z])';
103                end
104            end
105        else %numerical indexing
106            r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<i2>(-\d+)?)(?<j1>(_\d+)?)(?<j2>(-\d+)?)$','names');
107            if ~isempty(r)
108                sep1=r.sep1;
109                i1_str='(?<i1>\d+)';
110                if ~isempty(r.i2)
111                    i2_str='(?<i2>-\d+)';
112                end
113                if ~isempty(r.j1)
114                    j1_str='(?<j1>_\d+)';
115                end
116                if ~isempty(r.j2)
117                    j2_str='(?<j2>-\d+)';
118                end
119            end
120        end
121       
122        detect_string=['^' RootFile sep1 i1_str i2_str sep2 j1_str j2_str FileExt '$'];%string used in regexp to detect file indices
123        ListStruct=dir_uvmat(FilePath);% scan the content of the folder FilePath
124        ListCells=struct2cell(ListStruct);% transform dir struct to a cell arrray
125        ListFiles=ListCells(1,:);%list of file names
126        rr=regexp(ListFiles,detect_string,'names');
127        nbpair=numel(rr);
128        ref_i_list=zeros(1,nbpair);
129        ref_j_list=zeros(1,nbpair);
130        if nbpair==0% no detected file
131            RootFile='';
132        end
133        % scan the list of relevant files, extract the indices
134        for ifile=1:nbpair
135            %             rr=regexp(dirpair(ifile).name,detect_string,'names');
136            if ~isempty(rr{ifile})
137                i1=str2double(rr{ifile}.i1);
138                i2=str2double(regexprep(rr{ifile}.i2,'^-',''));
139                j1=stra2num(regexprep(rr{ifile}.j1,'^_',''));
140                j2=stra2num(regexprep(rr{ifile}.j2,'^-',''));
141                ref_i=i1;
142                if isempty(i2_input)
143                    if ~isnan(i2)% invalid file name if i2 does not exist in the input file
144                        break
145                    end
146                else
147                    ref_i=floor((i1+i2)/2);
148                end
149                ref_j=1;
150                if isempty(j1_input)
151                    if  ~isnan(j1)% invalid file name if j1 does not exist in the input file
152                        break
153                    end
154                else %j1_input is not empty
155                    if isnan(j1)% the detected name does not fit with the input
156                        break
157                    else
158                        ref_j=j1;
159                        if isempty(j2_input)
160                            if  ~isnan(j2)% invalid file name if j2 does not exist in the input file
161                                break
162                            end
163                        else
164                            ref_j=floor((j1+j2)/2);
165                        end
166                    end
167                end
168                % update the detected index series
169                if ~isnan(ref_i)&&~isnan(ref_j)
170                    if ref_i*ref_j>1000000
171                        disp('warning: inapropriate file name indexing: too large values for index scanning')
172                        ref_i_list(ifile)=i1_input;
173                        if isempty(j1_input)
174                            ref_j_list(ifile)=1;
175                        else
176                        ref_j_list(ifile)=j1_input;
177                        end
178                        break
179                    end
180                    ref_i_list(ifile)=ref_i;
181                    ref_j_list(ifile)=ref_j;
182                    nb_pairs=0;
183                    if ~isempty(i2_input)|| ~isempty(j2_input) %deals with  pairs
184                        if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1
185                            nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0));
186                        end
187                    end
188                    if i1==0
189                        i1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
190                    end
191                    if j1==0
192                        j1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
193                    end
194                    i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1;
195                    if ~isempty(i2_input)
196                        i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2;
197                    end
198                    if ~isempty(j1_input)
199                        j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
200                    end
201                    if ~isempty(j2_input)
202                        j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
203                        j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2;
204                    end
205                end
206            end
207        end
208        % look for the numerical string of the first files to update the NomType (take into account the 0 before the number)
209        max_j=max(ref_j_list);
210        if isempty(max_j)
211            ref_ij=ref_i_list;
212        else
213            ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
214        end
215        ind_select=find(ref_ij>0);
216       
217        if ~isempty(ind_select)
218            [tild,ifile_min]=min(ref_ij(ind_select));
219            [tild,tild,tild,tild,tild,tild,tild,tild,NomType]=fileparts_uvmat(ListFiles{ind_select(ifile_min)});% update the representation of indices (number of 0 before the number)
220            NomType=regexprep(NomType,['^' NomTypePref],'');
221            %% update the file type if the input file does not exist (pb of 0001)
222            if isempty(FileInfo.FileName)
223                [FileInfo,MovieObject]=get_file_info(fullfile(FilePath,ListFiles{ifile_min}));
224            end
225        end
226        %         end
227    end
228   
229    %% set to empty array the irrelevant index series
230    if isequal(i1_series,0), i1_series=[]; end
231    if isequal(i2_series,0), i2_series=[]; end
232    if isequal(j1_series,0), j1_series=[]; end
233    if isequal(j2_series,0), j2_series=[]; end
234   
235    %% case of isolated input file, not member of an  indexed series
236    if isempty(i1_series)
237        [PathDir,RootFile]=fileparts(fullfileinput);
238        [RootPath,SubDir,DirExt]=fileparts(PathDir);
239        SubDir=[SubDir DirExt];% include part after . in the name (considered as a file extension)
240        NomType='*';
241        i2_series=[];j1_series=[];j2_series=[];
242        %         i1_input=1;i2_input=[];j1_input=[];j2_input=[];
243        if ~exist(fullfileinput,'file')
244            RootFile='';
245            return
246        end
247    end
248end
249
250%% introduce the frame index in case of movies or multimage type
251if isfield(FileInfo,'NumberOfFrames') && FileInfo.NumberOfFrames >1
252    if isempty(i1_series)% if there is no file index, i denotes the frame index
253        i1_series=zeros(FileInfo.NumberOfFrames+1,2);% first column =0
254        i1_series(:,2)=(0:FileInfo.NumberOfFrames)'; % second column=frame index -1
255        i1_input=1;
256        NomType='*';
257    else  % if there is a file index, j denotes the frame index while i denotes the file index
258        if ~isempty(regexp(NomType,'ab$', 'once'))% recognized as a pair
259            RootFile=fullfile_uvmat('','',RootFile,'',NomType,i1_input,i2_input,j1_input,j2_input);% restitute the root name without the detected indices
260            i1_series=zeros(FileInfo.NumberOfFrames+1,2);% first column =0
261            i1_series(:,2)=(0:FileInfo.NumberOfFrames)'; % second column=frame index -1
262            j1_series=[];
263            i1_input=1;
264            NomType='*';
265        else
266            i1_series=i1_series(:,2)*ones(1,FileInfo.NumberOfFrames);%
267            i1_series=[zeros(size(i1_series,1),1) i1_series];
268            if ~isempty(i2_series)
269                i2_series=i2_series(:,2)*ones(1,FileInfo.NumberOfFrames);%
270            i2_series=[zeros(size(i2_series,1),1) i2_series];
271            end
272            j1_series=ones(size(i1_series,1),1)*(1:FileInfo.NumberOfFrames);%
273            j1_series=[zeros(size(i1_series,1),1) j1_series];
274            %  include the first index in the root name
275            r=regexp(NomType,'^(?<tiretnum>_?\d+)','names');%look for a number or _1 at the beginning of NomType
276            if ~isempty(r)
277                fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
278                if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
279                    rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
280                else% if a separator '_' is  detected
281                    rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
282                end
283                if ~isempty(rr)
284                    j1_input=1;
285                    j2_input=[];
286                end
287            end
288        end
289    end
290end
291
292%% sort pairs by decreasing index differences in case of multiple pairs at the same reference index
293if size(i2_series,3)>1 %pairs i1 -i2
294    diff_index=abs(i2_series-i1_series);
295    [tild,ind_pair]=sort(diff_index,3,'descend');
296    for ref_i=1:size(i1_series,1)
297        for ref_j=1:size(j1_series,2)
298            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
299            i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
300            if ~isempty(j1_series)
301                j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
302            end
303        end
304    end
305elseif size(j2_series,3)>1 %pairs j1 -j2
306    diff_index=abs(j2_series-j1_series);
307    [tild,ind_pair]=sort(diff_index,3,'descend');
308    for ref_i=1:size(i1_series,1)
309        for ref_j=1:size(j1_series,2)
310            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
311            if ~isempty(i2_series)
312                i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
313            end
314            j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
315            j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
316        end
317    end
318end
319i1_series=permute(i1_series,[3 2 1]);% permute dimensions
320i2_series=permute(i2_series,[3 2 1]);% permute dimensions
321j1_series=permute(j1_series,[3 2 1]);% permute dimensions
322j2_series=permute(j2_series,[3 2 1]);% permute dimensions
Note: See TracBrowser for help on using the repository browser.