% [geo] = gei2geo(gei) transform gei coordinates into geo (xyz)
%
% Initial version: Magnus Billvik, Swedish Institute of Space Physics,
%Uppsala, June 2005
% Revised by: Niklas Edberg, Swedish Institute of Space Physics, Uppsala, Oct 2005

function [longitude, latitude] = geo2longlat(geo)

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

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

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