      subroutine coeff_beta(idowhat,nx,nh,BETA,w,bph,q,bend)
      implicit none
!idowhat=1, allocate BETA to w, bph, q
!       =2, allocate w, bph, q to BETA
!
      integer k,i,nx,nh,j,idowhat
      double precision w(1:nx,1:nh),bph(nh),q(nh),
     &  bend,BETA(nx*nh+2*nh+1)
      if(idowhat.eq.1)then
        k=0
        do i=1,nx
          do j=1,nh
            k=k+1
            w(i,j)=BETA(k)
          enddo
        enddo
        do i=1,nh
          k=k+1
          bph(i)=BETA(k)
        enddo
        do i=1,nh
          k=k+1
          q(i)=BETA(k)
        enddo
        k=k+1
        bend=BETA(k)
      endif
!
      if(idowhat.eq.2)then
        k=0
        do i=1,nx
          do j=1,nh
            k=k+1
            BETA(k)=w(i,j)
          enddo
        enddo
        do i=1,nh
          k=k+1
          BETA(k)=bph(i)
        enddo
        do i=1,nh
          k=k+1
          BETA(k)=q(i)
        enddo
        k=k+1
        BETA(k)=bend
      endif
      return
      end
