function initialize(varargin)
%INTIIALIZE Build tscollection object

% take 2 or 3 arguments
% 1. tscollection object handle
% 2. time or a time series object 
% 3. name (optional)

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

% validate the number of the input arguments
error(nargchk(2,3,nargin));
% get handle
this = varargin{1};

% deal with tscollection object name first
if nargin==3
    if ischar(varargin{3})
        if isvarname(varargin{3}) || isempty(varargin{3})
            name=varargin{3};
        else
            error('please provide a valid name as the second input argument');
        end
    else
        error('the second input argument should be a string as the name');
    end
else
    name = '';
end
% assign props
this.Name = name;

% Create associated value array for the time vector
this.Time_ = tsdata.timeseriesArray;
this.Time_.Variable = hds.variable('Time');
this.Time_.Metadata = hds.variable('TimeInfo'); 
% Disable writes to time @ValueArray through member @timeseries
this.Time_.ReadOnly = 'on';

% Assign getfunction getfunction for time and time metadata
p = this.findprop('Time');
pInfo = this.findprop('TimeInfo'); 
p.GetFunction = @LocalReadData;
p.SetFunction = @LocalWriteData;
pInfo.GetFunction = @LocalReadMetaData;
pInfo.SetFunction = @LocalWriteMetaData;
% create @timemetadata object which is linked to 'this.Time_.Metadata'
this.TimeInfo = tsdata.timemetadata;

if isequal(class(varargin{2}),'tsdata.timeseries')
    % first input is a time series object, use its time as the common time
    % vector and insert the object
    tmp=varargin{2}.time;
    this.Time = tmp;
    this.TimeInfo=varargin{2}.timeinfo.copy;
    this.addts(varargin{2});
elseif isnumeric(varargin{2}) || iscell(varargin{2}) || (ischar(varargin{2}) && ...
        min(size(varargin{2}))>1) || isa(varargin{2},'hds.ArrayContainer')
    % Process time vector
    if isa(varargin{2},'hds.ArrayContainer')
        timesrc = varargin{2};
    else
        time = varargin{2};
        if ischar(time)
            time = cellstr(time);
        end
    end
    % sort time when necessary
    if ~isempty(time) 
        % Validate time vector
        if isnumeric(time)
           time = tsChkTime(time);
        end
        I = tssorttime(time);
        % Update the grid size to zero if it has changed. It will be reset 
        % by the Time property assignment. Note that explicitly settting the 
        % grid size to a non-zero value will prevent the init method
        % from being 'undone'
        if this.timeinfo.Length~=length(time(I))
            this.getTimeContainer.Data = [];
            this.timeInfo.reset([]);
        end
        % Assign the time vector
        if iscell(time)
            this.setAbsTime(time(I));	
            this.Time = tsChkTime(this.Time);
        else
            this.Time = time(I);
        end
    elseif ~isempty(timesrc)
        setdatasrc(this,timesrc,'Time');
    else
        error('timeseries:init:nosingleton',...
           'Time vectors must have non-empty time vectors')
    end
else
    error('timeseries:init:notime',...
        'The second argument must represent the time vector or the time series name')
end
    

%------- Local Functions ------------------


function valueToRead = LocalReadData(eventsrc,eventdata)

tmp=getTimeContainer(eventsrc);
if ~isempty(tmp)
	% Read value array
	if ~isempty(tmp.metadata)
        % Parse the stored data
        valueToRead = getData(tmp.metadata,getArray(tmp)); 
	else
        valueToRead = [];
	end
else
    valueToRead = []; 
end

function valueToRead = LocalReadMetaData(eventsrc,eventdata)

tmp=getTimeContainer(eventsrc);
if ~isempty(tmp)
   valueToRead = tmp.metadata;
else
   valueToRead = [];
end

function valueToWrite = LocalWriteData(eventsrc,eventdata)

tmp=getTimeContainer(eventsrc);
if ~isempty(tmp)
    if ~isvector(eventdata)
        error('Time vector must be a vector')
    end
    if size(eventdata,2)>1
        eventdata=eventdata';
    end
	if size(eventdata,1) ~= eventsrc.timeinfo.Length && eventsrc.timeinfo.Length ~=0
        error(['Time vector must have length of ' num2str(eventsrc.timeinfo.Length)])
    end
	% Store data (may fail if array container is read only)
	ValueArray.SampleSize = [1 1];
	if ~isempty(tmp.metadata)
        setArray(tmp,eventdata);
       %setArray(eventsrc.Time_,setData(eventsrc.Time_.metadata,eventdata));
       % update the @metadata to have the right length
%        set(eventsrc.Time_.metadata,'Accesstoken_', '@tscollection/initialize');
%        set(eventsrc.Time_.metadata,'Length', eventsrc.Length);
%        set(eventsrc.Time_.metadata,'Accesstoken_','');
	end
end
valueToWrite = [];

function valueToWrite = LocalWriteMetaData(eventsrc,eventdata)

tmp=getTimeContainer(eventsrc);
if ~isempty(tmp)
    set(tmp,'metadata',eventdata);
end
valueToWrite = [];


