function setSlice(this,Section,B,GridSize)
%GETSLICE  Extracts  slice from time series data storage
%
%   SETSLICE(ValueArray,Section,Array,GridSize)
%
%   Author(s): James G. Owen
%   Copyright 1986-2003 The MathWorks, Inc.
%   $Revision: 1.1.6.1 $ $Date: 2004/12/26 21:35:39 $

% RE: Assumes B is a subarray of A
% Overloaded HDS method

GridSizeB = cellfun('length',Section);
NsB = prod(GridSizeB);
isCellofSampleB = (isa(B,'cell') && prod(size(B))==NsB);

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modification to parent HDS code
% Get current array
%A = getArray(this); 
A = this.metadata.getData(this.Data);
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

% Align formats of A and B (cell array of samples vs. compound array
% aggregating grid and sample dimensions for evenly sized samples)
if isempty(A)
   % Creating new array
   if isCellofSampleB
      % Try converting data to homogenous array
      try
         [B,SampleSize] = utCell2Array(this,B);
      catch
         SampleSize = [1 1];
      end
   end
   if isempty(B)
      return
   end
   % Create new array of size grid size and data type inherited from SLICE
   if this.GridFirst
      A = hdsNewArray(B,[GridSize SampleSize]);
   else
      A = hdsNewArray(B,[SampleSize GridSize]);
   end
else
   % Modifying existing array
   SampleSize = this.SampleSize;
   isCellofSampleA = (isa(A,'cell') && isequal(SampleSize,[1 1]));
   if isCellofSampleB && ~isCellofSampleA
      % A is a compound array. Try converting B to same format
      try 
         [B_Array,SampleSizeB] = utCell2Array(this,B);
      catch
         SampleSizeB = [];
      end
      if isequal(SampleSize,SampleSizeB)
         % Conversion successful
         B = B_Array;
      else
         % Must convert A to cell format
         A = this.utArray2Cell(A);
         SampleSize = [1 1];
      end
   elseif isCellofSampleA && ~isCellofSampleB
      % Convert B to cell format
      B = this.utArray2Cell(B);
   end
end
this.SampleSize = SampleSize;

%Ns = prod(GridSize);
if this.GridFirst
    Ns=size(this.Data,1);
else
    tmp=size(this.Data);
    Ns=tmp(end);
end

% Perform assignment
if length(Section)==1
   % Absolute indexing
   if Ns>0
       A = this.utReshape(A,Ns);
   end
   if NsB>0
       B = this.utReshape(B,NsB);
   end
end
is = repmat({':'},[1 length(SampleSize)]);
if this.GridFirst
    if isempty(B)
        tmp=[Section is];
        A(tmp{:})=[];
        GridSize=GridSize-NsB;
    else
        A = hdsSetSlice(A,[Section is],B);
    end
else
    if isempty(B)
        tmp=[is Section];
        A(tmp{:})=[];
        GridSize=GridSize-NsB;
    else
        A = hdsSetSlice(A,[is Section],B);
    end
end
if length(Section)==1
   % Absolute indexing
   A = this.utReshape(A,GridSize);
end
%this.setArray(A)

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Modification to parent HDS code
%this.setArray(this.metadata.setData(A));
this.setArray(A);
