Changeset 327


Ignore:
Timestamp:
Dec 8, 2011, 4:32:31 PM (12 years ago)
Author:
gostiaux
Message:

New convention for naming

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/fileparts_uvmat.m

    r318 r327  
    1 %'name2display': extracts the root name and field numbers from an input filename
     1%'fileparts_uvmat': Splits a file name and recognize file naming convention
    22%--------------------------------------------------------------------
    3 %[RootPath,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(fileinput)
     3%[RootPath,SubDir,RootFile,i1,i2,j1,j2,Ext,NomType]=fileparts_uvmat(FileInput)
    44%
    55%OUTPUT:
    6 %filebasesub: filename without appendix
    7 %field_count: string for the first number i
    8 %str2: string for the second number i (only for .nc files)
    9 %str_a: string for the first number j
    10 %str_b:string for the second number j (only for .nc files)
    11 %ext: file extension
    12 %nom_type: char chain characterizing the file nomenclature: with values
    13 %   nom_type='': constant name [filebase ext] (default output if 'nom_type' is undefined)
    14 %   nom_type='*':constant name for a file representing a series (e.g. avi movie)
    15 %   nom_type='1','01',or '001'...': series of files with a single index i without separator(e.g. 'aa045.png').
    16 %   nom_type='_1','_01','_001'...':  series of files with a single index i with separator '_'(e.g. 'aa_045.png').
    17 %   nom_type='1a','1A','01a','01A',... with a numerical index and an index letter(e.g.'aa45b.png') (lower or upper case)
    18 %   nom_type='_1a','_1A','_01a','_01A',...: idem, with a separator '_' before the index
    19 %   nom_type='_1_1','_01_1',...: matrix of files with two indices i and j separated by '_'(e.g. 'aa_45_2.png')
    20 %   nom_type='1_1','01_1',...: matrix of files with two indices i and j separated by '_'(e.g. 'aa45_2.png')
    21 %   nom_type='_i1-i2': from pairs from a single index (e.g. 'aa_45-47.nc')
    22 %   nom_type='_i_j1-j2': pairs of j indices (e.g. 'aa_45_2-3.nc')
    23 %   nom_type='_i1-i2_j': pairs of i indices (e.g. 'aa_45-46_2.nc')
    24 %   nom_type='_1_ab','1_ab','01_ab'..., from pairs of '#' images (e.g.'aa045bc.nc'), ext='.nc'
    25 %subdir: name of the subdirectory for netcdf files
     6%RootFile: FileName without appendix
     7%i1: first number i
     8%i2: second number i (only for .nc files)
     9%j1: first number j
     10%j2: second number j (only for .nc files)
     11%Ext: file Extension
     12%NomType: char chain characterizing the file nomenclature: with values
     13%   NomType='': constant name [filebase Ext] (default output if 'NomType' is undefined)
     14%   NomType='*':constant name for a file representing a series (e.g. avi movie)
     15%   NomType='1','01',or '001'...': series of files with a single index i without separator(e.g. 'aa045.png').
     16%   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)
     17%   NomType='1_1','01_1',...: matrix of files with two indices i and j separated by '_'(e.g. 'aa45_2.png')
     18%   NomType='1-1': from pairs from a single index (e.g. 'aa_45-47.nc')
     19%   NomType='1_1-1': pairs of j indices (e.g. 'aa_45_2-3.nc')
     20%   NomType='1-1_1': pairs of i indices (e.g. 'aa_45-46_2.nc')
     21%   NomType='1_ab','01_ab','01_a-b'..., from pairs of '#' images (e.g.'aa045bc.nc'), Ext='.nc'
     22%SubDir: name of the SubDirectory for netcdf files
     23
     24%   OLD TYPES, not supported anymore
     25%   NomType='_1','_01','_001'...':  series of files with a single index i with separator '_'(e.g. 'aa_045.png').
     26%   NomType='_1a','_1A','_01a','_01A',...: idem, with a separator '_' before the index
     27%   NomType='_1_1','_01_1',...: matrix of files with two indices i and j separated by '_'(e.g. 'aa_45_2.png')
     28%   NomType='_i1-i2': from pairs from a single index (e.g. 'aa_45-47.nc')
     29%   NomType='_i_j1-j2': pairs of j indices (e.g. 'aa_45_2-3.nc')
     30%   NomType='_i1-i2_j': pairs of i indices (e.g. 'aa_45-46_2.nc')
     31%   NomType='_1_ab','1_ab','01_ab'..., from pairs of '#' images (e.g.'aa045bc.nc'), Ext='.nc'
     32
    2633%
    2734%INPUT:
    28 %fileinput: complete name of the file, including path
    29 
    30 function [RootPath,RootFile,i1,i2,j1,j2,ext,nom_type,subdir]=name2display2(fileinput)
    31 % siz=length(fileinput);
    32 % indcur=siz;
    33 % default values:
    34 % test_=0;
    35 i1=[];%character string
     35%FileInput: complete name of the file, including path
     36
     37function [RootPath,SubDir,RootFile,i1,i2,j1,j2,Ext,NomType]=fileparts_uvmat(FileInput)
     38
     39i1=[];
    3640i2=[];
    3741j1=[];
    3842j2=[];
    39 nom_type='';
    40 subdir='';
     43NomType='';
     44SubDir='';
    4145RootFile='';
    4246
    43 %select file extension
    44 [RootPath,filename,ext]=fileparts(fileinput);
     47if ~exist('FileInput','var')
     48    help fileparts_uvmat;
     49    test();
     50    return
     51end
     52
     53
     54[RootPath,FileName,Ext]=fileparts(FileInput);
     55
     56switch Ext
     57    case '.avi'
     58        NomType='*';
     59        return
     60    case {'.tif','.tiff'}
     61        if exist(FileInput,'file')
     62            info=iminfo(FileInput);
     63            if length(info)>1
     64                NomType='*';
     65                return
     66            end
     67        end
     68end
    4569
    4670% \D not a digit
     
    4872
    4973
    50 %% recursive test on filename stqrting from the end
    51 
    52 % test whether filename ends with a number or not
    53 r=regexp(filename,'.*\D(?<num1>\d+)\>','names');
    54 
    55 if ~isempty(r)% filename end matches num1
     74%% recursive test on FileName stqrting from the end
     75
     76% test whether FileName ends with a number or not
     77r=regexp(FileName,'.*\D(?<num1>\d+)\>','names');
     78
     79if ~isempty(r)% FileName end matches num1
    5680    num1=r.num1;
    57     r=regexp(filename,['.*\D(?<num2>\d+)(?<delim1>[-_])' num1 '\>'],'names');
    58     if ~isempty(r)% filename end matches num2+delim1+num1
     81    r=regexp(FileName,['.*\D(?<num2>\d+)(?<delim1>[-_])' num1 '\>'],'names');
     82    if ~isempty(r)% FileName end matches num2+delim1+num1
    5983        delim1=r.delim1;
    6084        num2=r.num2;
    61         r=regexp(filename,['.*\D(?<num3>\d+)(?<delim2>[-_])' num2 delim1 num1 '\>'],'names');
    62         if ~isempty(r) % filename end matches delim2 num2 delim1 num1
     85        r=regexp(FileName,['.*\D(?<num3>\d+)(?<delim2>[-_])' num2 delim1 num1 '\>'],'names');
     86        if ~isempty(r) % FileName end matches delim2 num2 delim1 num1
    6387            delim2=r.delim2;
    6488            num3=r.num3;
    6589            switch delim1
    6690                case '_'
    67                     j1=str2num(num1);
     91                    j1=str2double(num1);
    6892                    switch delim2
    6993                        case '-'
    70                             i1=str2num(num3);
    71                             i2=str2num(num2);
     94                            i1=str2double(num3);
     95                            i2=str2double(num2);
    7296                    end
    7397                case '-'
    74                     j1=str2num(num2);
    75                     j2=str2num(num1);
     98                    j1=str2double(num2);
     99                    j2=str2double(num1);
    76100                    switch delim2
    77101                        case '_'
    78                             i1=str2num(num3);
     102                            i1=str2double(num3);
    79103                    end
    80104            end
    81             nom_type=[get_type(num3) delim2 get_type(num2) delim1 get_type(num1)];
    82             RootFile=regexprep(filename,[num3 delim2 num2 delim1 num1],'');
     105            NomType=[get_type(num3) delim2 get_type(num2) delim1 get_type(num1)];
     106            RootFile=regexprep(FileName,[num3 delim2 num2 delim1 num1],'');
    83107        else
    84108            switch delim1
    85109                case '_'
    86                     i1=str2num(num2);
    87                     j1=str2num(num1);
     110                    i1=str2double(num2);
     111                    j1=str2double(num1);
    88112                case '-'
    89                     i1=str2num(num2);
    90                     i2=str2num(num1);
    91             end
    92             nom_type=[get_type(num2) delim1 get_type(num1)];
    93             RootFile=regexprep(filename,[num2 delim1 num1],'');
     113                    i1=str2double(num2);
     114                    i2=str2double(num1);
     115            end
     116            NomType=[get_type(num2) delim1 get_type(num1)];
     117            RootFile=regexprep(FileName,[num2 delim1 num1],'');
    94118        end
    95119    else% only one number at the end
    96         i1=str2num(num1);
    97         nom_type=get_type(num1);
    98         RootFile=regexprep(filename,num1,'');
     120        i1=str2double(num1);
     121        NomType=get_type(num1);
     122        RootFile=regexprep(FileName,num1,'');
    99123    end
    100 else% filename ends with a letter
    101     r=regexp(filename,'.*[^a^b^A^B](?<end_string>ab|AB|[abAB])\>','names');
     124else% FileName ends with a letter
     125    r=regexp(FileName,'.*[^a^b^A^B](?<end_string>ab|AB|[abAB])\>','names');
    102126    if ~isempty(r)
    103127        end_string=r.end_string;
    104         r=regexp(filename,['.+(?<delim1>[_-])' end_string '\>'],'names');
     128        r=regexp(FileName,['.+(?<delim1>[_-])' end_string '\>'],'names');
    105129        if ~isempty(r)
    106130            delim1=r.delim1;
    107             r=regexp(filename,['.*\D(?<num1>\d+)' delim1 end_string '\>'],'names');
     131            r=regexp(FileName,['.*\D(?<num1>\d+)' delim1 end_string '\>'],'names');
    108132            if ~isempty(r)
    109133                num1=r.num1;
    110                 nom_type=[get_type(num1) delim1 get_type(end_string)];
    111                 i1=str2num(num1);
     134                NomType=[get_type(num1) delim1 get_type(end_string)];
     135                i1=str2double(num1);
    112136                [j1,j2]=get_value(end_string);
    113                 RootFile=regexprep(filename,[num1 delim1 end_string],'');
     137                RootFile=regexprep(FileName,[num1 delim1 end_string],'');
    114138               
    115139            else
    116                 nom_type=get_type(end_string);
     140                NomType=get_type(end_string);
    117141                [j1,j2]=get_value(end_string);
    118                 RootFile=regexprep(filename,[end_string],'');
     142                RootFile=regexprep(FileName,end_string,'');
    119143
    120144            end
    121145        else
    122             r=regexp(filename,['.*\D(?<num1>\d+)' end_string '\>'],'names');
     146            r=regexp(FileName,['.*\D(?<num1>\d+)' end_string '\>'],'names');
    123147            if ~isempty(r)
    124148                num1=r.num1;
    125                 %                 r=regexp(filename,['.+(?<delim1>[-_])' num1 end_string '\>'],'names');
     149                %                 r=regexp(FileName,['.+(?<delim1>[-_])' num1 end_string '\>'],'names');
    126150                %                 if ~isempty(r)
    127151                %                     delim1=r.delim1;
    128152                %                     i1=num1;
    129153                %                     str_a=end_string;
    130                 %                     nom_type=[delim1 get_type(num1) get_type(end_string)];
    131                 %                     RootFile=regexprep(filename,[delim1 num1 end_string],'');
     154                %                     NomType=[delim1 get_type(num1) get_type(end_string)];
     155                %                     RootFile=regexprep(FileName,[delim1 num1 end_string],'');
    132156                %                 else
    133                 i1=str2num(num1);
     157                i1=str2double(num1);
    134158                [j1,j2]=get_value(end_string);
    135                 nom_type=[get_type(num1) get_type(end_string)];
    136                 RootFile=regexprep(filename,[num1 end_string],'');
     159                NomType=[get_type(num1) get_type(end_string)];
     160                RootFile=regexprep(FileName,[num1 end_string],'');
    137161                %                 end
    138162            else
     
    167191    otherwise       
    168192        if ~isempty(regexp(s,'\<\d+\>','ONCE'))
    169             switch s(1)
    170                 case '0'
     193%             switch s(1)
     194%                 case '0'
    171195                    type=num2str(1,['%0' num2str(length(s)) 'd']);
    172                 otherwise
    173                     type='1';
    174             end
     196%                 otherwise
     197%                     type='1';
     198%             end
    175199        else
    176200            type='';
     
    199223
    200224
     225
     226function test(name)
     227fprintf([...
     228    '######################################################\n'...
     229    '               Test for fileparts_uvmat                  \n'...
     230    '######################################################\n'...
     231    ]);
     232
     233if exist('name','var')
     234    FileName_list={name};
     235else
     236    FileName_list={...
     237        'Image1a.png'...
     238        'toto'...
     239        'B011_1.png'...
     240        'B001.png'...
     241        'B55a.tif'...
     242        'B002B.tiff'...
     243        'B_001.png'...
     244        'B_3331AB.png'...
     245        'aa45_2.png'...
     246        'B005.png'...
     247        'Image_3.jpg'...
     248        'Image_3-4.jpg'...
     249        'Image_3-4_2.jpg'...
     250        'Image_5_3-4.jpg'...
     251        'Image_3_ab.jpg'...
     252        'Image005AB.jpg'...
     253        'Image_3_ab.jpg'...
     254        'Image_3a-b.jpg'...
     255        'Image3_a.jpg'...
     256        'movie57.avi'...
     257        };
     258end
     259
     260for FileName=FileName_list
     261%     [RootPath,RootFile,i1,i2,str_a,str_b,Ext,NomType,SubDir]=name2display(FileName{1});
     262    [~,RootFile_bis,i1_bis,i2_bis,j1_bis,j2_bis,~,NomType_bis,SubDir_bis]=...
     263        fileparts_uvmat(FileName{1});
     264    fprintf([...
     265        'File name  : ' FileName{1}  '\n'...
     266        '  NomType  : '    NomType_bis '\n'...
     267        '  RootFile : '    RootFile_bis '\n'...
     268        '  i1 / i2     : '     num2str(i1_bis) ' / ' num2str(i2_bis) '\n'...
     269        '  j1 / j2      : '    num2str(j1_bis) ' / ' num2str(j2_bis) '\n'...
     270        ]);
     271end
     272
     273
Note: See TracChangeset for help on using the changeset viewer.