source: trunk/src/name_generator.m @ 19

Last change on this file since 19 was 19, checked in by gostiaux, 11 years ago

the private files have been moved down to the root folder

File size: 15.7 KB
Line 
1%'name_generator': creates a file name from a root name and indices.
2%---------------------------------------------------------------------
3% [filename,idetect,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%idetect: =1 if the file is detected, 0 otherwise
13%num_i1_out,num_j1_out,num_i2_out,num_j2_out,subdir_out: index numbers and subdirectory detected
14             %for free input (= to the corresponding input indices when comp_input=1)
15%---------------------------------------------------------------------
16% INPUT:
17% 'filebase': the root name,
18% 'num_i1: first labelling index i
19% 'num_j1', first labelling index j
20% 'ext': file name extension (e.g. '.png' or '.nc')
21% 'nom_type': string defining the kind of nomenclature used:
22     %nom_type='': constant name [filebase ext] (default output if 'nom_type' is undefined)
23     %nom_type='*': the same  file [filebase ext] contains successive fields (ex avi movies)
24     %nom_type='_i': series of files with a single index i preceded by '_'(e.g. 'aa_45.png').
25     %nom_type='#' series of indexed images wich is not series_i [filebase index ext], e.g. 'aa045.jpg' or 'aa45.tif'
26     %nom_type='_i_j' matrix of files with two indices i and j separated by '_'(e.g. 'aa_45_2.png')
27     %nom_type='_i1-i2' from pairs from a single index (e.g. 'aa_45-47.nc')
28     %nom_type='_i_j1-j2'pairs of j indices (e.g. 'aa_45_2-3.nc')
29     %nom_type='_i1-i2_j' pairs of i indices (e.g. 'aa_45-46_2.nc')
30     %nom_type='#a','#A' with a numerical index and an index letter(e.g.'aa045b.png'), OBSOLETE (replaced by 'series_i_j')
31     %nom_type='%03d' or '%04d', series of indexed images with numbers completed with zeros to 3 or 4 digits, e.g.'aa045.tif'
32     %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'
33     %nom_type='raw_SMD', same as '#a' but with no extension ext='', OBSOLETE
34     %nom_type='#_ab' from pairs of '#a' images (e.g. 'aa045bc.nc'), ext='.nc', OBSOLETE (replaced by 'netc_2D')
35     %nom_type='%3dab' from pairs of '%3da' images (e.g. 'aa045bc.nc'), ext='.nc', OBSOLETE (replaced by 'netc_2D')
36% A REVOIR: 'comp_input'=1 for writting =0 for reading ,  'comp_input'(for nom_type involving index pairs (e.g. netc))
37     %comp_input=1: the index pair is imposed,
38     %comp_input=0: the index pair is automatically searched, choosing the most recent  file in case of multiple choice
39% 'num_i2': second index i (for nom_type involving index pairs (e.g. netc))
40% 'num_j2': second index j (for nom_type involving index pairs (e.g. netc))
41% 'subdir': (used for nom_type=netc...) string representing the name of the subdirectory 'subdir' containing file.
42     %subdir='': no subdirectory,
43     %subdir='?', the file is first searched with no subdirectory, then in the most recently modified subdirectory if not detected.
44
45% A FAIRE: si comp_inpu=0, si _i_j n'existe pas, chercher _i,
46function [filename,num_i1_out,num_j1_out,num_i2_out,num_j2_out,subdir_out]=...
47           name_generator(filebase,num_i1,num_j1,ext,nom_type,comp_input,num_i2,num_j2,subdir);
48sizf=size(filebase);
49if (~ischar(filebase)||~isequal(sizf(1),1)),filebase='';end
50if ~ischar(ext),ext='';end
51% filename=[filebase ext];%default
52idetect=0;
53if ~exist('num_i1','var') || isequal(num_i1,[])
54    num_i1=1; %default
55end
56if ~exist('num_j1','var') || isequal(num_j1,[])
57    num_j1=1; %default
58end
59if ~exist('num_i2','var') || isequal(num_i2,[])
60    num_i2=num_i1; %default
61end
62if ~exist('subdir','var')|| isempty(subdir)
63    subdir='' ; %default
64end
65%detection of mask or mean: filebase of the form [root '_' xxx 'm$$$'] with xxx a number and $$$ three characters
66% if length(filebase)>4 & filebase(end-3)=='m';
67%     basedouble=double(filebase);
68%     val=(48>basedouble)|(basedouble>57);% select the non-numerical characters
69%     i=length(filebase)-4;
70%     while val(i)==0 & i>0
71%         i=i-1;
72%     end
73%     nbslice=str2num(filebase(i+1:end-4));
74%     if ~isequal(nbslice,[])
75%         num_i1=mod(num_i1-1,nbslice)+1; %take the rest in the division
76%         if isequal(nom_type,'png_old')|isequal(nom_type,'#a')
77%             num_j1=1;% mask name must end by 'a'
78%         end
79%     end
80% end
81test_pairs=isequal(nom_type,'netc_old')| isequal(nom_type,'netc_2D') | isequal(nom_type,'netc_3D')| isequal(nom_type,'_i1-i2_j1-j2')| ...
82  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');
83test_2D= isequal(nom_type,'netc_old') |isequal(nom_type,'netc_2D')|isequal(nom_type,'#_ab') |isequal(nom_type,'_i_j1-j2');
84test_3D=isequal(nom_type,'netc_3D') |isequal(nom_type,'netc_series')| isequal(nom_type,'_i1-i2_j')| isequal(nom_type,'_i1-i2');
85if isequal(nom_type,'series_i')| isequal(nom_type,'_i');
86        filename=[filebase '_' num2str(num_i1) ext];
87 %       idetect=(exist(filename,'file')==2);
88elseif length(nom_type)==5 && isequal(nom_type(1:3),'_%0')&& isequal(nom_type(5),'d');
89        filename=[filebase '_' num2str(num_i1,nom_type(2:5)) ext];
90 %       idetect=(exist(filename,'file')==2);
91elseif isequal(nom_type,'series_i_j')| isequal(nom_type,'_i_j')
92        filename=[filebase '_' num2str(num_i1) '_' num2str(num_j1) ext];
93 %       idetect=(exist(filename,'file')==2);
94elseif isequal(nom_type,'png_old')| isequal(nom_type,'#a')| isequal(nom_type,'#A')
95        filename=[filebase num2str(num_i1,'%03d') num2stra(num_j1,nom_type) ext];
96   %     idetect=(exist(filename,'file')==2);
97elseif  length(nom_type)>=5 & isequal(nom_type(2:3),'%0') & isequal(nom_type(5),'d')  %isequal(nom_type,'_%04dA') %camera PCO Toulouse
98        filename=[filebase nom_type(1) num2str(num_i1,nom_type(2:4)) num2stra(num_j1,nom_type) ext];
99 %       idetect=(exist(filename,'file')==2);         
100elseif isequal(nom_type,'raw_SMD') %suffix a, b, c without extension
101        filename=[filebase num2str(num_i1,'%03d') num2stra(num_j1,nom_type)];
102%        idetect=(exist(filename,'file')==2);
103elseif isequal(nom_type,'ima_num')| isequal(nom_type,'#')
104        filename=[filebase num2str(num_i1) ext];
105%        idetect=(exist(filename,'file')==2);
106elseif length(nom_type)>=4 & isequal(nom_type(1:2),'%0') & isequal(nom_type(end),'d')
107            filename=[filebase num2str(num_i1,nom_type) ext]; %test number with a 0 before
108%            idetect=(exist(filename,'file')==2);
109
110%case of derived file indexing (e.g. netcdf files)
111elseif test_pairs
112    if (~exist('num_j2','var')| isequal(num_j2,[])) & test_2D
113          num_j2=num_j1+1; %default if num_j2 undefined
114    end
115    if (~exist('num_i2','var')| isequal(num_i2,[])) & test_3D
116          num_i2=num_i1+1; %default if num_i2 undefined
117    end
118    filebasesub=filebase;
119    % get the root name filebasesub for the netcdf files
120    if  ~isequal(subdir,'') && ~isequal(subdir,'?')
121            [Path,Name]=fileparts(filebase);
122            filebasesub=fullfile(Path,subdir,Name);
123    end
124     %inexistant pair if num_i2=0 or num_j2=0
125    if isequal(num_i2,0)
126        filename=[filebasesub '*-*_' num2str(num_i1) ext];
127        return
128    end
129    if isequal(num_j2,0)
130        filename=[filebasesub '_' num2str(num_i1) '_*-*' ext];
131        return
132    end
133    % case of an imposed image pair (comp_input=1)
134    if  (exist('comp_input','var') & isequal(comp_input,1))
135            if isequal(nom_type,'netc_old')|isequal(nom_type,'#_ab')
136                if isequal(num2str(num_j1),num2str(num_j2))% case of displacements at the same time
137                    filename=[filebasesub num2str(num_i1,'%03d') '_' num2stra(num_j1,nom_type) ext];
138                else
139                    filename=[filebasesub num2str(num_i1,'%03d') '_' num2stra(num_j1,nom_type) num2stra(num_j2,nom_type) ext];
140                end
141            elseif isequal(nom_type,'netc_2D')|isequal(nom_type,'_i_j1-j2')
142                if isequal(num2str(num_j1),num2str(num_j2))% case of displacements at the same time
143                    filename=[filebasesub '_' num2str(num_i1) '_' num2str(num_j1) ext];
144                else
145                    filename=[filebasesub '_' num2str(num_i1) '_' num2str(num_j1) '-' num2str(num_j2) ext];
146                end
147            elseif isequal(nom_type,'netc_3D') || isequal(nom_type,'_i1-i2_j')
148                if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
149                      filename=[filebasesub '_' num2str(num_i1) '_' num2str(num_j1) ext];
150                else
151                    filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) '_' num2str(num_j1) ext];
152                end
153            elseif isequal(nom_type,'netc_series') || isequal(nom_type,'_i1-i2')
154                if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
155                     filename=[filebasesub '_' num2str(num_i1) ext];
156                else
157                    filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) ext];
158                end
159            elseif isequal(nom_type,'_i1-i2_j1-j2')
160                if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
161                    app1= [num2str(num_i1)];
162                else
163                    app1= [num2str(num_i1) '-' num2str(num_i2)];
164                end
165                if isequal(num2str(num_j1),num2str(num_j2))% case of displacements at the same time
166                    app2= [num2str(num_j1)];
167                else
168                    app2= [num2str(num_j1) '-' num2str(num_j2)];
169                end     
170                filename=[filebasesub '_' app1 '_' app2 ext];
171            end
172            idetect=1;
173           % idetect=(exist(filename,'file')==2);
174     % case of an image pair to determine (comp_input=0)
175    else
176            [filename,num_i1,num_j1,num_i2,num_j2,idetect]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type);
177    end
178   
179     %look for sub-directories containing netcdf files
180    if idetect==0 && isequal(subdir,'?')
181        [pathfile,name]=fileparts(filebase);
182        direct=dir(pathfile);%directory containing filebase
183        datedir=[];%default
184        idir=0;
185        indir=find(cell2mat({direct.isdir}));% find indices of subdirectories
186        direct=direct(indir([3:end]));% keep only the subdirectories,eliminating the two first terms '.' and '..'
187        lengthdir=length(direct);
188        if lengthdir==0
189            subdir='';% no subdirectory found
190        else
191            for idir=1:lengthdir
192                date_str=direct(idir).date;%string of the date of last modification
193                datedir(idir)=0;%default
194                char_code=double(date_str);% code of the date characters
195                special_char=(char_code>127); %non standard Ascii character (e.g. date in french)
196                if isempty(find(special_char))% standard Ascii character
197                    datedir(idir)=datenum(date_str);
198                end                           
199%                 datedir(idir)=datenum(direct(idir).date); %absolute date of last directory modification
200            end
201            [mostrec,indrec]=max(datedir);% most recently modified subdir chosen by default
202            subdir=direct(indrec).name; %chosen directory
203        end
204        filebasesub=fullfile(pathfile,subdir,name);
205        %if the image pair is imposed
206        if (exist('comp_input','var') & isequal(comp_input,1))
207            if isequal(nom_type,'netc_old')|isequal(nom_type,'#_ab')
208                filename=[filebasesub num2str(num_i1,'%03d') '_' num2stra(num_j1,nom_type) num2stra(num_j2,nom_type) ext];
209            elseif isequal(nom_type,'netc_2D')|isequal(nom_type,'_i1_j1-j2')
210                filename=[filebasesub '_' num2str(num_i1) '_' num2str(num_j1) '-' num2str(num_i2) ext];
211            elseif isequal(nom_type,'netc_3D')|isequal(nom_type,'_i1-i2_j')
212                filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) '_' num2str(num_j1) ext];
213            elseif isequal(nom_type,'netc_series')|isequal(nom_type,'_i1-i2')
214                filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) ext];
215            end
216            idetect=(exist(filename,'file')==2);
217        else
218            [filename,num_i1,num_j1,num_i2,num_j2,idetect]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type);             
219        end
220    end
221% elseif isequal(nom_type,'none')|isequal(nom_type,'')|isequal(nom_type,'*')
222else
223    filebasesub=filebase;
224    if ~isequal(subdir,'') && ~isequal(subdir,'?')
225            [Path,Name]=fileparts(filebase);
226            filebasesub=fullfile(Path,subdir,Name);
227    end
228    filename=[filebasesub ext];
229    idetect=(exist(filename,'file')==2); 
230end
231num_i1_out=num_i1;
232num_j1_out=num_j1;
233if exist('num_i2','var'), num_i2_out=num_i2; else, num_i2_out=[]; end;
234if exist('num_j2','var'), num_j2_out=num_j2; else, num_j2_out=[]; end;
235if ~isequal(subdir,'?'), subdir_out=subdir; else, subdir_out='';end;
236
237%---------------------------------------------------------------
238% search the appropriate image pair (netcdf file) corresponding to a given
239% image number
240%-------------------------------------------------------------------
241function [filename,num_i1,num_j1,num_i2,num_j2,idetect]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type)
242% for nom_type=netc_2D or netc_old, it searches all the pairs corresponding
243% to num_i1, and chooses the most recent file.
244%for nom_type=netc_3D or netc_series, it searches all the pairs (num_i1
245%num_i2), with num_i1 as the first  index, and chooses the most recent file.
246
247filename=[];num_j2=[];idetect=0;%default values
248if isequal(nom_type,'netc_old')|isequal(nom_type,'#_ab')
249    dirpair=dir([filebasesub num2str(num_i1,'%03d') '_*.nc']);
250elseif isequal(nom_type,'netc_2D')|isequal(nom_type,'_i_j1-j2')
251    dirpair=dir([filebasesub '_' num2str(num_i1) '_*-*.nc']);
252elseif isequal(nom_type,'netc_3D')|isequal(nom_type,'_i1-i2_j')
253    dirpair=dir([filebasesub '_' num2str(num_i1) '-*_' num2str(num_j1) '.nc']);
254elseif isequal(nom_type,'netc_series')|isequal(nom_type,'_i1-i2')
255    dirpair=dir([filebasesub '_' num2str(num_i1) '-*.nc']);
256    if isempty(dirpair)
257        dirpair=dir([filebasesub '_*-' num2str(num_i2) '.nc']);
258    end
259end
260nbpair=length(dirpair);
261if nbpair >= 1 %choose the most recent file if several are found
262    idetect=1; %detected pair
263    for ipair=1:nbpair
264         date_str=dirpair(ipair).date;%string of the date of last modification
265         datepair(ipair)=0;%default
266         char_code=double(date_str);% code of the date characters
267         special_char=(char_code>127); %non standard Ascii character (e.g. date in french)
268         if isempty(find(special_char))% standard Ascii character
269             datepair(ipair)=datenum(date_str);
270         end   
271      %  datepair(ipair)=datenum(dirpair(ipair).date);
272    end
273    [choice,indpair]=max(datepair);
274%     [filebase,field_count,str2,str_a,str_b,ext,nom_type]=name2display(dirpair(indpair).name);
275    [pathname,file,field_count,str2,str_a,str_b,ext,nom_type]=name2display(dirpair(indpair).name);
276    num_i1=str2num(field_count);
277    num_i2=str2num(str2);
278    num_j1=stra2num(str_a);
279    num_j2=stra2num(str_b);
280     pathname=fileparts(filebasesub);% CORRIGE LE 6 JUIN (ETAIT DESACTIVE)
281    filename=fullfile(pathname,dirpair(indpair).name);
282end
283
284
Note: See TracBrowser for help on using the repository browser.