source: trunk/src/fileparts_uvmat.m @ 965

Last change on this file since 965 was 924, checked in by g7moreau, 8 years ago
  • Update Copyright Copyright 2008-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
File size: 9.3 KB
Line 
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-2016, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
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
47function [RootPath,SubDir,RootFile,i1,i2,j1,j2,FileExt,NomType]=fileparts_uvmat(FileInput)
48RootPath='';
49SubDir='';
50RootFile='';
51i1=[];
52i2=[];
53j1=[];
54j2=[];
55FileExt='';
56NomType='';
57
58%% display help and test function in the absence of input arument
59if ~exist('FileInput','var')
60    help fileparts_uvmat;
61    test();
62    return
63end
64
65%% default root name output
66[RootPath,FileName,FileExt]=fileparts(FileInput);
67RootFile=FileName;
68
69%% case of input file name which is a pure number
70if ~isnan(str2double(FileName))
71    RootFile='';
72    i1=str2double(FileName);
73    return
74end
75
76%% recursive test on FileName starting from the end
77% test whether FileName ends with a number or not
78r=regexp(FileName,'(?<num1>\d+)$','names');% \D = not a digit, \d =digit
79
80if ~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            switch delim1
97                case '_'
98                    j1=str2double(num1);
99                    switch delim2
100                        case '-'
101                            i1=str2double(num3);
102                            i2=str2double(num2);
103                    end
104                case '-'
105                    j1=str2double(num2);
106                    j2=str2double(num1);
107                    switch delim2
108                        case '_'
109                            i1=str2double(num3);
110                    end
111            end
112            NomType=[get_type(num3) delim2 get_type(num2) delim1 get_type(num1)];
113            RootFile=regexprep(FileName,[num3 delim2 num2 delim1 num1 '$'],'');
114        else
115            switch delim1
116                case '_'
117                    i1=str2double(num2);
118                    j1=str2double(num1);
119                case '-'
120                    i1=str2double(num2);
121                    i2=str2double(num1);
122            end
123            NomType=[get_type(num2) delim1 get_type(num1)];
124            RootFile=regexprep(FileName,[num2 delim1 num1 '$'],'');
125        end
126        NomType=regexprep(NomType,'-1','-2'); %set 1-2 instead of 1-1
127    else% only one number at the end
128        i1=str2double(num1);
129        NomType=get_type(num1);
130        RootFile=regexprep(FileName,[num1 '$'],'');
131    end
132else% FileName ends with a letter
133    %r=regexp(FileName,'.*[^a^b^A^B](?<end_string>ab|AB|[abAB])\>','names');
134    NomType='';
135    r=regexp(RootFile,'\D*(?<num1>\d+)(?<end_string>[a-z]|[A-Z]|[a-z][a-z]|[A-Z][A-Z])$','names');
136    if ~isempty(r)
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');
141        if ~isempty(r)
142            NomType=['_' get_type(r.end_string)];
143            RootFile=regexprep(RootFile,[r.num1 '_' r.end_string '$'],'');
144        end
145    end
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
151end
152
153%% suppress '_' at the end of RootFile, put it on NomType
154% if strcmp(RootFile(end),'_')
155%     RootFile(end)=[];
156detect=regexp(RootFile,'_$'); %detect '_' at the end of RootFILE
157if ~isempty(detect)
158    RootFile=regexprep(RootFile,'_$','');
159    NomType=['_' NomType];
160end
161
162%% extract subdirectory for pairs i1-i2 or j1-j2 (or ab, AB)
163% if ~isempty(i2) || ~isempty(j2)
164    r=regexp(RootPath,'\<(?<newrootpath>.+)(\\|/)(?<subdir>[^\\^/]+)(\\|/)*\>','names');
165    if ~isempty(r)
166        SubDir=r.subdir;
167        RootPath=r.newrootpath;
168    end
169% end
170
171
172
173function type=get_type(s)
174% returns the type of a label string:
175%   for numbers, whether filled with 0 or not.
176type='';%default
177
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';
187        end
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
195end
196
197
198
199function [j1,j2]=get_value(s)
200% returns the value of a label string:
201%   for numbers, whether filled with 0 or not.
202%   for letters, either a, A or ab, AB.
203j1=[];
204j2=[];
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);
212end
213if ~isempty(index)
214    j1=index(1);
215    if length(index)==2
216        j2=index(2);
217    end
218end
219
220
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'...
247        'Image005AD.jpg'...
248        'Image_3_ac.jpg'...
249        'Image_3a-b.jpg'...
250        'Image3_a.jpg'...
251        'movie57.avi'...
252        'merged_20_12_1.png'...
253        };
254end
255
256for FileName=FileName_list
257%     [RootPath,RootFile,i1,i2,str_a,str_b,FileExt,NomType,SubDir]=name2display(FileName{1});
258    [tild,SubDir,RootFile,i1,i2,j1,j2,tild,NomType]=...
259        fileparts_uvmat(FileName{1});
260    fprintf([...
261        'File name  : ' FileName{1}  '\n'...
262        '  NomType  : '    NomType '\n'...
263        '  RootFile : '    RootFile '\n'...
264        '  i1 / i2     : '     num2str(i1) ' / ' num2str(i2) '\n'...
265        '  j1 / j2      : '    num2str(j1) ' / ' num2str(j2) '\n'...
266        ]);
267end
268
269
Note: See TracBrowser for help on using the repository browser.