source: trunk/src/find_file_series.m @ 469

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

several bugs corrected
introductyion of functions find_imadoc and read_multimadoc which simplifiy the functions under series

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