function points_llr = cart2llr(points_cart)

% cart2llr - convert cartesian coordinates to latitude longitude radius
%
%   points_cart: 3 x npoints matrix with XYZ coordinates
%   points_llr:  3 x npoints matrix with lat, long, rad (deg,deg,km)
%   Author: Mireia Leon Dasi

npoints = size(points_cart,2);
points_llr = zeros(3,npoints);

for i = 1:npoints
    x = points_cart(1,i);
    y = points_cart(2,i);
    z = points_cart(3,i);
    
    r = norm([x y z]);
    p = norm([x y 0]);
    
    lat = atan2d(z,p);
    long = atan2d(y,x);
    
    if long < 0
        long = long + 360;
    end
    
    points_llr(1,i) = lat;
    points_llr(2,i) = long;
    points_llr(3,i) = r;
end