source: trunk/src/find_file_series.m @ 534

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

various cleaning

File size: 12.5 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            i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1;
193            if ~isempty(i2_input)
194                i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2;
195            end
196            if ~isempty(j1_input)
197                j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
198            end
199            if ~isempty(j2_input)
200                j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1;
201                j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2;
202            end
203        end
204    end
205    % look for the numerical string of the first files to update the NomType (take into account the 0 before the number)
206    max_j=max(ref_j_list);
207    if isempty(max_j)
208        ref_ij=ref_i_list;
209    else
210        ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i.
211    end
212    ind_select=find(ref_ij>0);
213    if isempty(ind_select)
214        RootFile='';
215        NomType='';
216    else
217        [tild,ifile_min]=min(ref_ij(ind_select));
218        [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)
219        NomType=regexprep(NomType,['^' NomTypePref],'');
220        %% update the file type if the input file does not exist (pb of 0001)
221        if isempty(FileType)
222            [FileType,tild,MovieObject]=get_file_type(fullfile(FilePath,dirpair(ifile_min).name));
223        end
224    end
225end
226
227%% set to empty array the irrelevant index series
228if isequal(i1_series,0), i1_series=[]; end
229if isequal(i2_series,0), i2_series=[]; end
230if isequal(j1_series,0), j1_series=[]; end
231if isequal(j2_series,0), j2_series=[]; end
232
233%% introduce the frame index in case of movies or multimage type
234if isfield(FileInfo,'NumberOfFrames') && FileInfo.NumberOfFrames >1
235    if isempty(i1_series)
236        i1_series=(1:FileInfo.NumberOfFrames)';
237        i1_input=1;
238        NomType='*';
239    else
240        i1_series=i1_series(:,2)*ones(1,FileInfo.NumberOfFrames);
241        i1_series=[i1_series(:,1) i1_series];
242        j1_series=ones(size(i1_series,1),1)*(0:FileInfo.NumberOfFrames);
243        %  include the first index in the root name
244        r=regexp(NomType,'^(?<tiretnum>_?\d+)','names');%look for a number or _1 at the beginning of NomType
245        if ~isempty(r)
246            fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput
247            if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not  detected
248                rr=regexp(fileinput_end,'^(?<i1>\d+)','names');
249            else% if a separator '_' is  detected
250                rr=regexp(fileinput_end,'^(?<i1>_\d+)','names');
251            end
252            if ~isempty(rr)
253%                 RootFile=[RootFile rr.i1];% new root file
254%                 NomTypePref=r.tiretnum;
255%                 NomType=regexprep(NomType,['^'  NomTypePref],'');
256              %  i1_input=j1_input;
257              %  i2_input=j2_input;
258                j1_input=1;
259                j2_input=[];
260            end
261        end
262    end
263end
264
265%% sort pairs by decreasing index differences in case of multiple pairs at the same reference index
266if size(i2_series,3)>1 %pairs i1 -i2
267    diff_index=abs(i2_series-i1_series);
268    [tild,ind_pair]=sort(diff_index,3,'descend');
269    for ref_i=1:size(i1_series,1)
270        for ref_j=1:size(j1_series,2)
271            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
272            i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
273            if ~isempty(j1_series)
274                j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
275            end
276        end
277    end
278elseif size(j2_series,3)>1 %pairs j1 -j2
279    diff_index=abs(j2_series-j1_series);
280    [tild,ind_pair]=sort(diff_index,3,'descend');
281    for ref_i=1:size(i1_series,1)
282        for ref_j=1:size(j1_series,2)
283            i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
284            if ~isempty(i2_series)
285                i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
286            end
287            j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
288            j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:));
289        end
290    end
291end
292i1_series=permute(i1_series,[3 2 1]);% permute dimensions
293i2_series=permute(i2_series,[3 2 1]);% permute dimensions
294j1_series=permute(j1_series,[3 2 1]);% permute dimensions
295j2_series=permute(j2_series,[3 2 1]);% permute dimensions
Note: See TracBrowser for help on using the repository browser.