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 | |
---|
38 | function [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 |
---|
45 | FileType=''; |
---|
46 | Object=[]; |
---|
47 | i1_series=zeros(1,1,1); |
---|
48 | i2_series=zeros(1,1,1); |
---|
49 | j1_series=zeros(1,1,1); |
---|
50 | j2_series=zeros(1,1,1); |
---|
51 | switch 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 |
---|
99 | end |
---|
100 | |
---|
101 | if 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 |
---|
108 | else |
---|
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 | i1=str2num(rr.i1); |
---|
184 | i2=str2num(rr.i2); |
---|
185 | j1=stra2num(rr.j1); |
---|
186 | j2=stra2num(rr.j2); |
---|
187 | ref_i=i1; |
---|
188 | if isempty(i2_input) |
---|
189 | if ~isempty(i2)% invalid file name if i2 does not exist in the input file |
---|
190 | break |
---|
191 | end |
---|
192 | else |
---|
193 | ref_i=floor((i1+i2)/2); |
---|
194 | end |
---|
195 | ref_j=1; |
---|
196 | if isempty(j1_input) |
---|
197 | if ~isempty(j1)% invalid file name if j1 does not exist in the input file |
---|
198 | break |
---|
199 | end |
---|
200 | else %j1_input is not empty |
---|
201 | if isempty(j1)% the detected name does not fit with the input |
---|
202 | break |
---|
203 | else |
---|
204 | ref_j=j1; |
---|
205 | if isempty(j2_input) |
---|
206 | if ~isempty(j2)% invalid file name if j2 does not exist in the input file |
---|
207 | break |
---|
208 | end |
---|
209 | else |
---|
210 | ref_j=floor((j1+j2)/2); |
---|
211 | end |
---|
212 | end |
---|
213 | end |
---|
214 | % update the detected index series |
---|
215 | ref_i_list(ifile)=ref_i; |
---|
216 | ref_j_list(ifile)=ref_j; |
---|
217 | nb_pairs=0; |
---|
218 | if ~isempty(i2_input)|| ~isempty(j2_input) %deals with pairs |
---|
219 | if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1 |
---|
220 | nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0)); |
---|
221 | end |
---|
222 | end |
---|
223 | i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1; |
---|
224 | if ~isempty(i2_input) |
---|
225 | i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2; |
---|
226 | end |
---|
227 | if ~isempty(j1_input) |
---|
228 | j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1; |
---|
229 | end |
---|
230 | if ~isempty(j2_input) |
---|
231 | j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1; |
---|
232 | j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2; |
---|
233 | end |
---|
234 | end |
---|
235 | % look for the numerical string of the first files to update the NomType (take into account the 0 before the number) |
---|
236 | max_j=max(ref_j_list); |
---|
237 | if isempty(max_j) |
---|
238 | ref_ij=ref_i_list; |
---|
239 | else |
---|
240 | ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i. |
---|
241 | end |
---|
242 | [tild,ifile_min]=min(ref_ij(ref_ij>0)); |
---|
243 | if isempty(ifile_min) |
---|
244 | RootPath=''; |
---|
245 | RootFile=''; |
---|
246 | NomType=''; |
---|
247 | else |
---|
248 | [tild,tild,tild,tild,tild,tild,tild,tild,NomType]=fileparts_uvmat(dirpair(ifile_min).name); |
---|
249 | end |
---|
250 | end |
---|
251 | |
---|
252 | %% set to empty array the irrelevant index series |
---|
253 | if isequal(i1_series,0), i1_series=[]; end |
---|
254 | if isequal(i2_series,0), i2_series=[]; end |
---|
255 | if isequal(j1_series,0), j1_series=[]; end |
---|
256 | if isequal(j2_series,0), j2_series=[]; end |
---|
257 | |
---|
258 | %% sort pairs by decreasing index differences in case of multiple pairs at the same reference index |
---|
259 | if size(i2_series,3)>1 %pairs i1 -i2 |
---|
260 | diff_index=abs(i2_series-i1_series); |
---|
261 | [tild,ind_pair]=sort(diff_index,3,'descend'); |
---|
262 | for ref_i=1:size(i1_series,1) |
---|
263 | for ref_j=1:size(j1_series,2) |
---|
264 | i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
265 | i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
266 | if ~isempty(j1_series) |
---|
267 | j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
268 | end |
---|
269 | end |
---|
270 | end |
---|
271 | elseif size(j2_series,3)>1 %pairs j1 -j2 |
---|
272 | diff_index=abs(j2_series-j1_series); |
---|
273 | [tild,ind_pair]=sort(diff_index,3,'descend'); |
---|
274 | for ref_i=1:size(i1_series,1) |
---|
275 | for ref_j=1:size(j1_series,2) |
---|
276 | i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
277 | if ~isempty(i2_series) |
---|
278 | i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
279 | end |
---|
280 | j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
281 | j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
282 | end |
---|
283 | end |
---|
284 | end |
---|
285 | |
---|