      subroutine lltocb(xlon,ylat,i,j,n,k,ks1,ks2)
      use cugen, only: xctoxm

      implicit none
      include 'param_o.h'

      real::xlon,ylat,xmap,ymap,xmap0,xsi,eta,csgm,sngm
      integer::i,j,n,k,kmap,ind,il,jl,ks1,ks2
      real,dimension(3)::xe
      real,dimension(2)::xm
      real,dimension(2,3)::dxmdxc
      real::csal,snal,snbt,csbt,x2,x1,x0,y2,y1,y0,z2,z1,z0

       csal = Cos (alfa)  
       snal = Sin (alfa)  
       csbt = Cos (beta)  
       snbt = Sin (beta)  
      csgm=cos(gamm)
      sngm=sin(gamm)

       x2 = Cos (ylat) * Cos (xlon)  
       y2 = Cos (ylat) * Sin (xlon)  
       z2 = Sin (ylat)  
        x0 = x2 * csgm - y2 * sngm
        y0 = x2 * sngm + y2 * csgm
        z0 = z2  
       x1 = x0  
       y1 = y0 * csbt - z0 * snbt  
       z1 = y0 * snbt + z0 * csbt  
       x0 = x1 * csal - y1 * snal  
       y0 = x1 * snal + y1 * csal  
       z0 = z1  
       xe(1)=x0
       xe(2)=y0
       xe(3)=z0

       call xctoxm(xe,xm,dxmdxc,kmap)  

        xmap=(1.+xm(1))*0.5
        ymap=(1.+xm(2))*0.5

        xsi=xmap*(im-1)
        eta=ymap*(jm-1)
        il=xsi+1
        jl=eta+1
        xsi=xsi-il+1
        eta=eta-jl+1

!TEMP----------------------------------------------
        xsi=max(xsi,0.0)
        eta=max(eta,0.0)
!TEMP----------------------------------------------

        n=kmap

        if(xsi.lt.0.5.and.eta.lt.0.5)then
          i=il
          j=jl
          k=3
        else if(xsi.lt.0.5.and.eta.ge.0.5)then
          i=il
          j=jl+1
          k=1
        else if(xsi.ge.0.5.and.eta.lt.0.5)then
          i=il+1
          j=jl
          k=2
        else
          i=il+1
          j=jl+1
          k=4
        endif

        ks1=xsi*4.+1
        ks2=eta*4.+1

!TEST#####################################################
     if(ks2==0) then
       print *,'LLTOCB: ymap,xm(2),jm,eta,n,ks2=', &
                ymap,xm(2),jm,eta,n,ks2
     end if
!TEST#####################################################
        

        end subroutine lltocb
