% UTP_AO_JOIN a set of UTPs for the ao/join method
%
% M Hewitson 06-08-08
%
% $Id: utp_ao_join_fsdata.m,v 1.7 2010/07/05 08:11:03 mauro Exp $
%

% <MethodDescription>
%
% The join method of the ao class join multiple AOs into a single AO. This
% UTP join only analysis objects with fsdata. The other data objects will
% be tested in an other UTP.
%
% </MethodDescription>

function results = utp_ao_join_fsdata(varargin)

  % Check the inputs
  if nargin == 0

    % Some keywords
    class   = 'ao';
    mthd    = 'join';

    results = [];
    disp('******************************************************');
    disp(['****  Running UTPs for ' class '/' mthd]);
    disp('******************************************************');

    % Exception list for the UTPs:
    [ple1,ple2,ple3,ple4,ple5,ple6] = get_test_ples();

    % Run the tests
    results = [results utp_01];    % getInfo call
    results = [results utp_02];    % Vector input
    results = [results utp_03];    % Matrix input
    results = [results utp_04];    % List input
    results = [results utp_05];    % Test with mixed input
    results = [results utp_06];    % Test history is working
    results = [results utp_07];    % Test the modify call works
    results = [results utp_08];    % Test input data shape == output data shape

    a1 = ao(1:30, randn(30,1)+2*10, plist('type', 'fsdata'));
    a2 = ao(1.1:30.1, randn(30,1)+2*10, plist('type', 'fsdata'));
    results = [results utp_11(mthd, [a1 a2], ple1)];    % Test plotinfo doesn't disappear
    
    disp('Done.');
    disp('******************************************************');

  elseif nargin == 1 % Check for UTP functions
    if strcmp(varargin{1}, 'isutp')
      results = 1;
    else
      results = 0;
    end
  else
    error('### Incorrect inputs')
  end

  %% UTP_01

  % <TestDescription>
  %
  % Tests that the getInfo call works for this method.
  %
  % </TestDescription>
  function result = utp_01


    % <SyntaxDescription>
    %
    % Test that the getInfo call works for no sets, all sets, and each set
    % individually.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % Call for no sets
      io(1) = eval([class '.getInfo(''' mthd ''', ''None'')']);
      % Call for all sets
      io(2) = eval([class '.getInfo(''' mthd ''')']);
      % Call for each set
      for kk=1:numel(io(2).sets)
        io(kk+2) = eval([class '.getInfo(''' mthd ''', ''' io(2).sets{kk} ''')']);
      end
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that getInfo call returned an minfo object in all cases.
    % 2) Check that all plists have the correct parameters.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % check we have minfo objects
      if isa(io, 'minfo')
        %%% SET 'None'
        if ~isempty(io(1).sets), atest = false; end
        if ~isempty(io(1).plists), atest = false; end
        %%% Check all Sets
        if ~any(strcmpi(io(2).sets, 'Default')), atest = false; end
        if numel(io(2).plists) ~= numel(io(2).sets), atest = false; end
        %%%%%%%%%%   SET 'Default'
        if io(3).plists.nparams ~= 3, atest = false; end
        % Check key
        if ~io(3).plists.isparam('zerofill'), atest = false; end
        if ~io(3).plists.isparam('sameT0'), atest = false; end
        if ~io(3).plists.isparam('fstol'), atest = false; end
        % Check default value
        if ~isequal(io(3).plists.find('zerofill'), 'no'), atest = false; end
        if ~isequal(io(3).plists.find('samet0'), 'no'), atest = false; end
        if ~isequal(io(3).plists.find('fstol'), 1e-6), atest = false; end
        % Check options
        if ~isequal(io(3).plists.getOptionsForParam('zerofill'), {'yes', 'no'}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('samet0'), {'yes', 'no'}), atest = false; end
        if ~isequal(io(3).plists.getOptionsForParam('fstol'), {1e-6}), atest = false; end
      end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_01

  %% UTP_02

  % <TestDescription>
  %
  % Tests that the join method works with a vector of AOs as input.
  %
  % </TestDescription>
  function result = utp_02

    % <SyntaxDescription>
    %
    % Test that the join method works for a vector of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      a1 = ao(1:30, randn(30,1)+2*10, plist('type', 'fsdata'));
      a2 = ao(1.1:30.1, randn(30,1)+2*10, plist('type', 'fsdata'));
      a3 = ao(1.2:30.2, randn(30,1)+2*10, plist('type', 'fsdata'));
      avec = [a1, a2, a3];
      out  = join(avec);
      mout = rebuild(out);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output is exact one AO.
    % 2) Check that the output have the correct data.
    % 3) Check the re-built object
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= 1, atest = false; end
      % Compute reference data
      x0 = [];
      y0 = [];
      for oo = 1:numel(avec)
        if isempty(x0)
          idxBefore = [];
          idxAfter  = 1:numel(avec(oo).y);
        else
          idxBefore = find(avec(oo).x < x0(1));
          idxAfter  = find(avec(oo).x > x0(end));
        end
        x0 = [avec(oo).x(idxBefore); x0; avec(oo).x(idxAfter)];
        y0 = [avec(oo).y(idxBefore); y0; avec(oo).y(idxAfter)];
      end
      if ~isequal(out.x, x0), atest = false; end
      if ~isequal(out.y, y0), atest = false; end
      if ~eq(out, mout, ple2), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_02

  %% UTP_03

  % <TestDescription>
  %
  % Tests that the join method works with a matrix of AOs as input.
  %
  % </TestDescription>
  function result = utp_03

    % <SyntaxDescription>
    %
    % Tests that the join method works with a matrix of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      a1 = ao(1.1:30.1, randn(30,1)+2*10, plist('type', 'fsdata'));
      a2 = ao(1.2:30.2, randn(30,1)+2*10, plist('type', 'fsdata'));
      a3 = ao(1.3:30.3, randn(30,1)+2*10, plist('type', 'fsdata'));
      a4 = ao(1.4:30.4, randn(30,1)+2*10, plist('type', 'fsdata'));
      a5 = ao(1.5:30.5, randn(30,1)+2*10, plist('type', 'fsdata'));
      a6 = ao(1.6:30.6, randn(30,1)+2*10, plist('type', 'fsdata'));
      amat = [a1, a2, a3; a4, a5, a6];
      out  = join(amat);
      mout = rebuild(out);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output is exact one AO.
    % 2) Check that the output have the correct data.
    % 3) Check the re-built object
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= 1, atest = false; end
      % Compute reference data
      x0 = [];
      y0 = [];
      for oo = 1:numel(amat)
        if isempty(x0)
          idxBefore = [];
          idxAfter  = 1:numel(amat(oo).y);
        else
          idxBefore = find(amat(oo).x < x0(1));
          idxAfter  = find(amat(oo).x > x0(end));
        end
        x0 = [amat(oo).x(idxBefore); x0; amat(oo).x(idxAfter)];
        y0 = [amat(oo).y(idxBefore); y0; amat(oo).y(idxAfter)];
      end
      if ~isequal(out.x, x0), atest = false; end
      if ~isequal(out.y, y0), atest = false; end
      if ~eq(out, mout, ple2), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_03

  %% UTP_04

  % <TestDescription>
  %
  % Tests that the join method works with a list of AOs as input.
  %
  % </TestDescription>
  function result = utp_04

    % <SyntaxDescription>
    %
    % Tests that the join method works with a list of AOs as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      a1 = ao(1:30, randn(30,1)+2*10, plist('type', 'fsdata'));
      a2 = ao(1.1:30.1, randn(30,1)+2*10, plist('type', 'fsdata'));
      a3 = ao(1.2:30.2, randn(30,1)+2*10, plist('type', 'fsdata'));
      out  = join(a1, a2, a3);
      mout = rebuild(out);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output is exact one AO.
    % 2) Check that the output have the correct data.
    % 3) Check the re-built object
    %
    % </AlgoDescription>

    atest = true;
    aoin  = [a1, a2, a3];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= 1, atest = false; end
      % Compute reference data
      x0 = [];
      y0 = [];
      for oo = 1:numel(aoin)
        if isempty(x0)
          idxBefore = [];
          idxAfter  = 1:numel(aoin(oo).y);
        else
          idxBefore = find(aoin(oo).x < x0(1));
          idxAfter  = find(aoin(oo).x > x0(end));
        end
        x0 = [aoin(oo).x(idxBefore); x0; aoin(oo).x(idxAfter)];
        y0 = [aoin(oo).y(idxBefore); y0; aoin(oo).y(idxAfter)];
      end
      if ~isequal(out.x, x0), atest = false; end
      if ~isequal(out.y, y0), atest = false; end
      if ~eq(out, mout, ple2), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_04

  %% UTP_05

  % <TestDescription>
  %
  % Tests that the join method works with a mix of different shaped AOs as
  % input.
  %
  % </TestDescription>
  function result = utp_05

    % <SyntaxDescription>
    %
    % Tests that the join method works with a mix of different shaped AOs
    % as input.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      a1 = ao(1.1:30.1, randn(30,1)+2*10, plist('type', 'fsdata'));
      a2 = ao(1.2:30.2, randn(30,1)+2*10, plist('type', 'fsdata'));
      a3 = ao(1.3:30.3, randn(30,1)+2*10, plist('type', 'fsdata'));
      a4 = ao(1.4:30.4, randn(30,1)+2*10, plist('type', 'fsdata'));
      out  = join(a1, a2, [a3 a4]);
      mout = rebuild(out);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the output is exact one AO.
    % 2) Check that the output have the correct data.
    % 3) Check the re-built object
    %
    % </AlgoDescription>

    atest = true;
    aoin  = [a1, a2, a3, a4];
    if stest
      % <AlgoCode>
      % Check we have the correct number of outputs
      if numel(out) ~= 1, atest = false; end
      % Compute reference data
      x0 = [];
      y0 = [];
      for oo = 1:numel(aoin)
        if isempty(x0)
          idxBefore = [];
          idxAfter  = 1:numel(aoin(oo).y);
        else
          idxBefore = find(aoin(oo).x < x0(1));
          idxAfter  = find(aoin(oo).x > x0(end));
        end
        x0 = [aoin(oo).x(idxBefore); x0; aoin(oo).x(idxAfter)];
        y0 = [aoin(oo).y(idxBefore); y0; aoin(oo).y(idxAfter)];
      end
      if ~isequal(out.x, x0), atest = false; end
      if ~isequal(out.y, y0), atest = false; end
      if ~eq(out, mout, ple2), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_05

  %% UTP_06

  % <TestDescription>
  %
  % Tests that the join method properly applies history.
  %
  % </TestDescription>
  function result = utp_06

    % <SyntaxDescription>
    %
    % Test that the result of applying the join method can be processed back.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      a1 = ao(1.1:30.1, randn(30,1)+2*10, plist('type', 'fsdata'));
      a2 = ao(1.2:30.2, randn(30,1)+2*10, plist('type', 'fsdata'));

      out  = join(a1, a2);
      mout = rebuild(out);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the last entry in the history of 'out' corresponds to
    %    'join'.
    % 2) Check that the re-built object is the same object as the input.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the last step in the history of 'out'
      if ~strcmp(out.hist.methodInfo.mname, 'join'), atest = false; end
      % The rebuilt object must be the same as 'out'
      if ~eq(mout, out, ple2), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_06

  %% UTP_07

  % <TestDescription>
  %
  % Tests that the join method can modify the input AO.
  %
  % </TestDescription>
  function result = utp_07

    % <SyntaxDescription>
    %
    % Test that the join method can modify the input AO by calling with no
    % output and that the method doesn't change the input of the function
    % notation (with a equal sign).
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      % copy at1 to work with
      a1 = ao(1.1:30.1, randn(30,1)+2*10, plist('type', 'fsdata'));
      a2 = ao(1.2:30.2, randn(30,1)+2*10, plist('type', 'fsdata'));
      a1.setT0('2009-02-12 14:00:00');
      a2.setT0('2009-02-12 14:00:25');

      amodi = ao(a1);
      aeq   = ao(a1);
      out = aeq.join(a2);
      amodi.join(a2);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that 'out' and 'aeq' are now different.
    % 2) Check that 'aeq' is not changed
    % 3) Check that the modified input is joined
    % 4) Check that out and amodi are the same
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check that 'out' and 'aeq' are now different.
      if eq(out, aeq, ple2), atest = false; end
      % Check that 'aeq' is not changed
      if ~eq(aeq, ao(a1), ple1), atest = false; end
      % Check that the modified input is joined
      aoin = [a1 a2];
      x0 = [];
      y0 = [];
      for oo = 1:numel(aoin)
        if isempty(x0)
          idxBefore = [];
          idxAfter  = 1:numel(aoin(oo).y);
        else
          idxBefore = find(aoin(oo).x < x0(1));
          idxAfter  = find(aoin(oo).x > x0(end));
        end
        x0 = [aoin(oo).x(idxBefore); x0; aoin(oo).x(idxAfter)];
        y0 = [aoin(oo).y(idxBefore); y0; aoin(oo).y(idxAfter)];
      end
      if ~isequal(amodi.x, x0), atest = false; end
      if ~isequal(amodi.y, y0), atest = false; end
      % Check that out and amodi are the same
      if ~eq(out, amodi, ple2), atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_07

  %% UTP_08

  % <TestDescription>
  %
  % Test the shape of the output.
  %
  % </TestDescription>
  function result = utp_08

    % <SyntaxDescription>
    %
    % Test that the join method keeps the data shape of the input object.
    % The input AO must be an AO with row data and an AO with column data.
    %
    % </SyntaxDescription>

    try
      % <SyntaxCode>
      a1 = ao(1.1:30.1, randn(30,1)+2*10, plist('type', 'fsdata'));
      a2 = ao(1.2:30.2, randn(1,30)+2*10, plist('type', 'fsdata'));

      out1  = join(a1, a2);
      out2  = join(a2, a1);
      % </SyntaxCode>
      stest = true;
    catch err
      disp(err.message)
      stest = false;
    end

    % <AlgoDescription>
    %
    % 1) Check that the shape of the data doesn't change.
    %
    % </AlgoDescription>

    atest = true;
    if stest
      % <AlgoCode>
      % Check the shape of the output data
      if size(out1.data.y,1 ) == 1, atest = false; end
      if size(out2.data.y,2 ) == 1, atest = false; end
      % </AlgoCode>
    else
      atest = false;
    end

    % Return a result structure
    result = utp_prepare_result(atest, stest, dbstack, mfilename);
  end % END UTP_08

end
