function delsample(tsin,method,value)
%DELSAMPLE  Delete a sample from a timeseries object
%
%   DELSAMPLE(TS,'index',VALUE) removes samples from the time series 
%   object TS. The index values in the vector VALUE correspond to indices 
%   in the time vector of TS and identify the samples you want to remove.
%   
%   DELSAMPLE(TS,'value',VALUE) removes samples from the time series 
%   object TS. The vector VALUE gives the time values corresponding to the
%   samples you want to remove.  
%
%   See also TSDATA.TIMESERIES/TIMESERIES TSDATA.TIMESERIES/ADDSAMPLE

%   Author(s): Rong Chen
%   Copyright 1986-2004 The MathWorks, Inc.
%   $Revision: 1.1.6.1 $  $Date: 2004/12/26 21:34:58 $

% -----------------------------------------------------------------------
% initialization: get index for both the grid variable (time) and dependent
% variales (data and quality).
% -----------------------------------------------------------------------
vars = cell2mat(get(tsin.Data_,{'Variable'}));
names=get(vars,{'Name'});
[junk,idxData] = setdiff(names,{'Time';'Quality'});
if isempty(idxData)
    error('timeseries:adddata:datavariablemissing',...
     'There is no data variable defined in the timeseries.  Rebuild the timeseries.');
end
[junk,idxQuality]=intersect(names,'Quality');
GridSize = [tsin.Grid_.Length];  % length = # dimensions

% get Time data
AllVars = getvars(tsin);
GridVars = tsin.Grid_.Variable;
c = tsin.Data_(GridVars(1)==AllVars);

% Process command
if isstr(method) && isvector(method)
    switch lower(method)
        case 'index'
            if ~isnumeric(value) || ~isvector(value)
                error('Absolute indices should be either a vector of integers.');
            else
                % make sure indices are unique
                Selection=unique(value);
                % check if all the indices are valid    
                if ~isequal(round(Selection),Selection) || any(Selection<0) || any(Selection>GridSize)
                    error('Invalid indices.')
                end
            end
        case 'value'
            tmp=getArray(c);
            try
                [c1,Selection,c3]=intersect(tmp,value);
            catch
                error('Value format does not match.')
            end
            if isempty(Selection)
                error('The required sample points are not found.');
            end
        case 'nearest'
            ;
        otherwise
            error('Unknown method.')
    end
else
    error('The first input argument, Method, should be a string.')
end

% Update grid info
tsin.Grid_.Length = tsin.Grid_.Length - length(Selection);
% Update data for grid variables
tmp=getArray(c);
tmp(Selection)=[];
c.setArray(tmp);
% Update data for dependent values
is = repmat({':'},[1 length(tsin.Data_(idxData).SampleSize)]);
if tsin.DataInfo.GridFirst
    tempIndex=[{Selection} is];
else
    tempIndex=[is {Selection}];
end
tsin.Data_(idxData).data(tempIndex{:})=[];
if ~isempty(tsin.Data_(idxQuality).data)
    tsin.Data_(idxQuality).data(Selection)=[];
end

