function [X,Y,uX,vY,p]=ll2stereo_vel(lon,lat,uE,vN,e,R,clon,clat)
%-----------------------------------------------------------------------
% function [X,Y,uX,vY,p]=ll2stereo_vel(lon,lat,uE,vN,e,R,clon,clat)
% 
% function to project local velocities [uE,vN] at locations
% [lon,lat] into stereographic projection centered at [clon,clat]
%
% INPUT: 
%   lon & lat: long and lat of velocity locations, size [Mx1], [Mx1]
%   uE & vN: East and North vel components, size [Mx1] and [Mx1]
%   e: eccentricity of ellipsoid, e.g., 0.081816153
%   R: equatorial radius of ellipsoid, e.g., 6378.273e3
%   [clon,clat]: center [lon,lat] of projection, e.g., [120,70];
% OUTPUT:
%   [X,Y]: x- and y- locations relative to center, [Mx1], [Mx1]
%   [uX,vY]: x- and y- components of projected velocities, [Mx1], [Mx1]
%   p: length distortion, 1 = length preseved, < 1 = [uX,uY] shrink
%
% functions used: get_latlon.m
% ATN 11.20.06
%------------------------------------------------------------------------

% Defining some constants:
conversion=R*pi/180;

% Calculating length and angles from north of velocity vectors:
%le=sqrt(uE.^2+vN.^2);theta=atan2(uE,vN).*180/pi;
le=sqrt(uE.^2+vN.^2);theta=90-atan2(vN,uE).*180/pi;

% Get end-points of velocity vectors:
ptB = get_latlon(theta,le./conversion,[lon,lat,zeros(size(lon))]);

% defining map projection:
mstruct=defaultm('stereo');
mstruct.origin=[clat,clon,0];
mstruct.geoid=[R,e];
mstruct.maplatlimit=[max(clat-10,-90),min(clat+10,90)];
mstruct.mapparallels=clat;
%mstruct.scalefactor=.9960922;
mstruct=defaultm(mstruct);

% Now projecting:
[X,Y]=mfwdtran(mstruct,lat,lon);
[xB,yB]=mfwdtran(mstruct,ptB(:,2),ptB(:,1));

uX=xB-X;
vY=yB-Y;

% calculating length distortions:
l1=sqrt(uX.^2+vY.^2);
[p,q]=polyfit(le,l1,1);lef=polyval(p,le);

return
