% MAKETESTFILES builds a set of test files for the current LTPDA version.
%
% M Hewitson 09-09-08
%
% $Id: makeTestFiles.m,v 1.8 2010/05/12 15:55:19 ingo Exp $
%
function makeTestFiles()
  
  mkPlist();
  
  mkAO();
  mkRational();
  mkParfrac();
  mkPzmodel();
  mkMiir();
  mkMfir();
  mkTimespan();
  mkSSM();
  
  mkCollection();
  mkMatrix;
  mkSmodel;
  mkPest;
  mkFilterbank;
  
end

%% PLIST
function mkPlist()
  
  
  pl1 = plist();
  pl2 = plist('a', 1, 'b', 2, 'c', 3);
  pl3 = plist('char', 'string', ...
    'number', 123, ...
    'logical', true, ...
    'cell', {'asd', [1 2 3; 4 5 6], [true false], 'asd'});
  
  pl4 = plist('spec', specwin('kaiser', 5, 20), ...
    'time', time(123456), ...
    'ao', ao(1:10, randn(10,1)), ...
    'miir', miir(plist('type', 'lowpass')));
  
  plvec = [pl1, pl2, pl3];
  plmat = [pl1, pl2, pl3; pl4, pl1, pl2];
  
  writeFiles([pl1, pl2, pl3, pl4], plvec, plmat);
  
end

%% AO
function mkAO()
  
  % tsdata ao
  aots = ao(plist('fs', 10, 'nsecs', 10, ...
    'tsfcn', 'sin(2*pi*1.4*t) + 0.1*randn(size(t))', ...
    't0', time('1980-12-01 12:43:12')));
  
  aots.setName();
  
  % fsdata ao
  aofs = aots.psd;
  aofs.setName();
  
  % xydata ao
  aoxy = ao(xydata(1:10,1:10));
  aoxy.setName();
  
  % cdata ao
  aoc = ao(magic(3));
  aoc.setName();
  
  plvec = [aots, aofs, aoxy];
  plmat = [aots, aofs, aoxy; aots, aoc, ao()];
  
  writeFiles([aots, aofs, aoxy, aoc], plvec, plmat);
  
end

%% Pzmodel
function mkPzmodel()
  
  pzm1 = pzmodel(1, pz(1), []);
  pzm1.setName();
  
  pzm2 = pzmodel(1, [], pz(1));
  pzm2.setName();
  
  pzm3 = pzmodel(1, 1, 10);
  pzm3.setName();
  
  pzm4 = pzmodel();
  pzm4.setName();
  
  pzm5 = pzmodel(2, 10, {1, [1 1]});
  pzm5.setName();
  
  pzmvec = [pzm1, pzm2, pzm3];
  pzmmat = [pzm1, pzm2, pzm3; pzm4, pzm5, pzm5];
  
  writeFiles([pzm1, pzm2, pzm3, pzm4, pzm5], pzmvec, pzmmat);
  
end

%% MFIR
function mkMfir()
  
  fir1 = mfir(plist('type', 'highpass'));
  fir1.setName();
  
  fir2 = mfir(plist('type', 'lowpass'));
  fir2.setName();
  
  fir3 = mfir(plist('type', 'lowpass', 'fc', .1, 'gain', 1, 'fs', 1, 'order', 64, 'WIN', specwin('Hanning', 65)));
  fir3.setName();
  
  pzm = pzmodel(1, [pz(1) pz(200)], pz(50));
  pl = plist('pzmodel', pzm, 'fs', 1000);
  fir4 = mfir(pl);
  fir4.setName();
  
  a1 = ao(plist('fsfcn', '1./(50+f)', 'f', linspace(0, 500, 1000)));
  a1.setFs(1000);
  pl = plist('ao', a1);
  fir5 = mfir(pl);
  fir5.setName();
  
  firvec = [fir1, fir2, fir3];
  firmat = [fir1, fir2, fir3; fir4, fir5, fir5];
  
  writeFiles([fir1, fir2, fir3, fir4, fir5], firvec, firmat);
  
end
%% MIIR
function mkMiir()
  
  iir1 = miir(plist('type', 'lowpass'));
  iir1.setName();
  
  iir2 = miir(plist('type', 'bandpass', 'fc', [0.01 0.1]));
  iir2.setName();
  
  iir3 = miir(plist('type', 'bandreject', 'fc', [0.01 0.1]));
  iir3.setName();
  
  pzm = pzmodel(1, [pz(1) pz(200)], pz(50));
  pzm.setName();
  pl = plist('pzmodel', pzm, 'fs', 1000);
  iir4 = miir(pl);
  iir4.setName();
  
  a  = [0.5 -0.01];
  b  = [1 0.1];
  fs = 1;
  iir5 = miir(a,b,fs);
  iir5.setName();
  
  iirvec = [iir1, iir2, iir3];
  iirmat = [iir1, iir2, iir3; iir4, iir5, iir5];
  
  writeFiles([iir1, iir2, iir3, iir4, iir5], iirvec, iirmat);
  
