function addts(h, data, varargin)
%ADDTS Add data vector or time series object into the tscollection object
%
% ADDTS(h, data, varargin) takes 2 to 4 arguments
% 1. h: tscollection object handle
% 2. data: can be a data vector or time series object
% 3. varargin(1): unique name for the time series object
% 4. varargin(2): a tsdata.metadata object which stores the datainfo
%
% Note: the time vector of the inserted time series object will be
% automatically discarded.

%   Author(s): James G. Owen, Rong Chen
%   Copyright 1986-2004 The MathWorks, Inc.
%   $Revision: 1.1.6.1 $  $Date: 2004/12/26 21:35:47 $

% validate the number of the input arguments
error(nargchk(2,4,nargin));

% deal with name
if nargin==2 
    if isequal(class(data),'tsdata.timeseries')
        name=data.name;
    else
        error('The name of the added time series must be provided.') 
    end
else
    name=varargin{1};
end
   
% check name string
if ~ischar(name) || isempty(name) 
    error('The name of the added time series must be a non-empty string') 
elseif any(strcmpi(name,h.getMembers))
    error('The name of the added time series conflicts with an existing member time series') 
end    
    
% create a local ts object based on data
if isequal(class(data),'tsdata.timeseries')
    if h.timeinfo.Length~=getGridSize(data,1)
        error('tscollection and time series have mismatching time vectors')
    end
	units = {'years', 'weeks', 'days', 'hours', 'mins', 'secs'};     
	tsIntimevec = tsunitconv(h.timeInfo.Units,data.timeInfo.Units)*data.Time;
	% If the tscollection has an absolute time vector any added time series
	% must have a matching abs time vector
	if ~isempty(h.timeInfo.Startdate) 
        if isempty(data.timeInfo.Startdate) 
            error('tscollection has an absolute time vector, added timeseries is relative')
        end
        % Accont for differences in References of the tscollection and the
        % added timeseries
        tsIntimevec = tsIntimevec+tsunitconv(h.timeInfo.Units,'days')*...
            (datenum(data.timeInfo.Startdate)-datenum(h.timeInfo.Startdate));
	else
        if ~isempty(data.timeInfo.Startdate) 
            warning('tscollection has a relatve time vector, added time series will be relative')
        end    
    end
	% Check that the time vectors match
    if h.time(end)-h.time(1)~=0
        if norm(tsIntimevec-h.time)/((h.time(end)-h.time(1))/h.timeinfo.Length)>1e-6
            error('tscollection and time series have mismatching time vectors')
        end
    else
        if norm(tsIntimevec-h.time)>1e-6
            error('tscollection and time series have mismatching time vectors')
        end
    end
    ts=data.copy;
else
    ts=tsdata.timeseries(data);
end
% update this ts object
% set(ts,'Time',h.Time,'TimeInfo',h.TimeInfo.copy,'Name', name);
tmp=ts.getContainer('Time');
tmp=h.getTimeContainer;
ts.timeinfo=h.timeinfo;
ts.name=name;

% check if datainfo is available
if nargin==4 && isa(varargin{2},'tsdata.metadata')
    ts.DataInfo = varargin{1};
end

% Add this ts object into the collection
if ~isempty(findprop(h,name)) 
    if any(strcmpi(name,methods(h)))
        error([name ' is reserved as a method name'])
    elseif ~isequal(class(h.name),'tsdata.timeseries')
        error([name ' is a reserved property name'])
    else
        error([name ' has already been taken.'])
    end
else
    p = schema.prop(h,name,'handle');
end
set(h,name,ts);
h.Listeners_.(name) = handle.listener(ts, findprop(ts,'Name'), 'PropertyPreSet', @(es,ed) tsNameChanged(h,es,ed));
p.AccessFlags.PublicSet = 'off';
p.AccessFlags.Serialize = 'off';

