Home > . > name_generator.m

# name_generator

## PURPOSE

'name_generator': creates a file name from a root name and indices.

## SYNOPSIS

function [filename,idetect,num_i1_out,num_j1_out,num_i2_out,num_j2_out,subdir_out]=name_generator(filebase,num_i1,num_j1,ext,nom_type,comp_input,num_i2,num_j2,subdir);

## DESCRIPTION

    'name_generator': creates a file name from a root name and indices.
    ---------------------------------------------------------------------
     [filename,idetect,num_i1_out,num_j1_out,num_i2_out,num_j2_out,subdir_out]=...
     name_generator(filebase,num_i1,num_j1,ext,nom_type,comp_input,num_i2,num_j2,subdir);
    ---------------------------------------------------------------------
     This function detects the existence the constructed file name and it can
     find indices according to file existence if they are not specified
    %rmq: this function is related to the reverse functions display2name and name2diplay
    ---------------------------------------------------------------------
     OUTPUT:
    filename: string representing the file name (including path)
    idetect: =1 if the file is detected, 0 otherwise
    num_i1_out,num_j1_out,num_i2_out,num_j2_out,subdir_out: index numbers and subdirectory detected
    for free input (= to the corresponding input indices when comp_input=1)
    ---------------------------------------------------------------------
     INPUT:
     'filebase': the root name,
     'num_i1: first labelling index i
     'num_j1', first labelling index j
     'ext': file name extension (e.g. '.png' or '.nc')
     'nom_type': string defining the kind of nomenclature used:
    nom_type='': constant name [filebase ext] (default output if 'nom_type' is undefined)
    nom_type='*': the same  file [filebase ext] contains successive fields (ex avi movies)
    nom_type='_i': series of files with a single index i preceded by '_'(e.g. 'aa_45.png').
    nom_type='#' series of indexed images wich is not series_i [filebase index ext], e.g. 'aa045.jpg' or 'aa45.tif'
    nom_type='_i_j' matrix of files with two indices i and j separated by '_'(e.g. 'aa_45_2.png')
    nom_type='_i1-i2' from pairs from a single index (e.g. 'aa_45-47.nc')
    nom_type='_i_j1-j2'pairs of j indices (e.g. 'aa_45_2-3.nc')
    nom_type='_i1-i2_j' pairs of i indices (e.g. 'aa_45-46_2.nc')
    nom_type='#a','#A' with a numerical index and an index letter(e.g.'aa045b.png'), OBSOLETE (replaced by 'series_i_j')
    nom_type='%03d' or '%04d', series of indexed images with numbers completed with zeros to 3 or 4 digits, e.g.'aa045.tif'
    nom_type='_%03d', '_%04d', or '_%05d', series of indexed images with _ and numbers completed with zeros to 3, 4 or 5 digits, e.g.'aa_045.tif'
    nom_type='raw_SMD', same as '#a' but with no extension ext='', OBSOLETE
    nom_type='#_ab' from pairs of '#a' images (e.g. 'aa045bc.nc'), ext='.nc', OBSOLETE (replaced by 'netc_2D')
    nom_type='%3dab' from pairs of '%3da' images (e.g. 'aa045bc.nc'), ext='.nc', OBSOLETE (replaced by 'netc_2D')
     A REVOIR: 'comp_input'=1 for writting =0 for reading ,  'comp_input'(for nom_type involving index pairs (e.g. netc))
    comp_input=1: the index pair is imposed,
    comp_input=0: the index pair is automatically searched, choosing the most recent  file in case of multiple choice
     'num_i2': second index i (for nom_type involving index pairs (e.g. netc))
     'num_j2': second index j (for nom_type involving index pairs (e.g. netc))
     'subdir': (used for nom_type=netc...) string representing the name of the subdirectory 'subdir' containing file.
    subdir='': no subdirectory,
    subdir='?', the file is first searched with no subdirectory, then in the most recently modified subdirectory if not detected.

## CROSS-REFERENCE INFORMATION

This function calls:

