function [lat,long]=xy2ll(lat0,long0,x0,y0,x,y,iinm)
% Call [lat,long]=xy2ll(lat0,long0,x0,y0,x,y,iinm);
% Computes (lat,long) of point that lies at (x,y) relative to (x0,y0), which 
% is same as point (lat0,long0).  iinm=0 or 1 means r given in km or nmi.
if nargin<7, iinm=0; end
if abs(lat0) > 90,
   disp(['Illegal latitude given: ',num2str(lat)])
end
delx=(x-x0); dely=(y-y0);
r=sqrt(delx.^2 + dely.^2);
phi=atan2(delx,dely);
%
pierad=pi/180; if iinm ==1, r=r*1.852; end;
a=r/6367.639;
c=pi/2 - lat0*pierad;
B=phi;
cosb=cos(a).*cos(c) + sin(a).*sin(c).*cos(B);
b=acos(cosb);
sinA=sin(a).*sin(B)./sin(b);
lat=90 - b/pierad; long=long0 + asin(sinA)/pierad;
%
% 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.
