[356] | 1 | %'find_file_series': check the content of an input file and find the corresponding file series |
---|
[332] | 2 | %-------------------------------------------------------------------------- |
---|
[398] | 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) |
---|
[332] | 4 | % |
---|
| 5 | % OUTPUT: |
---|
[371] | 6 | % RootFile: root file detected in fileinput, possibly modified for movies (indexing is then done on image view, not file) |
---|
[343] | 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) |
---|
[332] | 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 |
---|
[371] | 20 | % RootPath: path to the directory to be scanned |
---|
| 21 | % fileinput: name (without path) of the input file sample |
---|
[332] | 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 | |
---|
[398] | 39 | function [RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,Object,i1_input,i2_input,j1_input,j2_input]=find_file_series(FilePath,fileinput) |
---|
[332] | 40 | %------------------------------------------------------------------------ |
---|
[398] | 41 | |
---|
[332] | 42 | %% get input root name and nomenclature type |
---|
[398] | 43 | fullfileinput=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); |
---|
[332] | 45 | |
---|
[398] | 46 | |
---|
[332] | 47 | %% check for particular file types: images, movies, civ data |
---|
[339] | 48 | i1_series=zeros(1,1,1); |
---|
| 49 | i2_series=zeros(1,1,1); |
---|
| 50 | j1_series=zeros(1,1,1); |
---|
| 51 | j2_series=zeros(1,1,1); |
---|
[386] | 52 | % ifile_min=1;%default |
---|
[376] | 53 | [FileType,FileInfo,Object]=get_file_type(fullfileinput); |
---|
[445] | 54 | NbFrame=1; |
---|
| 55 | |
---|
[439] | 56 | switch FileType |
---|
| 57 | case 'multimage' |
---|
[445] | 58 | % NomType='*'; |
---|
| 59 | % i1_series=(1:FileInfo.NbFrame)'; |
---|
| 60 | NbFrame=FileInfo.NbFrame; |
---|
[439] | 61 | case {'video','mmreader'} |
---|
| 62 | NomType='*'; |
---|
[445] | 63 | NbFrame=FileInfo.NumberOfFrames; |
---|
| 64 | % i1_series=(1:FileInfo.NumberOfFrames)'; |
---|
[439] | 65 | end |
---|
| 66 | % if strcmp( FileType,'multimage')||strcmp( FileType,'video')||strcmp( FileType,'mmreader') |
---|
| 67 | % NomType='*'; |
---|
| 68 | % i1_series=(1:FileInfo.NumberOfFrames)'; |
---|
| 69 | % end |
---|
[445] | 70 | RootFile_i=''; |
---|
| 71 | NomTypePref=''; |
---|
[398] | 72 | if strcmp(NomType,'')||strcmp(NomType,'*') |
---|
[371] | 73 | if exist(fullfileinput,'file') |
---|
[398] | 74 | [tild,RootFile]=fileparts(fileinput);% case of constant name (no indexing), get the filename without its extension |
---|
[388] | 75 | else |
---|
[360] | 76 | RootFile=''; |
---|
[339] | 77 | end |
---|
[398] | 78 | i1_input=1;% the index now refer to the frame in the movie, choose 1 at opening |
---|
| 79 | i2_input=[]; |
---|
| 80 | j1_input=[]; |
---|
| 81 | j2_input=[]; |
---|
[388] | 82 | else |
---|
[395] | 83 | %% possibly include the first index in the root name, if there exists a corresponding xml file |
---|
[385] | 84 | r=regexp(NomType,'^(?<tiretnum>_?\d+)','names');%look for a number or _1 at the beginning of NomType |
---|
[384] | 85 | if ~isempty(r) |
---|
[388] | 86 | fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput |
---|
[395] | 87 | if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not detected |
---|
[388] | 88 | rr=regexp(fileinput_end,'^(?<i1>\d+)','names'); |
---|
| 89 | else% if a separator '_' is detected |
---|
| 90 | rr=regexp(fileinput_end,'^(?<i1>_\d+)','names'); |
---|
[384] | 91 | end |
---|
[388] | 92 | if ~isempty(rr) |
---|
[445] | 93 | RootFile_i=[RootFile rr.i1]; |
---|
| 94 | % checkpair=~isempty(regexp(NomType,'-','once'))||~isempty(regexp(NomType,'ab$','once'))||~isempty(regexp(NomType,'AB$','once'));%case of PIV results |
---|
| 95 | if exist(fullfile(RootPath,SubDir,[RootFile_i '.xml']),'file') %|| (checkpair && exist(fullfile(fileparts(RootPath),[RootFile_i '.xml']),'file')) |
---|
| 96 | RootFile=RootFile_i; |
---|
[391] | 97 | NomTypePref=r.tiretnum; |
---|
[388] | 98 | NomType=regexprep(NomType,['^' NomTypePref],''); |
---|
[398] | 99 | i1_input=j1_input; |
---|
[388] | 100 | i2_input=j2_input; |
---|
| 101 | j1_input=[]; |
---|
| 102 | j2_input=[]; |
---|
[395] | 103 | end |
---|
[384] | 104 | end |
---|
| 105 | end |
---|
[360] | 106 | %% analyse the list of existing files when relevant |
---|
| 107 | sep1=''; |
---|
[388] | 108 | i1_str='(?<i1>)';%will set i1=[]; |
---|
[360] | 109 | i1_star=''; |
---|
[388] | 110 | % r.sep2=''; |
---|
| 111 | i2_str='(?<i2>)';%will set i2=[]; |
---|
[360] | 112 | i2_star=''; |
---|
[388] | 113 | % sep3=''; |
---|
| 114 | j1_str='(?<j1>)';%will set j1=[]; |
---|
[360] | 115 | j1_star=''; |
---|
[388] | 116 | % sep4=''; |
---|
| 117 | j2_str='(?<j2>)';%will set j2=[]; |
---|
[360] | 118 | j2_star=''; |
---|
[388] | 119 | % NomTypeStr=NomType; |
---|
| 120 | %Look for cases with letter indexing for the second index |
---|
| 121 | r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<j1>[a|A])(?<j2>[b|B]?)$','names'); |
---|
[360] | 122 | if ~isempty(r) |
---|
[388] | 123 | sep1=r.sep1; |
---|
[399] | 124 | i1_str='(?<i1>\d+)'; |
---|
| 125 | if strcmp(lower(r.j1),r.j1)% lower case index |
---|
[388] | 126 | j1_str='(?<j1>[a-z])'; |
---|
| 127 | else |
---|
[399] | 128 | j1_str='(?<j1>[A-Z])'; % upper case index |
---|
[360] | 129 | end |
---|
[388] | 130 | j1_star='*'; |
---|
| 131 | if ~isempty(r.j2) |
---|
| 132 | if strcmp(lower(r.j1),r.j1) |
---|
| 133 | j2_str='(?<j2>[a-z])'; |
---|
| 134 | else |
---|
| 135 | j2_str='(?<j2>[A-Z])'; |
---|
| 136 | end |
---|
| 137 | j2_star='*'; |
---|
[360] | 138 | end |
---|
[388] | 139 | else %numerical indexing |
---|
| 140 | r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<i2>(-\d+)?)(?<j1>(_\d+)?)(?<j2>(-\d+)?)$','names'); |
---|
| 141 | if ~isempty(r) |
---|
| 142 | sep1=r.sep1; |
---|
| 143 | i1_str='(?<i1>\d+)'; |
---|
| 144 | i1_star='*'; |
---|
| 145 | if ~isempty(r.i2) |
---|
| 146 | i2_str='(?<i2>-\d+)'; |
---|
| 147 | i2_star='-*'; |
---|
[360] | 148 | end |
---|
[388] | 149 | if ~isempty(r.j1) |
---|
| 150 | j1_str='(?<j1>_\d+)'; |
---|
| 151 | j1_star='_*'; |
---|
[360] | 152 | end |
---|
[388] | 153 | if ~isempty(r.j2) |
---|
| 154 | j2_str='(?<j2>-\d+)'; |
---|
| 155 | j2_star='-*'; |
---|
[360] | 156 | end |
---|
| 157 | end |
---|
[332] | 158 | end |
---|
[388] | 159 | detect_string=['^' RootFile sep1 i1_str i2_str j1_str j2_str FileExt '$'];%string used in regexp to detect file indices |
---|
[360] | 160 | %find the string used to extract the relevant files with the command dir |
---|
[388] | 161 | star_string=[RootFile sep1 i1_star i2_star j1_star j2_star FileExt]; |
---|
[360] | 162 | wd=pwd;%current working directory |
---|
[398] | 163 | cd (FilePath)% move to the local dir to save time in the operation dir. |
---|
[388] | 164 | dirpair=dir(star_string);% look for relevant files in the file directory |
---|
[360] | 165 | cd(wd) |
---|
[339] | 166 | nbpair=numel(dirpair); |
---|
| 167 | ref_i_list=zeros(1,nbpair); |
---|
| 168 | ref_j_list=zeros(1,nbpair); |
---|
| 169 | if nbpair==0% no detected file |
---|
| 170 | RootFile=''; |
---|
| 171 | end |
---|
[360] | 172 | % scan the list of relevant files, extract the indices |
---|
[339] | 173 | for ifile=1:nbpair |
---|
[360] | 174 | rr=regexp(dirpair(ifile).name,detect_string,'names'); |
---|
[362] | 175 | if ~isempty(rr) |
---|
[388] | 176 | i1=str2num(rr.i1); |
---|
| 177 | i2=str2num(regexprep(rr.i2,'^-','')); |
---|
| 178 | j1=stra2num(regexprep(rr.j1,'^_','')); |
---|
| 179 | j2=stra2num(regexprep(rr.j2,'^-','')); |
---|
| 180 | ref_i=i1; |
---|
| 181 | if isempty(i2_input) |
---|
| 182 | if ~isempty(i2)% invalid file name if i2 does not exist in the input file |
---|
| 183 | break |
---|
| 184 | end |
---|
| 185 | else |
---|
| 186 | ref_i=floor((i1+i2)/2); |
---|
[339] | 187 | end |
---|
[388] | 188 | ref_j=1; |
---|
| 189 | if isempty(j1_input) |
---|
| 190 | if ~isempty(j1)% invalid file name if j1 does not exist in the input file |
---|
| 191 | break |
---|
| 192 | end |
---|
| 193 | else %j1_input is not empty |
---|
| 194 | if isempty(j1)% the detected name does not fit with the input |
---|
| 195 | break |
---|
| 196 | else |
---|
| 197 | ref_j=j1; |
---|
| 198 | if isempty(j2_input) |
---|
| 199 | if ~isempty(j2)% invalid file name if j2 does not exist in the input file |
---|
| 200 | break |
---|
| 201 | end |
---|
| 202 | else |
---|
| 203 | ref_j=floor((j1+j2)/2); |
---|
[342] | 204 | end |
---|
[339] | 205 | end |
---|
| 206 | end |
---|
[388] | 207 | % update the detected index series |
---|
| 208 | ref_i_list(ifile)=ref_i; |
---|
| 209 | ref_j_list(ifile)=ref_j; |
---|
| 210 | nb_pairs=0; |
---|
| 211 | if ~isempty(i2_input)|| ~isempty(j2_input) %deals with pairs |
---|
| 212 | if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1 |
---|
| 213 | nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0)); |
---|
| 214 | end |
---|
[339] | 215 | end |
---|
[388] | 216 | i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1; |
---|
| 217 | if ~isempty(i2_input) |
---|
| 218 | i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2; |
---|
| 219 | end |
---|
| 220 | if ~isempty(j1_input) |
---|
| 221 | j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1; |
---|
| 222 | end |
---|
| 223 | if ~isempty(j2_input) |
---|
| 224 | j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1; |
---|
| 225 | j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2; |
---|
| 226 | end |
---|
[334] | 227 | end |
---|
[339] | 228 | end |
---|
| 229 | % look for the numerical string of the first files to update the NomType (take into account the 0 before the number) |
---|
| 230 | max_j=max(ref_j_list); |
---|
[350] | 231 | if isempty(max_j) |
---|
| 232 | ref_ij=ref_i_list; |
---|
| 233 | else |
---|
| 234 | ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i. |
---|
| 235 | end |
---|
[421] | 236 | %[tild,ifile_min]=min(ref_ij(ref_ij>0)); |
---|
| 237 | ifile_min=find(ref_ij>0 , 1); |
---|
[360] | 238 | if isempty(ifile_min) |
---|
[388] | 239 | % RootPath=''; |
---|
[350] | 240 | RootFile=''; |
---|
| 241 | NomType=''; |
---|
| 242 | else |
---|
[372] | 243 | [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) |
---|
[384] | 244 | NomType=regexprep(NomType,['^' NomTypePref],''); |
---|
[350] | 245 | end |
---|
[386] | 246 | %% update the file type if the input file does not exist (pb of 0001) |
---|
| 247 | if isempty(FileType) |
---|
| 248 | [FileType,tild,Object]=get_file_type(dirpair(ifile_min).name); |
---|
| 249 | end |
---|
[339] | 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 | |
---|
[445] | 258 | %% introduce the frame index in case of movies or multimage type |
---|
| 259 | if NbFrame>1 |
---|
| 260 | if isempty(i1_series) |
---|
| 261 | i1_series=(1:NbFrame)'; |
---|
| 262 | i1_input=1; |
---|
| 263 | else |
---|
| 264 | j1_series=(1:NbFrame)'; |
---|
| 265 | j1_input=1; |
---|
| 266 | end |
---|
| 267 | end |
---|
| 268 | |
---|
[339] | 269 | %% sort pairs by decreasing index differences in case of multiple pairs at the same reference index |
---|
| 270 | if size(i2_series,3)>1 %pairs i1 -i2 |
---|
| 271 | diff_index=abs(i2_series-i1_series); |
---|
[353] | 272 | [tild,ind_pair]=sort(diff_index,3,'descend'); |
---|
[339] | 273 | for ref_i=1:size(i1_series,1) |
---|
| 274 | for ref_j=1:size(j1_series,2) |
---|
| 275 | i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 276 | i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 277 | if ~isempty(j1_series) |
---|
| 278 | j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 279 | end |
---|
| 280 | end |
---|
[334] | 281 | end |
---|
[339] | 282 | elseif size(j2_series,3)>1 %pairs j1 -j2 |
---|
| 283 | diff_index=abs(j2_series-j1_series); |
---|
[353] | 284 | [tild,ind_pair]=sort(diff_index,3,'descend'); |
---|
[339] | 285 | for ref_i=1:size(i1_series,1) |
---|
| 286 | for ref_j=1:size(j1_series,2) |
---|
| 287 | i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 288 | if ~isempty(i2_series) |
---|
| 289 | i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 290 | end |
---|
| 291 | j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 292 | j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 293 | end |
---|
| 294 | end |
---|
| 295 | end |
---|
| 296 | |
---|
[445] | 297 | % %% deals with frame index in movies |
---|
| 298 | % switch FileType |
---|
| 299 | % case 'multimage' |
---|
| 300 | % NomType='*'; |
---|
| 301 | % i1_series=(1:FileInfo.NbFrame)'; |
---|
| 302 | % case {'video','mmreader'} |
---|
| 303 | % NomType='*'; |
---|
| 304 | % i1_series=(1:FileInfo.NumberOfFrames)'; |
---|
| 305 | % end |
---|
| 306 | |
---|