[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); |
---|
| 46 | fullfileinput=fullfile(RootPath,fileinput); |
---|
[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); |
---|
[376] | 53 | [FileType,FileInfo,Object]=get_file_type(fullfileinput); |
---|
| 54 | if strcmp( FileType,'multimage')||strcmp( FileType,'video') |
---|
| 55 | NomType='*'; |
---|
[379] | 56 | i1_series=(1:FileInfo.NbFrame)'; |
---|
[332] | 57 | end |
---|
[339] | 58 | |
---|
[372] | 59 | if strcmp(NomType,'')||strcmp(NomType,'*')||strcmp(option,'filetype') |
---|
[371] | 60 | if exist(fullfileinput,'file') |
---|
| 61 | [tild,RootFile]=fileparts(fileinput);% case of constant name (no indexing) |
---|
| 62 | else |
---|
[360] | 63 | RootFile=''; |
---|
[339] | 64 | end |
---|
[384] | 65 | else |
---|
| 66 | %% possibly include the first index in the root name, if there exists a corresponding xml file |
---|
| 67 | NomTypePref=''; |
---|
| 68 | RootFileNew=RootFile; |
---|
[385] | 69 | % if ~isempty(regexp(NomType,['^_'])) |
---|
| 70 | % NomTypePref='_'; |
---|
| 71 | % RootFileNew=[RootFileNew '_']; |
---|
| 72 | % end RootPath=''; |
---|
| 73 | r=regexp(NomType,'^(?<tiretnum>_?\d+)','names');%look for a number or _1 at the beginning of NomType |
---|
| 74 | % r=regexp(NomType,['^' NomTypePref '(?<num1>\d+)'],'names');%look for a number at the beginning of NomTypeSt |
---|
[384] | 75 | if ~isempty(r) |
---|
[385] | 76 | NomTypePref=r.tiretnum; |
---|
[384] | 77 | fileinput_end=regexprep(fileinput,['^' RootFileNew],''); |
---|
| 78 | r=regexp(fileinput_end,'^(?<num1>\d+)','names'); |
---|
| 79 | if ~isempty(r) |
---|
| 80 | RootFileNew=[RootFileNew r.num1]; |
---|
| 81 | end |
---|
| 82 | if exist(fullfile(RootPath,[RootFileNew '.xml']),'file') |
---|
| 83 | RootFile=RootFileNew; |
---|
| 84 | NomType=regexprep(NomType,['^' NomTypePref],''); |
---|
| 85 | i2_input=j2_input; |
---|
| 86 | j1_input=[]; |
---|
| 87 | j2_input=[]; |
---|
[385] | 88 | else |
---|
| 89 | NomTypePref=''; |
---|
[384] | 90 | end |
---|
| 91 | end |
---|
[360] | 92 | %% analyse the list of existing files when relevant |
---|
| 93 | sep1=''; |
---|
| 94 | i1_str='(?<i1>)'; |
---|
| 95 | i1_star=''; |
---|
| 96 | sep2=''; |
---|
| 97 | i2_str='(?<i2>)'; |
---|
| 98 | i2_star=''; |
---|
| 99 | sep3=''; |
---|
| 100 | j1_str='(?<j1>)'; |
---|
| 101 | j1_star=''; |
---|
| 102 | sep4=''; |
---|
| 103 | j2_str='(?<j2>)'; |
---|
| 104 | j2_star=''; |
---|
| 105 | NomTypeStr=NomType; |
---|
| 106 | if ~isempty(regexp(NomTypeStr,'^_\d')) |
---|
| 107 | sep1='_'; |
---|
| 108 | NomTypeStr(1)=[];%remove '_' from the beginning of NomTypeStr |
---|
[332] | 109 | end |
---|
[360] | 110 | r=regexp(NomTypeStr,'^(?<num1>\d+)','names');%look for a number at the beginning of NomTypeStr |
---|
| 111 | if ~isempty(r) |
---|
| 112 | i1_str='(?<i1>\d+)'; |
---|
| 113 | i1_star='*'; |
---|
| 114 | NomTypeStr=regexprep(NomTypeStr,['^' r.num1],''); |
---|
| 115 | r=regexp(NomTypeStr,'^-(?<num2>\d+)','names');%look for a pair i1-i2 |
---|
| 116 | if ~isempty(r) |
---|
| 117 | sep2='-'; |
---|
| 118 | i2_str='(?<i2>\d+)'; |
---|
| 119 | i2_star='*'; |
---|
| 120 | NomTypeStr=regexprep(NomTypeStr,['^-' r.num2],''); |
---|
| 121 | end |
---|
| 122 | if ~isempty(regexp(NomTypeStr,'^_')); |
---|
| 123 | sep3='_'; |
---|
| 124 | NomTypeStr(1)=[];%remove '_' from the beginning of NomTypeStr |
---|
| 125 | end |
---|
| 126 | if ~isempty(regexp(NomTypeStr,'^[a|A]')); |
---|
| 127 | j1_str='(?<j1>[a-z]|[A-Z])'; |
---|
| 128 | j1_star='*'; |
---|
| 129 | if ~isempty(regexp(NomTypeStr,'[b|B]$')); |
---|
[365] | 130 | j2_str='(?<j2>[a-z]|[A-Z])'; |
---|
[360] | 131 | j2_star='*'; |
---|
| 132 | end |
---|
| 133 | else |
---|
| 134 | r=regexp(NomTypeStr,'^(?<num3>\d+)','names'); |
---|
| 135 | if ~isempty(r) |
---|
| 136 | j1_str='(?<j1>\d+)'; |
---|
| 137 | j1_star='*'; |
---|
| 138 | NomTypeStr=regexprep(NomTypeStr,['^' r.num3],''); |
---|
| 139 | end |
---|
| 140 | r=regexp(NomTypeStr,'-(?<num4>\d+)','names'); |
---|
| 141 | if ~isempty(r) |
---|
| 142 | sep4='-'; |
---|
| 143 | j2_str='(?<j2>\d+)'; |
---|
| 144 | j2_star='*'; |
---|
| 145 | end |
---|
| 146 | end |
---|
[332] | 147 | end |
---|
[371] | 148 | detect_string=['^' RootFile sep1 i1_str sep2 i2_str sep3 j1_str sep4 j2_str FileExt '$'];%string used in regexp to detect file indices |
---|
[360] | 149 | %find the string used to extract the relevant files with the command dir |
---|
[371] | 150 | star_string=[RootFile sep1 i1_star sep2 i2_star sep3 j1_star sep4 j2_star '*']; |
---|
[360] | 151 | wd=pwd;%current working directory |
---|
[371] | 152 | cd (RootPath)% move to the local dir to save time in the operation dir. |
---|
| 153 | dirpair=dir([star_string FileExt]);% look for relevant files in the file directory |
---|
[360] | 154 | cd(wd) |
---|
[339] | 155 | nbpair=numel(dirpair); |
---|
| 156 | ref_i_list=zeros(1,nbpair); |
---|
| 157 | ref_j_list=zeros(1,nbpair); |
---|
| 158 | if nbpair==0% no detected file |
---|
[385] | 159 | % RootPath=''; |
---|
[339] | 160 | RootFile=''; |
---|
| 161 | end |
---|
[360] | 162 | % scan the list of relevant files, extract the indices |
---|
[339] | 163 | for ifile=1:nbpair |
---|
[360] | 164 | rr=regexp(dirpair(ifile).name,detect_string,'names'); |
---|
[362] | 165 | if ~isempty(rr) |
---|
[360] | 166 | i1=str2num(rr.i1); |
---|
| 167 | i2=str2num(rr.i2); |
---|
| 168 | j1=stra2num(rr.j1); |
---|
| 169 | j2=stra2num(rr.j2); |
---|
[339] | 170 | ref_i=i1; |
---|
| 171 | if isempty(i2_input) |
---|
| 172 | if ~isempty(i2)% invalid file name if i2 does not exist in the input file |
---|
| 173 | break |
---|
| 174 | end |
---|
[334] | 175 | else |
---|
[339] | 176 | ref_i=floor((i1+i2)/2); |
---|
[334] | 177 | end |
---|
[339] | 178 | ref_j=1; |
---|
| 179 | if isempty(j1_input) |
---|
| 180 | if ~isempty(j1)% invalid file name if j1 does not exist in the input file |
---|
| 181 | break |
---|
| 182 | end |
---|
[342] | 183 | else %j1_input is not empty |
---|
| 184 | if isempty(j1)% the detected name does not fit with the input |
---|
| 185 | break |
---|
| 186 | else |
---|
| 187 | ref_j=j1; |
---|
| 188 | if isempty(j2_input) |
---|
| 189 | if ~isempty(j2)% invalid file name if j2 does not exist in the input file |
---|
| 190 | break |
---|
| 191 | end |
---|
| 192 | else |
---|
| 193 | ref_j=floor((j1+j2)/2); |
---|
[339] | 194 | end |
---|
| 195 | end |
---|
[332] | 196 | end |
---|
[339] | 197 | % update the detected index series |
---|
| 198 | ref_i_list(ifile)=ref_i; |
---|
| 199 | ref_j_list(ifile)=ref_j; |
---|
| 200 | nb_pairs=0; |
---|
| 201 | if ~isempty(i2_input)|| ~isempty(j2_input) %deals with pairs |
---|
[343] | 202 | if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1 |
---|
| 203 | nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0)); |
---|
[339] | 204 | end |
---|
[334] | 205 | end |
---|
[343] | 206 | i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1; |
---|
[339] | 207 | if ~isempty(i2_input) |
---|
[343] | 208 | i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2; |
---|
[332] | 209 | end |
---|
[339] | 210 | if ~isempty(j1_input) |
---|
[343] | 211 | j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1; |
---|
[332] | 212 | end |
---|
[339] | 213 | if ~isempty(j2_input) |
---|
[343] | 214 | j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1; |
---|
| 215 | j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2; |
---|
[339] | 216 | end |
---|
[362] | 217 | end |
---|
[339] | 218 | end |
---|
| 219 | % look for the numerical string of the first files to update the NomType (take into account the 0 before the number) |
---|
| 220 | max_j=max(ref_j_list); |
---|
[350] | 221 | if isempty(max_j) |
---|
| 222 | ref_ij=ref_i_list; |
---|
| 223 | else |
---|
| 224 | ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i. |
---|
| 225 | end |
---|
[360] | 226 | [tild,ifile_min]=min(ref_ij(ref_ij>0)); |
---|
| 227 | if isempty(ifile_min) |
---|
[385] | 228 | % RootPath=''; |
---|
[350] | 229 | RootFile=''; |
---|
| 230 | NomType=''; |
---|
| 231 | else |
---|
[372] | 232 | [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] | 233 | NomType=regexprep(NomType,['^' NomTypePref],''); |
---|
[350] | 234 | end |
---|
[339] | 235 | end |
---|
| 236 | |
---|
[372] | 237 | %% update the file type if the input file does not exist (pb of 0001) |
---|
[376] | 238 | if isempty(FileType) |
---|
| 239 | [FileType,tild,Object]=get_file_type(dirpair(ifile_min).name); |
---|
[372] | 240 | end |
---|
| 241 | |
---|
[339] | 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 | |
---|