function SP=data_composite(DATA,jdim_elim,jdim_expand)
%
% Reshapes DATA.data so that there is one fewer dimension
%
%
% COPYRIGHT (c) 2008
% 
% Applied Research Laboratories, The University of Texas at Austin
% (ARL:UT)
% All rights reserved.
% 
% This material may be reproduced by or for the U.S. Government
% only. This notice must appear in all copies of this file and its
% derivatives. Further distribution outside the U.S. Government is
% prohibited without the explicit written authorization from the
% Director of the Environmental Sciences Laboratory, ARL:UT.
% 
% THIS SOFTWARE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
% EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO THE
% IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
% PURPOSE, OR NONINFRINGEMENT.
% 
% IN NO EVENT WILL ARL:UT BE LIABLE FOR ANY DAMAGES WHATSOEVER ARISING
% OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN IF ARL:UT HAS
% BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
SP=structure_copy(DATA,{'data'});
if round(jdim_elim)~=jdim_elim | jdim_elim<0 | jdim_elim>DATA.ndim | jdim_elim==jdim_expand,
    error('User error: jdim_elim must be integer from 1 to DATA.ndim.');
end
if round(jdim_expand)~=jdim_expand | jdim_expand<0 | jdim_expand>DATA.ndim,
    error('User error: jdim_expand must be integer from 1 to DATA.ndim.');
end
%
dims=DATA.dims;
ndim_elim=DATA.dims(jdim_elim);
ndim_expand=DATA.dims(jdim_expand);
ndim_new=ndim_expand*ndim_elim + ndim_expand-1;
jdim_new=jdim_expand; if jdim_elim<jdim_new, jdim_new=jdim_new-1; end
%
SP.ndim=DATA.ndim-1;
iidim_keep=find([1:DATA.ndim]~=jdim_elim);
iidim_unch=find([1:DATA.ndim]~=jdim_elim & [1:DATA.ndim]~=jdim_expand);
SP.dims=DATA.dims(iidim_keep);
SP.dims(jdim_new)=ndim_new;

sc_diff=DATA.scales{jdim_expand}(min(2,ndim_expand)) - DATA.scales{jdim_expand}(1);
sc_min=DATA.scales{jdim_expand}(1) - sc_diff/2;
sc_max=DATA.scales{jdim_expand}(end) + sc_diff/2;
SP.scales=[];
SP.labels=[];
for jdim=1:length(iidim_keep),
    SP.scales{jdim}=DATA.scales{iidim_keep(jdim)};
    SP.labels{jdim}=DATA.labels{iidim_keep(jdim)};
end
if ndim_expand>1,
	SP.scales{jdim_new}=mylinspace(sc_min,sc_max,ndim_new);
	str=['(',DATA.labels{jdim_elim}(1),'=',num2str(DATA.scales{jdim_elim}(1),3),',', ...
            num2str(DATA.scales{jdim_elim}(end),3),')'];
	SP.labels{jdim_new}=[SP.labels{jdim_new},str];
else
	SP.scales{jdim_new}=DATA.scales{jdim_elim}; SP.labels{jdim_new}=DATA.labels{jdim_elim};
end
%
dmax=max(DATA.data(:));
SP.data=repmat(dmax,SP.dims);
SP.old_dims=[ndim_elim,ndim_expand];
SP.old_scales{1}=DATA.scales{jdim_elim};
SP.old_scales{2}=DATA.scales{jdim_expand};
SP.nsector=ndim_expand;
SP.ndim_elim=ndim_elim;
SP.idx_scales{1}=repmat(NaN,[1,ndim_new]);
SP.idx_scales{2}=repmat(NaN,[1,ndim_new]);
jrows=[1:ndim_elim];
if jdim_elim==1 & jdim_expand==2,
	for jexp=1:ndim_expand,
        SP.data(jrows,:)=DATA.data(:,jexp,:);
        SP.idx_scales{1}(jrows)=[1:ndim_elim];
        SP.idx_scales{2}(jrows)=jexp;
        jrows=jrows + ndim_elim + 1;
	end
elseif jdim_elim==2 & jdim_expand==1,
	for jexp=1:ndim_expand,
        SP.data(jrows,:)=DATA.data(jexp,:,:);
        SP.idx_scales{1}(jrows)=[1:ndim_elim];
        SP.idx_scales{2}(jrows)=jexp;
        jrows=jrows + ndim_elim + 1;
	end
else
    disp('Must make code in data_composite for this case!!');
end
