function fullname = set_filenames(filetype,parms)
%
%
%

% Init
filetype(end+1:4) = '_';

[parms,frac]          = take_from_struct(parms,'frac',  0.5);
[parms,seed]          = take_from_struct(parms,'seed',  1);
[parms,n_trn]         = take_from_struct(parms,'n_trn', 1);
[parms,n_tst]         = take_from_struct(parms,'n_tst', 1);
[parms,dbname]        = take_from_struct(parms,'dbname', 'mnist');
[parms,dig1]          = take_from_struct(parms,'dig1', []);
[parms,dig2]          = take_from_struct(parms,'dig2', []);
[parms,gf_str]        = take_from_struct(parms,'gf_str', []);
fill_mode_str         = calc_fill_mode_str(parms);
[parms,thresh]        = take_from_struct(parms,'thresh', []);
[parms,matches]       = take_from_struct(parms,'matches', 5);

dbstr = '';
dbname(end+1:5)='_';
switch(dbname(1:5))
 case 'visio',   dbstr = sprintf('tr%3.1f_m%d',thresh,matches);
 case 'visft',   dbstr = sprintf('m%d',matches);  
 case 'mnist',   dbstr = sprintf('%d-%d',dig1,dig2);
 case 'patch',   dbstr = sprintf('%d-%d',dig1,dig2);  
 case 'genef',   dbstr = sprintf('%s',gf_str);
 case 'votei',   dbstr = 'mat';
 case 'hepat',   dbstr = 'mat';
 case 'echoc',   dbstr = 'mat';
 case 'sick-',   dbstr = 'mat';
 case 'mushr',   dbstr = 'mat';
 case 'chess',   dbstr = 'mat';
 case 'ionos',   dbstr = 'mat';  
 case 'pima-',   dbstr = 'mat';  
 case 'hypot',   dbstr = 'mat';    
 case 'aneal',   dbstr = sprintf('%d-%d',dig1,dig2);
 otherwise
  error('\n\n Unknown dbname %s\n\n',dbname);
end


dirname = fullfile('FillAbsent','Runs',upper(dbname),dbstr);
tmpname = fullfile('/tmp',dirname);
if(exist(tmpname,'dir')==0)
  fprintf('Create directory "%s"\n',tmpname);
  [success,message] = mkdir('/tmp',dirname);
  if(success<1), error(message);end;
end
dirname = fullfile('/tmp',dirname);

if isempty(n_trn) && isempty(n_tst)
  n_str='';
else
  n_str = sprintf('_trn%d_tst%d',n_trn,n_tst);
end

switch(filetype)
  
 case 'kern', % Data after added noise
  parmstr = parms2str(parms);
  filename = sprintf('KRNL_%s_%s_%s_K%sK_miss%03d_r%02d_.mat',...
		     dbname,dbstr,fill_mode_str,parmstr,ceil(frac*100),seed);
 
 case 'nois', % Data after added noise
  filename = sprintf('NOIS_%s_%s_noised_miss%03d_r%02d%s.mat',...
		     dbname,dbstr,ceil(frac*100),seed,n_str);
 
 case 'fild', % Data after filled/flagged
  filename = sprintf('FILD_%s_%s_%s_filled_miss%03d_r%02d%s.mat',...
		     dbname,dbstr,fill_mode_str,ceil(frac*100), ...
		     seed,n_str);
 
 case 'trn_', % Data after spliting to trn/tst/vld
  filename = sprintf('TRN_%s_%s_%s_s%d_miss%03d%s.svm',...
		     dbname,dbstr,fill_mode_str,seed,ceil(frac*100),n_str);
 
 case 'tst_', % 
  filename = sprintf('TST_%s_%s_%s_s%d_miss%03d%s.svm',...
		     dbname,dbstr,fill_mode_str,seed,ceil(frac*100),n_str);    
 
 case 'vld_', % 
  filename = sprintf('VLD_%s_%s_%s_s%d_miss%03d%s.svm',...
		     dbname,dbstr,fill_mode_str,seed,ceil(frac*100),n_str);    
  
 case 'rslt', % Data after added noise
  parmstr = parms2str(parms);
  res_str ='';  
  if(isfield(parms,'n_restarts'))
    if(parms.n_restarts>1)
      res_str = sprintf('_res%d',parms.n_restarts);
    end
  end  
  filename = sprintf('RSLT_%s_%s_%s_K%sK_miss%03d%s_r%02d%s.mat', ...
		     dbname,dbstr,fill_mode_str,parmstr,ceil(frac* ...
						  100),res_str,seed,n_str);
 otherwise
  error('illegal filename filetype');
end

fullname = fullfile(dirname,filename);

return
end


% ============================================
function fill_mode_str = calc_fill_mode_str(parms)
%
%
%

  [parms,fill_mode]     = take_from_struct(parms,'fill_mode', []);
  [parms,n_neighbors]   = take_from_struct(parms,'n_neighbors', []);
  [parms,n_em_clusts]   = take_from_struct(parms,'n_em_clusts', []);  

  % Fill missing values
  switch fill_mode(1:4)
   case 'knnm',  fill_mode_str = sprintf('%s%d',fill_mode, n_neighbors);
   case 'clst',  fill_mode_str = sprintf('%s%d',fill_mode, n_em_clusts);    
   otherwise,   fill_mode_str = fill_mode;
  end
  
  return
end

% ============================================
function [parmstr] = parms2str(parms)
%
  gammastr ='';  
  if strcmp(parms.kertype(1:3),'rbf')
    gammastr = sprintf('_g%4.2f',parms.gamma);
  end
  
  dimstr ='';  
  if strcmp(parms.kertype(1:3),'pol')
    dimstr = sprintf('_d%d',parms.dim);
  end  
  
  sign1str ='';  
  if isfield(parms,'sign1')
     sign1str = sprintf('_%d',parms.sign1);
  end  

  sign2str ='';  
  if(isfield(parms,'sign2'))
     sign2str = sprintf('_%d',parms.sign2);
  end  
  
  sign3str ='';  
  if isfield(parms,'do_sign3')
    if(parms.do_sign3>0)    
      if(isfield(parms,'sign3'))
	sign3str = sprintf('_%d',parms.sign3);
      end  
    end  
  end

  cgstr ='';
  if isfield(parms,'cgstep')
    if(parms.cgstep>eps)
     cgstr = sprintf('_%d',fix(100*parms.cgstep));
    end
  end

   
  parmstr = sprintf('%s%s%s%s%s%s%s', parms.kertype,dimstr,gammastr, ...
		    sign1str,sign2str,sign3str,cgstr);


  return
end