function SP=data_decim(DATA,kdim_dec,new_scales,iiview);
%
% Decimates a data structure.
%
% Call SP=data_decim(DATA,kdim_dec,new_scales,iiview);
%
% DATA      = input data structure;
% kdim_dec	= dimension(s) over which to decimate (one or two dimensions only);
% new_scales= cell array of new scales to decimate from DATA {sc1 sc2 ... scn}
%             [pos indices] = indices, OR [neg indices] = indices to exclude, OR [min,max] (non-integers) for
%             min, max of axis; 
% iiview	= 1 to call view5 on the output SP (default is 0);
%
%
% 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_dec=  str2num(input(['          Enter dimension numbers over which to decimate: '],'s'));
    ndim_dec=length(kdim_dec);
    sc=cell(jdim_dec);
    for jdim_dec=1:ndim_dec
        jdim=kdim_dec(jdim_dec); delta=diff(DATA.scales{jdim}([1,2]));
        sc=    str2num(input(['For jdim=',int2str(jdim),', enter indices to keep (pos), indices to delete (neg), or [min,max] ', ...
                '(non-integer) interval to keep: '],'s'));
        new_scales{jdim_dec}=sc;
    end
    iiview  =  str2num(input(['       Automatically view output? (1=yes,0=no,[]=yes)   : '],'s'));
    if isempty(iiview), iiview=1; end
else
    if nargin<4 | isempty(iiview), iiview=0; end
end
%
SP=structure_copy(DATA,{'data'});
%
for jdim=1:7, KD{jdim}=1; end
for jdim=1:DATA.ndim, KD{jdim}=[1:DATA.dims(jdim)]; end
% Replace indices of dimenstions to be decimated:
ndim_dec=length(kdim_dec(:));
for jdim_dec=1:ndim_dec
    jdim=kdim_dec(jdim_dec); 
    sc=new_scales{jdim_dec};
    if any(round(sc)~=sc),
        if length(sc(:))~=2, error('Min, max specification must be two elements long'); end
        ii=find(DATA.scales{jdim} >= sc(1) & DATA.scales{jdim} <= sc(2));
        KD{jdim}=ii;
    elseif all(sc>0),
        KD{jdim}=sc;
    elseif all(sc<0),
		ii=ones(1,DATA.dims(jdim)); ii(-sc)=0;
        KD{jdim}=find(ii);
    else
        error('All indices must be positive or negative');
    end
end
%
% Fill dimensions over which interpolation will occur:
for jdim_dec=1:ndim_dec,
    jdim=kdim_dec(jdim_dec);
    SP.scales{jdim}=DATA.scales{jdim}(KD{jdim});
    if isfield(SP,'scales2'), SP.scales2{jdim}=DATA.scales2{jdim}(KD{jdim}); end
    SP.dims(jdim)=length(SP.scales{jdim});
end
SP.data=DATA.data(KD{1},KD{2},KD{3},KD{4},KD{5},KD{6},KD{7});
%
if iiview, view5(SP); end
