% COPY makes a (deep) copy of the input pest objects.
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% DESCRIPTION: COPY makes a deep copy of the input pest objects.
%
% CALL:        b = copy(a, flag)
%
% INPUTS:      a    - input pest object
%              flag - 1: make a deep copy, 0: return copies of handles
%
% OUTPUTS:     b - copy of inputs
%
% This is a transparent function and adds no history.
%
% VERSION:     $Id: copy.m,v 1.4 2010/06/25 15:11:47 ingo Exp $
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function varargout = copy(old, deepcopy)
  
  if deepcopy
    % Loop over input pest objects
    new = pest.newarray(size(old));
    obj = copy@ltpda_uoh(new, old, 1);
    
    for kk=1:numel(old)
      
      if ~isempty(old(kk).yunits)
        obj(kk).yunits = old(kk).yunits;
      end
      if ~isempty(old(kk).models)
        obj(kk).models = old(kk).models;
      end
      
      obj(kk).y     = old(kk).y;
      obj(kk).dy    = old(kk).dy;
      obj(kk).names = old(kk).names;
      obj(kk).pdf   = old(kk).pdf;
      obj(kk).cov   = old(kk).cov;
      obj(kk).corr  = old(kk).corr;
      obj(kk).chi2  = old(kk).chi2;
      obj(kk).dof   = old(kk).dof;
      obj(kk).chain = old(kk).chain;
      
    end
  else
    obj = old;
  end
  
  varargout{1} = obj;
end

