function ii=data_dim_lim(lims,axvals)
%
% Call ii=data_dim_lim(lims,axvals);
%
% lims      = [axmin,axmax] OR [idx1;idx2; ...] (integers) OR [val1;val2; ...]; OR
%             [axmin,axmax,iiskip] OR [axmin,axmax,-delta]
% ii        = indices into axvals to take;
%
%
% 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.
lmin=min(lims); lmax=max(lims); [nr,nc]=size(lims); nvals=length(axvals);
if nr==1 & nc==2 & all(lims==0), lims=[]; nr=0; nc=0; end       % Make 0,0 mean take all;
if nr==1 & nc==2,
	ii=find(axvals>=lims(1) & axvals<=lims(2));
    if isempty(ii),
        axlim_str=num2str([axvals(1),axvals(end)]);
        disp(['Dim limits outside range for dim ',int2str(jdim),': lims = ',int2str(lims(:)'), ...
            '; Axis limits = ',axlim_str]); status=-1;
        return; 
    end
elseif isempty(lims) | (nr==1 & nc==1 & lims(1)==0),
	ii=[1:nvals];
elseif nr==1 & nc==3,
	ii=find(axvals>=lims(1) & axvals<=lims(2));
	if lims(3)>0 & round(lims(3))==lims(3),
        iisk=lims(3);
	elseif lims(3)<0,
		iisk=nint(abs(lims(3)/(axvals(2)-axvals(1))));
	else
		disp(['Illegal lims for jdim = ',jdim,'; skip factor must be integer.']);
		return
	end
	ii=[ii(1):iisk:ii(end)];
elseif nc==1
    if ~all(round(lims)==lims) | lmin<1 | lmax>nvals,
% For dim_lims in column vector of axis values, take closest:
        diff_mat=abs(repmat(axvals(:),[1,nr])-repmat(lims(:)',[nvals,1]));
		[dum,ii]=min(diff_mat,[],1);
        lims=unique(ii); lmin=min(lims); lmax=max(lims); 
        [nr,nc]=size(lims);
    end
    ii=[lmin:lmax];
    if isempty(ii),
        axlim_str=num2str([axvals(1),axvals(end)]);
        disp(['Dim limits outside range for dim ',int2str(jdim),': lims = ',int2str(lims(:)'), ...
            '; Axis limits = ',axlim_str]); status=-1; return; 
    end            
else
	disp(['Illegal dim_lims for jdim = ',int2str(jdim)]); disp(lims); nr,nc, return
end