end

%% SSM
function mkSSM
  
  try
    s1 = ssm();
    s2 = ssm(plist('built-in', 'SMD'));
    s3 = ssm(plist('built-in', 'HARMONIC_OSC_1D'));
    
    sVec = [s1, s2, s3];
    sMat = [s1, s2, s3; s2, s1, s2];
    
    writeFiles([s1 s2 s3], sVec, sMat);
  catch
    warning('!!! Don''t know how to build this SSM object.');
  end
  
end

%% TIMESPAN
function mkTimespan()
  
  ts1 = timespan();
  ts1.setName();
  
  ts2 = timespan('2008-08-08 12:00:00', '2008-08-08 13:00:00');
  ts2.setName();
  
  ts3 = timespan(time(10), time(100));
  ts3.setName();
  
  ts4 = timespan(time(0) ,'14:00:00'); % timespan
  ts4.setName();
  
  pl = plist('timezone', 'GMT+06','start', time(1234),'end', time(12345));
  ts5 = timespan(pl);
  ts5.setName();
  
  tsvec = [ts4, ts2, ts5];
  
  tsmat = [ts1, ts2, ts5; ts4, ts3, ts2];
  
  writeFiles([ts1, ts2, ts3, ts4, ts5], tsvec, tsmat)
  
end

%% PARFRAC
function mkParfrac()
  
  pf1 = parfrac();
  pf1.setName();
  
  pf2 = parfrac([1 2], {4, 6+2i}, []);
  pf2.setName();
  
  pf3 = parfrac([1 2+1i 2-1i], [6 1+3i 1-3i], [1 2]);
  pf3.setName();
  
  pf4 = parfrac([1 2+1i 2-1i], [6 1+3i 1-3i], [1 2 3 4], 'my par frac', unit('V'), unit('Hz'));
  pf4.setName();
  
  pzm = pzmodel(1, pz(1), [pz(1,2), pz(1+2i)]);
  pl  = plist('pzmodel', pzm, 'name', 'new name', 'iunits', 'm^3', 'ounits', 'Hz');
  pf5 = parfrac(pl);
  pf5.setName();
  
  pfvec = [pf4, pf2, pf5];
  
  pfmat = [pf1, pf2, pf5; pf4, pf3, pf2];
  
  writeFiles([pf1, pf2, pf3, pf4, pf5], pfvec, pfmat)
  
end

%% RATIONAL
function mkRational()
  
  ra1 = rational();
  ra1.setName();
  
  ra2 = rational([1 2], [4, 6], 'my rational');
  ra2.setName();
  
  ra3 = rational([1 2 -3], [6 -7 1 -5], 'my par rat', unit('V'), unit('Hz'));
  ra3.setName();
  
  pzm = pzmodel(1, pz(1), [pz(1,2), pz(1+2i)]);
  pl  = plist('pzmodel', pzm, 'name', 'new name', 'iunits', 'm^3', 'ounits', 'Hz');
  ra4 = rational(pl);
  ra4.setName();
  
  ravec = [ra4, ra3, ra2];
  
  ramat = [ra1, ra2, ra3; ra4, ra3, ra2];
  
  writeFiles([ra1, ra2, ra3, ra4], ravec, ramat)
  
end

%% COLLECTION
function mkCollection()
  
  coll1 = collection();
  coll1.setName();
  
  coll2 = collection(rational([1 2], [4, 6], 'my rational'), ao(magic(8)), mfir(plist('type', 'lowpass')));
  coll2.setName();
  
  coll3 = collection(ao(1:51, randn(1,51) + 1.1i, 12.3), pzmodel(1, pz(1), [pz(1,2), pz(1+2i)]));
  coll3.setName();
  coll3.setPlotinfo(plist('color', 'black'));
  
  
  collVec = [coll1, coll2, coll3];
  
  collMat = [coll3, coll1, coll2; coll2 coll3, coll1];
  
  writeFiles([coll1, coll2, coll3], collVec, collMat)
  
end

%% MATRIX
function mkMatrix()
  
  m1 = matrix();
  m1.setName();
  
  m2 = matrix([ao(1), ao(2) ao(3); ao(4), ao(5), ao(6)]);
  m2.setName();
  
  m3 = matrix(ao(1), ao(2), ao(3));
  m3.setName();
  m3.setPlotinfo(plist('color', 'black'));
  
  
  mVec = [m1, m2, m3];
  
  mMat = [m3, m1, m2; m2 m3, m1];
  
  writeFiles([m1, m2, m3], mVec, mMat)
  
end

