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