- name2display 'name2display': extracts the root name and field numbers from an input filename
- num2stra 'num2stra': transform number to the corresponding character string depending on the nomenclature
- stra2num 'stra2num': transform letters (a, b, c) or numerical strings ('1','2'..) to the corresponding numbers

This function is called by:

- civ 'civ': function associated with the interface 'civ.fig' for PIV, spline interpolation and stereo PIV (patch)
- civ_3D 'civ_3D': function associated with the interface 'civ_3D.fig' for PIV in volume
- series 'series': master function associated to the GUI series.m for analysis field series
- uvmat 'uvmat': function associated with the GUI 'uvmat.fig' for images and data field visualization

## SUBFUNCTIONS

- function [filename,num_i1,num_j1,num_i2,num_j2,idetect]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type)

## SOURCE CODE

    0001 %'name_generator': creates a file name from a root name and indices.
    0002 %---------------------------------------------------------------------
    0003 % [filename,idetect,num_i1_out,num_j1_out,num_i2_out,num_j2_out,subdir_out]=...
    0004             % name_generator(filebase,num_i1,num_j1,ext,nom_type,comp_input,num_i2,num_j2,subdir);
    0005 %---------------------------------------------------------------------
    0006 % This function detects the existence the constructed file name and it can
    0007 % find indices according to file existence if they are not specified
    0008 %%rmq: this function is related to the reverse functions display2name and name2diplay
    0009 %---------------------------------------------------------------------
    0010 % OUTPUT:
    0011 %filename: string representing the file name (including path)
    0012 %idetect: =1 if the file is detected, 0 otherwise
    0013 %num_i1_out,num_j1_out,num_i2_out,num_j2_out,subdir_out: index numbers and subdirectory detected
    0014              %for free input (= to the corresponding input indices when comp_input=1)
    0015 %---------------------------------------------------------------------
    0016 % INPUT:
    0017 % 'filebase': the root name,
    0018 % 'num_i1: first labelling index i
    0019 % 'num_j1', first labelling index j
    0020 % 'ext': file name extension (e.g. '.png' or '.nc')
    0021 % 'nom_type': string defining the kind of nomenclature used:
    0022      %nom_type='': constant name [filebase ext] (default output if 'nom_type' is undefined)
    0023      %nom_type='*': the same  file [filebase ext] contains successive fields (ex avi movies)
    0024      %nom_type='_i': series of files with a single index i preceded by '_'(e.g. 'aa_45.png').
    0025      %nom_type='#' series of indexed images wich is not series_i [filebase index ext], e.g. 'aa045.jpg' or 'aa45.tif'
    0026      %nom_type='_i_j' matrix of files with two indices i and j separated by '_'(e.g. 'aa_45_2.png')
    0027      %nom_type='_i1-i2' from pairs from a single index (e.g. 'aa_45-47.nc')
    0028      %nom_type='_i_j1-j2'pairs of j indices (e.g. 'aa_45_2-3.nc')
    0029      %nom_type='_i1-i2_j' pairs of i indices (e.g. 'aa_45-46_2.nc')
    0030      %nom_type='#a','#A' with a numerical index and an index letter(e.g.'aa045b.png'), OBSOLETE (replaced by 'series_i_j')
    0031      %nom_type='%03d' or '%04d', series of indexed images with numbers completed with zeros to 3 or 4 digits, e.g.'aa045.tif'
    0032      %nom_type='_%03d', '_%04d', or '_%05d', series of indexed images with _ and numbers completed with zeros to 3, 4 or 5 digits, e.g.'aa_045.tif'
    0033      %nom_type='raw_SMD', same as '#a' but with no extension ext='', OBSOLETE
    0034      %nom_type='#_ab' from pairs of '#a' images (e.g. 'aa045bc.nc'), ext='.nc', OBSOLETE (replaced by 'netc_2D')
    0035      %nom_type='%3dab' from pairs of '%3da' images (e.g. 'aa045bc.nc'), ext='.nc', OBSOLETE (replaced by 'netc_2D')
    0036 % A REVOIR: 'comp_input'=1 for writting =0 for reading ,  'comp_input'(for nom_type involving index pairs (e.g. netc))
    0037      %comp_input=1: the index pair is imposed,
    0038      %comp_input=0: the index pair is automatically searched, choosing the most recent  file in case of multiple choice
    0039 % 'num_i2': second index i (for nom_type involving index pairs (e.g. netc))
    0040 % 'num_j2': second index j (for nom_type involving index pairs (e.g. netc))
    0041 % 'subdir': (used for nom_type=netc...) string representing the name of the subdirectory 'subdir' containing file.
    0042      %subdir='': no subdirectory,
    0043      %subdir='?', the file is first searched with no subdirectory, then in the most recently modified subdirectory if not detected.
    0044
    0045 % A FAIRE: si comp_inpu=0, si _i_j n'existe pas, chercher _i,
    0046 function [filename,idetect,num_i1_out,num_j1_out,num_i2_out,num_j2_out,subdir_out]=...
    0047            name_generator(filebase,num_i1,num_j1,ext,nom_type,comp_input,num_i2,num_j2,subdir);
    0048 sizf=size(filebase);
    0049 if (~ischar(filebase)||~isequal(sizf(1),1)),filebase='';end
    0050 if ~ischar(ext),ext='';end
    0051 % filename=[filebase ext];%default
    0052 idetect=0;
    0053 if ~exist('num_i1','var') || isequal(num_i1,[])
    0054     num_i1=1; %default
    0055 end
    0056 if ~exist('num_j1','var') || isequal(num_j1,[])
    0057     num_j1=1; %default
    0058 end
    0059 if ~exist('num_i2','var') || isequal(num_i2,[])
    0060     num_i2=num_i1; %default
    0061 end
    0062 if ~exist('subdir','var')|| isempty(subdir)
    0063     subdir='' ; %default
    0064 end
    0065 %detection of mask or mean: filebase of the form [root '_' xxx 'm$$$'] with xxx a number and $$$ three characters
    0066 if length(filebase)>4 & filebase(end-3)=='m';
    0067     basedouble=double(filebase);
    0068     val=(48>basedouble)|(basedouble>57);% select the non-numerical characters
    0069     i=length(filebase)-4;
    0070     while val(i)==0 & i>0
    0071         i=i-1;
    0072     end
    0073     nbslice=str2num(filebase(i+1:end-4));
    0074     if ~isequal(nbslice,[])
    0075         num_i1=mod(num_i1-1,nbslice)+1; %take the rest in the division
    0076         if isequal(nom_type,'png_old')|isequal(nom_type,'#a')
    0077             num_j1=1;% mask name must end by 'a'
    0078         end
    0079     end
    0080 end
    0081 test_pairs=isequal(nom_type,'netc_old')| isequal(nom_type,'netc_2D') | isequal(nom_type,'netc_3D')| isequal(nom_type,'_i1-i2_j1-j2')| ...
    0082   isequal(nom_type,'netc_series')| isequal(nom_type,'#_ab')| isequal(nom_type,'_i_j1-j2')| isequal(nom_type,'_i1-i2_j')| isequal(nom_type,'_i1-i2');
    0083 test_2D= isequal(nom_type,'netc_old') |isequal(nom_type,'netc_2D')|isequal(nom_type,'#_ab') |isequal(nom_type,'_i_j1-j2');
    0084 test_3D=isequal(nom_type,'netc_3D') |isequal(nom_type,'netc_series')| isequal(nom_type,'_i1-i2_j')| isequal(nom_type,'_i1-i2');
    0085 if isequal(nom_type,'series_i')| isequal(nom_type,'_i');
    0086         filename=[filebase '_' num2str(num_i1) ext];
    0087         idetect=(exist(filename,'file')==2);
    0088 elseif length(nom_type)==5 && isequal(nom_type(1:3),'_%0')&& isequal(nom_type(5),'d');
    0089         filename=[filebase '_' num2str(num_i1,nom_type(2:5)) ext];
    0090         idetect=(exist(filename,'file')==2);
    0091 elseif isequal(nom_type,'series_i_j')| isequal(nom_type,'_i_j')
    0092         filename=[filebase '_' num2str(num_i1) '_' num2str(num_j1) ext];
    0093         idetect=(exist(filename,'file')==2);
    0094 elseif isequal(nom_type,'png_old')| isequal(nom_type,'#a')| isequal(nom_type,'#A')
    0095         filename=[filebase num2str(num_i1,'%03d') num2stra(num_j1,nom_type) ext];
    0096         idetect=(exist(filename,'file')==2);
    0097 elseif  length(nom_type)>=5 & isequal(nom_type(2:3),'%0') & isequal(nom_type(5),'d')  %isequal(nom_type,'_%04dA') %camera PCO Toulouse
    0098         filename=[filebase nom_type(1) num2str(num_i1,nom_type(2:4)) num2stra(num_j1,nom_type) ext];
    0099         idetect=(exist(filename,'file')==2);
    0100 elseif isequal(nom_type,'raw_SMD') %suffix a, b, c without extension
    0101         filename=[filebase num2str(num_i1,'%03d') num2stra(num_j1,nom_type)];
    0102         idetect=(exist(filename,'file')==2);
    0103 elseif isequal(nom_type,'ima_num')| isequal(nom_type,'#')
    0104         filename=[filebase num2str(num_i1) ext];
    0105         idetect=(exist(filename,'file')==2);
    0106 elseif length(nom_type)>=4 & isequal(nom_type(1:2),'%0') & isequal(nom_type(end),'d')
    0107             filename=[filebase num2str(num_i1,nom_type) ext]; %test number with a 0 before
    0108             idetect=(exist(filename,'file')==2);
    0109
    0110 %case of derived file indexing (e.g. netcdf files)
    0111 elseif test_pairs
    0112     if (~exist('num_j2','var')| isequal(num_j2,[])) & test_2D
    0113           num_j2=num_j1+1; %default if num_j2 undefined
    0114     end
    0115     if (~exist('num_i2','var')| isequal(num_i2,[])) & test_3D
    0116           num_i2=num_i1+1; %default if num_i2 undefined
    0117     end
    0118     filebasesub=filebase;
    0119     % get the root name filebasesub for the netcdf files
    0120     if  ~isequal(subdir,'') && ~isequal(subdir,'?')
    0121             [Path,Name]=fileparts(filebase);
    0122             filebasesub=fullfile(Path,subdir,Name);
    0123     end
    0124      %inexistant pair if num_i2=0 or num_j2=0
    0125     if isequal(num_i2,0)
    0126         filename=[filebasesub '*-*_' num2str(num_i1) ext];
    0127         return
    0128     end
    0129     if isequal(num_j2,0)
    0130         filename=[filebasesub '_' num2str(num_i1) '_*-*' ext];
    0131         return
    0132     end
    0133     % case of an imposed image pair (comp_input=1)
    0134     if  (exist('comp_input','var') & isequal(comp_input,1))
    0135             if isequal(nom_type,'netc_old')|isequal(nom_type,'#_ab')
    0136                 if isequal(num2str(num_j1),num2str(num_j2))% case of displacements at the same time
    0137                     filename=[filebasesub num2str(num_i1,'%03d') '_' num2stra(num_j1,nom_type) ext];
    0138                 else
    0139                     filename=[filebasesub num2str(num_i1,'%03d') '_' num2stra(num_j1,nom_type) num2stra(num_j2,nom_type) ext];
    0140                 end
    0141             elseif isequal(nom_type,'netc_2D')|isequal(nom_type,'_i_j1-j2')
    0142                 if isequal(num2str(num_j1),num2str(num_j2))% case of displacements at the same time
    0143                     filename=[filebasesub '_' num2str(num_i1) '_' num2str(num_j1) ext];
    0144                 else
    0145                     filename=[filebasesub '_' num2str(num_i1) '_' num2str(num_j1) '-' num2str(num_j2) ext];
    0146                 end
    0147             elseif isequal(nom_type,'netc_3D') || isequal(nom_type,'_i1-i2_j')
    0148                 if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
    0149                       filename=[filebasesub '_' num2str(num_i1) '_' num2str(num_j1) ext];
    0150                 else
    0151                     filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) '_' num2str(num_j1) ext];
    0152                 end
    0153             elseif isequal(nom_type,'netc_series') || isequal(nom_type,'_i1-i2')
    0154                 if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
    0155                      filename=[filebasesub '_' num2str(num_i1) ext];
    0156                 else
    0157                     filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) ext];
    0158                 end
    0159             elseif isequal(nom_type,'_i1-i2_j1-j2')
    0160                 if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
    0161                     app1= [num2str(num_i1)];
    0162                 else
    0163                     app1= [num2str(num_i1) '-' num2str(num_i2)];
    0164                 end
    0165                 if isequal(num2str(num_j1),num2str(num_j2))% case of displacements at the same time
    0166                     app2= [num2str(num_j1)];
    0167                 else
    0168                     app2= [num2str(num_j1) '-' num2str(num_j2)];
    0169                 end
    0170                 filename=[filebasesub '_' app1 '_' app2 ext];
    0171             end
    0172             idetect=(exist(filename,'file')==2);
    0173      % case of an image pair to determine (comp_input=0)
    0174     else
    0175             [filename,num_i1,num_j1,num_i2,num_j2,idetect]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type);
    0176     end
    0177
    0178      %look for sub-directories containing netcdf files
    0179     if idetect==0 && isequal(subdir,'?')
    0180         [pathfile,name]=fileparts(filebase);
    0181         direct=dir(pathfile);%directory containing filebase
    0182         datedir=[];%default
    0183         idir=0;
    0184         indir=find(cell2mat({direct.isdir}));% find indices of subdirectories
    0185         direct=direct(indir([3:end]));% keep only the subdirectories,eliminating the two first terms '.' and '..'
    0186         lengthdir=length(direct);
    0187         if lengthdir==0
    0188             subdir='';% no subdirectory found
    0189         else
    0190             for idir=1:lengthdir
    0191                 date_str=direct(idir).date;%string of the date of last modification
    0192                 datedir(idir)=0;%default
    0193                 char_code=double(date_str);% code of the date characters
    0194                 special_char=(char_code>127); %non standard Ascii character (e.g. date in french)
    0195                 if isempty(find(special_char))% standard Ascii character
    0196                     datedir(idir)=datenum(date_str);
    0197                 end
    0198 %                 datedir(idir)=datenum(direct(idir).date); %absolute date of last directory modification
    0199             end
    0200             [mostrec,indrec]=max(datedir);% most recently modified subdir chosen by default
    0201             subdir=direct(indrec).name; %chosen directory
    0202         end
    0203         filebasesub=fullfile(pathfile,subdir,name);
    0204         %if the image pair is imposed
    0205         if (exist('comp_input','var') & isequal(comp_input,1))
    0206             if isequal(nom_type,'netc_old')|isequal(nom_type,'#_ab')
    0207                 filename=[filebasesub num2str(num_i1,'%03d') '_' num2stra(num_j1,nom_type) num2stra(num_j2,nom_type) ext];
    0208             elseif isequal(nom_type,'netc_2D')|isequal(nom_type,'_i1_j1-j2')
    0209                 filename=[filebasesub '_' num2str(num_i1) '_' num2str(num_j1) '-' num2str(num_i2) ext];
    0210             elseif isequal(nom_type,'netc_3D')|isequal(nom_type,'_i1-i2_j')
    0211                 filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) '_' num2str(num_j1) ext];
    0212             elseif isequal(nom_type,'netc_series')|isequal(nom_type,'_i1-i2')
    0213                 filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) ext];
    0214             end
    0215             idetect=(exist(filename,'file')==2);
    0216         else
    0217             [filename,num_i1,num_j1,num_i2,num_j2,idetect]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type);
    0218         end
    0219     end
    0220 % elseif isequal(nom_type,'none')|isequal(nom_type,'')|isequal(nom_type,'*')
    0221 else
    0222     filebasesub=filebase;
    0223     if ~isequal(subdir,'') && ~isequal(subdir,'?')
    0224             [Path,Name]=fileparts(filebase);
    0225             filebasesub=fullfile(Path,subdir,Name);
    0226     end
    0227     filename=[filebasesub ext];
    0228     idetect=(exist(filename,'file')==2);
    0229 end
    0230 num_i1_out=num_i1;
    0231 num_j1_out=num_j1;
    0232 if exist('num_i2','var'), num_i2_out=num_i2; else, num_i2_out=[]; end;
    0233 if exist('num_j2','var'), num_j2_out=num_j2; else, num_j2_out=[]; end;
    0234 if ~isequal(subdir,'?'), subdir_out=subdir; else, subdir_out='';end;
    0235
    0236 %---------------------------------------------------------------
    0237 % search the appropriate image pair (netcdf file) corresponding to a given
    0238 % image number
    0239 %-------------------------------------------------------------------
    0240 function [filename,num_i1,num_j1,num_i2,num_j2,idetect]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type)
    0241 % for nom_type=netc_2D or netc_old, it searches all the pairs corresponding
    0242 % to num_i1, and chooses the most recent file.
    0243 %for nom_type=netc_3D or netc_series, it searches all the pairs (num_i1
    0244 %num_i2), with num_i1 as the first  index, and chooses the most recent file.
    0245
    0246 filename=[];num_j2=[];idetect=0;%default values
    0247 if isequal(nom_type,'netc_old')|isequal(nom_type,'#_ab')
    0248     dirpair=dir([filebasesub num2str(num_i1,'%03d') '_*.nc']);
    0249 elseif isequal(nom_type,'netc_2D')|isequal(nom_type,'_i_j1-j2')
    0250     dirpair=dir([filebasesub '_' num2str(num_i1) '_*-*.nc']);
    0251 elseif isequal(nom_type,'netc_3D')|isequal(nom_type,'_i1-i2_j')
    0252     dirpair=dir([filebasesub '_' num2str(num_i1) '-*_' num2str(num_j1) '.nc']);
    0253 elseif isequal(nom_type,'netc_series')|isequal(nom_type,'_i1-i2')
    0254     dirpair=dir([filebasesub '_' num2str(num_i1) '-*.nc']);
    0255     if isempty(dirpair)
    0256         dirpair=dir([filebasesub '_*-' num2str(num_i2) '.nc']);
    0257     end
    0258 end
    0259 nbpair=length(dirpair);
    0260 if nbpair >= 1 %choose the most recent file if several are found
    0261     idetect=1; %detected pair
    0262     for ipair=1:nbpair
    0263          date_str=dirpair(ipair).date;%string of the date of last modification
    0264          datepair(ipair)=0;%default
    0265          char_code=double(date_str);% code of the date characters
    0266          special_char=(char_code>127); %non standard Ascii character (e.g. date in french)
    0267          if isempty(find(special_char))% standard Ascii character
    0268              datepair(ipair)=datenum(date_str);
    0269          end
    0270       %  datepair(ipair)=datenum(dirpair(ipair).date);
    0271     end
    0272     [choice,indpair]=max(datepair);
    0273 %     [filebase,field_count,str2,str_a,str_b,ext,nom_type]=name2display(dirpair(indpair).name);
    0274     [pathname,file,field_count,str2,str_a,str_b,ext,nom_type]=name2display(dirpair(indpair).name);
    0275     num_i1=str2num(field_count);
    0276     num_i2=str2num(str2);
    0277     num_j1=stra2num(str_a);
    0278     num_j2=stra2num(str_b);
    0279      pathname=fileparts(filebasesub);% CORRIGE LE 6 JUIN (ETAIT DESACTIVE)
    0280     filename=fullfile(pathname,dirpair(indpair).name);
    0281 end
    0282
    0283