source: trunk/src/fileparts_uvmat.m @ 1141

Last change on this file since 1141 was 1131, checked in by sommeria, 9 months ago

updated to read pivdata from fluidimage

File size: 10.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%=======================================================================
[1126]30% Copyright 2008-2024, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France
[809]31%   http://www.legi.grenoble-inp.fr
[1127]32%   Joel.Sommeria - Joel.Sommeria (A) univ-grenoble-alpes.fr
[809]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;
[1131]96            ipair=1;
97            jpair=1;
[318]98            switch delim1
99                case '_'
[327]100                    j1=str2double(num1);
[318]101                    switch delim2
102                        case '-'
[327]103                            i1=str2double(num3);
104                            i2=str2double(num2);
[1131]105                            if i2<=i1
106                                ipair=0;
107                            end
[318]108                    end
109                case '-'
[327]110                    j1=str2double(num2);
111                    j2=str2double(num1);
[1131]112                    if j2<=j1
113                                jpair=0;
114                    end
[318]115                    switch delim2
116                        case '_'
[327]117                            i1=str2double(num3);
[318]118                    end
119            end
[1131]120            if ipair && jpair
[327]121            NomType=[get_type(num3) delim2 get_type(num2) delim1 get_type(num1)];
[384]122            RootFile=regexprep(FileName,[num3 delim2 num2 delim1 num1 '$'],'');
[1131]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       
[318]137        else
[1131]138            ipair=1;
[318]139            switch delim1
140                case '_'
[327]141                    i1=str2double(num2);
142                    j1=str2double(num1);
[318]143                case '-'
[327]144                    i1=str2double(num2);
145                    i2=str2double(num1);
[1131]146                    if i2<=i1
147                        ipair=0;
148                    end
[318]149            end
[1131]150            if ipair
[327]151            NomType=[get_type(num2) delim1 get_type(num1)];
[379]152            RootFile=regexprep(FileName,[num2 delim1 num1 '$'],'');
[1131]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
[318]159        end
[334]160        NomType=regexprep(NomType,'-1','-2'); %set 1-2 instead of 1-1
[318]161    else% only one number at the end
[327]162        i1=str2double(num1);
163        NomType=get_type(num1);
[384]164        RootFile=regexprep(FileName,[num1 '$'],'');
[318]165    end
[327]166else% FileName ends with a letter
[334]167    %r=regexp(FileName,'.*[^a^b^A^B](?<end_string>ab|AB|[abAB])\>','names');
168    NomType='';
[405]169    r=regexp(RootFile,'\D*(?<num1>\d+)(?<end_string>[a-z]|[A-Z]|[a-z][a-z]|[A-Z][A-Z])$','names');
[318]170    if ~isempty(r)
[334]171        NomType=get_type(r.end_string);
172        RootFile=regexprep(RootFile,[r.num1 r.end_string '$'],'');
173    else % case with separator '_'
[1022]174        r=regexp(RootFile,'\D*(?<num1>\d+)_(?<end_string>[a-z]|[A-Z]|[a-z][a-z]|[A-Z][A-Z])$','names');
[318]175        if ~isempty(r)
[334]176            NomType=['_' get_type(r.end_string)];
177            RootFile=regexprep(RootFile,[r.num1 '_' r.end_string '$'],'');
178        end
[318]179    end
[334]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
[318]185end
186
[334]187%% suppress '_' at the end of RootFile, put it on NomType
[1131]188detect=regexp(RootFile,'_$','once'); %detect '_' at the end of RootFILE
[390]189if ~isempty(detect)
190    RootFile=regexprep(RootFile,'_$','');
[334]191    NomType=['_' NomType];
[333]192end
[318]193
[339]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
[318]200
201
[437]202
[318]203function type=get_type(s)
204% returns the type of a label string:
205%   for numbers, whether filled with 0 or not.
[334]206type='';%default
[318]207
[334]208if ~isempty(regexp(s,'\<\d+\>','ONCE'))
209    type=num2str(1,['%0' num2str(length(s)) 'd']);
210else
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';
[318]217        end
[334]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
[318]225end
226
227
[334]228
[318]229function [j1,j2]=get_value(s)
[334]230% returns the value of a label string:
[318]231%   for numbers, whether filled with 0 or not.
232%   for letters, either a, A or ab, AB.
233j1=[];
234j2=[];
[334]235code=double(s); % ascii code of the input string
236if 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
238elseif 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
240else
241    index=str2num(s);
[318]242end
[334]243if ~isempty(index)
244    j1=index(1);
245    if length(index)==2
246        j2=index(2);
247    end
248end
[318]249
250
[327]251function test(name)
252fprintf([...
253    '######################################################\n'...
254    '               Test for fileparts_uvmat                  \n'...
255    '######################################################\n'...
256    ]);
257
258if exist('name','var')
259    FileName_list={name};
260else
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'...
[334]277        'Image005AD.jpg'...
278        'Image_3_ac.jpg'...
[327]279        'Image_3a-b.jpg'...
280        'Image3_a.jpg'...
281        'movie57.avi'...
[346]282        'merged_20_12_1.png'...
[327]283        };
284end
285
286for FileName=FileName_list
[334]287%     [RootPath,RootFile,i1,i2,str_a,str_b,FileExt,NomType,SubDir]=name2display(FileName{1});
[353]288    [tild,SubDir,RootFile,i1,i2,j1,j2,tild,NomType]=...
[327]289        fileparts_uvmat(FileName{1});
290    fprintf([...
291        'File name  : ' FileName{1}  '\n'...
[333]292        '  NomType  : '    NomType '\n'...
293        '  RootFile : '    RootFile '\n'...
294        '  i1 / i2     : '     num2str(i1) ' / ' num2str(i2) '\n'...
295        '  j1 / j2      : '    num2str(j1) ' / ' num2str(j2) '\n'...
[327]296        ]);
297end
298
299
Note: See TracBrowser for help on using the repository browser.