% A test script for the gapfilling method
%
% J Sanjuan 28-01-08
%
% $Id: test_ao_gapfilling.m,v 1.3 2009/02/02 15:20:38 hewitson Exp $
%
function test_ao_gapfilling()
  
  % Make test AOs
  % stationary data
  nsecs = 1e4;
  fs    = 1;
  
  pl = plist();
  pl.append('nsecs', nsecs);
  pl.append('fs', fs);
  pl.append('tsfcn', 'randn(size(t))');
  
  a1 = ao(pl);
  a1.setName('a1');
  %a1.setX(1:1e4);
  
  %a1x = ao(pl);
  %a1x.setName('a1x');
  %a1x.setX(1e4+1:2e4);
  
  % non-stationary data
  pl = plist();
  pl.append('nsecs', nsecs);
  pl.append('fs', fs);
  pl.append('tsfcn', '0.001*t+randn(size(t))');
  
  a2 = ao(pl);
  a2.setName('a2');
  
  % non-stationary data (ii)
  pl = plist();
  pl.append('nsecs', nsecs);
  pl.append('fs', fs);
  pl.append('tsfcn', 'exp(t/2.5e3)+randn(size(t))');
  
  a3 = ao(pl);
  a3.setName('a3');
  
  % Split by time the previous data
  
  pl = plist('split_type', 'times', 'times', [0.0 6000.0 6000.0 8999 8999 1e4]);
  
  ao_new = split(a1, pl);
  
  %times = [0 6000 6000 8999 8999 1e4];
  
  %pl = plist('times', times);
  
  b = split(a1, pl);
  
  Ta_stat = b(1);
  Ta_stat.setX(1:6000);
  
  Tc_stat = b(3);
  Tc_stat.setX(9000:1e4);
  
  clear b
  
  b = split(a2, pl);
  
  Ta_nonstat = b(1);
  Ta_nonstat.setX(1:6000);
  
  Tc_nonstat = b(3);
  Tc_nonstat.setX(9000:1e4);
  
  clear b
  
  b = split(a3, pl);
  
  Tx = b(1);
  Tx.setX(1:6000);
  
  Ty = b(3);
  Ty.setX(9000:1e4);
  
  % Use gapfilling
  % parameters list
  pl = plist('method', 'spline');
  pl = plist('addnoise', 'yes');
  
  % stationary data, a1
  a1_refilled = gapfilling(Ta_stat, Tc_stat) % default parmeters
  a1_refilled_spline = gapfilling(Ta_stat, Tc_stat, pl); % using pl
  
  % non-stationary data, a2
  a2_refilled = gapfilling(Ta_nonstat, Tc_nonstat) % default parmeters
  a2_refilled_spline = gapfilling(Ta_nonstat, Tc_nonstat, pl); % using pl
  
  % non-stationary data, a3
  a3_refilled = gapfilling(Tx, Ty) % default parmeters
  a3_refilled_spline = gapfilling(Tx, Ty, pl); % using pl
  
  % Plotting
  iplot(a1, a1_refilled, a1_refilled_spline);
  iplot(a2, a2_refilled, a2_refilled_spline);
  iplot(a3_refilled, a3_refilled_spline);
  
  % plot history for a1
  plot(a1_refilled.hist)
  
  % Reproduce from history
  a_out = rebuild(a1_refilled);
  a_out2 = rebuild(a2_refilled);
  
  iplot(a_out);
  
end
