function [az,elev,r] = cart2sphere(x,y,z)
%cart2sphere Transform Cartesian to spherical coordinates.
%   [TH,PHI,R] = CART2SPH(X,Y,Z) transforms corresponding elements of
%   data stored in Cartesian coordinates X,Y,Z to spherical
%   coordinates (azimuth TH, elevation PHI, and radius R).  The arrays
%   X,Y, and Z must be the same size (or any of them can be scalar).
%   TH and PHI are returned in radians.
%
%   TH is the counterclockwise angle in the xy plane measured from the
%   positive x axis.  PHI is the elevation angle from the xy plane.
%
% Initial version: Magnus Billvik, Swedish Institute of Space Physics,
% Uppsala, June 2005

r = sqrt(x.^2+y.^2+z.^2);
elev = atan2(y,sqrt(z.^2+x.^2));
% az = atan2(x,z);
az = atan2(-z,x);