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