function SP=data_interp(DATA,kdim_int,iimeth,new_scales,iiview);
%
% Call SP=data_interp(DATA,kdim_int,iimeth,new_scales,iiview);
%
% DATA      = input data structure;
% kdim_int	= dimension(s) over which to interpolate (one or two dimensions only);
% iimeth    = 1=linear, 2=cubic, 3=spline;
% new_scales= cell array of new scales to which to interpolate DATA.data .
%             Make this cell array the same length as DATA.scales.
% iiview	= 1 to call view5 on the output SP (default is 0);
%
% Example call: SP=data_interp(DT_200,[1 3],1,DT_400.scales,1);
%   This creates SP which is the DT_200 dataset interpolated to have the same dimensions 
%   as DT_400.
%
%
% 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.
if nargin==1,
    str=sprintf('    %s   ',DATA.labels{1:DATA.ndim}); disp(['Axis labels: ',str]);
    str=['Axis limits: '];
    for jdim=1:DATA.ndim, str=[str,'   ',sprintf('[%1.2f %1.2f]',DATA.scales{jdim}([1,end]))]; end
    disp(str)
    if isfield(DATA.scales2) & ~isempty(DATA.scales2),
        str=['Second scales axis limits: '];
        for jdim=1:DATA.ndim, 
            str=[str,'   ',sprintf('[%1.2f %1.2f]',DATA.scales2{jdim}([1,end]))]; 
        end
        disp(str)
    end
    str=sprintf('       %i       ',DATA.dims); disp(['Axis lengths: ',str]);
    kdim_int=  str2num(input(['          Enter dimension numbers over which to collapse: '],'s'));
    iimeth  =  str2num(input(['Enter interpolation method (1=linear, 2=cubic, 3=spline): '],'s'));
    ndim_int=length(kdim_int);
    new_scales=DATA.scales;
    for jdim_int=1:ndim_int
        jdim=kdim_int(jdim_int); delta=diff(DATA.scales{jdim}([1,2]));
        sc=    str2num(input(['Enter delta for jdim=',int2str(jdim),' (now=',num2str(delta),'): '],'s'));
        new_scales{jdim}=[DATA.scales{jdim}(1):delta:DATA.scales{jdim}(end)];
    end
    iiview  =  str2num(input(['       Automatically view output? (1=yes,0=no,[]=yes)   : '],'s'));
    if isempty(iiview), iiview=1; end
else
    if nargin<5 | isempty(iiview), iiview=0; end
end
%
if isfield(DATA,'iidB'), iidB=DATA.iidB; else, iidB=1; end
%
SP=structure_copy(DATA,{'data'});
%
if iidB==1,
	data=10.^(DATA.data/10);
elseif iidB==0,
	data=DATA.data;
	iidB=0;
elseif iidB==-1,
	data=zmagsq(DATA.data);
	SP.dlab=[SP.dlab,'=>dB']; iidB=1;
end
%
dims_data=DATA.dims; dims_data(end+1:7)=1; dims_data(kdim_int)=0; 
[dims_loop,jdim_loop]=sort(dims_data);
for jdim=1:7, KD{jdim}=1; end
for jdim=1:DATA.ndim, KD{jdim}=[1:SP.dims(jdim)]; end
%
% Fill dimensions over which interpolation will occur:
ndim_int=length(kdim_int); kdim_int=sort(kdim_int);
jdim1=kdim_int(1); jdim2=kdim_int(end);
dims_out=dims_data;
for jdim_int=1:ndim_int,
    jdim=kdim_int(jdim_int); 
    SP.scales{jdim}=new_scales{jdim}; 
    SP.dims(jdim)=length(SP.scales{jdim});
% %     SP.labels{jdim}=[SP.labels{jdim},'(interp)'];
    KD{jdim}=[1:DATA.dims(jdim)]; 
    dims_out(jdim)=length(SP.scales{jdim});
end
%
x=DATA.scales{kdim_int(1)}; y=DATA.scales{kdim_int(end)};
xi=new_scales{kdim_int(1)}(:)'; 
yi=new_scales{kdim_int(end)}(:);
meths={'linear','cubic','spline'}; meth=meths{iimeth};
%
for j3=1:dims_data(jdim_loop(3)),
    KD{jdim_loop(3)}=j3;
    for j4=1:dims_data(jdim_loop(4)),
        KD{jdim_loop(4)}=j4;
        for j5=1:dims_data(jdim_loop(5)),
            KD{jdim_loop(5)}=j5;
            for j6=1:dims_data(jdim_loop(6)),
                KD{jdim_loop(6)}=j6;
                for j7=1:dims_data(jdim_loop(7)),
					KD{jdim_loop(7)}=j7;
                    for jdim_int=1:ndim_int, jdim=kdim_int(jdim_int); KD{jdim}=[1:DATA.dims(jdim)]; end
                    %KD{1},KD{2},KD{3},KD{4},KD{5},KD{6},KD{7}
					data0=squeeze(data(KD{1},KD{2},KD{3},KD{4},KD{5},KD{6},KD{7}));
                    if ndim_int==1,
                        data_int=interp1(x,data0,xi,meth);
                    else
                        data_int=interp2(y,x,data0,yi,xi,meth);
                    end
                    for jdim_int=1:ndim_int, jdim=kdim_int(jdim_int); KD{jdim}=[1:SP.dims(jdim)]; end
                    SP.data(KD{1},KD{2},KD{3},KD{4},KD{5},KD{6},KD{7})=data_int;
                end
            end
        end
    end
end
SP.iidB=iidB;
%
if iidB==1,
	SP.data=10*log10(max(SP.data,1e-20));
	cmax=ceil(max(SP.data(:))/5)*5; cmin=max(floor(min(SP.data(:))/5)*5,cmax-60);
elseif iidB==0,
%	SP.data=sqrt(SP.data);
	cmax=max(SP.data(:)); cmin=min(SP.data(:));
elseif iidB==-1,
    cmax=max(dB(SP.data(:))); cmin=min(dB(SP.data(:)));
end
%
if iiview, view5(SP); end
