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