| 1 | %'fileparts_uvmat': splits a file name in root and indices and recognize file naming convention
|
|---|
| 2 | %--------------------------------------------------------------------
|
|---|
| 3 | %[RootPath,SubDir,RootFile,i1,i2,j1,j2,Ext,NomType]=fileparts_uvmat(FileInput)
|
|---|
| 4 | %
|
|---|
| 5 | %OUTPUT:
|
|---|
| 6 | %RootPath: path to the base file
|
|---|
| 7 | %SubDir: name of the SubDirectory for netcdf files (NomTypes with index pairs 1-2 or ab )
|
|---|
| 8 | %RootFile: FileName without appendix
|
|---|
| 9 | %i1: first number i
|
|---|
| 10 | %i2: second number i (only for .nc files)
|
|---|
| 11 | %j1: first number j
|
|---|
| 12 | %j2: second number j (only for .nc files)
|
|---|
| 13 | %FileExt: file Extension
|
|---|
| 14 | %NomType: char chain characterizing the file nomenclature: with values
|
|---|
| 15 | % NomType='': constant name [filebase FileExt] (default output if 'NomType' is undefined)
|
|---|
| 16 | % NomType='*':constant name for a file representing a series (e.g. avi movie)
|
|---|
| 17 | % NomType='1','01',or '001'...': series of files with a single index i without separator(e.g. 'aa045.png').
|
|---|
| 18 | % NomType='1a','1A','01a','01A','1_a','01_A',... with a numerical index and an index letter(e.g.'aa45b.png') (lower or upper case)
|
|---|
| 19 | % NomType='1_1','01_1',...: matrix of files with two indices i and j separated by '_'(e.g. 'aa45_2.png')
|
|---|
| 20 | % NomType='1-1': from pairs from a single index (e.g. 'aa_45-47.nc')
|
|---|
| 21 | % NomType='1_1-2': pairs of j indices (e.g. 'aa_45_2-3.nc')
|
|---|
| 22 | % NomType='1-2_1': pairs of i indices (e.g. 'aa_45-46_2.nc')
|
|---|
| 23 | % NomType='1_ab','01_ab','01ab'..., from pairs of '#' images (e.g.'aa045bc.nc'), FileExt='.nc'
|
|---|
| 24 | %SubDir: name of the SubDirectory for netcdf files
|
|---|
| 25 | %
|
|---|
| 26 | %INPUT:
|
|---|
| 27 | %FileInput: complete name of the file, including path
|
|---|
| 28 |
|
|---|
| 29 | %=======================================================================
|
|---|
| 30 | % Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
|
|---|
| 31 | % http://www.legi.grenoble-inp.fr
|
|---|
| 32 | % Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
|
|---|
| 33 | %
|
|---|
| 34 | % This file is part of the toolbox UVMAT.
|
|---|
| 35 | %
|
|---|
| 36 | % UVMAT is free software; you can redistribute it and/or modify
|
|---|
| 37 | % it under the terms of the GNU General Public License as published
|
|---|
| 38 | % by the Free Software Foundation; either version 2 of the license,
|
|---|
| 39 | % or (at your option) any later version.
|
|---|
| 40 | %
|
|---|
| 41 | % UVMAT is distributed in the hope that it will be useful,
|
|---|
| 42 | % but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 43 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 44 | % GNU General Public License (see LICENSE.txt) for more details.
|
|---|
| 45 | %=======================================================================
|
|---|
| 46 |
|
|---|
| 47 | function [RootPath,SubDir,RootFile,i1,i2,j1,j2,FileExt,NomType]=fileparts_uvmat(FileInput)
|
|---|
| 48 | RootPath='';
|
|---|
| 49 | SubDir='';
|
|---|
| 50 | RootFile='';
|
|---|
| 51 | i1=[];
|
|---|
| 52 | i2=[];
|
|---|
| 53 | j1=[];
|
|---|
| 54 | j2=[];
|
|---|
| 55 | FileExt='';
|
|---|
| 56 | NomType='';
|
|---|
| 57 |
|
|---|
| 58 | %% display help and test function in the absence of input arument
|
|---|
| 59 | if ~exist('FileInput','var')
|
|---|
| 60 | help fileparts_uvmat;
|
|---|
| 61 | test();
|
|---|
| 62 | return
|
|---|
| 63 | end
|
|---|
| 64 |
|
|---|
| 65 | %% default root name output
|
|---|
| 66 | [RootPath,FileName,FileExt]=fileparts(FileInput);
|
|---|
| 67 | RootFile=FileName;
|
|---|
| 68 |
|
|---|
| 69 | %% case of input file name which is a pure number
|
|---|
| 70 | if ~isnan(str2double(FileName))
|
|---|
| 71 | RootFile='';
|
|---|
| 72 | i1=str2double(FileName);
|
|---|
| 73 | return
|
|---|
| 74 | end
|
|---|
| 75 |
|
|---|
| 76 | %% recursive test on FileName starting from the end
|
|---|
| 77 | % test whether FileName ends with a number or not
|
|---|
| 78 | r=regexp(FileName,'(?<num1>\d+)$','names');% \D = not a digit, \d =digit
|
|---|
| 79 |
|
|---|
| 80 | if ~isempty(r)% FileName end matches num1
|
|---|
| 81 | num1=r.num1;
|
|---|
| 82 | r=regexp(FileName,['(?<num2>\d+)(?<delim1>[-_])' num1 '$'],'names');
|
|---|
| 83 | if ~isempty(r)% FileName end matches num2+delim1+num1
|
|---|
| 84 | delim1=r.delim1;
|
|---|
| 85 | num2=r.num2;
|
|---|
| 86 | switch delim1
|
|---|
| 87 | case '_'
|
|---|
| 88 | delim2_to_match='-';
|
|---|
| 89 | case '-'
|
|---|
| 90 | delim2_to_match='_';
|
|---|
| 91 | end
|
|---|
| 92 | r=regexp(FileName,['.*\D(?<num3>\d+)(?<delim2>' delim2_to_match ')' num2 delim1 num1 '$'],'names');
|
|---|
| 93 | if ~isempty(r) % FileName end matches num3 delim2 num2 delim1 num1
|
|---|
| 94 | delim2=r.delim2;
|
|---|
| 95 | num3=r.num3;
|
|---|
| 96 | ipair=1;
|
|---|
| 97 | jpair=1;
|
|---|
| 98 | switch delim1
|
|---|
| 99 | case '_'
|
|---|
| 100 | j1=str2double(num1);
|
|---|
| 101 | switch delim2
|
|---|
| 102 | case '-'
|
|---|
| 103 | i1=str2double(num3);
|
|---|
| 104 | i2=str2double(num2);
|
|---|
| 105 | if i2<=i1
|
|---|
| 106 | ipair=0;
|
|---|
| 107 | end
|
|---|
| 108 | end
|
|---|
| 109 | case '-'
|
|---|
| 110 | j1=str2double(num2);
|
|---|
| 111 | j2=str2double(num1);
|
|---|
| 112 | if j2<=j1
|
|---|
| 113 | jpair=0;
|
|---|
| 114 | end
|
|---|
| 115 | switch delim2
|
|---|
| 116 | case '_'
|
|---|
| 117 | i1=str2double(num3);
|
|---|
| 118 | end
|
|---|
| 119 | end
|
|---|
| 120 | if ipair && jpair
|
|---|
| 121 | NomType=[get_type(num3) delim2 get_type(num2) delim1 get_type(num1)];
|
|---|
| 122 | RootFile=regexprep(FileName,[num3 delim2 num2 delim1 num1 '$'],'');
|
|---|
| 123 | else
|
|---|
| 124 | if ipair==0
|
|---|
| 125 | i1=i2;
|
|---|
| 126 | i2=[];
|
|---|
| 127 | NomType=[get_type(num2) delim1 get_type(num1)];
|
|---|
| 128 | RootFile=regexprep(FileName,[ num2 delim1 num1 '$'],'');
|
|---|
| 129 | elseif jpair==0
|
|---|
| 130 | j1=j2;
|
|---|
| 131 | j2=[];
|
|---|
| 132 | NomType=get_type(num1);
|
|---|
| 133 | RootFile=regexprep(FileName,[num1 '$'],'');
|
|---|
| 134 | end
|
|---|
| 135 | end
|
|---|
| 136 |
|
|---|
| 137 | else
|
|---|
| 138 | ipair=1;
|
|---|
| 139 | switch delim1
|
|---|
| 140 | case '_'
|
|---|
| 141 | i1=str2double(num2);
|
|---|
| 142 | j1=str2double(num1);
|
|---|
| 143 | case '-'
|
|---|
| 144 | i1=str2double(num2);
|
|---|
| 145 | i2=str2double(num1);
|
|---|
| 146 | if i2<=i1
|
|---|
| 147 | ipair=0;
|
|---|
| 148 | end
|
|---|
| 149 | end
|
|---|
| 150 | if ipair
|
|---|
| 151 | NomType=[get_type(num2) delim1 get_type(num1)];
|
|---|
| 152 | RootFile=regexprep(FileName,[num2 delim1 num1 '$'],'');
|
|---|
| 153 | else
|
|---|
| 154 | i1=i2;
|
|---|
| 155 | i2=[];% no pair detected if i2<=i1
|
|---|
| 156 | NomType= get_type(num1);
|
|---|
| 157 | RootFile=regexprep(FileName,[ num1 '$'],'');
|
|---|
| 158 | end
|
|---|
| 159 | end
|
|---|
| 160 | NomType=regexprep(NomType,'-1','-2'); %set 1-2 instead of 1-1
|
|---|
| 161 | else% only one number at the end
|
|---|
| 162 | i1=str2double(num1);
|
|---|
| 163 | NomType=get_type(num1);
|
|---|
| 164 | RootFile=regexprep(FileName,[num1 '$'],'');
|
|---|
| 165 | end
|
|---|
| 166 | else% FileName ends with a letter
|
|---|
| 167 | %r=regexp(FileName,'.*[^a^b^A^B](?<end_string>ab|AB|[abAB])\>','names');
|
|---|
| 168 | NomType='';
|
|---|
| 169 | r=regexp(RootFile,'\D*(?<num1>\d+)(?<end_string>[a-z]|[A-Z]|[a-z][a-z]|[A-Z][A-Z])$','names');
|
|---|
| 170 | if ~isempty(r)
|
|---|
| 171 | NomType=get_type(r.end_string);
|
|---|
| 172 | RootFile=regexprep(RootFile,[r.num1 r.end_string '$'],'');
|
|---|
| 173 | else % case with separator '_'
|
|---|
| 174 | r=regexp(RootFile,'\D*(?<num1>\d+)_(?<end_string>[a-z]|[A-Z]|[a-z][a-z]|[A-Z][A-Z])$','names');
|
|---|
| 175 | if ~isempty(r)
|
|---|
| 176 | NomType=['_' get_type(r.end_string)];
|
|---|
| 177 | RootFile=regexprep(RootFile,[r.num1 '_' r.end_string '$'],'');
|
|---|
| 178 | end
|
|---|
| 179 | end
|
|---|
| 180 | if ~isempty(NomType)
|
|---|
| 181 | [j1,j2]=get_value(r.end_string);
|
|---|
| 182 | i1=str2double(r.num1);
|
|---|
| 183 | NomType=[get_type(r.num1) NomType];
|
|---|
| 184 | end
|
|---|
| 185 | end
|
|---|
| 186 |
|
|---|
| 187 | %% suppress '_' at the end of RootFile, put it on NomType
|
|---|
| 188 | detect=regexp(RootFile,'_$','once'); %detect '_' at the end of RootFILE
|
|---|
| 189 | if ~isempty(detect)
|
|---|
| 190 | RootFile=regexprep(RootFile,'_$','');
|
|---|
| 191 | NomType=['_' NomType];
|
|---|
| 192 | end
|
|---|
| 193 |
|
|---|
| 194 | %% extract subdirectory for pairs i1-i2 or j1-j2 (or ab, AB)
|
|---|
| 195 | r=regexp(RootPath,'\<(?<newrootpath>.+)(\\|/)(?<subdir>[^\\^/]+)(\\|/)*\>','names');
|
|---|
| 196 | if ~isempty(r)
|
|---|
| 197 | SubDir=r.subdir;
|
|---|
| 198 | RootPath=r.newrootpath;
|
|---|
| 199 | end
|
|---|
| 200 |
|
|---|
| 201 |
|
|---|
| 202 |
|
|---|
| 203 | function type=get_type(s)
|
|---|
| 204 | % returns the type of a label string:
|
|---|
| 205 | % for numbers, whether filled with 0 or not.
|
|---|
| 206 | type='';%default
|
|---|
| 207 |
|
|---|
| 208 | if ~isempty(regexp(s,'\<\d+\>','ONCE'))
|
|---|
| 209 | type=num2str(1,['%0' num2str(length(s)) 'd']);
|
|---|
| 210 | else
|
|---|
| 211 | code=double(s); % ascii code of the input string
|
|---|
| 212 | if code >= 65 & code <= 90 % test on ascii code for capital letters
|
|---|
| 213 | if length(s)==1
|
|---|
| 214 | type='A';
|
|---|
| 215 | elseif length(s)==2
|
|---|
| 216 | type='AB';
|
|---|
| 217 | end
|
|---|
| 218 | elseif code >= 97 & code <= 122 % test on ascii code for small letters
|
|---|
| 219 | if length(s)==1
|
|---|
| 220 | type='a';
|
|---|
| 221 | elseif length(s)==2
|
|---|
| 222 | type='ab';
|
|---|
| 223 | end
|
|---|
| 224 | end
|
|---|
| 225 | end
|
|---|
| 226 |
|
|---|
| 227 |
|
|---|
| 228 |
|
|---|
| 229 | function [j1,j2]=get_value(s)
|
|---|
| 230 | % returns the value of a label string:
|
|---|
| 231 | % for numbers, whether filled with 0 or not.
|
|---|
| 232 | % for letters, either a, A or ab, AB.
|
|---|
| 233 | j1=[];
|
|---|
| 234 | j2=[];
|
|---|
| 235 | code=double(s); % ascii code of the input string
|
|---|
| 236 | if code >= 65 & code <= 90 % test on ascii code for capital letters
|
|---|
| 237 | index=double(s)-64; %change capital letters to corresponding number in the alphabet
|
|---|
| 238 | elseif code >= 97 & code <= 122 % test on ascii code for small letters
|
|---|
| 239 | index=double(s)-96; %change small letters to corresponding number in the alphabet
|
|---|
| 240 | else
|
|---|
| 241 | index=str2num(s);
|
|---|
| 242 | end
|
|---|
| 243 | if ~isempty(index)
|
|---|
| 244 | j1=index(1);
|
|---|
| 245 | if length(index)==2
|
|---|
| 246 | j2=index(2);
|
|---|
| 247 | end
|
|---|
| 248 | end
|
|---|
| 249 |
|
|---|
| 250 |
|
|---|
| 251 | function test(name)
|
|---|
| 252 | fprintf([...
|
|---|
| 253 | '######################################################\n'...
|
|---|
| 254 | ' Test for fileparts_uvmat \n'...
|
|---|
| 255 | '######################################################\n'...
|
|---|
| 256 | ]);
|
|---|
| 257 |
|
|---|
| 258 | if exist('name','var')
|
|---|
| 259 | FileName_list={name};
|
|---|
| 260 | else
|
|---|
| 261 | FileName_list={...
|
|---|
| 262 | 'Image1a.png'...
|
|---|
| 263 | 'toto'...
|
|---|
| 264 | 'B011_1.png'...
|
|---|
| 265 | 'B001.png'...
|
|---|
| 266 | 'B55a.tif'...
|
|---|
| 267 | 'B002B.tiff'...
|
|---|
| 268 | 'B_001.png'...
|
|---|
| 269 | 'B_3331AB.png'...
|
|---|
| 270 | 'aa45_2.png'...
|
|---|
| 271 | 'B005.png'...
|
|---|
| 272 | 'Image_3.jpg'...
|
|---|
| 273 | 'Image_3-4.jpg'...
|
|---|
| 274 | 'Image_3-4_2.jpg'...
|
|---|
| 275 | 'Image_5_3-4.jpg'...
|
|---|
| 276 | 'Image_3_ab.jpg'...
|
|---|
| 277 | 'Image005AD.jpg'...
|
|---|
| 278 | 'Image_3_ac.jpg'...
|
|---|
| 279 | 'Image_3a-b.jpg'...
|
|---|
| 280 | 'Image3_a.jpg'...
|
|---|
| 281 | 'movie57.avi'...
|
|---|
| 282 | 'merged_20_12_1.png'...
|
|---|
| 283 | };
|
|---|
| 284 | end
|
|---|
| 285 |
|
|---|
| 286 | for FileName=FileName_list
|
|---|
| 287 | % [RootPath,RootFile,i1,i2,str_a,str_b,FileExt,NomType,SubDir]=name2display(FileName{1});
|
|---|
| 288 | [tild,SubDir,RootFile,i1,i2,j1,j2,tild,NomType]=...
|
|---|
| 289 | fileparts_uvmat(FileName{1});
|
|---|
| 290 | fprintf([...
|
|---|
| 291 | 'File name : ' FileName{1} '\n'...
|
|---|
| 292 | ' NomType : ' NomType '\n'...
|
|---|
| 293 | ' RootFile : ' RootFile '\n'...
|
|---|
| 294 | ' i1 / i2 : ' num2str(i1) ' / ' num2str(i2) '\n'...
|
|---|
| 295 | ' j1 / j2 : ' num2str(j1) ' / ' num2str(j2) '\n'...
|
|---|
| 296 | ]);
|
|---|
| 297 | end
|
|---|
| 298 |
|
|---|
| 299 |
|
|---|