%% Function MAP2XY
function [x,y]=map2xy(dlon,dlat,arg1,arg2,arg3,arg4,arg5)
% Converts x-y kilometer coordinates to lon/lat in spherical reference frame
%
% Usage
%   function [x,y]=map2xy(dlon,dlat,xln xlt xlnkm xltkm rota)
%   function [x,y]=map2xy(dlon,dlat,[xln xlt xlnkm xltkm rota])
% Inputs
%   LON - Decimal longitude coordinates
%   LAT - Decimal latitude coordinates
%   XLN   - Decimal longitude origin
%   YLT   - Decimal latitude origin
%   XLNKM - One minute of longitude in kilometers (setorg 2nd output argument)
%   YLTKM - One minute of latitude in kilometers (setorg 1st output argument)
%   ROTA  - Anticlockwise in degrees
% Outputs
%   X     - x coordinates (+ve WEST for ROTA=0)(km)
%           NOTE: X-axis points to left of Y-axis
%   Y     - y coordinates (+ve north for ROTA =0)(km)

%
% Derived from xy2map    wsdw, (Aug 2009) 

if nargin == 7
 xln=arg1;
 xlt=arg2;
 xlnkm=arg3;
 xltkm=arg4;
 rota=arg5;
elseif nargin ==3
 xln=arg1(1);
 xlt=arg1(2);
 xlnkm=arg1(3);
 xltkm=arg1(4);
 rota=arg1(5);
end

% Rotation parameters
rad=pi/180.;
csr=cos(rota*rad);
snr=sin(rota*rad);

% Coordinate conversion
rlat = (dlat - xlt)*60*xltkm;
rlon = (dlon - xln)*60*xlnkm;

y = rlat*csr - rlon*snr;
x = -rlon*csr - rlat*snr;

end
