source: trunk/src/fileparts_uvmat.m @ 977

Last change on this file since 977 was 977, checked in by g7moreau, 4 years ago
  • Update Copyright 2008-2017 notice
File size: 9.3 KB
RevLine 
[575]1%'fileparts_uvmat': splits a file name in root and indices and recognize file naming convention
[318]2%--------------------------------------------------------------------
[327]3%[RootPath,SubDir,RootFile,i1,i2,j1,j2,Ext,NomType]=fileparts_uvmat(FileInput)
[318]4%
5%OUTPUT:
[334]6%RootPath: path to the base file
7%SubDir: name of the SubDirectory for netcdf files (NomTypes with index pairs 1-2 or ab )
[327]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)
[334]13%FileExt: file Extension
[327]14%NomType: char chain characterizing the file nomenclature: with values
[334]15%   NomType='': constant name [filebase FileExt] (default output if 'NomType' is undefined)
[327]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')
[334]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'
[327]24%SubDir: name of the SubDirectory for netcdf files
[318]25%
26%INPUT:
[327]27%FileInput: complete name of the file, including path
[318]28
[809]29%=======================================================================
[977]30% Copyright 2008-2017, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
[809]31%   http://www.legi.grenoble-inp.fr
32%   Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.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
[334]47function [RootPath,SubDir,RootFile,i1,i2,j1,j2,FileExt,NomType]=fileparts_uvmat(FileInput)
48RootPath='';
49SubDir='';
50RootFile='';
[327]51i1=[];
[318]52i2=[];
53j1=[];
54j2=[];
[334]55FileExt='';
[327]56NomType='';
[318]57
[334]58%% display help and test function in the absence of input arument
[327]59if ~exist('FileInput','var')
60    help fileparts_uvmat;
61    test();
62    return
63end
[318]64
[339]65%% default root name output
[334]66[RootPath,FileName,FileExt]=fileparts(FileInput);
[339]67RootFile=FileName;
[327]68
[339]69%% case of input file name which is a pure number
[334]70if ~isnan(str2double(FileName))
71    RootFile='';
72    i1=str2double(FileName);
73    return
74end
[339]75
76%% recursive test on FileName starting from the end
[327]77% test whether FileName ends with a number or not
[853]78r=regexp(FileName,'(?<num1>\d+)$','names');% \D = not a digit, \d =digit
[318]79
[327]80if ~isempty(r)% FileName end matches num1
[318]81    num1=r.num1;
[853]82    r=regexp(FileName,['(?<num2>\d+)(?<delim1>[-_])' num1 '$'],'names');
[327]83    if ~isempty(r)% FileName end matches num2+delim1+num1
[318]84        delim1=r.delim1;
85        num2=r.num2;
[352]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');
[346]93        if ~isempty(r) % FileName end matches num3 delim2 num2 delim1 num1
[318]94            delim2=r.delim2;
95            num3=r.num3;
96            switch delim1
97                case '_'
[327]98                    j1=str2double(num1);
[318]99                    switch delim2
100                        case '-'
[327]101                            i1=str2double(num3);
102                            i2=str2double(num2);
[318]103                    end
104                case '-'
[327]105                    j1=str2double(num2);
106                    j2=str2double(num1);
[318]107                    switch delim2
108                        case '_'
[327]109                            i1=str2double(num3);
[318]110                    end
111            end
[327]112            NomType=[get_type(num3) delim2 get_type(num2) delim1 get_type(num1)];
[384]113            RootFile=regexprep(FileName,[num3 delim2 num2 delim1 num1 '$'],'');
[318]114        else
115            switch delim1
116                case '_'
[327]117                    i1=str2double(num2);
118                    j1=str2double(num1);
[318]119                case '-'
[327]120                    i1=str2double(num2);
121                    i2=str2double(num1);
[318]122            end
[327]123            NomType=[get_type(num2) delim1 get_type(num1)];
[379]124            RootFile=regexprep(FileName,[num2 delim1 num1 '$'],'');
[318]125        end
[334]126        NomType=regexprep(NomType,'-1','-2'); %set 1-2 instead of 1-1
[318]127    else% only one number at the end
[327]128        i1=str2double(num1);
129        NomType=get_type(num1);
[384]130        RootFile=regexprep(FileName,[num1 '$'],'');
[318]131    end
[327]132else% FileName ends with a letter
[334]133    %r=regexp(FileName,'.*[^a^b^A^B](?<end_string>ab|AB|[abAB])\>','names');
134    NomType='';
[405]135    r=regexp(RootFile,'\D*(?<num1>\d+)(?<end_string>[a-z]|[A-Z]|[a-z][a-z]|[A-Z][A-Z])$','names');
[318]136    if ~isempty(r)
[334]137        NomType=get_type(r.end_string);
138        RootFile=regexprep(RootFile,[r.num1 r.end_string '$'],'');
139    else % case with separator '_'
140        r=regexp(RootFile,'\D(?<num1>\d+)_(?<end_string>[a-z]|[A-Z]|[a-z][a-z]|[A-Z][A-Z])$','names');
[318]141        if ~isempty(r)
[334]142            NomType=['_' get_type(r.end_string)];
143            RootFile=regexprep(RootFile,[r.num1 '_' r.end_string '$'],'');
144        end
[318]145    end
[334]146    if ~isempty(NomType)
147        [j1,j2]=get_value(r.end_string);
148        i1=str2double(r.num1);
149        NomType=[get_type(r.num1) NomType];
150    end
[318]151end
152
[334]153%% suppress '_' at the end of RootFile, put it on NomType
[390]154% if strcmp(RootFile(end),'_')
155%     RootFile(end)=[];
156detect=regexp(RootFile,'_$'); %detect '_' at the end of RootFILE
157if ~isempty(detect)
158    RootFile=regexprep(RootFile,'_$','');
[334]159    NomType=['_' NomType];
[333]160end
[318]161
[339]162%% extract subdirectory for pairs i1-i2 or j1-j2 (or ab, AB)
[437]163% if ~isempty(i2) || ~isempty(j2)
[339]164    r=regexp(RootPath,'\<(?<newrootpath>.+)(\\|/)(?<subdir>[^\\^/]+)(\\|/)*\>','names');
165    if ~isempty(r)
166        SubDir=r.subdir;
167        RootPath=r.newrootpath;
168    end
[334]169% end
[318]170
171
[437]172
[318]173function type=get_type(s)
174% returns the type of a label string:
175%   for numbers, whether filled with 0 or not.
[334]176type='';%default
[318]177
[334]178if ~isempty(regexp(s,'\<\d+\>','ONCE'))
179    type=num2str(1,['%0' num2str(length(s)) 'd']);
180else
181    code=double(s); % ascii code of the input string
182    if code >= 65 & code <= 90 % test on ascii code for capital letters
183        if length(s)==1
184            type='A';
185        elseif length(s)==2
186            type='AB';
[318]187        end
[334]188    elseif  code >= 97 & code <= 122 % test on ascii code for small letters
189        if length(s)==1
190            type='a';
191        elseif length(s)==2
192            type='ab';
193        end
194    end
[318]195end
196
197
[334]198
[318]199function [j1,j2]=get_value(s)
[334]200% returns the value of a label string:
[318]201%   for numbers, whether filled with 0 or not.
202%   for letters, either a, A or ab, AB.
203j1=[];
204j2=[];
[334]205code=double(s); % ascii code of the input string
206if code >= 65 & code <= 90 % test on ascii code for capital letters
207    index=double(s)-64; %change capital letters to corresponding number in the alphabet
208elseif code >= 97 & code <= 122 % test on ascii code for small letters
209    index=double(s)-96; %change small letters to corresponding number in the alphabet
210else
211    index=str2num(s);
[318]212end
[334]213if ~isempty(index)
214    j1=index(1);
215    if length(index)==2
216        j2=index(2);
217    end
218end
[318]219
220
[327]221function test(name)
222fprintf([...
223    '######################################################\n'...
224    '               Test for fileparts_uvmat                  \n'...
225    '######################################################\n'...
226    ]);
227
228if exist('name','var')
229    FileName_list={name};
230else
231    FileName_list={...
232        'Image1a.png'...
233        'toto'...
234        'B011_1.png'...
235        'B001.png'...
236        'B55a.tif'...
237        'B002B.tiff'...
238        'B_001.png'...
239        'B_3331AB.png'...
240        'aa45_2.png'...
241        'B005.png'...
242        'Image_3.jpg'...
243        'Image_3-4.jpg'...
244        'Image_3-4_2.jpg'...
245        'Image_5_3-4.jpg'...
246        'Image_3_ab.jpg'...
[334]247        'Image005AD.jpg'...
248        'Image_3_ac.jpg'...
[327]249        'Image_3a-b.jpg'...
250        'Image3_a.jpg'...
251        'movie57.avi'...
[346]252        'merged_20_12_1.png'...
[327]253        };
254end
255
256for FileName=FileName_list
[334]257%     [RootPath,RootFile,i1,i2,str_a,str_b,FileExt,NomType,SubDir]=name2display(FileName{1});
[353]258    [tild,SubDir,RootFile,i1,i2,j1,j2,tild,NomType]=...
[327]259        fileparts_uvmat(FileName{1});
260    fprintf([...
261        'File name  : ' FileName{1}  '\n'...
[333]262        '  NomType  : '    NomType '\n'...
263        '  RootFile : '    RootFile '\n'...
264        '  i1 / i2     : '     num2str(i1) ' / ' num2str(i2) '\n'...
265        '  j1 / j2      : '    num2str(j1) ' / ' num2str(j2) '\n'...
[327]266        ]);
267end
268
269
Note: See TracBrowser for help on using the repository browser.