%% SMODEL
function mkSmodel()
  
  model1 = smodel();
  model1.setName();
  
  model2 = smodel('C1*X1  + C2*X2 + C3*X3');
  model2.setParams('C1', 1.00447715161685);
  model2.setParams('C2', 2.06131342419899);
  model2.setParams('C3', 3.03410245878967);
  model2.setXvals(randn(1,100));
  %   model2.setXvar({'X1', 'X2', 'X3'});
  model2.setXvar('X1');
  model2.setXunits(unit('T', 'm', 'C'));
  model2.setYunits('m');
  model2.setPlotinfo(plist('color', 'black'));
  model2.setName();
  
  modelVec = [model1, model2];
  
  modelMat = [model1, model1 model2; model1 model2, model1];
  
  writeFiles([model1, model2], modelVec, modelMat)
  
end

%% PEST
function mkPest()
  
  model = smodel('C1*X1  + C2*X2 + C3*X3');
  model.setParams('C1', 1.00447715161685);
  model.setParams('C2', 2.06131342419899);
  model.setParams('C3', 3.03410245878967);
  %   model.setXvar({'X1', 'X2', 'X3'});
  model.setXvar('X1');
  model.setXunits(unit('T', 'm', 'C'));
  model.setYunits('m');
  model.setName();
  
  pest1 = pest([1;2.06;3.03], {'C1', 'C2', 'C3'}, [0.0917;0.089;0.0957], [0.00841 -0.000604 0.000455;-0.000604 0.00791 -0.000246;0.000455 -0.000246 0.00916]);
  pest1.setYunits(unit('[m T^(-1)]','[m m^(-1)]','[m C^(-1)]'));
  pest1.setModels(model);
  pest1.setName();
  
  pest2 = pest();
  pest2.setName();
  
  pestVec = [pest1, pest2];
  
  pestMat = [pest1, pest1 pest2; pest1 pest2, pest1];
  
  writeFiles([pest1, pest2], pestVec, pestMat)
  
end

%% FILTERBANK
function mkFilterbank()

  a1 = ao(plist('fsfcn', '1./(50+f)', 'f', linspace(0, 500, 1000)));
  a1.setFs(1000);
  pl = plist('ao', a1);
  
  f1 = mfir(pl);
  f2 = mfir(plist('Type', 'lowpass'));
  f3 = mfir(plist('type', 'lowpass', 'fc', .1, 'gain', 1, 'fs', 1, 'order', 64, 'WIN', specwin('Hanning', 65)));
  
  pzm = pzmodel(1, [pz(1) pz(200)], pz(50));
  pzm.setName();
  pl = plist('pzmodel', pzm, 'fs', 1000);
  
  f4 = miir(pl);
  f5 = miir();
  f6 = miir(plist('type', 'bandreject', 'fc', [0.01 0.1]));
  
  fb1 = filterbank();
  fb1.setName();
  
  fb2 = filterbank([f1, f2, f3], 'serial');
  fb2.setPlotinfo(plist('color', 'black'));
  fb2.setName();
  
  fb3 = filterbank(f4, f5, f6);
  
  fbVec = [fb1, fb2, fb3];
  
  fbMat = [fb1, fb3 fb2; fb1 fb2, fb3];
  
  writeFiles([fb1, fb2, fb3], fbVec, fbMat)
  
end

%% Write Function
function writeFiles(objs, obj_vec, obj_mat)
  
  % class
  cln = class(objs);
  
  % Get the toolbox version
  tbxver = getappdata(0, 'ltpda_version');
  
  % get only the version string without the MATLAB version
  tbxver = strtok(tbxver);
  
  % remove the points between the subversions
  tbxver = strrep(tbxver, '.', '');
  
  %%%%%%%%%%   Save single objects   %%%%%%%%%%
  for ii = 1:numel(objs)
    
    % Create filename
    filename = sprintf('%s_%s_%d', cln, tbxver, ii);
    
    % Save XML
    save(objs(ii), [filename '.xml']);
    if ~strcmp(tbxver, '191')
      % Save MAT
      save(objs(ii), [filename '.mat']);
    end
  end
  
  %%%%%%%%%%   Save vector of object   %%%%%%%%%%
  
  % Create filename
  filename = sprintf('%s_%s_vec', cln, tbxver);
  
  % Save XML
  save(obj_vec, [filename '.xml']);
  if ~strcmp(tbxver, '191')
    % Save MAT
    save(obj_vec, [filename '.mat']);
  end
  
  %%%%%%%%%%   Save matrix of object   %%%%%%%%%%
  
  % Create filename
  filename = sprintf('%s_%s_mat', cln, tbxver);
  
  % Save XML
  save(obj_mat, [filename '.xml']);
  if ~strcmp(tbxver, '191')
    % Save MAT
    save(obj_mat, [filename '.mat']);
  end
  
end
