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