function [iiz,z_out]=adi_find_indices(z,z_lims,zlab);
%
% Call [iiz,z_out]=adi_find_indices(z,z_lims,zlab);
%
% Finds indices in z corresponding to limits given in z_lims, where
%
% z_lims	= [z1,z2]	        ==> z >= z1 & z <= z2, OR
% 			  [z1;z2; ...zn]    ==> z values closes to [z1;z2; ...zn]; OR
%             []             ==>    all z values;
% z_out     = z(iiz);
%
%
% 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.
[nr,nc]=size(z_lims); nz=length(z);
if nr==0,
    iiz=[1:nz];
elseif nr==1 & nc==2,
	dz=(z(nz) - z(1))/(max(1,nz-1));
	iiz=find(z>=z_lims(1)-dz/2 & z<=z_lims(2)+dz/2);
	if isempty(iiz), 
		z_lims,z(1),z(nz), 
		error(['USER ERROR: no overlap in ',zlab])
	end
elseif nc==1,
    iiz=zeros(length(z_lims),1);
    for jz=1:length(z_lims); [dum,iimin]=min(abs(z-z_lims(jz))); iiz(jz)=iimin; end
else
    disp(z),disp(nz),disp(z_lims),disp(zlab),
	error('z_lims must be [zmin,zmax] or [z1;z2; ... zn]');
end
%%z([1,end]),nz,length(z),z_out,length(iiz)
z_out=z(iiz);
