cart_to_latlon Subroutine

private subroutine cart_to_latlon(np, q, xs, ys)

Arguments

Type IntentOptional Attributes Name
integer, intent(in) :: np
real(kind=ESMF_KIND_R8), intent(inout) :: q(3,np)
real(kind=ESMF_KIND_R8), intent(inout) :: xs(np)
real(kind=ESMF_KIND_R8), intent(inout) :: ys(np)

Source Code

 subroutine cart_to_latlon(np, q, xs, ys)
! vector version of cart_to_latlon1
  integer, intent(in):: np
  real(ESMF_KIND_R8), intent(inout):: q(3,np)
  real(ESMF_KIND_R8), intent(inout):: xs(np), ys(np)
! local
  real(ESMF_KIND_R8), parameter:: esl=1.e-10
  real(ESMF_KIND_R8) :: p(3)
  real(ESMF_KIND_R8) :: dist, lat, lon
  integer i,k

  do i=1,np
     do k=1,3
        p(k) = q(k,i)
     enddo
     dist = sqrt(p(1)**2 + p(2)**2 + p(3)**2)
     do k=1,3
        p(k) = p(k) / dist
     enddo

     if ( (abs(p(1))+abs(p(2)))  < esl ) then
          lon = 0.
     else
          lon = atan2( p(2), p(1) )   ! range [-pi,pi]
     endif

     if ( lon < 0.) lon = 2.*pi + lon
     lat = asin(p(3))
     
     xs(i) = lon
     ys(i) = lat
! q Normalized:
     do k=1,3
        q(k,i) = p(k)
     enddo
  enddo

 end  subroutine cart_to_latlon