[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: |
---|
[339] | 6 | % RootPath,RootFile: root path and root name 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 |
---|
| 20 | % fileinput: name (including path) of the input file |
---|
| 21 | % |
---|
| 22 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
| 23 | % Copyright 2011, LEGI / CNRS-UJF-INPG, joel.sommeria@legi.grenoble-inp.fr |
---|
| 24 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
| 25 | % This file is part of the toolbox UVMAT. |
---|
| 26 | % |
---|
| 27 | % UVMAT is free software; you can redistribute it and/or modify |
---|
| 28 | % it under the terms of the GNU General Public License as published by |
---|
| 29 | % the Free Software Foundation; either version 2 of the License, or |
---|
| 30 | % (at your option) any later version. |
---|
| 31 | % |
---|
| 32 | % UVMAT is distributed in the hope that it will be useful, |
---|
| 33 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 34 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 35 | % GNU General Public License (file UVMAT/COPYING.txt) for more details. |
---|
| 36 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
| 37 | |
---|
[339] | 38 | function [RootPath,RootFile,i1_series,i2_series,j1_series,j2_series,NomType,FileType,Object]=find_file_series(fileinput) |
---|
[332] | 39 | %------------------------------------------------------------------------ |
---|
| 40 | |
---|
| 41 | %% get input root name and nomenclature type |
---|
[353] | 42 | [RootPath,SubDir,RootFile,tild,i2_input,j1_input,j2_input,FileExt,NomType]=fileparts_uvmat(fileinput); |
---|
[332] | 43 | |
---|
| 44 | %% check for particular file types: images, movies, civ data |
---|
| 45 | FileType=''; |
---|
| 46 | Object=[]; |
---|
[339] | 47 | i1_series=zeros(1,1,1); |
---|
| 48 | i2_series=zeros(1,1,1); |
---|
| 49 | j1_series=zeros(1,1,1); |
---|
| 50 | j2_series=zeros(1,1,1); |
---|
[334] | 51 | switch FileExt |
---|
| 52 | % ancillary files, no field indexing |
---|
| 53 | case {'.civ','.log','.cmx','.cmx2','.txt','.bat'} |
---|
| 54 | FileType='txt'; |
---|
| 55 | NomType=''; |
---|
| 56 | case '.fig' |
---|
| 57 | FileType='figure'; |
---|
| 58 | NomType=''; |
---|
| 59 | case '.xml' |
---|
| 60 | FileType='xml'; |
---|
| 61 | NomType=''; |
---|
| 62 | case '.xls' |
---|
| 63 | FileType='xls'; |
---|
| 64 | NomType=''; |
---|
| 65 | otherwise |
---|
| 66 | if ~isempty(FileExt)&& ~isempty(imformats(FileExt(2:end))) |
---|
[339] | 67 | try |
---|
| 68 | imainfo=imfinfo(fileinput); |
---|
| 69 | FileType='image'; |
---|
| 70 | if length(imainfo) >1 %case of image with multiple frames |
---|
| 71 | NomType='*'; |
---|
| 72 | FileType='multimage'; |
---|
[354] | 73 | i1_series=(1:length(imainfo))'; |
---|
[339] | 74 | [RootPath,RootFile]=fileparts(fileinput); |
---|
| 75 | end |
---|
[334] | 76 | end |
---|
[332] | 77 | else |
---|
[334] | 78 | try |
---|
| 79 | Data=nc2struct(fileinput,'ListGlobalAttribute','absolut_time_T0','Conventions'); |
---|
| 80 | if ~isempty(Data.absolut_time_T0') |
---|
| 81 | FileType='civx'; % test for civx velocity fields |
---|
[354] | 82 | elseif strcmp(Data.Conventions,'uvmat/civdata') |
---|
[334] | 83 | FileType='civdata'; % test for civx velocity fields |
---|
| 84 | else |
---|
| 85 | FileType='netcdf'; |
---|
| 86 | end |
---|
| 87 | end |
---|
| 88 | try |
---|
[354] | 89 | if exist('VideoReader','file')%recent version of Matlab |
---|
[360] | 90 | Object=VideoReader(fileinput); |
---|
[354] | 91 | else |
---|
[360] | 92 | Object=mmreader(fileinput);%older Matlab function for movies |
---|
[354] | 93 | end |
---|
[334] | 94 | NomType='*'; |
---|
[360] | 95 | FileType='video'; |
---|
[354] | 96 | i1_series=(1:get(Object,'NumberOfFrames'))'; |
---|
[334] | 97 | end |
---|
| 98 | end |
---|
[332] | 99 | end |
---|
[339] | 100 | |
---|
[334] | 101 | if strcmp(NomType,'')||strcmp(NomType,'*') |
---|
[339] | 102 | if exist(fileinput,'file') |
---|
[360] | 103 | [RootPath,RootFile]=fileparts(fileinput);% case of constant name (no indexing) |
---|
[339] | 104 | else |
---|
[360] | 105 | RootPath=''; |
---|
| 106 | RootFile=''; |
---|
[339] | 107 | end |
---|
[360] | 108 | else |
---|
| 109 | %% analyse the list of existing files when relevant |
---|
| 110 | sep1=''; |
---|
| 111 | i1_str='(?<i1>)'; |
---|
| 112 | i1_star=''; |
---|
| 113 | sep2=''; |
---|
| 114 | i2_str='(?<i2>)'; |
---|
| 115 | i2_star=''; |
---|
| 116 | sep3=''; |
---|
| 117 | j1_str='(?<j1>)'; |
---|
| 118 | j1_star=''; |
---|
| 119 | sep4=''; |
---|
| 120 | j2_str='(?<j2>)'; |
---|
| 121 | j2_star=''; |
---|
| 122 | NomTypeStr=NomType; |
---|
| 123 | if ~isempty(regexp(NomTypeStr,'^_\d')) |
---|
| 124 | sep1='_'; |
---|
| 125 | NomTypeStr(1)=[];%remove '_' from the beginning of NomTypeStr |
---|
[332] | 126 | end |
---|
[360] | 127 | r=regexp(NomTypeStr,'^(?<num1>\d+)','names');%look for a number at the beginning of NomTypeStr |
---|
| 128 | if ~isempty(r) |
---|
| 129 | i1_str='(?<i1>\d+)'; |
---|
| 130 | i1_star='*'; |
---|
| 131 | NomTypeStr=regexprep(NomTypeStr,['^' r.num1],''); |
---|
| 132 | r=regexp(NomTypeStr,'^-(?<num2>\d+)','names');%look for a pair i1-i2 |
---|
| 133 | if ~isempty(r) |
---|
| 134 | sep2='-'; |
---|
| 135 | i2_str='(?<i2>\d+)'; |
---|
| 136 | i2_star='*'; |
---|
| 137 | NomTypeStr=regexprep(NomTypeStr,['^-' r.num2],''); |
---|
| 138 | end |
---|
| 139 | if ~isempty(regexp(NomTypeStr,'^_')); |
---|
| 140 | sep3='_'; |
---|
| 141 | NomTypeStr(1)=[];%remove '_' from the beginning of NomTypeStr |
---|
| 142 | end |
---|
| 143 | if ~isempty(regexp(NomTypeStr,'^[a|A]')); |
---|
| 144 | j1_str='(?<j1>[a-z]|[A-Z])'; |
---|
| 145 | j1_star='*'; |
---|
| 146 | if ~isempty(regexp(NomTypeStr,'[b|B]$')); |
---|
| 147 | j2_str='(?<j1>[a-z]|[A-Z])'; |
---|
| 148 | j2_star='*'; |
---|
| 149 | end |
---|
| 150 | else |
---|
| 151 | r=regexp(NomTypeStr,'^(?<num3>\d+)','names'); |
---|
| 152 | if ~isempty(r) |
---|
| 153 | j1_str='(?<j1>\d+)'; |
---|
| 154 | j1_star='*'; |
---|
| 155 | NomTypeStr=regexprep(NomTypeStr,['^' r.num3],''); |
---|
| 156 | end |
---|
| 157 | r=regexp(NomTypeStr,'-(?<num4>\d+)','names'); |
---|
| 158 | if ~isempty(r) |
---|
| 159 | sep4='-'; |
---|
| 160 | j2_str='(?<j2>\d+)'; |
---|
| 161 | j2_star='*'; |
---|
| 162 | end |
---|
| 163 | end |
---|
[332] | 164 | end |
---|
[360] | 165 | detect_string=[sep1 i1_str sep2 i2_str sep3 j1_str sep4 j2_str];%string used in regexp to detect file indices |
---|
| 166 | %find the string used to extract the relevant files with the command dir |
---|
| 167 | star_string=['*' sep1 i1_star sep2 i2_star sep3 j1_star sep4 j2_star '*']; |
---|
| 168 | wd=pwd;%current working directory |
---|
| 169 | RR=fullfile(RootPath,SubDir); |
---|
| 170 | cd (RR)% move to the local dir to save time in the operation dir. |
---|
| 171 | dirpair=dir([RootFile star_string FileExt]);% look for relevant files in the file directory |
---|
| 172 | cd(wd) |
---|
[339] | 173 | nbpair=numel(dirpair); |
---|
| 174 | ref_i_list=zeros(1,nbpair); |
---|
| 175 | ref_j_list=zeros(1,nbpair); |
---|
| 176 | if nbpair==0% no detected file |
---|
| 177 | RootPath=''; |
---|
| 178 | RootFile=''; |
---|
| 179 | end |
---|
[360] | 180 | % scan the list of relevant files, extract the indices |
---|
[339] | 181 | for ifile=1:nbpair |
---|
[360] | 182 | rr=regexp(dirpair(ifile).name,detect_string,'names'); |
---|
[362] | 183 | if ~isempty(rr) |
---|
[360] | 184 | i1=str2num(rr.i1); |
---|
| 185 | i2=str2num(rr.i2); |
---|
| 186 | j1=stra2num(rr.j1); |
---|
| 187 | j2=stra2num(rr.j2); |
---|
[339] | 188 | ref_i=i1; |
---|
| 189 | if isempty(i2_input) |
---|
| 190 | if ~isempty(i2)% invalid file name if i2 does not exist in the input file |
---|
| 191 | break |
---|
| 192 | end |
---|
[334] | 193 | else |
---|
[339] | 194 | ref_i=floor((i1+i2)/2); |
---|
[334] | 195 | end |
---|
[339] | 196 | ref_j=1; |
---|
| 197 | if isempty(j1_input) |
---|
| 198 | if ~isempty(j1)% invalid file name if j1 does not exist in the input file |
---|
| 199 | break |
---|
| 200 | end |
---|
[342] | 201 | else %j1_input is not empty |
---|
| 202 | if isempty(j1)% the detected name does not fit with the input |
---|
| 203 | break |
---|
| 204 | else |
---|
| 205 | ref_j=j1; |
---|
| 206 | if isempty(j2_input) |
---|
| 207 | if ~isempty(j2)% invalid file name if j2 does not exist in the input file |
---|
| 208 | break |
---|
| 209 | end |
---|
| 210 | else |
---|
| 211 | ref_j=floor((j1+j2)/2); |
---|
[339] | 212 | end |
---|
| 213 | end |
---|
[332] | 214 | end |
---|
[339] | 215 | % update the detected index series |
---|
| 216 | ref_i_list(ifile)=ref_i; |
---|
| 217 | ref_j_list(ifile)=ref_j; |
---|
| 218 | nb_pairs=0; |
---|
| 219 | if ~isempty(i2_input)|| ~isempty(j2_input) %deals with pairs |
---|
[343] | 220 | if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1 |
---|
| 221 | nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0)); |
---|
[339] | 222 | end |
---|
[334] | 223 | end |
---|
[343] | 224 | i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1; |
---|
[339] | 225 | if ~isempty(i2_input) |
---|
[343] | 226 | i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2; |
---|
[332] | 227 | end |
---|
[339] | 228 | if ~isempty(j1_input) |
---|
[343] | 229 | j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1; |
---|
[332] | 230 | end |
---|
[339] | 231 | if ~isempty(j2_input) |
---|
[343] | 232 | j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1; |
---|
| 233 | j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2; |
---|
[339] | 234 | end |
---|
[362] | 235 | end |
---|
[339] | 236 | end |
---|
| 237 | % look for the numerical string of the first files to update the NomType (take into account the 0 before the number) |
---|
| 238 | max_j=max(ref_j_list); |
---|
[350] | 239 | if isempty(max_j) |
---|
| 240 | ref_ij=ref_i_list; |
---|
| 241 | else |
---|
| 242 | ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i. |
---|
| 243 | end |
---|
[360] | 244 | [tild,ifile_min]=min(ref_ij(ref_ij>0)); |
---|
| 245 | if isempty(ifile_min) |
---|
[350] | 246 | RootPath=''; |
---|
| 247 | RootFile=''; |
---|
| 248 | NomType=''; |
---|
| 249 | else |
---|
[360] | 250 | [tild,tild,tild,tild,tild,tild,tild,tild,NomType]=fileparts_uvmat(dirpair(ifile_min).name); |
---|
[350] | 251 | end |
---|
[339] | 252 | end |
---|
| 253 | |
---|
| 254 | %% set to empty array the irrelevant index series |
---|
| 255 | if isequal(i1_series,0), i1_series=[]; end |
---|
| 256 | if isequal(i2_series,0), i2_series=[]; end |
---|
| 257 | if isequal(j1_series,0), j1_series=[]; end |
---|
| 258 | if isequal(j2_series,0), j2_series=[]; end |
---|
| 259 | |
---|
| 260 | %% sort pairs by decreasing index differences in case of multiple pairs at the same reference index |
---|
| 261 | if size(i2_series,3)>1 %pairs i1 -i2 |
---|
| 262 | diff_index=abs(i2_series-i1_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 | i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 268 | if ~isempty(j1_series) |
---|
| 269 | j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 270 | end |
---|
| 271 | end |
---|
[334] | 272 | end |
---|
[339] | 273 | elseif size(j2_series,3)>1 %pairs j1 -j2 |
---|
| 274 | diff_index=abs(j2_series-j1_series); |
---|
[353] | 275 | [tild,ind_pair]=sort(diff_index,3,'descend'); |
---|
[339] | 276 | for ref_i=1:size(i1_series,1) |
---|
| 277 | for ref_j=1:size(j1_series,2) |
---|
| 278 | i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 279 | if ~isempty(i2_series) |
---|
| 280 | i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 281 | end |
---|
| 282 | j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 283 | j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 284 | end |
---|
| 285 | end |
---|
| 286 | end |
---|
| 287 | |
---|