[8] | 1 | %'name_generator': creates a file name from a root name and indices.
|
---|
[89] | 2 | %------------------------------------------------------------------------
|
---|
[122] | 3 | % [filename,num_i1_out,num_j1_out,num_i2_out,num_j2_out,subdir_out]=...
|
---|
[85] | 4 | % name_generator(filebase,num_i1,num_j1,ext,nom_type,comp_input,num_i2,num_j2,subdir);
|
---|
[89] | 5 | %------------------------------------------------------------------------
|
---|
[8] | 6 | % This function detects the existence the constructed file name and it can
|
---|
| 7 | % find indices according to file existence if they are not specified
|
---|
[85] | 8 | % rmq: this function is related to the reverse functions display2name and name2diplay
|
---|
[89] | 9 | %------------------------------------------------------------------------
|
---|
[8] | 10 | % OUTPUT:
|
---|
[85] | 11 | % filename: string representing the file name (including path)
|
---|
| 12 | % num_i1_out,num_j1_out,num_i2_out,num_j2_out,subdir_out: index numbers and subdirectory detected
|
---|
| 13 | % for free input (= to the corresponding input indices when comp_input=1)
|
---|
[89] | 14 | %------------------------------------------------------------------------
|
---|
[8] | 15 | % INPUT:
|
---|
| 16 | % 'filebase': the root name,
|
---|
| 17 | % 'num_i1: first labelling index i
|
---|
| 18 | % 'num_j1', first labelling index j
|
---|
| 19 | % 'ext': file name extension (e.g. '.png' or '.nc')
|
---|
[122] | 20 | %nom_type: char chain characterizing the file nomenclature: with values
|
---|
| 21 | % nom_type='': constant name [filebase ext] (default output if 'nom_type' is undefined)
|
---|
| 22 | % nom_type='*':constant name for a file representing a series (e.g. avi movie)
|
---|
| 23 | % nom_type='1','01',or '001'...': series of files with a single index i without separator(e.g. 'aa045.png').
|
---|
| 24 | % nom_type='_1','_01','_001'...': series of files with a single index i with separator '_'(e.g. 'aa_045.png').
|
---|
| 25 | % nom_type='1a','1A','01a','01A',... with a numerical index and an index letter(e.g.'aa45b.png') (lower or upper case)
|
---|
| 26 | % nom_type='_1a','_1A','_01a','_01A',...: idem, with a separator '_' before the index
|
---|
| 27 | % nom_type='_1_1','_01_1',...: matrix of files with two indices i and j separated by '_'(e.g. 'aa_45_2.png')
|
---|
[332] | 28 | % nom_type='_1-2': from pairs from a single index (e.g. 'aa_45-47.nc')
|
---|
| 29 | % nom_type='_1_1-2': pairs of j indices (e.g. 'aa_45_2-3.nc')
|
---|
| 30 | % nom_type='_1-2_1': pairs of i indices (e.g. 'aa_45-46_2.nc')
|
---|
[122] | 31 | % nom_type='_1_ab','1_ab','01_ab'..., from pairs of '#' images (e.g.'aa045bc.nc'), ext='.nc'
|
---|
[85] | 32 | %'comp_input' (for nom_type involving index pairs (e.g. netc))
|
---|
| 33 | % comp_input=1: the index pair is imposed,
|
---|
| 34 | % comp_input=0: the index pair is automatically searched, choosing the most recent file in case of multiple choice
|
---|
[8] | 35 | % 'num_i2': second index i (for nom_type involving index pairs (e.g. netc))
|
---|
| 36 | % 'num_j2': second index j (for nom_type involving index pairs (e.g. netc))
|
---|
| 37 | % 'subdir': (used for nom_type=netc...) string representing the name of the subdirectory 'subdir' containing file.
|
---|
[85] | 38 | % subdir='': no subdirectory,
|
---|
| 39 | % subdir='?', the file is first searched with no subdirectory, then in the most recently modified subdirectory if not detected.
|
---|
[8] | 40 |
|
---|
| 41 | % A FAIRE: si comp_inpu=0, si _i_j n'existe pas, chercher _i,
|
---|
| 42 | function [filename,num_i1_out,num_j1_out,num_i2_out,num_j2_out,subdir_out]=...
|
---|
[122] | 43 | name_generator(filebase,num_i1,num_j1,ext,nom_type,comp_input,num_i2,num_j2,subdir)
|
---|
[8] | 44 | sizf=size(filebase);
|
---|
| 45 | if (~ischar(filebase)||~isequal(sizf(1),1)),filebase='';end
|
---|
[89] | 46 | if ~exist('ext','var')
|
---|
| 47 | ext='';
|
---|
| 48 | end
|
---|
| 49 | if ~exist('nom_type','var')
|
---|
| 50 | nom_type='';
|
---|
| 51 | end
|
---|
[8] | 52 | if ~ischar(ext),ext='';end
|
---|
[127] | 53 | % idetect=0;
|
---|
[89] | 54 | if ~exist('num_i1','var') || isempty(num_i1) || isnan(num_i1)
|
---|
[8] | 55 | num_i1=1; %default
|
---|
| 56 | end
|
---|
[89] | 57 | if ~exist('num_j1','var') || isempty(num_j1) || isnan(num_j1)
|
---|
[8] | 58 | num_j1=1; %default
|
---|
| 59 | end
|
---|
[89] | 60 | if ~exist('num_i2','var') || isempty(num_i2) || isnan(num_i2)
|
---|
[8] | 61 | num_i2=num_i1; %default
|
---|
| 62 | end
|
---|
[89] | 63 | if ~exist('num_j2','var') || isempty(num_j2) || isnan(num_j2)
|
---|
[65] | 64 | num_j2=num_j1; %default
|
---|
| 65 | end
|
---|
[8] | 66 | if ~exist('subdir','var')|| isempty(subdir)
|
---|
| 67 | subdir='' ; %default
|
---|
| 68 | end
|
---|
[65] | 69 | num_i1_out=num_i1;%default output
|
---|
| 70 | num_j1_out=num_j1;%default output
|
---|
| 71 | num_i2_out=num_i2;%default output
|
---|
| 72 | num_j2_out=num_j2;%default output
|
---|
[332] | 73 | test_pairs=numel(nom_type)>=2 &&(strcmp(nom_type,'_1-2_1-2')|| strcmp(nom_type(end-1:end),'ab')|| strcmp(nom_type(end-1:end),'AB')||...
|
---|
| 74 | strcmp(nom_type,'_1_1-2')|| strcmp(nom_type,'_1-2_1')||strcmp(nom_type,'_1-2'));
|
---|
| 75 | %test_2D= strcmp(nom_type(end-1:end),'ab')|| strcmp(nom_type(end-1:end),'AB') ||strcmp(nom_type,'_1_1-2');
|
---|
| 76 | %test_3D=strcmp(nom_type,'_1-2_1')|| strcmp(nom_type,'_1-2');
|
---|
[122] | 77 | if ~isequal(subdir,'') && ~isequal(subdir,'?')
|
---|
| 78 | [Path,Name]=fileparts(filebase);
|
---|
| 79 | filename=fullfile(Path,subdir,Name);
|
---|
| 80 | else
|
---|
| 81 | filename=filebase;%default
|
---|
| 82 | end
|
---|
| 83 | if ~test_pairs%case of a single index i, and possibly j
|
---|
[127] | 84 | % numlength=numel(nom_type);
|
---|
| 85 | nom_type_mod=nom_type;
|
---|
[122] | 86 | num_j_str='';
|
---|
[138] | 87 | if strcmp(nom_type,'1')
|
---|
| 88 | filename=[filename num2str(num_i1) ext];
|
---|
| 89 | elseif length(nom_type)<=1%fixed name , no indexing, for instance '*'
|
---|
| 90 | filename=[filename ext];
|
---|
[122] | 91 | else
|
---|
[138] | 92 | nom_type_mod=nom_type;
|
---|
| 93 | if strcmp(nom_type(1),'_')
|
---|
| 94 | filename=[filename '_'];
|
---|
| 95 | nom_type_mod(1)=[];
|
---|
| 96 | end
|
---|
| 97 | if strcmp(nom_type_mod(end),'a')
|
---|
| 98 | nom_type_mod(end)=[];
|
---|
| 99 | num_j_str=char(num_j1+96);% lower letter corresponding to the index
|
---|
| 100 | elseif strcmp(nom_type_mod(end),'A')
|
---|
| 101 | nom_type(end)=[];
|
---|
| 102 | num_j_str=char(num_j1+64);% lower letter corresponding to the index
|
---|
| 103 | elseif isequal(numel(regexp(nom_type_mod,'_')),1)%if a second separator '_' exists in nom_type
|
---|
| 104 | num_j_str=['_' num2str(num_j1)];
|
---|
| 105 | nom_type_mod(regexp(nom_type_mod,'_'):end)=[];
|
---|
| 106 | else
|
---|
| 107 | num_j1_out=[];%no index j
|
---|
| 108 | end
|
---|
| 109 | if ~isnan(str2double(nom_type_mod))
|
---|
| 110 | numtype=['%0' num2str(length(nom_type_mod)) 'd'];%indicate the number of digits (0 before the number)
|
---|
| 111 | filename=[filename num2str(num_i1,numtype) num_j_str ext];
|
---|
| 112 | num_i2_out=num_i1_out;
|
---|
| 113 | num_j2_out=num_j1_out;
|
---|
| 114 | else %fixed name
|
---|
| 115 | filename=[filename ext];
|
---|
| 116 | end
|
---|
[122] | 117 | end
|
---|
[127] | 118 |
|
---|
[8] | 119 | %case of derived file indexing (e.g. netcdf files)
|
---|
[122] | 120 | else
|
---|
[8] | 121 | % case of an imposed image pair (comp_input=1)
|
---|
[122] | 122 | if (exist('comp_input','var') && isequal(comp_input,1))
|
---|
| 123 | if strcmp(nom_type(1),'_')
|
---|
| 124 | filename=[filename '_'];
|
---|
| 125 | nom_type(1)=[];
|
---|
| 126 | end
|
---|
| 127 | if strcmp(nom_type(end-1:end),'AB')||strcmp(nom_type(end-1:end),'ab')
|
---|
| 128 | if strcmp(nom_type(end-1:end),'AB')
|
---|
| 129 | nchar=64;
|
---|
[8] | 130 | else
|
---|
[122] | 131 | nchar=96;
|
---|
[8] | 132 | end
|
---|
[122] | 133 | if isequal(num_j1,num_j2)% case of displacements at the same time
|
---|
| 134 | num_j_str=char(num_j1+nchar);
|
---|
| 135 | else
|
---|
| 136 | num_j_str=[char(num_j1+nchar) char(num_j2+nchar)];
|
---|
| 137 | end
|
---|
| 138 | if strcmp(nom_type(end-2),'_')
|
---|
| 139 | numstr=['%0' num2str(numel(nom_type)-3) 'd'];
|
---|
| 140 | num_j_str=['_' num_j_str];
|
---|
| 141 | else
|
---|
| 142 | numstr=['%0' num2str(numel(nom_type)-2) 'd'];
|
---|
| 143 | end
|
---|
| 144 | filename=[filename num2str(num_i1,numstr) num_j_str ext];
|
---|
[65] | 145 | num_i2_out=num_i1;
|
---|
[332] | 146 | elseif isequal(nom_type,'1_1-2')
|
---|
[8] | 147 | if isequal(num2str(num_j1),num2str(num_j2))% case of displacements at the same time
|
---|
[122] | 148 | filename=[filename num2str(num_i1) '_' num2str(num_j1) ext];
|
---|
[8] | 149 | else
|
---|
[122] | 150 | filename=[filename num2str(num_i1) '_' num2str(num_j1) '-' num2str(num_j2) ext];
|
---|
[8] | 151 | end
|
---|
[65] | 152 | num_i2_out=num_i1;
|
---|
[332] | 153 | elseif isequal(nom_type,'1-2_1')
|
---|
[8] | 154 | if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
|
---|
[122] | 155 | filename=[filename num2str(num_i1) '_' num2str(num_j1) ext];
|
---|
[8] | 156 | else
|
---|
[122] | 157 | filename=[filename num2str(num_i1) '-' num2str(num_i2) '_' num2str(num_j1) ext];
|
---|
[8] | 158 | end
|
---|
[65] | 159 | num_j2_out=num_j1;
|
---|
[332] | 160 | elseif isequal(nom_type,'1-2')
|
---|
[8] | 161 | if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
|
---|
[122] | 162 | filename=[filename num2str(num_i1) ext];
|
---|
[8] | 163 | else
|
---|
[122] | 164 | filename=[filename num2str(num_i1) '-' num2str(num_i2) ext];
|
---|
[8] | 165 | end
|
---|
[65] | 166 | num_j2_out=num_j1;
|
---|
[332] | 167 | elseif isequal(nom_type,'1-2_1-2')
|
---|
[8] | 168 | if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
|
---|
[127] | 169 | app1= num2str(num_i1);
|
---|
[8] | 170 | else
|
---|
| 171 | app1= [num2str(num_i1) '-' num2str(num_i2)];
|
---|
| 172 | end
|
---|
| 173 | if isequal(num2str(num_j1),num2str(num_j2))% case of displacements at the same time
|
---|
[127] | 174 | app2= num2str(num_j1);
|
---|
[8] | 175 | else
|
---|
| 176 | app2= [num2str(num_j1) '-' num2str(num_j2)];
|
---|
| 177 | end
|
---|
[122] | 178 | filename=[filename app1 '_' app2 ext];
|
---|
[8] | 179 | end
|
---|
| 180 | idetect=1;
|
---|
| 181 | % idetect=(exist(filename,'file')==2);
|
---|
| 182 | % case of an image pair to determine (comp_input=0)
|
---|
| 183 | else
|
---|
[122] | 184 | [filename,num_i1_out,num_j1_out,num_i2_out,num_j2_out,idetect]=search_pair(filename,num_i1,num_j1,num_i2,nom_type);
|
---|
[8] | 185 | end
|
---|
| 186 |
|
---|
| 187 | %look for sub-directories containing netcdf files
|
---|
| 188 | if idetect==0 && isequal(subdir,'?')
|
---|
| 189 | [pathfile,name]=fileparts(filebase);
|
---|
| 190 | direct=dir(pathfile);%directory containing filebase
|
---|
| 191 | datedir=[];%default
|
---|
[127] | 192 | % idir=0;
|
---|
[8] | 193 | indir=find(cell2mat({direct.isdir}));% find indices of subdirectories
|
---|
[127] | 194 | direct=direct(indir(3:end));% keep only the subdirectories,eliminating the two first terms '.' and '..'
|
---|
[8] | 195 | lengthdir=length(direct);
|
---|
| 196 | if lengthdir==0
|
---|
| 197 | subdir='';% no subdirectory found
|
---|
| 198 | else
|
---|
| 199 | for idir=1:lengthdir
|
---|
| 200 | date_str=direct(idir).date;%string of the date of last modification
|
---|
| 201 | datedir(idir)=0;%default
|
---|
| 202 | char_code=double(date_str);% code of the date characters
|
---|
| 203 | special_char=(char_code>127); %non standard Ascii character (e.g. date in french)
|
---|
[127] | 204 | if isempty(find(special_char,1))% standard Ascii character
|
---|
[8] | 205 | datedir(idir)=datenum(date_str);
|
---|
| 206 | end
|
---|
| 207 | % datedir(idir)=datenum(direct(idir).date); %absolute date of last directory modification
|
---|
| 208 | end
|
---|
| 209 | [mostrec,indrec]=max(datedir);% most recently modified subdir chosen by default
|
---|
| 210 | subdir=direct(indrec).name; %chosen directory
|
---|
| 211 | end
|
---|
| 212 | filebasesub=fullfile(pathfile,subdir,name);
|
---|
| 213 | %if the image pair is imposed
|
---|
[127] | 214 | if (exist('comp_input','var') && isequal(comp_input,1))
|
---|
| 215 | if isequal(nom_type,'#_ab')
|
---|
[8] | 216 | filename=[filebasesub num2str(num_i1,'%03d') '_' num2stra(num_j1,nom_type) num2stra(num_j2,nom_type) ext];
|
---|
[332] | 217 | elseif isequal(nom_type,'_1_1-2')
|
---|
[8] | 218 | filename=[filebasesub '_' num2str(num_i1) '_' num2str(num_j1) '-' num2str(num_i2) ext];
|
---|
[332] | 219 | elseif isequal(nom_type,'_1-2_1')
|
---|
[8] | 220 | filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) '_' num2str(num_j1) ext];
|
---|
[332] | 221 | elseif isequal(nom_type,'_1-2')
|
---|
[8] | 222 | filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) ext];
|
---|
| 223 | end
|
---|
[127] | 224 | % idetect=(exist(filename,'file')==2);
|
---|
[8] | 225 | else
|
---|
[127] | 226 | [filename,num_i1_out,num_j1_out,num_i2_out,num_j2_out]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type);
|
---|
[8] | 227 | end
|
---|
| 228 | end
|
---|
| 229 | end
|
---|
[127] | 230 | if ~strcmp(subdir,'?')
|
---|
| 231 | subdir_out=subdir;
|
---|
| 232 | else
|
---|
| 233 | subdir_out='';
|
---|
| 234 | end
|
---|
[8] | 235 |
|
---|
[89] | 236 | %------------------------------------------------------------------------
|
---|
| 237 | % --- search the appropriate image pair (netcdf file) corresponding to a given image number
|
---|
[8] | 238 | function [filename,num_i1,num_j1,num_i2,num_j2,idetect]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type)
|
---|
[89] | 239 | %------------------------------------------------------------------------
|
---|
[8] | 240 | % for nom_type=netc_2D or netc_old, it searches all the pairs corresponding
|
---|
| 241 | % to num_i1, and chooses the most recent file.
|
---|
| 242 | %for nom_type=netc_3D or netc_series, it searches all the pairs (num_i1
|
---|
| 243 | %num_i2), with num_i1 as the first index, and chooses the most recent file.
|
---|
| 244 |
|
---|
| 245 | filename=[];num_j2=[];idetect=0;%default values
|
---|
[127] | 246 | if isequal(nom_type,'#_ab')
|
---|
[8] | 247 | dirpair=dir([filebasesub num2str(num_i1,'%03d') '_*.nc']);
|
---|
[332] | 248 | elseif isequal(nom_type,'_1_1-2')
|
---|
[8] | 249 | dirpair=dir([filebasesub '_' num2str(num_i1) '_*-*.nc']);
|
---|
[332] | 250 | elseif isequal(nom_type,'_1-2_1')
|
---|
[8] | 251 | dirpair=dir([filebasesub '_' num2str(num_i1) '-*_' num2str(num_j1) '.nc']);
|
---|
[332] | 252 | elseif isequal(nom_type,'_1-2')
|
---|
[8] | 253 | dirpair=dir([filebasesub '_' num2str(num_i1) '-*.nc']);
|
---|
| 254 | if isempty(dirpair)
|
---|
| 255 | dirpair=dir([filebasesub '_*-' num2str(num_i2) '.nc']);
|
---|
| 256 | end
|
---|
| 257 | end
|
---|
| 258 | nbpair=length(dirpair);
|
---|
| 259 | if nbpair >= 1 %choose the most recent file if several are found
|
---|
| 260 | idetect=1; %detected pair
|
---|
[127] | 261 | datepair=zeros(1,nbpair);%default
|
---|
[8] | 262 | for ipair=1:nbpair
|
---|
| 263 | date_str=dirpair(ipair).date;%string of the date of last modification
|
---|
| 264 | char_code=double(date_str);% code of the date characters
|
---|
| 265 | special_char=(char_code>127); %non standard Ascii character (e.g. date in french)
|
---|
| 266 | if isempty(find(special_char))% standard Ascii character
|
---|
| 267 | datepair(ipair)=datenum(date_str);
|
---|
| 268 | end
|
---|
| 269 | end
|
---|
| 270 | [choice,indpair]=max(datepair);
|
---|
[127] | 271 | [pathname,file,field_count,str2,str_a,str_b]=name2display(dirpair(indpair).name);
|
---|
| 272 | num_i1=str2double(field_count);
|
---|
| 273 | num_i2=str2double(str2);
|
---|
[221] | 274 | if isnan(num_i2)
|
---|
| 275 | num_i2=num_i1;
|
---|
| 276 | end
|
---|
| 277 | num_j1=stra2num(str_a);
|
---|
| 278 | num_j2=stra2num(str_b);
|
---|
[8] | 279 | pathname=fileparts(filebasesub);% CORRIGE LE 6 JUIN (ETAIT DESACTIVE)
|
---|
| 280 | filename=fullfile(pathname,dirpair(indpair).name);
|
---|
| 281 | end
|
---|
| 282 |
|
---|
| 283 |
|
---|