[334] | 1 | %'find_file_series': check the content onf an input field 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 |
---|
[334] | 42 | % [RootPath,RootFile,~,~,~,~,FileExt,NomType,SubDir]=name2display(fileinput); |
---|
[339] | 43 | [RootPath,SubDir,RootFile,~,i2_input,j1_input,j2_input,FileExt,NomType]=fileparts_uvmat(fileinput); |
---|
[332] | 44 | |
---|
| 45 | %% check for particular file types: images, movies, civ data |
---|
| 46 | FileType=''; |
---|
| 47 | Object=[]; |
---|
[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); |
---|
[334] | 52 | switch FileExt |
---|
| 53 | % ancillary files, no field indexing |
---|
| 54 | case {'.civ','.log','.cmx','.cmx2','.txt','.bat'} |
---|
| 55 | FileType='txt'; |
---|
| 56 | NomType=''; |
---|
| 57 | case '.fig' |
---|
| 58 | FileType='figure'; |
---|
| 59 | NomType=''; |
---|
| 60 | case '.xml' |
---|
| 61 | FileType='xml'; |
---|
| 62 | NomType=''; |
---|
| 63 | case '.xls' |
---|
| 64 | FileType='xls'; |
---|
| 65 | NomType=''; |
---|
| 66 | otherwise |
---|
| 67 | if ~isempty(FileExt)&& ~isempty(imformats(FileExt(2:end))) |
---|
[339] | 68 | try |
---|
| 69 | imainfo=imfinfo(fileinput); |
---|
| 70 | FileType='image'; |
---|
| 71 | if length(imainfo) >1 %case of image with multiple frames |
---|
| 72 | NomType='*'; |
---|
| 73 | FileType='multimage'; |
---|
| 74 | i1_series=1:length(imainfo); |
---|
| 75 | [RootPath,RootFile]=fileparts(fileinput); |
---|
| 76 | end |
---|
[334] | 77 | end |
---|
[332] | 78 | else |
---|
[334] | 79 | try |
---|
| 80 | Data=nc2struct(fileinput,'ListGlobalAttribute','absolut_time_T0','Conventions'); |
---|
| 81 | if ~isempty(Data.absolut_time_T0') |
---|
| 82 | FileType='civx'; % test for civx velocity fields |
---|
| 83 | elseif strcmp(Data.Conventions','uvmat/civdata') |
---|
| 84 | FileType='civdata'; % test for civx velocity fields |
---|
| 85 | else |
---|
| 86 | FileType='netcdf'; |
---|
| 87 | end |
---|
| 88 | end |
---|
| 89 | try |
---|
| 90 | Object=VideoReader(fileinput); |
---|
| 91 | NomType='*'; |
---|
| 92 | FileType='video'; |
---|
[339] | 93 | i1_series=[1:get(Object,'NumberOfFrames')]; |
---|
[334] | 94 | [RootPath,RootFile]=fileparts(fileinput); |
---|
| 95 | end |
---|
| 96 | end |
---|
[332] | 97 | end |
---|
[339] | 98 | |
---|
| 99 | %% get the list of existing files when relevant |
---|
[334] | 100 | if strcmp(NomType,'')||strcmp(NomType,'*') |
---|
[339] | 101 | if exist(fileinput,'file') |
---|
[334] | 102 | [RootPath,RootFile]=fileparts(fileinput);% case of constant name (no indexing) |
---|
[339] | 103 | else |
---|
| 104 | RootPath=''; |
---|
| 105 | RootFile=''; |
---|
| 106 | end |
---|
[334] | 107 | else |
---|
[332] | 108 | if strcmp(SubDir,'') |
---|
| 109 | filebasesub=fullfile(RootPath,RootFile); |
---|
| 110 | else |
---|
| 111 | filebasesub=fullfile(RootPath,SubDir,RootFile); |
---|
| 112 | end |
---|
| 113 | detect_string=regexprep(NomType,'\d','*');%replace numbers by '*' |
---|
| 114 | old_string=''; |
---|
| 115 | detect_string=regexprep(detect_string,'[ab]$','*');%suppress the possible letter ab at the end |
---|
| 116 | detect_string=regexprep(detect_string,'[AB]$','*');%suppress the possible letter ab at the end |
---|
| 117 | detect_string=regexprep(detect_string,'[a|A]$','*');%suppress a possible second letter a,A at the end |
---|
| 118 | while ~strcmp(detect_string,old_string)%removes multiple '*' |
---|
| 119 | old_string=detect_string; |
---|
| 120 | detect_string=regexprep(detect_string,'**','*'); |
---|
| 121 | end |
---|
| 122 | dirpair=dir([filebasesub detect_string FileExt]); |
---|
[339] | 123 | nbpair=numel(dirpair); |
---|
| 124 | ref_i_list=zeros(1,nbpair); |
---|
| 125 | ref_j_list=zeros(1,nbpair); |
---|
| 126 | if nbpair==0% no detected file |
---|
| 127 | RootPath=''; |
---|
| 128 | RootFile=''; |
---|
| 129 | end |
---|
| 130 | for ifile=1:nbpair |
---|
| 131 | [~,~,~,i1,i2,j1,j2]=fileparts_uvmat(dirpair(ifile).name); |
---|
| 132 | ref_i=i1; |
---|
| 133 | if isempty(i2_input) |
---|
| 134 | if ~isempty(i2)% invalid file name if i2 does not exist in the input file |
---|
| 135 | break |
---|
| 136 | end |
---|
[334] | 137 | else |
---|
[339] | 138 | ref_i=floor((i1+i2)/2); |
---|
[334] | 139 | end |
---|
[339] | 140 | ref_j=1; |
---|
| 141 | if isempty(j1_input) |
---|
| 142 | if ~isempty(j1)% invalid file name if j1 does not exist in the input file |
---|
| 143 | break |
---|
| 144 | end |
---|
[342] | 145 | else %j1_input is not empty |
---|
| 146 | if isempty(j1)% the detected name does not fit with the input |
---|
| 147 | break |
---|
| 148 | else |
---|
| 149 | ref_j=j1; |
---|
| 150 | if isempty(j2_input) |
---|
| 151 | if ~isempty(j2)% invalid file name if j2 does not exist in the input file |
---|
| 152 | break |
---|
| 153 | end |
---|
| 154 | else |
---|
| 155 | ref_j=floor((j1+j2)/2); |
---|
[339] | 156 | end |
---|
| 157 | end |
---|
[332] | 158 | end |
---|
[339] | 159 | % update the detected index series |
---|
| 160 | ref_i_list(ifile)=ref_i; |
---|
| 161 | ref_j_list(ifile)=ref_j; |
---|
| 162 | nb_pairs=0; |
---|
| 163 | if ~isempty(i2_input)|| ~isempty(j2_input) %deals with pairs |
---|
[343] | 164 | if size(i1_series,1)>=ref_i+1 && size(i1_series,2)>=ref_j+1 |
---|
| 165 | nb_pairs=numel(find(i1_series(ref_i+1,ref_j+1,:)~=0)); |
---|
[339] | 166 | end |
---|
[334] | 167 | end |
---|
[343] | 168 | i1_series(ref_i+1,ref_j+1,nb_pairs+1)=i1; |
---|
[339] | 169 | if ~isempty(i2_input) |
---|
[343] | 170 | i2_series(ref_i+1,ref_j+1,nb_pairs+1)=i2; |
---|
[332] | 171 | end |
---|
[339] | 172 | if ~isempty(j1_input) |
---|
[343] | 173 | j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1; |
---|
[332] | 174 | end |
---|
[339] | 175 | if ~isempty(j2_input) |
---|
[343] | 176 | j1_series(ref_i+1,ref_j+1,nb_pairs+1)=j1; |
---|
| 177 | j2_series(ref_i+1,ref_j+1,nb_pairs+1)=j2; |
---|
[339] | 178 | end |
---|
| 179 | end |
---|
| 180 | % look for the numerical string of the first files to update the NomType (take into account the 0 before the number) |
---|
| 181 | max_j=max(ref_j_list); |
---|
| 182 | ref_ij=ref_i_list*max_j+ref_j_list; % ordered by index i, then by j for a given i. |
---|
| 183 | [~,ifile]=min(ref_ij(ref_ij>0)); |
---|
[334] | 184 | [~,~,~,~,~,~,~,~,NomType]=fileparts_uvmat(dirpair(ifile).name); |
---|
[339] | 185 | end |
---|
| 186 | |
---|
| 187 | %% set to empty array the irrelevant index series |
---|
| 188 | if isequal(i1_series,0), i1_series=[]; end |
---|
| 189 | if isequal(i2_series,0), i2_series=[]; end |
---|
| 190 | if isequal(j1_series,0), j1_series=[]; end |
---|
| 191 | if isequal(j2_series,0), j2_series=[]; end |
---|
| 192 | |
---|
| 193 | %% sort pairs by decreasing index differences in case of multiple pairs at the same reference index |
---|
| 194 | if size(i2_series,3)>1 %pairs i1 -i2 |
---|
| 195 | diff_index=abs(i2_series-i1_series); |
---|
| 196 | [~,ind_pair]=sort(diff_index,3,'descend'); |
---|
| 197 | for ref_i=1:size(i1_series,1) |
---|
| 198 | for ref_j=1:size(j1_series,2) |
---|
| 199 | i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 200 | i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 201 | if ~isempty(j1_series) |
---|
| 202 | j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 203 | end |
---|
| 204 | end |
---|
[334] | 205 | end |
---|
[339] | 206 | elseif size(j2_series,3)>1 %pairs j1 -j2 |
---|
| 207 | diff_index=abs(j2_series-j1_series); |
---|
| 208 | [~,ind_pair]=sort(diff_index,3,'descend'); |
---|
| 209 | for ref_i=1:size(i1_series,1) |
---|
| 210 | for ref_j=1:size(j1_series,2) |
---|
| 211 | i1_series(ref_i,ref_j,:)=i1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 212 | if ~isempty(i2_series) |
---|
| 213 | i2_series(ref_i,ref_j,:)=i2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 214 | end |
---|
| 215 | j1_series(ref_i,ref_j,:)=j1_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 216 | j2_series(ref_i,ref_j,:)=j2_series(ref_i,ref_j,ind_pair(ref_i,ref_j,:)); |
---|
| 217 | end |
---|
| 218 | end |
---|
| 219 | end |
---|
| 220 | |
---|