function [ur,vr,thetad]=rotate(u,v)
%function [ur,vr,thetad]=rotate(u,v)
%           Rotates into direction of maximum variance
%     u is vector velocity, ur is rotated vector velocity
%	 direction of theta is counterclockwise, based on E vel 1st, N vel second
%
if any( (u(:)==nan)|(v(:)==nan) )
     disp('NaNs found')
	ur=ones(u)*nan;
	vr=ur;
     g=find( (u(:)~=nan)&(v(:)~=nan) );
     u=u(g);
	v=v(g);
else
     g=1:length(u(:));
end
cv=cov([u(:) v(:)]);
theta=0.5*atan2(2.*cv(2,1),(cv(1,1)-cv(2,2)) );
thetad=theta*180./pi
vr(g)=-u*sin(theta)+v*cos(theta);
ur(g)=u*cos(theta)+v*sin(theta);
