%                gridell.m
%                             for generating ellipse from grid data
im=sqrt(-1);
g=find(umag(:)~=nan);
ell=ones(length(umag(:)),4)*nan;

%         first convert back to sine, cosine
ucomplex=exp(log(umag(g))+im*uphase(g)*pi/180);
vcomplex=exp(log(vmag(g))+im*vphase(g)*pi/180);
ellipse(real(ucomplex),imag(ucomplex),real(vcomplex),imag(vcomplex));
for i=1:4
    ell(g,i)=ans(:,i);
end


major=remat(ell(:,1),umag);
minor=remat(ell(:,2),umag);
orient=ones(major)*90-remat(ell(:,3),umag);
phase=remat(ell(:,4),umag);
switch=g(find(orient(g)<-70));
orient(switch)=orient(switch)+ones(switch)*180;
phase(switch)=phase(switch)-ones(switch)*180;
switch=g(find(phase(g)>180));
phase(switch)=phase(switch)-ones(switch)*360;
