source: trunk/src/find_file_series.m @ 362

Last change on this file since 362 was 362, checked in by sommeria, 13 years ago

various bugs corrected

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