function [data1out,data2out,s1,s2] = tsAlignSizes(data1,gridfirst1,data2,gridfirst2)
%
% If the time vector is aligned to differing dimensions, a 'transpose' is
% perfomed so that both time vectors are aligned to the first dimension.
% s1 and s2 are the sizes of the output arrays.
%
%   Author(s): James G. Owen, Rong Chen
%   Copyright 1986-2004 The MathWorks, Inc.
%   $Revision: 1.1.6.1 $ $Date: 2004/12/26 21:46:03 $

if gridfirst1 == gridfirst2
    data1out = data1;
    data2out = data2;
elseif ~gridfirst1 % Force all data to have time vector gridfirst
    try
       data1out = permute(data1,[ndims(data1) 1:ndims(data1)-1]);
    catch
        try
            data1out = permute(double(data1),[ndims(data1) 1:ndims(data1)-1]);
        catch
            errstr = sprintf('%s\n%s','Permute failed for this type of ordinate data object.', ...
                 'Try implementing a double cast method or overloaded addition for this object');
            error('tsAlignSizes:badcast',errstr)
        end
    end
    data2out=data2;
elseif ~gridfirst2 % Force all data to have time vector gridfirst
    try
       data2out = permute(data2,[ndims(data2) 1:ndims(data2)-1]);
    catch
       try
           data2out = permute(double(data2),[ndims(data2) 1:ndims(data2)-1]);
       catch
           errstr = sprintf('%s\n%s','Permute failed for this type of ordinate data object.', ...
                'Try implementing a double cast method or overloaded addition for this object');
           error('tsAlignSizes:badcast',errstr)
       end
    end
    data1out=data1;
end
s1 = hdsGetSize(data1out);
s2 = hdsGetSize(data2out);    
