source: trunk/src/find_file_series.m @ 384

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

pb with masks corrected
bug in fileparts_uvmat corrected
find_file_series corrected so that it finds the root name corresponding tothe existing xml file : for instance Dalsa1_0001 gives root=Dalsa1 instead of Dalsa
bug corrected in uvmat, runpm (j1 and j2 set to 1 when not visible)

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