cartesian_to_spherical Subroutine

public pure subroutine cartesian_to_spherical(rvec, r, alpha, beta)

Convert Cartesian (x,y,z) to spherical (r,alpha,beta).

Arguments

Type IntentOptional Attributes Name
real(kind=wp), intent(in), dimension(3) :: rvec

[x,y,z] vector

real(kind=wp), intent(out) :: r

magnitude

real(kind=wp), intent(out) :: alpha

right ascension [rad]

real(kind=wp), intent(out) :: beta

declination [rad]


Source Code

    pure subroutine cartesian_to_spherical(rvec,r,alpha,beta)

    implicit none

    real(wp),dimension(3),intent(in) :: rvec     !! [x,y,z] vector
    real(wp),intent(out)             :: r        !! magnitude
    real(wp),intent(out)             :: alpha    !! right ascension [rad]
    real(wp),intent(out)             :: beta     !! declination [rad]

    real(wp) :: r1

    r1 = rvec(1)*rvec(1)+rvec(2)*rvec(2)
    r  = sqrt(r1+rvec(3)*rvec(3))

    if (r/=zero) then
        beta = atan2(rvec(3),sqrt(r1))
        if (r1/=zero) then
            alpha = atan2(rvec(2),rvec(1))
        else
            alpha = zero
        end if
    else
        alpha = zero
        beta = zero
    end if

    end subroutine cartesian_to_spherical