function [range,bearing]=ll2rb(lat1,long1,lat2,long2,iinm)
% Call [range,bearing]=ll2rb(lat1,long1,lat2,long2,iinm).
% Computes range in km (iinm=0) or nautical miles (iinm=1) and bearing
% in radians from point at (lat1,long1) to point(s) in (lat2,long2).
% Input lats and longs must be in degrees (divide minutes by 60 and add
% to degrees).
%
pierad=pi/180;
if abs(lat1) > 90 | abs(lat2) < -90,
   disp(['Illegal latitude given: ',num2str(lat1),', ',num2str( lat2)])
end
lat1=pierad*lat1; long1=pierad*long1; lat2=pierad*lat2; long2=pierad*long2;
if lat1==lat2 & long1==long2, range=0; bearing=0; return; end;
dlong=long2 - long1;
% Sin-cos switch since theta=90-latitude:
sinth1=cos(lat1);
costh1=sin(lat1);
sinth2=cos(lat2);
costh2=sin(lat2);
t=costh1.*costh2 + sinth1.*sinth2.*cos(dlong);
if abs(t) > 1, t=sign(1.,t), end
range=6367.639*acos(t); if iinm==1, range=range/1.852; end
if sinth1 < 1.e-6, bearing=long2, return, end
yy=sinth1.*sinth2.*sin(dlong); xx=costh2 - costh1.*t;
bearing=atan2(yy,xx);
%
% 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.
