%tdistnf  computes distance along transect using lon and lat data

%set global variables
%global lat lon emat trandist deltapos bottom xint yint
 global lat lon emat trandist deltapos bottom xint yint,depthint,mindepth,maxdepth,left,step,ic,tflag,mflag,moc,filename
      
%convert from spherical to rectangular coordinates
%xint=length(emat(:,4))/yint
xint
yint
%pause
lats=lat*pi/180;
lons=lon*pi/180;
rc=[cos(lons).*cos(lats), sin(lons).*cos(lats), sin(lats)];
dot=sum(rc(1:(xint-1),:)'.*rc(2:xint,:)')';
agl=acos(dot);

%multiply by the radius of the earth in Km

distance=6.36e6*agl; %e6 gives distance in meter; e3 gives  it in kilometers

%reshape the distance data into a yint x xint matrix

%distance=[zeros(yint,1);distance];
%dist=zeros(yint,xint);
%dist(:)=distance;
dist=[0;distance;];

%compute the total distance travelled along the transect from the distances
%between each successive pair of points

temp=dist';
i=find(isnan(temp)==1); %find the nans and replace with 0 to avoid having values past nan set to nan.
temp(i)=0;
distmat=(cumsum(temp));
trandist=real(distmat(1,:));

