function yi=mylinear(x,y,xi,iinan);
% Call yi=mylinear(x,y,xi,iinan);
% My simple version of 1-D linear interpolation on a uniform x-axis.
% Set iinan=0 to extrapolate yi's correspdonding to out-of-bounds xi's
%			1 to set yi corresponding to out-of-bounds xi to NaN, 
%			2 to set them to 1e-300;
% NOTE: x must be a vector.  But if xi and y are same-sized matrices, then each
% column of y is interpolated by the corresponding column of xi to get yi.
if nargin<4, iinan=0; end
%
[nri,nci]=size(xi); [nr,nc]=size(x); [nry,ncy]=size(y);
if nri==1 & nr~=1, x=x(:)'; y=y(:)'; end
if nci==1 & nc~=1, x=x(:); y=y(:); end
nx=length(x);
delx=(x(nx) - x(1))/max(1,nx-1);
ii=floor((xi - x(1))/delx) + 1; ii=min(max(ii,1),nx-1);
fac2=(xi - x(ii))/delx; fac1=1 - fac2;
% If xi and y are same-sized matrices, adjust indices ii accordingly:
if nri==nry & nci==ncy & nri>1 & nci>1,
	jj=ones(nri,1)*(nri*[0:nci-1]);
	ii=ii + jj;
end
yi=fac1.*y(ii) + fac2.*y(ii+1);
%
if iinan==1,
	xmin=min(x(1),x(nx)); xmax=max(x(1),x(nx));
	ii=find(xi>xmax | xi<xmin);
	yi(ii)=NaN;
elseif iinan==2,
	if x(1)<x(nx),
		ii=find(xi<x(1)); jj=find(xi>x(nx));
	else
		ii=find(xi<x(nx)); jj=find(xi>x(1));
	end
	yi(ii)=1e-30; yi(jj)=1e-30;
elseif iinan==3,
	if x(1)<x(nx),
		ii=find(xi<x(1)); jj=find(xi>x(nx));
		yi(ii)=y(1); yi(jj)=y(nx);
	else
		ii=find(xi<x(nx)); jj=find(xi>x(1));
	    yi(ii)=y(nx); yi(jj)=y(1);
	end
	yi(ii)=1e-30; yi(jj)=1e-30;
end
%
% 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.
