% FROMTSFCN Construct an ao from a ts-function string
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% FUNCTION:    fromTSfcn
%
% DESCRIPTION: Construct an ao from a ts-function string
%
% CALL:        a = fromTSfcn(pl)
%
% PARAMETER:   pl: Parameter list object
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function a = fromTSfcn(a, pli)
  
  VERSION = '$Id: fromTSfcn.m,v 1.25 2011/08/16 06:13:02 hewitson Exp $';
  
  % get AO info
  ii = ao.getInfo('ao', 'From Time-series Function');
  
  % Set the method version string in the minfo object
  ii.setMversion([VERSION '-->' ii.mversion]);
  
  % Add default values
  pl = applyDefaults(ii.plists, pli);
  pl.getSetRandState();
  
  nsecs       = find(pl, 'nsecs');
  fs          = find(pl, 'fs');
  fcn         = find(pl, 'tsfcn');
  t0          = find(pl, 't0');
  toffset     = find(pl, 'toffset');
  
  % Build t vector
  if isempty(nsecs) || nsecs == 0
    error('### Please provide ''Nsecs'' for ts-function constructor.');
  end
  if  isempty(fs) || fs == 0
    error('### Please provide ''fs'' for ts-function constructor.');
  end
  
  
  % make time vector
  t = [0:1/fs:nsecs-1/fs]';
  
  % make y data
  y = eval([fcn ';']);
  
  % if the user passed a string which is not a function of t, then a
  % constant value is calculated
  if numel(t) ~= numel(y)
    if numel(y) == 1
      disp('The function input is not a function of t; a constant value is calculated.');
      y = y*ones(size(t));
    else
      error('### The function input size does not match the time base size');
    end
  end
    
  % Make an analysis object
  a.data = tsdata(t,y);
  
  % set t0
  a.setT0(t0);
  
  % set toffset
  a.setToffset(toffset);
  
  % Set xunits and yunits
  a.setXunits(pl.find('xunits'));
  a.setYunits(pl.find('yunits'));
  
  % Add history
  a.addHistory(ii, pl, [], []);
  % Set object properties from the plist
  a.setObjectProperties(pl);
  
end


