% [longitude, latitude] = meo2longlat(meo) transform meo=mrx (xyz) coordinates into meo (longlat)
%
%Initial version: Niklas Edberg, Swedish Institute of Space Physics, Uppsala, Oct 2005

function [longitude, latitude] = meo2longlat(meo)

longitude=ones(1,length(meo));    % declare the variabel before to speed up the loop!
latitude=ones(1,length(meo));    

rho=sqrt(meo(1,:).^2+meo(2,:).^2);
latitude=atan2(meo(3,:),rho).*180./pi;

%r=sqrt(meo(1,:).^2+meo(2,:).^2+meo(3,:).^2);

for i=1:length(meo)
    longitude(i)=acos(meo(1,i)/rho(i))*180/pi;
    if meo(2,i)<=0
        longitude(i)=360-longitude(i);
    end
    if longitude(i)>360
        longitude(i)=longitude(i)-360;
    end
end