source: trunk/src/find_file_series.m @ 854

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