function [az, el, r] = ctos(x,y,z)
%function ctos(x,y,z,az,el,r)
% function ctos calculate azimuth, elevation and distance from x, y, and z
%  *              x       - x coordinate of image-model reflection
%  *              y       - y coordinate of image-model reflection
%  *              z       - z coordinate of image-model reflection
%  *              az      - azimuth incidence angles (degrees)
%  *              el      - elevation incidence angles (degrees)
%  *              r       - distance (m??)
% Translate from C code to matlab by Dennis Park 15.May.2014

    %need to make this values as Global
    DPR = 57.29578;            % degrees per radian conversion  
    TINY = 0.1^30;             % no zero dividing!              

    r = sqrt( x*x + y*y + z*z );
    el = asin( z/r ) * DPR;
    if z == 0;
        el = 0;
    end
    
% 	if x == 0
%         x = TINY;
%     end
    
    rad = atan ( y / x );
    
	if x > 0
        az = 90 - ( rad * DPR );
    elseif x < 0
        az = 270 - ( rad * DPR );
    else 
        az = 0;
    end
end