source: trunk/src/find_file_series.m @ 554

Last change on this file since 554 was 554, checked in by sommeria, 12 years ago

find_file_series modified to deal with zero indices

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