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