function [x,y] = distxy(lat,lon)
%
%function [x,y] = distxy(lat,lon)
%
% Function to calculate cumulative distance along a ship track
%    relative to the first point. Output x & y components.
%    (To calculate absolute distance use "dist=distcalc(lat,lon)"
%    Assumes Lat & Lon are in decimal degrees and are limited
%    to less than 100 or so miles.  If over that limit
%    distorsions may exist due to sphere to plane projection.
%
% Jim Manning 12/92 .. conversion of Fortran Code "Distan.for"
%
pi=3.1415926;
pid180=pi/180.;
alat=lat(1)*pid180;
alon=lon(1)*pid180;
%
% hold latitude constant and calculate the e/w translation ("x")
%
blat=lat(1)*pid180;
for i=1:length(lat)
blon=lon(i)*pid180;
ci=cos(alat)*cos(blat)*cos(alon-blon)+sin(alat)*sin(blat);
x(i)=6371*tan(sqrt(abs(1-ci*ci)/ci));
%
% assign a sign assuming input longitude is negative; positive towards the east
%
  if blon <= alon 
   x(i)=x(i)*(-1.);
  end;
end;
%
% hold longitude constant and calculate the n/s translation ("y")
%
blon=lon(1)*pid180;
for i=1:length(lat)
blat=lat(i)*pid180;
ci=cos(alat)*cos(blat)*cos(alon-blon)+sin(alat)*sin(blat);
y(i)=6371*tan(sqrt(abs(1-ci*ci)/ci));
%
% assign a sign; positive towards the north
%
  if blat <= alat
   y(i)=y(i)*(-1.);
  end;
end;
