!&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
       subroutine octa2llh( h, hg, lmax) 
!      *****************************************************************
!      *                                                               *
!      *   Set scalar array for graphical output on cubic grid         *
!      *                                                               *
!      *****************************************************************
!----------------------------------------------------------------------
       USE mod_graph
      implicit none
      include 'param_o.h'
!----------------------------------------------------------------------
       real,parameter::one=1.
!-----------------------------------------------------------------------
       integer::lmax,i,j,l
       real,dimension(0:im+1,0:jm+1,nm,lmax)::h
       real,dimension(igm,jgm,lmax)::hg
       real::xmap,ymap,xmap0,xsi,eta,omxsi,ometa
       integer::kmap,ind,il,jl
       real,parameter::d1r3=1./3.,d2r3=2.*d1r3,tri=3.
       logical::lx1,lx2,lx3,ly1,ly2,ly3
       real::x1a(4),x2a(4),ya(4,4),dy

	x1a=(/-1.,0.,1.,2./)
	x2a=(/-1.,0.,1.,2./)

!-----------------------------------------------------------------------
!test
!
! Find HG
!
      Do j=1,jgm
      Do i=1,igm
        xmap=XMP0(i,j)
        ymap=YMP0(i,j)
        kmap=KMP0(i,j)

	lx1=xmap.lt.d1r3
	lx3=xmap.ge.d2r3
	lx2=.not.(lx1.or.lx3)
	ly1=ymap.lt.d1r3
	ly3=ymap.ge.d2r3
	ly2=.not.(ly1.or.ly3)

      If(kmap.gt.0) Then	
	
	If(ly1) Then
	    If(lx1) Then
	      ind=6
	    Else If(lx2) Then
	      ind=7
	      xmap=xmap-d1r3
            Else
	      ind=8
	      xmap=xmap-d2r3
	    End If
	Else If(ly2) Then
	  ymap=ymap-d1r3
	    If(lx1) Then
	      ind=9
	    Else If(lx2) Then
	      ind=10
	      xmap=xmap-d1r3
	    Else
	       ind=11
	       xmap=xmap-d2r3
            End If
	Else 
	  ymap=ymap-d2r3
	    If(lx1) Then
	      ind=12
	    Else If(lx2) Then
	      ind=13
	      xmap=xmap-d1r3
	    Else
	      ind=14
	      xmap=xmap-d2r3
	    End If
	End If

      Else

	If(ly1) Then
	    If(lx1) Then
	      ind=8
	        xmap0=ymap
              ymap=d1r3-xmap
	      xmap=xmap0
	    Else If(lx2) Then
	      ind=1
	      xmap=xmap-d1r3
	    Else
	      ind=6
		xmap0=d1r3-ymap
	      ymap=xmap-d2r3
	      xmap=xmap0
	    End If
	Else If(ly2) Then
	  ymap=ymap-d1r3
	    If(lx1) Then
	      ind=2
	    Else If(lx2) Then
	      ind=3
	      xmap=xmap-d1r3
	    Else
	      ind=4
	      xmap=xmap-d2r3
	    End If
	Else
	    If(lx1) Then
	      ind=14
		xmap0=one-ymap
              ymap=xmap
	      xmap=xmap0
	    Else If(lx2) Then
	      ind=5
    	      xmap=xmap-d1r3
	      ymap=ymap-d2r3
	    Else
	      ind=12
		xmap0=ymap-d2r3
              ymap=one-xmap
	      xmap=xmap0
	    End If
	End If

      End If

	xmap=tri*xmap
	ymap=tri*ymap
	xsi=xmap*(im-1)
	eta=ymap*(jm-1)
	  il=xsi+1
	  jl=eta+1
        xsi=xsi-il+1
	eta=eta-jl+1
	omxsi=one-xsi
	ometa=one-eta
!	if(il.eq.1.or.il.eq.im-1.or.jl.eq.1.or.jl.eq.jm-1)then
	  HG(i,j,:)=H(il,jl,ind,:)*omxsi*ometa+H(il+1,jl,ind,:)*xsi*ometa  &
                 +H(il,jl+1,ind,:)*omxsi*eta+H(il+1,jl+1,ind,:)*xsi*eta
!        else
!	  do l=1,lmax
!          ya=H(il-1:il+2,jl-1:jl+2,ind,l)
!	   call polin2(x1a,x2a,ya,4,4,xsi,eta,hg(i,j,l),dy)
!	   if(l.eq.13)then
!	   print *,i,j,l,il,jl,ind,hg(i,j,l),h(il,jl,ind,l),dy,xsi,eta
!	   endif
!          enddo
!        endif

       if(i.eq.141.and.j.eq.53)then
!           print *,i,j,il,jl,ind
!          print *,i,j,il,jl,ind,H(il,jl,ind,:)/9.81,H(il,jl+1,ind,:)/9.81,   &
!	         H(il+1,jl,ind,:)/9.81,H(il+1,jl+1,ind,:)/9.81,xsi+il,eta+jl
!           stop
       endif
      End Do
      End Do
!-----------------------------------------------------------------------
                               Return
                               End
