[356] | 1 | %'find_file_series': check the content of an input file and find the corresponding file series |
---|
[332] | 2 | %-------------------------------------------------------------------------- |
---|
[783] | 3 | % function [RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileInfo,Object,i1_input,i2_input,j1_input,j2_input]=find_file_series(FilePath,fileinput,checkxml) |
---|
[332] | 4 | % |
---|
| 5 | % OUTPUT: |
---|
[783] | 6 | % RootPath: path to the dir containing the input file |
---|
| 7 | % SubDir: data dir containing the input file series |
---|
[371] | 8 | % RootFile: root file detected in fileinput, possibly modified for movies (indexing is then done on image view, not file) |
---|
[609] | 9 | % i1_series(pair,ref_j+1, ref_i+1),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 |
---|
[343] | 10 | % (ref_i+1 is used to deal with the image index zero sometimes used) |
---|
[332] | 11 | % NomType: nomenclature type corrected after checking the first file (problem of 0 before the number string) |
---|
[783] | 12 | % FileInfo: structure containing info on the input files (assumed identical on the whole series) |
---|
| 13 | % FileInfo.FileType: type of file, = |
---|
| 14 | % = 'image', usual image as recognised by Matlab |
---|
| 15 | % = 'multimage', image series stored in a single file |
---|
| 16 | % = 'civx', netcdf file with civx convention |
---|
| 17 | % = 'civdata', civ data with new convention |
---|
| 18 | % = 'netcdf' other netcdf files |
---|
| 19 | % = 'video': movie recognised by VideoReader (e;g. avi) |
---|
| 20 | % MovieObject: video object (=[] otherwise |
---|
| 21 | % i1_input,i2_input,j1_input,j2_input: indices of the input file, or of the first file in the series if the input file does not exist |
---|
[332] | 22 | % |
---|
| 23 | %INPUT |
---|
[783] | 24 | % FilePath: path to the directory to be scanned |
---|
[371] | 25 | % fileinput: name (without path) of the input file sample |
---|
[667] | 26 | % checkxml: =1(default) take into account xml file existence to possibly include indexes in RootFile |
---|
| 27 | % =0: do not take into account xml file existence |
---|
[809] | 28 | |
---|
| 29 | %======================================================================= |
---|
| 30 | % Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France |
---|
| 31 | % http://www.legi.grenoble-inp.fr |
---|
| 32 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr |
---|
[332] | 33 | % |
---|
| 34 | % This file is part of the toolbox UVMAT. |
---|
[809] | 35 | % |
---|
[332] | 36 | % UVMAT is free software; you can redistribute it and/or modify |
---|
[809] | 37 | % it under the terms of the GNU General Public License as published |
---|
| 38 | % by the Free Software Foundation; either version 2 of the license, |
---|
| 39 | % or (at your option) any later version. |
---|
| 40 | % |
---|
[332] | 41 | % UVMAT is distributed in the hope that it will be useful, |
---|
| 42 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 43 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
[809] | 44 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
| 45 | %======================================================================= |
---|
[332] | 46 | |
---|
[783] | 47 | function [RootPath,SubDir,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileInfo,MovieObject,i1_input,i2_input,j1_input,j2_input]=find_file_series(FilePath,fileinput,checkxml) |
---|
[332] | 48 | %------------------------------------------------------------------------ |
---|
[398] | 49 | |
---|
[332] | 50 | %% get input root name and nomenclature type |
---|
[398] | 51 | fullfileinput=fullfile(FilePath,fileinput);% input file name with path |
---|
[790] | 52 | [FileInfo,MovieObject]=get_file_info(fullfileinput); |
---|
[332] | 53 | |
---|
| 54 | %% check for particular file types: images, movies, civ data |
---|
[790] | 55 | checkfileindexing=0; |
---|
| 56 | if isfield(FileInfo,'FileIndexing') && strcmp(FileInfo.FileIndexing,'on') |
---|
| 57 | [RootPath,SubDir,RootFile,i1_input,i2_input,j1_input,j2_input,FileExt,NomType]=fileparts_uvmat(fullfileinput); |
---|
| 58 | i1_series=zeros(1,1,1); |
---|
| 59 | i2_series=zeros(1,1,1); |
---|
| 60 | j1_series=zeros(1,1,1); |
---|
| 61 | j2_series=zeros(1,1,1); |
---|
| 62 | checkfileindexing=1; |
---|
| 63 | else % no file indexing |
---|
| 64 | [PathDir,RootFile]=fileparts(fullfileinput); |
---|
| 65 | [RootPath,SubDir,DirExt]=fileparts(PathDir); |
---|
| 66 | SubDir=[SubDir DirExt];% include part after . in the name (considered as a file extension) |
---|
| 67 | NomType='*'; |
---|
| 68 | i1_series=[];i2_series=[];j1_series=[];j2_series=[]; |
---|
| 69 | i1_input=1;i2_input=[];j1_input=[];j2_input=[]; |
---|
[867] | 70 | if ~exist(fullfileinput,'file') |
---|
| 71 | RootFile=''; |
---|
| 72 | return |
---|
| 73 | end |
---|
[790] | 74 | end |
---|
[472] | 75 | if ~exist(FilePath,'dir') |
---|
| 76 | return % don't go further if the dir path does not exist |
---|
| 77 | end |
---|
[790] | 78 | if checkfileindexing |
---|
[445] | 79 | NomTypePref=''; |
---|
[867] | 80 | if isempty(NomType)||strcmp(NomType,'*') |
---|
[371] | 81 | if exist(fullfileinput,'file') |
---|
[398] | 82 | [tild,RootFile]=fileparts(fileinput);% case of constant name (no indexing), get the filename without its extension |
---|
[388] | 83 | else |
---|
[360] | 84 | RootFile=''; |
---|
[339] | 85 | end |
---|
[388] | 86 | else |
---|
[667] | 87 | %% if checkxml=1, possibly include the first index in the root name, if there exists a corresponding xml file |
---|
| 88 | if ~exist('checkxml','var')||checkxml |
---|
| 89 | r=regexp(NomType,'^(?<tiretnum>_|\d+)','names');%look for a number or _1 at the beginning of NomType |
---|
| 90 | if ~isempty(r) %if NomType begins by a number or _1 |
---|
| 91 | fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput |
---|
| 92 | if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not detected |
---|
| 93 | rr=regexp(fileinput_end,'^(?<i1>\d+)','names'); |
---|
| 94 | else% if a separator '_' is detected |
---|
| 95 | rr=regexp(fileinput_end,'^(?<i1>_\d+)','names'); |
---|
| 96 | end |
---|
| 97 | if ~isempty(rr) |
---|
| 98 | RootFile_i=[RootFile rr.i1];% new root file |
---|
| 99 | %look for an xml file correspoonding to the new root name |
---|
| 100 | if exist(fullfile(RootPath,SubDir,[RootFile_i '.xml']),'file') || (strcmp(FileExt,'.nc') && exist(fullfile(RootPath,[RootFile_i '.xml']),'file')) |
---|
| 101 | RootFile=RootFile_i; |
---|
| 102 | NomTypePref=r.tiretnum; |
---|
| 103 | NomType=regexprep(NomType,['^' NomTypePref],''); |
---|
| 104 | i1_input=j1_input; |
---|
| 105 | i2_input=j2_input; |
---|
| 106 | j1_input=[]; |
---|
| 107 | j2_input=[]; |
---|
| 108 | end |
---|
| 109 | end |
---|
[384] | 110 | end |
---|
| 111 | end |
---|
[360] | 112 | %% analyse the list of existing files when relevant |
---|
| 113 | sep1=''; |
---|
[558] | 114 | sep2=''; |
---|
[388] | 115 | i1_str='(?<i1>)';%will set i1=[]; |
---|
[360] | 116 | i1_star=''; |
---|
[388] | 117 | i2_str='(?<i2>)';%will set i2=[]; |
---|
[360] | 118 | i2_star=''; |
---|
[388] | 119 | j1_str='(?<j1>)';%will set j1=[]; |
---|
[360] | 120 | j1_star=''; |
---|
[388] | 121 | j2_str='(?<j2>)';%will set j2=[]; |
---|
[360] | 122 | j2_star=''; |
---|
[388] | 123 | %Look for cases with letter indexing for the second index |
---|
[558] | 124 | r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<sep2>_?)(?<j1>[a|A])(?<j2>[b|B]?)$','names'); |
---|
[360] | 125 | if ~isempty(r) |
---|
[388] | 126 | sep1=r.sep1; |
---|
[558] | 127 | sep2=r.sep2; |
---|
[399] | 128 | i1_str='(?<i1>\d+)'; |
---|
[558] | 129 | i1_star='*'; |
---|
[399] | 130 | if strcmp(lower(r.j1),r.j1)% lower case index |
---|
[388] | 131 | j1_str='(?<j1>[a-z])'; |
---|
| 132 | else |
---|
[399] | 133 | j1_str='(?<j1>[A-Z])'; % upper case index |
---|
[360] | 134 | end |
---|
[388] | 135 | j1_star='*'; |
---|
| 136 | if ~isempty(r.j2) |
---|
| 137 | if strcmp(lower(r.j1),r.j1) |
---|
| 138 | j2_str='(?<j2>[a-z])'; |
---|
| 139 | else |
---|
| 140 | j2_str='(?<j2>[A-Z])'; |
---|
| 141 | end |
---|
| 142 | j2_star='*'; |
---|
[360] | 143 | end |
---|
[388] | 144 | else %numerical indexing |
---|
| 145 | r=regexp(NomType,'^(?<sep1>_?)(?<i1>\d+)(?<i2>(-\d+)?)(?<j1>(_\d+)?)(?<j2>(-\d+)?)$','names'); |
---|
| 146 | if ~isempty(r) |
---|
| 147 | sep1=r.sep1; |
---|
| 148 | i1_str='(?<i1>\d+)'; |
---|
| 149 | i1_star='*'; |
---|
| 150 | if ~isempty(r.i2) |
---|
| 151 | i2_str='(?<i2>-\d+)'; |
---|
| 152 | i2_star='-*'; |
---|
[360] | 153 | end |
---|
[388] | 154 | if ~isempty(r.j1) |
---|
| 155 | j1_str='(?<j1>_\d+)'; |
---|
| 156 | j1_star='_*'; |
---|
[360] | 157 | end |
---|
[388] | 158 | if ~isempty(r.j2) |
---|
| 159 | j2_str='(?<j2>-\d+)'; |
---|
| 160 | j2_star='-*'; |
---|
[360] | 161 | end |
---|
| 162 | end |
---|
[332] | 163 | end |
---|
[558] | 164 | detect_string=['^' RootFile sep1 i1_str i2_str sep2 j1_str j2_str FileExt '$'];%string used in regexp to detect file indices |
---|
[360] | 165 | %find the string used to extract the relevant files with the command dir |
---|
[558] | 166 | star_string=[RootFile sep1 i1_star i2_star sep2 j1_star j2_star FileExt]; |
---|
[360] | 167 | wd=pwd;%current working directory |
---|
[398] | 168 | cd (FilePath)% move to the local dir to save time in the operation dir. |
---|
[388] | 169 | dirpair=dir(star_string);% look for relevant files in the file directory |
---|
[360] | 170 | cd(wd) |
---|
[339] | 171 | nbpair=numel(dirpair); |
---|
| 172 | ref_i_list=zeros(1,nbpair); |
---|
| 173 | ref_j_list=zeros(1,nbpair); |
---|
| 174 | if nbpair==0% no detected file |
---|
| 175 | RootFile=''; |
---|
| 176 | end |
---|
[360] | 177 | % scan the list of relevant files, extract the indices |
---|
[339] | 178 | for ifile=1:nbpair |
---|
[360] | 179 | rr=regexp(dirpair(ifile).name,detect_string,'names'); |
---|
[362] | 180 | if ~isempty(rr) |
---|
[388] | 181 | i1=str2num(rr.i1); |
---|
| 182 | i2=str2num(regexprep(rr.i2,'^-','')); |
---|
| 183 | j1=stra2num(regexprep(rr.j1,'^_','')); |
---|
| 184 | j2=stra2num(regexprep(rr.j2,'^-','')); |
---|
| 185 | ref_i=i1; |
---|
| 186 | if isempty(i2_input) |
---|
| 187 | if ~isempty(i2)% invalid file name if i2 does not exist in the input file |
---|
| 188 | break |
---|
| 189 | end |
---|
| 190 | else |
---|
| 191 | ref_i=floor((i1+i2)/2); |
---|
[339] | 192 | end |
---|
[388] | 193 | ref_j=1; |
---|
| 194 | if isempty(j1_input) |
---|
| 195 | if ~isempty(j1)% invalid file name if j1 does not exist in the input file |
---|
| 196 | break |
---|
| 197 | end |
---|
| 198 | else %j1_input is not empty |
---|
| 199 | if isempty(j1)% the detected name does not fit with the input |
---|
| 200 | break |
---|
| 201 | else |
---|
| 202 | ref_j=j1; |
---|
| 203 | if isempty(j2_input) |
---|
| 204 | if ~isempty(j2)% invalid file name if j2 does not exist in the input file |
---|
| 205 | break |
---|
| 206 | end |
---|
| 207 | else |
---|
| 208 | ref_j=floor((j1+j2)/2); |
---|
[342] | 209 | end |
---|
[339] | 210 | end |
---|
| 211 | end |
---|
[388] | 212 | % update the detected index series |
---|
[788] | 213 | if ~isempty(ref_i)&&~isempty(ref_j) |
---|
| 214 | ref_i_list(ifile)=ref_i; |
---|
| 215 | ref_j_list(ifile)=ref_j; |
---|
| 216 | nb_pairs=0; |
---|
| 217 | if ~isempty(i2_input)|| ~isempty(j2_input) %deals with pairs |
---|
| 218 | if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1 |
---|
| 219 | nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0)); |
---|
| 220 | end |
---|
[388] | 221 | end |
---|
[788] | 222 | if i1==0 |
---|
| 223 | i1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0) |
---|
| 224 | end |
---|
| 225 | if j1==0 |
---|
| 226 | j1=-1;% set index 0 to -1 to distinguish from the absent index (set to 0) |
---|
| 227 | end |
---|
| 228 | i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1; |
---|
| 229 | if ~isempty(i2_input) |
---|
| 230 | i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2; |
---|
| 231 | end |
---|
| 232 | if ~isempty(j1_input) |
---|
| 233 | j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1; |
---|
| 234 | end |
---|
| 235 | if ~isempty(j2_input) |
---|
| 236 | j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1; |
---|
| 237 | j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2; |
---|
| 238 | end |
---|
[339] | 239 | end |
---|
[334] | 240 | end |
---|
[339] | 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); |
---|
[350] | 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 |
---|
[456] | 249 | ind_select=find(ref_ij>0); |
---|
[788] | 250 | |
---|
[456] | 251 | if isempty(ind_select) |
---|
[788] | 252 | % RootFile=''; |
---|
| 253 | % NomType=''; |
---|
[350] | 254 | else |
---|
[456] | 255 | [tild,ifile_min]=min(ref_ij(ind_select)); |
---|
[494] | 256 | [tild,tild,tild,tild,tild,tild,tild,tild,NomType]=fileparts_uvmat(dirpair(ind_select(ifile_min)).name);% update the representation of indices (number of 0 before the number) |
---|
[384] | 257 | NomType=regexprep(NomType,['^' NomTypePref],''); |
---|
[497] | 258 | %% update the file type if the input file does not exist (pb of 0001) |
---|
[783] | 259 | if isempty(FileInfo.FileType) |
---|
[784] | 260 | [FileInfo,MovieObject]=get_file_info(fullfile(FilePath,dirpair(ifile_min).name)); |
---|
[497] | 261 | end |
---|
[350] | 262 | end |
---|
[339] | 263 | end |
---|
| 264 | |
---|
| 265 | %% set to empty array the irrelevant index series |
---|
| 266 | if isequal(i1_series,0), i1_series=[]; end |
---|
| 267 | if isequal(i2_series,0), i2_series=[]; end |
---|
| 268 | if isequal(j1_series,0), j1_series=[]; end |
---|
| 269 | if isequal(j2_series,0), j2_series=[]; end |
---|
[784] | 270 | end |
---|
[790] | 271 | % %% detect rdvision format |
---|
| 272 | % if strcmp(FileExt,'.bin') |
---|
| 273 | % if exist(fullfile(RootPath,SubDir,[RootFile '.seq']),'file') |
---|
| 274 | % FileInfo.FileType='rdvision'; |
---|
| 275 | % FileInfo.SeqFile=[RootFile '.seq']; |
---|
| 276 | % end |
---|
| 277 | % end |
---|
[784] | 278 | |
---|
[445] | 279 | %% introduce the frame index in case of movies or multimage type |
---|
[484] | 280 | if isfield(FileInfo,'NumberOfFrames') && FileInfo.NumberOfFrames >1 |
---|
[783] | 281 | if isempty(i1_series)% if there is no file index, i denotes the frame index |
---|
[609] | 282 | i1_series=zeros(FileInfo.NumberOfFrames+1,2);% first column =0 |
---|
| 283 | i1_series(:,2)=(0:FileInfo.NumberOfFrames)'; % second column=frame index -1 |
---|
[445] | 284 | i1_input=1; |
---|
[483] | 285 | NomType='*'; |
---|
[783] | 286 | else % if there is a file index, j denotes the frame index while i denotes the file index |
---|
[788] | 287 | if ~isempty(regexp(NomType,'ab$', 'once'))% recognized as a pair |
---|
[783] | 288 | RootFile=fullfile_uvmat('','',RootFile,'',NomType,i1_input,i2_input,j1_input,j2_input);% restitute the root name without the detected indices |
---|
| 289 | i1_series=zeros(FileInfo.NumberOfFrames+1,2);% first column =0 |
---|
| 290 | i1_series(:,2)=(0:FileInfo.NumberOfFrames)'; % second column=frame index -1 |
---|
| 291 | j1_series=[]; |
---|
| 292 | i1_input=1; |
---|
| 293 | NomType='*'; |
---|
| 294 | else |
---|
| 295 | i1_series=i1_series(:,2)*ones(1,FileInfo.NumberOfFrames);% |
---|
| 296 | i1_series=[zeros(size(i1_series,1),1) i1_series]; |
---|
| 297 | j1_series=ones(size(i1_series,1),1)*(1:FileInfo.NumberOfFrames);% |
---|
| 298 | j1_series=[zeros(size(i1_series,1),1) j1_series]; |
---|
| 299 | % include the first index in the root name |
---|
| 300 | r=regexp(NomType,'^(?<tiretnum>_?\d+)','names');%look for a number or _1 at the beginning of NomType |
---|
| 301 | if ~isempty(r) |
---|
| 302 | fileinput_end=regexprep(fileinput,['^' RootFile],'');%remove RootFile at the beginning of fileinput |
---|
| 303 | if isempty(regexp(r.tiretnum,'^_','once'))% if a separator '_' is not detected |
---|
| 304 | rr=regexp(fileinput_end,'^(?<i1>\d+)','names'); |
---|
| 305 | else% if a separator '_' is detected |
---|
| 306 | rr=regexp(fileinput_end,'^(?<i1>_\d+)','names'); |
---|
| 307 | end |
---|
| 308 | if ~isempty(rr) |
---|
| 309 | j1_input=1; |
---|
| 310 | j2_input=[]; |
---|
| 311 | end |
---|
[483] | 312 | end |
---|
| 313 | end |
---|
[445] | 314 | end |
---|
| 315 | end |
---|
| 316 | |
---|
[339] | 317 | %% sort pairs by decreasing index differences in case of multiple pairs at the same reference index |
---|
| 318 | if size(i2_series,3)>1 %pairs i1 -i2 |
---|
| 319 | diff_index=abs(i2_series-i1_series); |
---|
[353] | 320 | [tild,ind_pair]=sort(diff_index,3,'descend'); |
---|
[339] | 321 | for ref_i=1:size(i1_series,1) |
---|
| 322 | for ref_j=1:size(j1_series,2) |
---|
| 323 | i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 324 | i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 325 | if ~isempty(j1_series) |
---|
| 326 | j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 327 | end |
---|
| 328 | end |
---|
[334] | 329 | end |
---|
[339] | 330 | elseif size(j2_series,3)>1 %pairs j1 -j2 |
---|
| 331 | diff_index=abs(j2_series-j1_series); |
---|
[353] | 332 | [tild,ind_pair]=sort(diff_index,3,'descend'); |
---|
[339] | 333 | for ref_i=1:size(i1_series,1) |
---|
| 334 | for ref_j=1:size(j1_series,2) |
---|
| 335 | i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 336 | if ~isempty(i2_series) |
---|
| 337 | i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 338 | end |
---|
| 339 | j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 340 | j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 341 | end |
---|
| 342 | end |
---|
| 343 | end |
---|
[512] | 344 | i1_series=permute(i1_series,[3 2 1]);% permute dimensions |
---|
| 345 | i2_series=permute(i2_series,[3 2 1]);% permute dimensions |
---|
| 346 | j1_series=permute(j1_series,[3 2 1]);% permute dimensions |
---|
| 347 | j2_series=permute(j2_series,[3 2 1]);% permute dimensions |
---|