source: trunk/src/name_generator.m @ 128

Last change on this file since 128 was 127, checked in by sommeria, 13 years ago

merge_proj.m: cleaning
sub_field.m: ?
dataview.m: modifs not finished
name-generator: bug repair for new nom_type
nc2struct: cleaning
uvmat: cleaning
read_civxdata: transform all variables to double
civ: bug repair for new nom_type, not finished

File size: 13.5 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 '_'];
89        nom_type_mod(1)=[];
90    end
91    if strcmp(nom_type_mod(end),'a')
92        nom_type_mod(end)=[];
93        num_j_str=char(num_j1+96);% lower letter corresponding to the index
94    elseif strcmp(nom_type_mod(end),'A')
95        nom_type(end)=[];
96        num_j_str=char(num_j1+64);% lower letter corresponding to the index
97    elseif isequal(numel(regexp(nom_type_mod,'_')),1)%if a second separator '_' exists in nom_type
98        num_j_str=['_' num2str(num_j1)];
99        nom_type_mod(regexp(nom_type_mod,'_'):end)=[];
100    else
101        num_j1_out=[];%no index j
102    end
103    if ~isnan(str2double(nom_type_mod))   
104        numtype=['%0' num2str(length(nom_type_mod)) 'd'];%indicate the number of digits (0 before the number)
105        filename=[filename num2str(num_i1,numtype) num_j_str ext];
106        num_i2_out=num_i1_out;
107        num_j2_out=num_j1_out;
108    else %fixed name , no indexing, for instance '*'
109%           filebasesub=filebase;
110        filename=[filename ext];
111    end
112
113%case of derived file indexing (e.g. netcdf files)
114else
115    % case of an imposed image pair (comp_input=1)
116    if  (exist('comp_input','var') && isequal(comp_input,1))
117            if strcmp(nom_type(1),'_')
118                filename=[filename '_'];
119                nom_type(1)=[];
120            end
121            if strcmp(nom_type(end-1:end),'AB')||strcmp(nom_type(end-1:end),'ab')
122                if strcmp(nom_type(end-1:end),'AB')
123                    nchar=64;
124                else
125                    nchar=96;
126                end
127                if isequal(num_j1,num_j2)% case of displacements at the same time
128                    num_j_str=char(num_j1+nchar);
129                else
130                    num_j_str=[char(num_j1+nchar) char(num_j2+nchar)];
131                end
132                if strcmp(nom_type(end-2),'_')
133                    numstr=['%0' num2str(numel(nom_type)-3) 'd'];
134                    num_j_str=['_' num_j_str];
135                else
136                    numstr=['%0' num2str(numel(nom_type)-2) 'd'];
137                end
138                filename=[filename num2str(num_i1,numstr) num_j_str ext];
139                num_i2_out=num_i1;
140            elseif isequal(nom_type,'i_j1-j2')
141                if isequal(num2str(num_j1),num2str(num_j2))% case of displacements at the same time
142                    filename=[filename num2str(num_i1) '_' num2str(num_j1) ext];
143                else
144                    filename=[filename num2str(num_i1) '_' num2str(num_j1) '-' num2str(num_j2) ext];
145                end
146                num_i2_out=num_i1;
147            elseif  isequal(nom_type,'i1-i2_j')
148                if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
149                      filename=[filename num2str(num_i1) '_' num2str(num_j1) ext];
150                else
151                    filename=[filename num2str(num_i1) '-' num2str(num_i2) '_' num2str(num_j1) ext];
152                end
153                num_j2_out=num_j1;
154            elseif  isequal(nom_type,'i1-i2')
155                if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
156                     filename=[filename num2str(num_i1) ext];
157                else
158                    filename=[filename num2str(num_i1) '-' num2str(num_i2) ext];
159                end
160                num_j2_out=num_j1;
161            elseif isequal(nom_type,'i1-i2_j1-j2')
162                if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
163                    app1= num2str(num_i1);
164                else
165                    app1= [num2str(num_i1) '-' num2str(num_i2)];
166                end
167                if isequal(num2str(num_j1),num2str(num_j2))% case of displacements at the same time
168                    app2= num2str(num_j1);
169                else
170                    app2= [num2str(num_j1) '-' num2str(num_j2)];
171                end     
172                filename=[filename app1 '_' app2 ext];
173            end
174            idetect=1;
175           % idetect=(exist(filename,'file')==2);
176     % case of an image pair to determine (comp_input=0)
177    else
178          [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);
179    end
180   
181     %look for sub-directories containing netcdf files
182    if idetect==0 && isequal(subdir,'?')
183        [pathfile,name]=fileparts(filebase);
184        direct=dir(pathfile);%directory containing filebase
185        datedir=[];%default
186%         idir=0;
187        indir=find(cell2mat({direct.isdir}));% find indices of subdirectories
188        direct=direct(indir(3:end));% keep only the subdirectories,eliminating the two first terms '.' and '..'
189        lengthdir=length(direct);
190        if lengthdir==0
191            subdir='';% no subdirectory found
192        else
193            for idir=1:lengthdir
194                date_str=direct(idir).date;%string of the date of last modification
195                datedir(idir)=0;%default
196                char_code=double(date_str);% code of the date characters
197                special_char=(char_code>127); %non standard Ascii character (e.g. date in french)
198                if isempty(find(special_char,1))% standard Ascii character
199                    datedir(idir)=datenum(date_str);
200                end                           
201%                 datedir(idir)=datenum(direct(idir).date); %absolute date of last directory modification
202            end
203            [mostrec,indrec]=max(datedir);% most recently modified subdir chosen by default
204            subdir=direct(indrec).name; %chosen directory
205        end
206        filebasesub=fullfile(pathfile,subdir,name);
207        %if the image pair is imposed
208        if (exist('comp_input','var') && isequal(comp_input,1))
209            if isequal(nom_type,'#_ab')
210                filename=[filebasesub num2str(num_i1,'%03d') '_' num2stra(num_j1,nom_type) num2stra(num_j2,nom_type) ext];
211            elseif isequal(nom_type,'_i1_j1-j2')
212                filename=[filebasesub '_' num2str(num_i1) '_' num2str(num_j1) '-' num2str(num_i2) ext];
213            elseif isequal(nom_type,'_i1-i2_j')
214                filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) '_' num2str(num_j1) ext];
215            elseif isequal(nom_type,'_i1-i2')
216                filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) ext];
217            end
218%             idetect=(exist(filename,'file')==2);
219        else
220            [filename,num_i1_out,num_j1_out,num_i2_out,num_j2_out]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type);             
221        end
222    end
223end
224if ~strcmp(subdir,'?')
225    subdir_out=subdir;
226else
227    subdir_out='';
228end
229
230%------------------------------------------------------------------------
231% --- search the appropriate image pair (netcdf file) corresponding to a given image number
232function [filename,num_i1,num_j1,num_i2,num_j2,idetect]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type)
233%------------------------------------------------------------------------
234% for nom_type=netc_2D or netc_old, it searches all the pairs corresponding
235% to num_i1, and chooses the most recent file.
236%for nom_type=netc_3D or netc_series, it searches all the pairs (num_i1
237%num_i2), with num_i1 as the first  index, and chooses the most recent file.
238
239filename=[];num_j2=[];idetect=0;%default values
240if isequal(nom_type,'#_ab')
241    dirpair=dir([filebasesub num2str(num_i1,'%03d') '_*.nc']);
242elseif isequal(nom_type,'_i_j1-j2')
243    dirpair=dir([filebasesub '_' num2str(num_i1) '_*-*.nc']);
244elseif isequal(nom_type,'_i1-i2_j')
245    dirpair=dir([filebasesub '_' num2str(num_i1) '-*_' num2str(num_j1) '.nc']);
246elseif isequal(nom_type,'_i1-i2')
247    dirpair=dir([filebasesub '_' num2str(num_i1) '-*.nc']);
248    if isempty(dirpair)
249        dirpair=dir([filebasesub '_*-' num2str(num_i2) '.nc']);
250    end
251end
252nbpair=length(dirpair);
253if nbpair >= 1 %choose the most recent file if several are found
254    idetect=1; %detected pair
255    datepair=zeros(1,nbpair);%default
256    for ipair=1:nbpair
257         date_str=dirpair(ipair).date;%string of the date of last modification
258         char_code=double(date_str);% code of the date characters
259         special_char=(char_code>127); %non standard Ascii character (e.g. date in french)
260         if isempty(find(special_char))% standard Ascii character
261             datepair(ipair)=datenum(date_str);
262         end   
263    end
264    [choice,indpair]=max(datepair);
265    [pathname,file,field_count,str2,str_a,str_b]=name2display(dirpair(indpair).name);
266    num_i1=str2double(field_count);
267    num_i2=str2double(str2);
268    num_j1=stra2double(str_a);
269    num_j2=stra2double(str_b);
270     pathname=fileparts(filebasesub);% CORRIGE LE 6 JUIN (ETAIT DESACTIVE)
271    filename=fullfile(pathname,dirpair(indpair).name);
272end
273
274
Note: See TracBrowser for help on using the repository browser.