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