      subroutine cnind(pp,cind,k,im,jm)

      implicit none
      integer,parameter::nm=6
      integer::k,im,jm
      real,dimension(k,k,nm,4,4,3)::pp
      integer,dimension(k,k,4,4,3)::cind
      real,dimension(3)::xc
      real,dimension(2)::xm
      real,dimension(2,3)::dxmdxc
      integer::i,j,n,c1,c2,n1

      do i=1,k
      do j=1,k
      do c1=1,4
      do c2=1,4
         xc=pp(i,j,1,c1,c2,:)
	 call xctoxm(xc,xm,dxmdxc,n1)
	 cind(i,j,c1,c2,3)=n1
	 cind(i,j,c1,c2,1)=int(0.5*(xm(1)+1)*(im-1)+1)-1
	 cind(i,j,c1,c2,2)=int(0.5*(xm(2)+1)*(im-1)+1)-1
	 if(cind(i,j,c1,c2,3).eq.2)then
	   cind(i,j,c1,c2,1)=cind(i,j,c1,c2,1)+im-1
         else if(cind(i,j,c1,c2,3).eq.4)then
	   cind(i,j,c1,c2,1)=cind(i,j,c1,c2,1)-im+1
         else if(cind(i,j,c1,c2,3).eq.5)then
	   cind(i,j,c1,c2,2)=cind(i,j,c1,c2,2)+im-1
         else if(cind(i,j,c1,c2,3).eq.6)then
	   cind(i,j,c1,c2,2)=cind(i,j,c1,c2,2)-im+1
         endif

	 cind(i,j,c1,c2,3)=1
	 if(cind(i,j,c1,c2,1).lt.0)then
	   cind(i,j,c1,c2,1)=0
         else if(cind(i,j,c1,c2,1).gt.(im-2))then
	   cind(i,j,c1,c2,1)=im-2
         endif

	 if(cind(i,j,c1,c2,2).lt.0)then
	   cind(i,j,c1,c2,2)=0
         else if(cind(i,j,c1,c2,2).gt.(jm-2))then
	   cind(i,j,c1,c2,2)=jm-2
         endif
       enddo
       enddo
       enddo
       enddo

       cind(1,1,1,1,1)=0
       cind(1,1,1,1,2)=0
       cind(1,1,1,4,1)=1
       cind(1,1,1,4,2)=1

       cind(k,1,2,2,1)=im-2
       cind(k,1,2,2,2)=0
       cind(k,1,2,3,1)=im-3
       cind(k,1,2,3,2)=1

       cind(1,k,3,3,1)=0
       cind(1,k,3,3,2)=jm-2
       cind(1,k,3,2,1)=1
       cind(1,k,3,2,2)=jm-3

       cind(k,k,4,4,1)=im-2
       cind(k,k,4,4,2)=jm-2
       cind(k,k,4,1,1)=im-3
       cind(k,k,4,1,2)=jm-3

       end subroutine cnind
