% ZEROPAD zero pads the input data series.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: ZEROPAD zero pads the input data series.
%
% CALL:        b = zeropad(a, pl)
%
% <a href="matlab:utils.helper.displayMethodInfo('ao', 'zeropad')">Parameters Description</a>
%
% VERSION:     $Id: zeropad.m,v 1.28 2011/04/08 08:56:15 hewitson Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = zeropad(varargin)

  % Check if this is a call for parameters
  if utils.helper.isinfocall(varargin{:})
    varargout{1} = getInfo(varargin{3});
    return
  end

  import utils.const.*
  utils.helper.msg(msg.PROC3, 'running %s/%s', mfilename('class'), mfilename);

  % Collect input variable names
  in_names = cell(size(varargin));
  for ii = 1:nargin,in_names{ii} = inputname(ii);end

  % Collect all AOs and plists
  [as, ao_invars] = utils.helper.collect_objects(varargin(:), 'ao', in_names);
  pl              = utils.helper.collect_objects(varargin(:), 'plist', in_names);

  %%% Decide on a deep copy or a modify
  bs = copy(as, nargout);

  % combine plists
  pl = parse(pl, getDefaultPlist());
  
  % Extract necessary parameters
  Factor = find(pl, 'Factor');
  N      = find(pl, 'N');
  pos    = find(pl, 'position');
  
  if isempty(Factor) && isempty(N)
    error('### Specify either a padding Factor, or a number of samples.');
  end

  % Loop over input AOs
  for j=1:numel(bs)
    if ~isa(bs(j).data, 'tsdata')
      error('Zero padding only works on time-series. AO %s is not a time-series.', ao_invars{j});
    else
      % Check the time-series is evenly samples
      if ~isempty(bs(j).data.x)
        error('### Zero padding only makes sense on evenly sampled time-series. Resample first.');
      end
      if isempty(N)
        if Factor < 2
          error('### Padding factor must be >= 2')
        end
        if strcmpi(pos, 'pre')
          bs(j).data.setY([repmat(zeros(size(bs(j).data.getY)), Factor-1, 1); bs(j).data.getY]);
        elseif strcmpi(pos, 'post')
          bs(j).data.setY([bs(j).data.getY; repmat(zeros(size(bs(j).data.getY)), Factor-1, 1)]);
        else 
          error('Unknown ''position'' to pad. Choose either ''pre'' or ''post'' for the ''position'' value.');
        end
      else
        if N > 0
          if strcmpi(pos, 'pre')
            bs(j).data.setY([zeros(N,1); bs(j).data.getY]);
          elseif strcmpi(pos, 'post')
            bs(j).data.setY([bs(j).data.getY; zeros(N,1)]);
          else
            error('Unknown ''position'' to pad. Choose either ''pre'' or ''post'' for the ''position'' value.');
          end
        else
          error('### Number of samples to pad must be > 0');
        end
      end
      % Set name
      bs(j).name = sprintf('zeropad(%s)', ao_invars{j});
      % Correct Nsecs
      bs(j).data.fixNsecs;
      % Add history
      bs(j).addHistory(getInfo('None'), pl, ao_invars(j), bs(j).hist);
      % clear errors
      bs(j).clearErrors;
    end
  end

  % Set output
  if nargout == numel(bs)
    % List of outputs
    for ii = 1:numel(bs)
      varargout{ii} = bs(ii);
    end
  else
    % Single output
    varargout{1} = bs;
  end
end

%--------------------------------------------------------------------------
% Get Info Object
%--------------------------------------------------------------------------
function ii = getInfo(varargin)
  if nargin == 1 && strcmpi(varargin{1}, 'None')
    sets = {};
    pl   = [];
  else
    sets = {'Default'};
    pl   = getDefaultPlist;
  end
  % Build info object
  ii = minfo(mfilename, 'ao', 'ltpda', utils.const.categories.sigproc, '$Id: zeropad.m,v 1.28 2011/04/08 08:56:15 hewitson Exp $', sets, pl);
end

%--------------------------------------------------------------------------
% Get Default Plist
%--------------------------------------------------------------------------
function plout = getDefaultPlist()
  persistent pl;  
  if exist('pl', 'var')==0 || isempty(pl)
    pl = buildplist();
  end
  plout = pl;  
end

function pl = buildplist()
  pl = plist();
  
  % Factor
  p = param({'Factor', 'Pad to <Factor> times the input data length.'}, paramValue.DOUBLE_VALUE(2));
  pl.append(p);
  
  % N
  p = param({'N', 'Pad with N zero samples.'}, paramValue.EMPTY_DOUBLE);
  pl.append(p);
  
  % Position
  p = param({'Position', 'Where to pad: before or after.'}, {2, {'pre', 'post'}, paramValue.SINGLE});
  pl.append(p);
  
  
end
% END


% PARAMETERS: 'Factor'  - Pad to <Factor> times the input data length
%                         [default: 2]
%           or
%             'N'       - Pad with N zero samples

