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