source: trunk/src/find_file_series.m @ 1027

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