source: trunk/src/name_generator.m @ 146

Last change on this file since 146 was 138, checked in by sommeria, 14 years ago

bug repair for name generation in case of constant name (no indexing)

File size: 13.7 KB
Line 
1%'name_generator': creates a file name from a root name and indices.
2%------------------------------------------------------------------------
3% [filename,num_i1_out,num_j1_out,num_i2_out,num_j2_out,subdir_out]=...
4%        name_generator(filebase,num_i1,num_j1,ext,nom_type,comp_input,num_i2,num_j2,subdir);
5%------------------------------------------------------------------------           
6% This function detects the existence the constructed file name and it can
7% find indices according to file existence if they are not specified
8% rmq: this function is related to the reverse functions display2name and name2diplay
9%------------------------------------------------------------------------
10% OUTPUT:
11% filename: string representing the file name (including path)
12% num_i1_out,num_j1_out,num_i2_out,num_j2_out,subdir_out: index numbers and subdirectory detected
13%            for free input (= to the corresponding input indices when comp_input=1)
14%------------------------------------------------------------------------
15% INPUT:
16% 'filebase': the root name,
17% 'num_i1: first labelling index i
18% 'num_j1', first labelling index j
19% 'ext': file name extension (e.g. '.png' or '.nc')
20%nom_type: char chain characterizing the file nomenclature: with values
21%   nom_type='': constant name [filebase ext] (default output if 'nom_type' is undefined)
22%   nom_type='*':constant name for a file representing a series (e.g. avi movie)
23%   nom_type='1','01',or '001'...': series of files with a single index i without separator(e.g. 'aa045.png').
24%   nom_type='_1','_01','_001'...':  series of files with a single index i with separator '_'(e.g. 'aa_045.png').
25%   nom_type='1a','1A','01a','01A',... with a numerical index and an index letter(e.g.'aa45b.png') (lower or upper case)
26%   nom_type='_1a','_1A','_01a','_01A',...: idem, with a separator '_' before the index
27%   nom_type='_1_1','_01_1',...: matrix of files with two indices i and j separated by '_'(e.g. 'aa_45_2.png')
28%   nom_type='_i1-i2': from pairs from a single index (e.g. 'aa_45-47.nc')
29%   nom_type='_i_j1-j2': pairs of j indices (e.g. 'aa_45_2-3.nc')
30%   nom_type='_i1-i2_j': pairs of i indices (e.g. 'aa_45-46_2.nc')
31%   nom_type='_1_ab','1_ab','01_ab'..., from pairs of '#' images (e.g.'aa045bc.nc'), ext='.nc'
32%'comp_input' (for nom_type involving index pairs (e.g. netc))
33%       comp_input=1: the index pair is imposed,
34%       comp_input=0: the index pair is automatically searched, choosing the most recent  file in case of multiple choice
35% 'num_i2': second index i (for nom_type involving index pairs (e.g. netc))
36% 'num_j2': second index j (for nom_type involving index pairs (e.g. netc))
37% 'subdir': (used for nom_type=netc...) string representing the name of the subdirectory 'subdir' containing file.
38%       subdir='': no subdirectory,
39%       subdir='?', the file is first searched with no subdirectory, then in the most recently modified subdirectory if not detected.
40
41% A FAIRE: si comp_inpu=0, si _i_j n'existe pas, chercher _i,
42function [filename,num_i1_out,num_j1_out,num_i2_out,num_j2_out,subdir_out]=...
43           name_generator(filebase,num_i1,num_j1,ext,nom_type,comp_input,num_i2,num_j2,subdir)
44sizf=size(filebase);
45if (~ischar(filebase)||~isequal(sizf(1),1)),filebase='';end
46if ~exist('ext','var')
47    ext='';
48end
49if ~exist('nom_type','var')
50    nom_type='';
51end
52if ~ischar(ext),ext='';end
53% idetect=0;
54if ~exist('num_i1','var') || isempty(num_i1) || isnan(num_i1)
55    num_i1=1; %default
56end
57if ~exist('num_j1','var') ||  isempty(num_j1) || isnan(num_j1)
58    num_j1=1; %default
59end
60if ~exist('num_i2','var') ||  isempty(num_i2) || isnan(num_i2)
61    num_i2=num_i1; %default
62end
63if ~exist('num_j2','var') || isempty(num_j2) || isnan(num_j2)
64    num_j2=num_j1; %default
65end
66if ~exist('subdir','var')|| isempty(subdir)
67    subdir='' ; %default
68end
69num_i1_out=num_i1;%default output
70num_j1_out=num_j1;%default output
71num_i2_out=num_i2;%default output
72num_j2_out=num_j2;%default output
73test_pairs=numel(nom_type)>=2 &&(strcmp(nom_type,'_i1-i2_j1-j2')|| strcmp(nom_type(end-1:end),'ab')|| strcmp(nom_type(end-1:end),'AB')||...
74                strcmp(nom_type,'_i_j1-j2')|| strcmp(nom_type,'_i1-i2_j')||strcmp(nom_type,'_i1-i2'));
75%test_2D= strcmp(nom_type(end-1:end),'ab')|| strcmp(nom_type(end-1:end),'AB') ||strcmp(nom_type,'_i_j1-j2');
76%test_3D=strcmp(nom_type,'_i1-i2_j')|| strcmp(nom_type,'_i1-i2');
77if ~isequal(subdir,'') && ~isequal(subdir,'?')
78      [Path,Name]=fileparts(filebase);
79      filename=fullfile(Path,subdir,Name);
80else
81    filename=filebase;%default
82end
83if ~test_pairs%case of a single index i, and possibly j
84%     numlength=numel(nom_type);
85    nom_type_mod=nom_type;
86    num_j_str='';
87    if strcmp(nom_type,'1')
88        filename=[filename num2str(num_i1) ext];
89    elseif length(nom_type)<=1%fixed name , no indexing, for instance '*'
90        filename=[filename ext];
91    else
92        nom_type_mod=nom_type;
93        if strcmp(nom_type(1),'_')
94            filename=[filename '_'];
95            nom_type_mod(1)=[];
96        end
97        if strcmp(nom_type_mod(end),'a')
98            nom_type_mod(end)=[];
99            num_j_str=char(num_j1+96);% lower letter corresponding to the index
100        elseif strcmp(nom_type_mod(end),'A')
101            nom_type(end)=[];
102            num_j_str=char(num_j1+64);% lower letter corresponding to the index
103        elseif isequal(numel(regexp(nom_type_mod,'_')),1)%if a second separator '_' exists in nom_type
104            num_j_str=['_' num2str(num_j1)];
105            nom_type_mod(regexp(nom_type_mod,'_'):end)=[];
106        else
107            num_j1_out=[];%no index j
108        end
109        if ~isnan(str2double(nom_type_mod))   
110            numtype=['%0' num2str(length(nom_type_mod)) 'd'];%indicate the number of digits (0 before the number)
111            filename=[filename num2str(num_i1,numtype) num_j_str ext];
112            num_i2_out=num_i1_out;
113            num_j2_out=num_j1_out;
114        else %fixed name
115            filename=[filename ext];
116        end
117    end
118
119%case of derived file indexing (e.g. netcdf files)
120else
121    % case of an imposed image pair (comp_input=1)
122    if  (exist('comp_input','var') && isequal(comp_input,1))
123            if strcmp(nom_type(1),'_')
124                filename=[filename '_'];
125                nom_type(1)=[];
126            end
127            if strcmp(nom_type(end-1:end),'AB')||strcmp(nom_type(end-1:end),'ab')
128                if strcmp(nom_type(end-1:end),'AB')
129                    nchar=64;
130                else
131                    nchar=96;
132                end
133                if isequal(num_j1,num_j2)% case of displacements at the same time
134                    num_j_str=char(num_j1+nchar);
135                else
136                    num_j_str=[char(num_j1+nchar) char(num_j2+nchar)];
137                end
138                if strcmp(nom_type(end-2),'_')
139                    numstr=['%0' num2str(numel(nom_type)-3) 'd'];
140                    num_j_str=['_' num_j_str];
141                else
142                    numstr=['%0' num2str(numel(nom_type)-2) 'd'];
143                end
144                filename=[filename num2str(num_i1,numstr) num_j_str ext];
145                num_i2_out=num_i1;
146            elseif isequal(nom_type,'i_j1-j2')
147                if isequal(num2str(num_j1),num2str(num_j2))% case of displacements at the same time
148                    filename=[filename num2str(num_i1) '_' num2str(num_j1) ext];
149                else
150                    filename=[filename num2str(num_i1) '_' num2str(num_j1) '-' num2str(num_j2) ext];
151                end
152                num_i2_out=num_i1;
153            elseif  isequal(nom_type,'i1-i2_j')
154                if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
155                      filename=[filename num2str(num_i1) '_' num2str(num_j1) ext];
156                else
157                    filename=[filename num2str(num_i1) '-' num2str(num_i2) '_' num2str(num_j1) ext];
158                end
159                num_j2_out=num_j1;
160            elseif  isequal(nom_type,'i1-i2')
161                if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
162                     filename=[filename num2str(num_i1) ext];
163                else
164                    filename=[filename num2str(num_i1) '-' num2str(num_i2) ext];
165                end
166                num_j2_out=num_j1;
167            elseif isequal(nom_type,'i1-i2_j1-j2')
168                if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
169                    app1= num2str(num_i1);
170                else
171                    app1= [num2str(num_i1) '-' num2str(num_i2)];
172                end
173                if isequal(num2str(num_j1),num2str(num_j2))% case of displacements at the same time
174                    app2= num2str(num_j1);
175                else
176                    app2= [num2str(num_j1) '-' num2str(num_j2)];
177                end     
178                filename=[filename app1 '_' app2 ext];
179            end
180            idetect=1;
181           % idetect=(exist(filename,'file')==2);
182     % case of an image pair to determine (comp_input=0)
183    else
184          [filename,num_i1_out,num_j1_out,num_i2_out,num_j2_out,idetect]=search_pair(filename,num_i1,num_j1,num_i2,nom_type);
185    end
186   
187     %look for sub-directories containing netcdf files
188    if idetect==0 && isequal(subdir,'?')
189        [pathfile,name]=fileparts(filebase);
190        direct=dir(pathfile);%directory containing filebase
191        datedir=[];%default
192%         idir=0;
193        indir=find(cell2mat({direct.isdir}));% find indices of subdirectories
194        direct=direct(indir(3:end));% keep only the subdirectories,eliminating the two first terms '.' and '..'
195        lengthdir=length(direct);
196        if lengthdir==0
197            subdir='';% no subdirectory found
198        else
199            for idir=1:lengthdir
200                date_str=direct(idir).date;%string of the date of last modification
201                datedir(idir)=0;%default
202                char_code=double(date_str);% code of the date characters
203                special_char=(char_code>127); %non standard Ascii character (e.g. date in french)
204                if isempty(find(special_char,1))% standard Ascii character
205                    datedir(idir)=datenum(date_str);
206                end                           
207%                 datedir(idir)=datenum(direct(idir).date); %absolute date of last directory modification
208            end
209            [mostrec,indrec]=max(datedir);% most recently modified subdir chosen by default
210            subdir=direct(indrec).name; %chosen directory
211        end
212        filebasesub=fullfile(pathfile,subdir,name);
213        %if the image pair is imposed
214        if (exist('comp_input','var') && isequal(comp_input,1))
215            if isequal(nom_type,'#_ab')
216                filename=[filebasesub num2str(num_i1,'%03d') '_' num2stra(num_j1,nom_type) num2stra(num_j2,nom_type) ext];
217            elseif isequal(nom_type,'_i1_j1-j2')
218                filename=[filebasesub '_' num2str(num_i1) '_' num2str(num_j1) '-' num2str(num_i2) ext];
219            elseif isequal(nom_type,'_i1-i2_j')
220                filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) '_' num2str(num_j1) ext];
221            elseif isequal(nom_type,'_i1-i2')
222                filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) ext];
223            end
224%             idetect=(exist(filename,'file')==2);
225        else
226            [filename,num_i1_out,num_j1_out,num_i2_out,num_j2_out]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type);             
227        end
228    end
229end
230if ~strcmp(subdir,'?')
231    subdir_out=subdir;
232else
233    subdir_out='';
234end
235
236%------------------------------------------------------------------------
237% --- search the appropriate image pair (netcdf file) corresponding to a given image number
238function [filename,num_i1,num_j1,num_i2,num_j2,idetect]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type)
239%------------------------------------------------------------------------
240% for nom_type=netc_2D or netc_old, it searches all the pairs corresponding
241% to num_i1, and chooses the most recent file.
242%for nom_type=netc_3D or netc_series, it searches all the pairs (num_i1
243%num_i2), with num_i1 as the first  index, and chooses the most recent file.
244
245filename=[];num_j2=[];idetect=0;%default values
246if isequal(nom_type,'#_ab')
247    dirpair=dir([filebasesub num2str(num_i1,'%03d') '_*.nc']);
248elseif isequal(nom_type,'_i_j1-j2')
249    dirpair=dir([filebasesub '_' num2str(num_i1) '_*-*.nc']);
250elseif isequal(nom_type,'_i1-i2_j')
251    dirpair=dir([filebasesub '_' num2str(num_i1) '-*_' num2str(num_j1) '.nc']);
252elseif isequal(nom_type,'_i1-i2')
253    dirpair=dir([filebasesub '_' num2str(num_i1) '-*.nc']);
254    if isempty(dirpair)
255        dirpair=dir([filebasesub '_*-' num2str(num_i2) '.nc']);
256    end
257end
258nbpair=length(dirpair);
259if nbpair >= 1 %choose the most recent file if several are found
260    idetect=1; %detected pair
261    datepair=zeros(1,nbpair);%default
262    for ipair=1:nbpair
263         date_str=dirpair(ipair).date;%string of the date of last modification
264         char_code=double(date_str);% code of the date characters
265         special_char=(char_code>127); %non standard Ascii character (e.g. date in french)
266         if isempty(find(special_char))% standard Ascii character
267             datepair(ipair)=datenum(date_str);
268         end   
269    end
270    [choice,indpair]=max(datepair);
271    [pathname,file,field_count,str2,str_a,str_b]=name2display(dirpair(indpair).name);
272    num_i1=str2double(field_count);
273    num_i2=str2double(str2);
274    num_j1=stra2double(str_a);
275    num_j2=stra2double(str_b);
276     pathname=fileparts(filebasesub);% CORRIGE LE 6 JUIN (ETAIT DESACTIVE)
277    filename=fullfile(pathname,dirpair(indpair).name);
278end
279
280
Note: See TracBrowser for help on using the repository browser.