source: trunk/src/find_file_series.m @ 437

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

civ improved to deal with movies. Introduction of a file type mmreader needed for Matlab 2009 (Videoreader not available)

File size: 11.3 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% Object: 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,Object,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% ifile_min=1;%default
53[FileType,FileInfo,Object]=get_file_type(fullfileinput);
54if strcmp( FileType,'multimage')||strcmp( FileType,'video')||strcmp( FileType,'mmreader')
55    NomType='*';
56    i1_series=(1:FileInfo.NumberOfFrames)';
57end
58
59if strcmp(NomType,'')||strcmp(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
65    i1_input=1;% the index now refer to the frame in the movie, choose 1 at opening
66    i2_input=[];
67    j1_input=[];
68    j2_input=[];
69else
70    %% possibly include the first index in the root name, if there exists a corresponding xml file
71    NomTypePref='';
72    r=regexp(NomType,'^(?<tiretnum>_?\d+)','names');%look for a number or _1 at the beginning of NomType
73    if ~isempty(r)
74        fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
75        if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
76            rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
77        else% if a separator '_' is  detected
78            rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
79        end
80        if ~isempty(rr)
81            RootFileNew=[RootFile rr.i1];
82            checkpair=~isempty(regexp(NomType,'-','once'))||~isempty(regexp(NomType,'ab$','once'))||~isempty(regexp(NomType,'AB$','once'));%case of PIV results
83            if exist(fullfile(RootPath,[RootFileNew '.xml']),'file') %|| (checkpair && exist(fullfile(fileparts(RootPath),[RootFileNew '.xml']),'file'))
84                RootFile=RootFileNew;
85                NomTypePref=r.tiretnum;
86                NomType=regexprep(NomType,['^'  NomTypePref],'');
87                i1_input=j1_input;
88                i2_input=j2_input;
89                j1_input=[];
90                j2_input=[];
91            end       
92        end
93    end
94    %% analyse the list of existing files when relevant
95    sep1='';
96    i1_str='(?<i1>)';%will set i1=[];
97    i1_star='';
98    %     r.sep2='';
99    i2_str='(?<i2>)';%will set i2=[];
100    i2_star='';
101    %     sep3='';
102    j1_str='(?<j1>)';%will set j1=[];
103    j1_star='';
104    %     sep4='';
105    j2_str='(?<j2>)';%will set j2=[];
106    j2_star='';
107    %   NomTypeStr=NomType;
108    %Look for cases with letter indexing for the second index
109    r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<j1>[a|A])(?<j2>[b|B]?)$','names');
110    if ~isempty(r)
111        sep1=r.sep1;
112        i1_str='(?<i1>\d+)';
113        if strcmp(lower(r.j1),r.j1)% lower case index
114            j1_str='(?<j1>[a-z])';
115        else
116           j1_str='(?<j1>[A-Z])'; % upper case index
117        end
118        j1_star='*';
119        if ~isempty(r.j2)
120           if strcmp(lower(r.j1),r.j1)
121            j2_str='(?<j2>[a-z])';
122            else
123           j2_str='(?<j2>[A-Z])';
124           end
125            j2_star='*';
126        end
127    else %numerical indexing
128        r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<i2>(-\d+)?)(?<j1>(_\d+)?)(?<j2>(-\d+)?)$','names');
129        if ~isempty(r)
130            sep1=r.sep1;
131            i1_str='(?<i1>\d+)';
132            i1_star='*';
133            if ~isempty(r.i2)
134                i2_str='(?<i2>-\d+)';
135                i2_star='-*';
136            end
137            if ~isempty(r.j1)
138                j1_str='(?<j1>_\d+)';
139                j1_star='_*';
140            end
141            if ~isempty(r.j2)
142                j2_str='(?<j2>-\d+)';
143                j2_star='-*';
144            end
145        end
146    end
147    detect_string=['^' RootFile sep1 i1_str i2_str j1_str j2_str FileExt '$'];%string used in regexp to detect file indices
148    %find the string used to extract the relevant files with the command dir
149    star_string=[RootFile sep1 i1_star i2_star  j1_star j2_star FileExt];
150    wd=pwd;%current working directory
151    cd (FilePath)% move to the local dir to save time in the operation dir.
152    dirpair=dir(star_string);% look for relevant files in the file directory
153    cd(wd)
154    nbpair=numel(dirpair);
155    ref_i_list=zeros(1,nbpair);
156    ref_j_list=zeros(1,nbpair);
157    if nbpair==0% no detected file
158        RootFile='';
159    end
160    % scan the list of relevant files, extract the indices
161    for ifile=1:nbpair
162        rr=regexp(dirpair(ifile).name,detect_string,'names');
163        if ~isempty(rr)
164            i1=str2num(rr.i1);
165            i2=str2num(regexprep(rr.i2,'^-',''));
166            j1=stra2num(regexprep(rr.j1,'^_',''));
167            j2=stra2num(regexprep(rr.j2,'^-',''));
168            ref_i=i1;
169            if isempty(i2_input)
170                if ~isempty(i2)% invalid file name if i2 does not exist in the input file
171                    break
172                end
173            else
174                ref_i=floor((i1+i2)/2);
175            end
176            ref_j=1;
177            if isempty(j1_input)
178                if  ~isempty(j1)% invalid file name if j1 does not exist in the input file
179                    break
180                end
181            else %j1_input is not empty
182                if isempty(j1)% the detected name does not fit with the input
183                    break
184                else
185                    ref_j=j1;
186                    if isempty(j2_input)
187                        if  ~isempty(j2)% invalid file name if j2 does not exist in the input file
188                            break
189                        end
190                    else
191                        ref_j=floor((j1+j2)/2);
192                    end
193                end
194            end
195            % update the detected index series
196            ref_i_list(ifile)=ref_i;
197            ref_j_list(ifile)=ref_j;
198            nb_pairs=0;
199            if ~isempty(i2_input)|| ~isempty(j2_input) %deals with  pairs
200                if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1
201                    nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0));
202                end
203            end
204            i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1;
205            if ~isempty(i2_input)
206                i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2;
207            end
208            if ~isempty(j1_input)
209                j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
210            end
211            if ~isempty(j2_input)
212                j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
213                j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2;
214            end
215        end
216    end
217    % look for the numerical string of the first files to update the NomType (take into account the 0 before the number)
218    max_j=max(ref_j_list);
219    if isempty(max_j)
220        ref_ij=ref_i_list;
221    else
222        ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
223    end
224    %[tild,ifile_min]=min(ref_ij(ref_ij>0));
225    ifile_min=find(ref_ij>0 , 1);
226    if isempty(ifile_min)
227        %         RootPath='';
228        RootFile='';
229        NomType='';
230    else
231        [tild,tild,tild,tild,tild,tild,tild,tild,NomType]=fileparts_uvmat(dirpair(ifile_min).name);% update the representation of indices (number of 0 before the number)
232        NomType=regexprep(NomType,['^' NomTypePref],'');
233    end
234    %% update the file type if the input file does not exist (pb of 0001)
235    if isempty(FileType)
236        [FileType,tild,Object]=get_file_type(dirpair(ifile_min).name);
237    end
238end
239
240%% set to empty array the irrelevant index series
241if isequal(i1_series,0), i1_series=[]; end
242if isequal(i2_series,0), i2_series=[]; end
243if isequal(j1_series,0), j1_series=[]; end
244if isequal(j2_series,0), j2_series=[]; end
245
246%% sort pairs by decreasing index differences in case of multiple pairs at the same reference index
247if size(i2_series,3)>1 %pairs i1 -i2
248    diff_index=abs(i2_series-i1_series);
249    [tild,ind_pair]=sort(diff_index,3,'descend');
250    for ref_i=1:size(i1_series,1)
251        for ref_j=1:size(j1_series,2)
252            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
253            i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
254            if ~isempty(j1_series)
255                j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
256            end
257        end
258    end
259elseif size(j2_series,3)>1 %pairs j1 -j2
260    diff_index=abs(j2_series-j1_series);
261    [tild,ind_pair]=sort(diff_index,3,'descend');
262    for ref_i=1:size(i1_series,1)
263        for ref_j=1:size(j1_series,2)
264            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
265            if ~isempty(i2_series)
266                i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
267            end
268            j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
269            j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
270        end
271    end
272end
273
Note: See TracBrowser for help on using the repository browser.