source: trunk/src/name_generator.m @ 65

Last change on this file since 65 was 65, checked in by sommeria, 11 years ago

name_generator: remove unneeded indices at output
civ.m: & introduced at the end of system command lines (beginning by !) to allow matlab operations during civ in run mode
uvmat.m: debugging in dealing with projections
prove image pair movies superposed to velocity vectors
mouse: put circles along detected vectors

File size: 15.2 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('num_j2','var') || isequal(num_i2,[])
63    num_j2=num_j1; %default
64end
65if ~exist('subdir','var')|| isempty(subdir)
66    subdir='' ; %default
67end
68num_i1_out=num_i1;%default output
69num_j1_out=num_j1;%default output
70num_i2_out=num_i2;%default output
71num_j2_out=num_j2;%default output
72
73test_pairs=isequal(nom_type,'netc_old')| isequal(nom_type,'netc_2D') | isequal(nom_type,'netc_3D')| isequal(nom_type,'_i1-i2_j1-j2')| ...
74  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');
75test_2D= isequal(nom_type,'netc_old') |isequal(nom_type,'netc_2D')|isequal(nom_type,'#_ab') |isequal(nom_type,'_i_j1-j2');
76test_3D=isequal(nom_type,'netc_3D') |isequal(nom_type,'netc_series')| isequal(nom_type,'_i1-i2_j')| isequal(nom_type,'_i1-i2');
77if isequal(nom_type,'series_i')| isequal(nom_type,'_i');
78        filename=[filebase '_' num2str(num_i1) ext];
79        num_i2_out=num_i1;
80        num_j1_out=[];
81        num_j2_out=[];
82elseif length(nom_type)==5 && isequal(nom_type(1:3),'_%0')&& isequal(nom_type(5),'d');
83        filename=[filebase '_' num2str(num_i1,nom_type(2:5)) ext];
84        num_i2_out=num_i1;
85        num_j2_out=num_j1;
86elseif isequal(nom_type,'series_i_j')| isequal(nom_type,'_i_j')
87        filename=[filebase '_' num2str(num_i1) '_' num2str(num_j1) ext];
88        num_i2_out=num_i1;
89        num_j2_out=num_j1;
90elseif isequal(nom_type,'png_old')| isequal(nom_type,'#a')| isequal(nom_type,'#A')
91        filename=[filebase num2str(num_i1,'%03d') num2stra(num_j1,nom_type) ext];
92        num_i2_out=num_i1;
93        num_j2_out=num_j1;
94elseif  length(nom_type)>=5 & isequal(nom_type(2:3),'%0') & isequal(nom_type(5),'d')  %isequal(nom_type,'_%04dA') %camera PCO Toulouse
95        filename=[filebase nom_type(1) num2str(num_i1,nom_type(2:4)) num2stra(num_j1,nom_type) ext];
96        num_i2_out=num_i1;
97        num_j2_out=num_j1;   
98elseif isequal(nom_type,'raw_SMD') %suffix a, b, c without extension
99        filename=[filebase num2str(num_i1,'%03d') num2stra(num_j1,nom_type)];
100        num_i2_out=num_i1;
101        num_j2_out=num_j1;
102elseif isequal(nom_type,'ima_num')| isequal(nom_type,'#')
103        filename=[filebase num2str(num_i1) ext];
104        num_i2_out=num_i1;
105        num_j1_out=[];
106        num_j2_out=[];
107elseif length(nom_type)>=4 & isequal(nom_type(1:2),'%0') & isequal(nom_type(end),'d')
108        filename=[filebase num2str(num_i1,nom_type) ext]; %test number with a 0 before
109        num_i2_out=num_i1;
110        num_j1_out=[];
111        num_j2_out=[];
112
113%case of derived file indexing (e.g. netcdf files)
114elseif test_pairs
115    filebasesub=filebase;
116    % get the root name filebasesub for the netcdf files
117    if  ~isequal(subdir,'') && ~isequal(subdir,'?')
118            [Path,Name]=fileparts(filebase);
119            filebasesub=fullfile(Path,subdir,Name);
120    end
121     %inexistant pair if num_i2=0 or num_j2=0
122%     if isequal(num_i2,0)
123%         filename=[filebasesub '*-*_' num2str(num_i1) ext];
124%         return
125%     end
126%     if isequal(num_j2,0)
127%         filename=[filebasesub '_' num2str(num_i1) '_*-*' ext];
128%         return
129%     end
130    % case of an imposed image pair (comp_input=1)
131    if  (exist('comp_input','var') & isequal(comp_input,1))
132            if isequal(nom_type,'netc_old')|isequal(nom_type,'#_ab')
133                if isequal(num2str(num_j1),num2str(num_j2))% case of displacements at the same time
134                    filename=[filebasesub num2str(num_i1,'%03d') '_' num2stra(num_j1,nom_type) ext];
135                else
136                    filename=[filebasesub num2str(num_i1,'%03d') '_' num2stra(num_j1,nom_type) num2stra(num_j2,nom_type) ext];
137                end
138                num_i2_out=num_i1;
139            elseif isequal(nom_type,'netc_2D')|isequal(nom_type,'_i_j1-j2')
140                if isequal(num2str(num_j1),num2str(num_j2))% case of displacements at the same time
141                    filename=[filebasesub '_' num2str(num_i1) '_' num2str(num_j1) ext];
142                else
143                    filename=[filebasesub '_' num2str(num_i1) '_' num2str(num_j1) '-' num2str(num_j2) ext];
144                end
145                num_i2_out=num_i1;
146            elseif isequal(nom_type,'netc_3D') || isequal(nom_type,'_i1-i2_j')
147                if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
148                      filename=[filebasesub '_' num2str(num_i1) '_' num2str(num_j1) ext];
149                else
150                    filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) '_' num2str(num_j1) ext];
151                end
152                num_j2_out=num_j1;
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                num_j2_out=num_j1;
160            elseif isequal(nom_type,'_i1-i2_j1-j2')
161                if isequal(num2str(num_i1),num2str(num_i2))% case of displacements at the same time
162                    app1= [num2str(num_i1)];
163                else
164                    app1= [num2str(num_i1) '-' num2str(num_i2)];
165                end
166                if isequal(num2str(num_j1),num2str(num_j2))% case of displacements at the same time
167                    app2= [num2str(num_j1)];
168                else
169                    app2= [num2str(num_j1) '-' num2str(num_j2)];
170                end     
171                filename=[filebasesub '_' app1 '_' app2 ext];
172            end
173            idetect=1;
174           % idetect=(exist(filename,'file')==2);
175     % case of an image pair to determine (comp_input=0)
176    else
177            [filename,num_i1_out,num_j1_out,num_i2_out,num_j2_out,idetect]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type);
178    end
179   
180     %look for sub-directories containing netcdf files
181    if idetect==0 && isequal(subdir,'?')
182        [pathfile,name]=fileparts(filebase);
183        direct=dir(pathfile);%directory containing filebase
184        datedir=[];%default
185        idir=0;
186        indir=find(cell2mat({direct.isdir}));% find indices of subdirectories
187        direct=direct(indir([3:end]));% keep only the subdirectories,eliminating the two first terms '.' and '..'
188        lengthdir=length(direct);
189        if lengthdir==0
190            subdir='';% no subdirectory found
191        else
192            for idir=1:lengthdir
193                date_str=direct(idir).date;%string of the date of last modification
194                datedir(idir)=0;%default
195                char_code=double(date_str);% code of the date characters
196                special_char=(char_code>127); %non standard Ascii character (e.g. date in french)
197                if isempty(find(special_char))% standard Ascii character
198                    datedir(idir)=datenum(date_str);
199                end                           
200%                 datedir(idir)=datenum(direct(idir).date); %absolute date of last directory modification
201            end
202            [mostrec,indrec]=max(datedir);% most recently modified subdir chosen by default
203            subdir=direct(indrec).name; %chosen directory
204        end
205        filebasesub=fullfile(pathfile,subdir,name);
206        %if the image pair is imposed
207        if (exist('comp_input','var') & isequal(comp_input,1))
208            if isequal(nom_type,'netc_old')|isequal(nom_type,'#_ab')
209                filename=[filebasesub num2str(num_i1,'%03d') '_' num2stra(num_j1,nom_type) num2stra(num_j2,nom_type) ext];
210            elseif isequal(nom_type,'netc_2D')|isequal(nom_type,'_i1_j1-j2')
211                filename=[filebasesub '_' num2str(num_i1) '_' num2str(num_j1) '-' num2str(num_i2) ext];
212            elseif isequal(nom_type,'netc_3D')|isequal(nom_type,'_i1-i2_j')
213                filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) '_' num2str(num_j1) ext];
214            elseif isequal(nom_type,'netc_series')|isequal(nom_type,'_i1-i2')
215                filename=[filebasesub '_' num2str(num_i1) '-' num2str(num_i2) ext];
216            end
217            idetect=(exist(filename,'file')==2);
218        else
219            [filename,num_i1_out,num_j1_out,num_i2_out,num_j2_out,idetect]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type);             
220        end
221    end
222% elseif isequal(nom_type,'none')|isequal(nom_type,'')|isequal(nom_type,'*')
223else
224    filebasesub=filebase;
225    if ~isequal(subdir,'') && ~isequal(subdir,'?')
226            [Path,Name]=fileparts(filebase);
227            filebasesub=fullfile(Path,subdir,Name);
228    end
229    filename=[filebasesub ext];
230    idetect=(exist(filename,'file')==2); 
231end
232if ~isequal(subdir,'?'), subdir_out=subdir; else, subdir_out='';end;
233
234%---------------------------------------------------------------
235% search the appropriate image pair (netcdf file) corresponding to a given
236% image number
237%-------------------------------------------------------------------
238function [filename,num_i1,num_j1,num_i2,num_j2,idetect]=search_pair(filebasesub,num_i1,num_j1,num_i2,nom_type)
239% for nom_type=netc_2D or netc_old, it searches all the pairs corresponding
240% to num_i1, and chooses the most recent file.
241%for nom_type=netc_3D or netc_series, it searches all the pairs (num_i1
242%num_i2), with num_i1 as the first  index, and chooses the most recent file.
243
244filename=[];num_j2=[];idetect=0;%default values
245if isequal(nom_type,'netc_old')|isequal(nom_type,'#_ab')
246    dirpair=dir([filebasesub num2str(num_i1,'%03d') '_*.nc']);
247elseif isequal(nom_type,'netc_2D')|isequal(nom_type,'_i_j1-j2')
248    dirpair=dir([filebasesub '_' num2str(num_i1) '_*-*.nc']);
249elseif isequal(nom_type,'netc_3D')|isequal(nom_type,'_i1-i2_j')
250    dirpair=dir([filebasesub '_' num2str(num_i1) '-*_' num2str(num_j1) '.nc']);
251elseif isequal(nom_type,'netc_series')|isequal(nom_type,'_i1-i2')
252    dirpair=dir([filebasesub '_' num2str(num_i1) '-*.nc']);
253    if isempty(dirpair)
254        dirpair=dir([filebasesub '_*-' num2str(num_i2) '.nc']);
255    end
256end
257nbpair=length(dirpair);
258if nbpair >= 1 %choose the most recent file if several are found
259    idetect=1; %detected pair
260    for ipair=1:nbpair
261         date_str=dirpair(ipair).date;%string of the date of last modification
262         datepair(ipair)=0;%default
263         char_code=double(date_str);% code of the date characters
264         special_char=(char_code>127); %non standard Ascii character (e.g. date in french)
265         if isempty(find(special_char))% standard Ascii character
266             datepair(ipair)=datenum(date_str);
267         end   
268      %  datepair(ipair)=datenum(dirpair(ipair).date);
269    end
270    [choice,indpair]=max(datepair);
271%     [filebase,field_count,str2,str_a,str_b,ext,nom_type]=name2display(dirpair(indpair).name);
272    [pathname,file,field_count,str2,str_a,str_b,ext,nom_type]=name2display(dirpair(indpair).name);
273    num_i1=str2num(field_count);
274    num_i2=str2num(str2);
275    num_j1=stra2num(str_a);
276    num_j2=stra2num(str_b);
277     pathname=fileparts(filebasesub);% CORRIGE LE 6 JUIN (ETAIT DESACTIVE)
278    filename=fullfile(pathname,dirpair(indpair).name);
279end
280
281
Note: See TracBrowser for help on using the repository browser.