function h = subsasgn(h, S, input)
%SUBSASGN  Overload

%   Author(s): P. Gahinet
%   Copyright 1986-2003 The MathWorks, Inc.
%   $Revision: 1.1.6.1 $  $Date: 2004/12/26 21:36:00 $

%   Struct S should have the following format with length equal 1:
%           S.type='.'
%           S.subs=the name string

if length(S)== 1 && strcmp(S.type,'.') && ischar(S.subs)
    % case 1: add/update a @timeseries object
    if isa(input,'tsdata.timeseries')
        % check that the sizes match
        if h.timeinfo.Length~=getGridSize(input,1)
            error('tscollection and time series have mismatching time vectors')
        end
        units = {'years', 'weeks', 'days', 'hours', 'mins', 'secs'};     
        tsIntimevec = tsunitconv(h.timeInfo.Units,input.timeInfo.Units)*input.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(input.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(input.timeInfo.Startdate)-datenum(h.timeInfo.Startdate));
        else
            if ~isempty(input.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
        % Create a copy of the added timeseries
        newtscopy = input.copy;
        % Use the @tscollection data storage for the new time vector
        % set(newtscopy,'Time',h.Time,'TimeInfo',h.TimeInfo.copy,'name',S.subs);
        tmp=newtscopy.getContainer('Time');
        tmp=h.getTimeContainer;
        newtscopy.timeinfo=h.timeinfo;
        newtscopy.name=S.subs;

        % If the named property exists as a timeseries then update
        if ~isempty(findprop(h,S.subs)) 
            if any(strcmpi(S.subs,methods(h)))
                error([S.subs ' is reserved as a method name'])
            end
            if ~isequal(class(h.(S.subs)),'tsdata.timeseries')
                error('Specified name is a reserved property name')
            end
        else
            p = schema.prop(h,S.subs,'handle');
        end
        p=h.findprop(S.subs);
        p.AccessFlags.PublicSet = 'on';
        set(h,S.subs,newtscopy);
        p.AccessFlags.PublicSet = 'off';
        return
    % case 2: update time 
    elseif strcmpi(S.subs,'time')
        % check that the sizes match
        if ~isvector(input)
            error('time must be a vector');
        end
        if h.timeinfo.Length~=length(input)
            error('tscollection and the time vector have mismatching time vectors')
        end
        try
            h.time=input;
        catch
            error(lasterr)'
        end
        return
    % case 3: update timemetadata 
    elseif strcmpi(S.subs,'timeinfo') && isa(input,'tsdata.timemetadata')
        error('You are not allowed to change the @timemetadata object handle here');
        return
    % case 4: update name
    elseif strcmpi(S.subs,'name') && isvarname(input)
        h.name=input;
        return
    else
        error('grammer not supported')
    end
    % Creates new timeseries property on assignment if necessary
	% TO DO: Need to add ValueArray for quality
elseif length(S)>= 2 && isequal(S.type,'.') && ischar(S(1).subs) && ischar(S(2).subs)
        % access contents of the @timeseries object
        if strcmpi(S(2).subs,'time') || strcmpi(S(2).subs,'timeinfo')
            % don't allow change time or timeinfo in @timeseries object
            error('You are not allowed to change the time or timeinfo fields in the @timeseries object here');
        end
        if nargout>0
            varargout = cell(1,nargout);
            varargout{:} = builtin('subsasgn',h,S,input);
        else
            builtin('subsasgn',h,S,input);
        end
        return
elseif length(S)>= 3 && ischar(S(1).subs) && ischar(S(2).subs)
        % access contents of the @timeseries object
        if strcmpi(S(2).subs,'time') || strcmpi(S(2).subs,'timeinfo')
            % don't allow change time or timeinfo in @timeseries object
            error('You are not allowed to change the time or timeinfo fields in the @timeseries object here');
        end
        % access contents of the @timeseries object
        if nargout>0
            varargout = cell(1,nargout);
            varargout{:} = builtin('subsasgn',h,S,input);
        else
            builtin('subsasgn',h,S,input);
        end
        return
else
    error('grammer not supported')
    return
end



