source: trunk/src/find_file_series.m @ 690

Last change on this file since 690 was 667, checked in by sommeria, 11 years ago

a few bugs corrected.
multimask introduced in series
displ_uvmat transformed into disp_uvmat

File size: 13.4 KB
Line 
1%'find_file_series': check the content of an input file and find the corresponding file series
2%--------------------------------------------------------------------------
3% function [RootPath,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,Object,i1_input,i2_input,j1_input,j2_input]=find_file_series(fileinput)
4%
5% OUTPUT:
6% RootFile: root file detected in fileinput, possibly modified for movies (indexing is then done on image view, not file)
7% 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
8%  (ref_i+1 is used to deal with the image index zero sometimes used)
9% NomType: nomenclature type corrected after checking the first file (problem of 0 before the number string)
10% FileType: type of file, =
11%       = 'image', usual image as recognised by Matlab
12%       = 'multimage', image series stored in a single file
13%       = 'civx', netcdf file with civx convention
14%       = 'civdata', civ data with new convention
15%       = 'netcdf' other netcdf files
16%       = 'video': movie recognised by VideoReader (e;g. avi)
17% MovieObject: video object (=[] otherwise)
18%
19%INPUT
20% RootPath: path to the directory to be scanned
21% fileinput: name (without path) of the input file sample
22% checkxml: =1(default) take into account xml file existence to possibly include indexes in RootFile
23%           =0: do not take into account xml file existence
24%
25%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
26%  Copyright  2011, LEGI / CNRS-UJF-INPG, joel.sommeria@legi.grenoble-inp.fr
27%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
28%     This file is part of the toolbox UVMAT.
29%
30%     UVMAT is free software; you can redistribute it and/or modify
31%     it under the terms of the GNU General Public License as published by
32%     the Free Software Foundation; either version 2 of the License, or
33%     (at your option) any later version.
34%
35%     UVMAT is distributed in the hope that it will be useful,
36%     but WITHOUT ANY WARRANTY; without even the implied warranty of
37%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
38%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
39%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
40
41function [RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,FileInfo,MovieObject,i1_input,i2_input,j1_input,j2_input]=find_file_series(FilePath,fileinput,checkxml)
42%------------------------------------------------------------------------
43
44%% get input root name and nomenclature type
45fullfileinput=fullfile(FilePath,fileinput);% input file name with path
46[RootPath,SubDir,RootFile,i1_input,i2_input,j1_input,j2_input,FileExt,NomType]=fileparts_uvmat(fullfileinput);
47
48
49%% check for particular file types: images, movies, civ data
50i1_series=zeros(1,1,1);
51i2_series=zeros(1,1,1);
52j1_series=zeros(1,1,1);
53j2_series=zeros(1,1,1);
54[FileType,FileInfo,MovieObject]=get_file_type(fullfileinput);
55if ~exist(FilePath,'dir')
56    return % don't go further if the dir path does not exist
57end
58NomTypePref='';
59if isempty(NomType)
60    if exist(fullfileinput,'file')
61        [tild,RootFile]=fileparts(fileinput);% case of constant name (no indexing), get the filename without its extension
62    else
63        RootFile='';
64    end
65else
66    %% if checkxml=1, possibly include the first index in the root name, if there exists a corresponding xml file
67    if ~exist('checkxml','var')||checkxml
68        r=regexp(NomType,'^(?<tiretnum>_|\d+)','names');%look for a number or _1 at the beginning of NomType
69        if ~isempty(r) %if NomType begins by a number or _1
70            fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
71            if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
72                rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
73            else% if a separator '_' is  detected
74                rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
75            end
76            if ~isempty(rr)
77                RootFile_i=[RootFile rr.i1];% new root file
78                %look for an xml file correspoonding to the new root name
79                if exist(fullfile(RootPath,SubDir,[RootFile_i '.xml']),'file') || (strcmp(FileExt,'.nc') && exist(fullfile(RootPath,[RootFile_i '.xml']),'file'))
80                    RootFile=RootFile_i;
81                    NomTypePref=r.tiretnum;
82                    NomType=regexprep(NomType,['^'  NomTypePref],'');
83                    i1_input=j1_input;
84                    i2_input=j2_input;
85                    j1_input=[];
86                    j2_input=[];
87                end
88            end
89        end
90    end
91    %% analyse the list of existing files when relevant
92    sep1='';
93    sep2='';
94    i1_str='(?<i1>)';%will set i1=[];
95    i1_star='';
96    i2_str='(?<i2>)';%will set i2=[];
97    i2_star='';
98    j1_str='(?<j1>)';%will set j1=[];
99    j1_star='';
100    j2_str='(?<j2>)';%will set j2=[];
101    j2_star='';
102    %Look for cases with letter indexing for the second index
103    r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<sep2>_?)(?<j1>[a|A])(?<j2>[b|B]?)$','names');
104    if ~isempty(r)
105        sep1=r.sep1;
106        sep2=r.sep2;
107        i1_str='(?<i1>\d+)';
108        i1_star='*';
109        if strcmp(lower(r.j1),r.j1)% lower case index
110            j1_str='(?<j1>[a-z])';
111        else
112           j1_str='(?<j1>[A-Z])'; % upper case index
113        end
114        j1_star='*';
115        if ~isempty(r.j2)
116           if strcmp(lower(r.j1),r.j1)
117            j2_str='(?<j2>[a-z])';
118            else
119           j2_str='(?<j2>[A-Z])';
120           end
121            j2_star='*';
122        end
123    else %numerical indexing
124        r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<i2>(-\d+)?)(?<j1>(_\d+)?)(?<j2>(-\d+)?)$','names');
125        if ~isempty(r)
126            sep1=r.sep1;
127            i1_str='(?<i1>\d+)';
128            i1_star='*';
129            if ~isempty(r.i2)
130                i2_str='(?<i2>-\d+)';
131                i2_star='-*';
132            end
133            if ~isempty(r.j1)
134                j1_str='(?<j1>_\d+)';
135                j1_star='_*';
136            end
137            if ~isempty(r.j2)
138                j2_str='(?<j2>-\d+)';
139                j2_star='-*';
140            end
141        end
142    end
143    detect_string=['^' RootFile sep1 i1_str i2_str sep2 j1_str j2_str FileExt '$'];%string used in regexp to detect file indices
144    %find the string used to extract the relevant files with the command dir
145    star_string=[RootFile sep1 i1_star i2_star sep2 j1_star j2_star FileExt];
146    wd=pwd;%current working directory
147    cd (FilePath)% move to the local dir to save time in the operation dir.
148    dirpair=dir(star_string);% look for relevant files in the file directory
149    cd(wd)
150    nbpair=numel(dirpair);
151    ref_i_list=zeros(1,nbpair);
152    ref_j_list=zeros(1,nbpair);
153    if nbpair==0% no detected file
154        RootFile='';
155    end
156    % scan the list of relevant files, extract the indices
157    for ifile=1:nbpair
158        rr=regexp(dirpair(ifile).name,detect_string,'names');
159        if ~isempty(rr)
160            i1=str2num(rr.i1);
161            i2=str2num(regexprep(rr.i2,'^-',''));
162            j1=stra2num(regexprep(rr.j1,'^_',''));
163            j2=stra2num(regexprep(rr.j2,'^-',''));
164            ref_i=i1;
165            if isempty(i2_input)
166                if ~isempty(i2)% invalid file name if i2 does not exist in the input file
167                    break
168                end
169            else
170                ref_i=floor((i1+i2)/2);
171            end
172            ref_j=1;
173            if isempty(j1_input)
174                if  ~isempty(j1)% invalid file name if j1 does not exist in the input file
175                    break
176                end
177            else %j1_input is not empty
178                if isempty(j1)% the detected name does not fit with the input
179                    break
180                else
181                    ref_j=j1;
182                    if isempty(j2_input)
183                        if  ~isempty(j2)% invalid file name if j2 does not exist in the input file
184                            break
185                        end
186                    else
187                        ref_j=floor((j1+j2)/2);
188                    end
189                end
190            end
191            % update the detected index series
192            ref_i_list(ifile)=ref_i;
193            ref_j_list(ifile)=ref_j;
194            nb_pairs=0;
195            if ~isempty(i2_input)|| ~isempty(j2_input) %deals with  pairs
196                if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1
197                    nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0));
198                end
199            end
200            if i1==0
201                i1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
202            end
203            if j1==0
204                j1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0)
205            end
206            i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1;
207            if ~isempty(i2_input)
208                i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2;
209            end
210            if ~isempty(j1_input)
211                j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
212            end
213            if ~isempty(j2_input)
214                j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
215                j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2;
216            end
217        end
218    end
219    % look for the numerical string of the first files to update the NomType (take into account the 0 before the number)
220    max_j=max(ref_j_list);
221    if isempty(max_j)
222        ref_ij=ref_i_list;
223    else
224        ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
225    end
226    ind_select=find(ref_ij>0);
227    if isempty(ind_select)
228        RootFile='';
229        NomType='';
230    else
231        [tild,ifile_min]=min(ref_ij(ind_select));
232        [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)
233        NomType=regexprep(NomType,['^' NomTypePref],'');
234        %% update the file type if the input file does not exist (pb of 0001)
235        if isempty(FileType)
236            [FileType,FileInfo,MovieObject]=get_file_type(fullfile(FilePath,dirpair(ifile_min).name));
237        end
238    end
239end
240
241%% set to empty array the irrelevant index series
242if isequal(i1_series,0), i1_series=[]; end
243if isequal(i2_series,0), i2_series=[]; end
244if isequal(j1_series,0), j1_series=[]; end
245if isequal(j2_series,0), j2_series=[]; end
246
247%% introduce the frame index in case of movies or multimage type
248if isfield(FileInfo,'NumberOfFrames') && FileInfo.NumberOfFrames >1
249    if isempty(i1_series)
250        i1_series=zeros(FileInfo.NumberOfFrames+1,2);% first column =0
251        i1_series(:,2)=(0:FileInfo.NumberOfFrames)'; % second column=frame index -1
252        i1_input=1;
253        NomType='*';
254    else
255        i1_series=i1_series(:,2)*ones(1,FileInfo.NumberOfFrames);
256        i1_series=[zeros(size(i1_series,1),1) i1_series];
257        j1_series=ones(size(i1_series,1),1)*(1:FileInfo.NumberOfFrames);
258        j1_series=[zeros(size(i1_series,1),1) j1_series];
259        %  include the first index in the root name
260        r=regexp(NomType,'^(?<tiretnum>_?\d+)','names');%look for a number or _1 at the beginning of NomType
261        if ~isempty(r)
262            fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
263            if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
264                rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
265            else% if a separator '_' is  detected
266                rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
267            end
268            if ~isempty(rr)
269%                 RootFile=[RootFile rr.i1];% new root file
270%                 NomTypePref=r.tiretnum;
271%                 NomType=regexprep(NomType,['^'  NomTypePref],'');
272              %  i1_input=j1_input;
273              %  i2_input=j2_input;
274                j1_input=1;
275                j2_input=[];
276            end
277        end
278    end
279end
280
281%% sort pairs by decreasing index differences in case of multiple pairs at the same reference index
282if size(i2_series,3)>1 %pairs i1 -i2
283    diff_index=abs(i2_series-i1_series);
284    [tild,ind_pair]=sort(diff_index,3,'descend');
285    for ref_i=1:size(i1_series,1)
286        for ref_j=1:size(j1_series,2)
287            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
288            i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
289            if ~isempty(j1_series)
290                j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
291            end
292        end
293    end
294elseif size(j2_series,3)>1 %pairs j1 -j2
295    diff_index=abs(j2_series-j1_series);
296    [tild,ind_pair]=sort(diff_index,3,'descend');
297    for ref_i=1:size(i1_series,1)
298        for ref_j=1:size(j1_series,2)
299            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
300            if ~isempty(i2_series)
301                i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
302            end
303            j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
304            j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
305        end
306    end
307end
308i1_series=permute(i1_series,[3 2 1]);% permute dimensions
309i2_series=permute(i2_series,[3 2 1]);% permute dimensions
310j1_series=permute(j1_series,[3 2 1]);% permute dimensions
311j2_series=permute(j2_series,[3 2 1]);% permute dimensions
Note: See TracBrowser for help on using the repository browser.