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