subroutine gnomonic_ed(im, lamda, theta)
!-----------------------------------------------------
! Equal distance along the 4 edges of the cubed sphere
!-----------------------------------------------------
! Properties:
! * defined by intersections of great circles
! * max(dx,dy; global) / min(dx,dy; global) = sqrt(2) = 1.4142
! * Max(aspect ratio) = 1.06089
! * the N-S coordinate curves are const longitude on the 4 faces with equator
! For C2000: (dx_min, dx_max) = (3.921, 5.545) in km unit
! This is the grid of choice for global cloud resolving
integer, intent(in):: im
real(ESMF_KIND_R8), intent(out):: lamda(im+1,im+1)
real(ESMF_KIND_R8), intent(out):: theta(im+1,im+1)
! Local:
real(ESMF_KIND_R8) pp(3,im+1,im+1)
real(ESMF_KIND_R8) p1(2), p2(2)
real(ESMF_KIND_R8):: rsq3, alpha, delx, dely
integer i, j, k
rsq3 = 1./sqrt(3.)
alpha = asin( rsq3 )
! Ranges:
! lamda = [0.75*pi, 1.25*pi]
! theta = [-alpha, alpha]
dely = 2.*alpha / real(im,ESMF_KIND_R8)
! Define East-West edges:
do j=1,im+1
lamda(1, j) = 0.75*pi ! West edge
lamda(im+1,j) = 1.25*pi ! East edge
theta(1, j) = -alpha + dely*real(j-1,ESMF_KIND_R8) ! West edge
theta(im+1,j) = theta(1,j) ! East edge
enddo
! Get North-South edges by symmetry:
do i=2,im
call mirror_latlon(lamda(1,1), theta(1,1), lamda(im+1,im+1), theta(im+1,im+1), &
lamda(1,i), theta(1,i), lamda(i,1), theta(i, 1) )
lamda(i,im+1) = lamda(i,1)
theta(i,im+1) = -theta(i,1)
enddo
! Set 4 corners:
call latlon2xyz2(lamda(1 , 1), theta(1, 1), pp(1, 1, 1))
call latlon2xyz2(lamda(im+1, 1), theta(im+1, 1), pp(1,im+1, 1))
call latlon2xyz2(lamda(1, im+1), theta(1, im+1), pp(1, 1,im+1))
call latlon2xyz2(lamda(im+1,im+1), theta(im+1,im+1), pp(1,im+1,im+1))
! Map edges on the sphere back to cube:
! Intersections at x=-rsq3
i=1
do j=2,im
call latlon2xyz2(lamda(i,j), theta(i,j), pp(1,i,j))
pp(2,i,j) = -pp(2,i,j)*rsq3/pp(1,i,j)
pp(3,i,j) = -pp(3,i,j)*rsq3/pp(1,i,j)
enddo
j=1
do i=2,im
call latlon2xyz2(lamda(i,j), theta(i,j), pp(1,i,1))
pp(2,i,1) = -pp(2,i,1)*rsq3/pp(1,i,1)
pp(3,i,1) = -pp(3,i,1)*rsq3/pp(1,i,1)
enddo
do j=1,im+1
do i=1,im+1
pp(1,i,j) = -rsq3
enddo
enddo
do j=2,im+1
do i=2,im+1
! Copy y-z face of the cube along j=1
pp(2,i,j) = pp(2,i,1)
! Copy along i=1
pp(3,i,j) = pp(3,1,j)
enddo
enddo
call cart_to_latlon( (im+1)*(im+1), pp, lamda, theta)
end subroutine gnomonic_ed