! *****************************************************************************
    SUBROUTINE xyz_to_vab_@<la_max_local>@_@<lb_max_local>@ (prefactor, coef_xyz, & 
                           lp, la_max_local, lb_max_local, rp, ra, rab, vab, coset,&
                           la_min_local, lb_min_local, maxl, lvab, hvab)
 

      USE kinds,                        ONLY: dp
       ! PARAMETERS
       INTEGER, INTENT(IN) :: lp, la_max_local, lb_max_local, & 
                               maxl, lb_min_local, la_min_local, &
                               lvab,hvab

       INTEGER,  DIMENSION(-1:maxl,-1:maxl,-1:maxl), &
                       INTENT(IN)     :: coset

       REAL(KIND=dp), INTENT(IN) :: prefactor
       
       REAL(KIND=dp),  DIMENSION(((lp+1)*(lp+2)*(lp+3))/6),&
                         INTENT(INOUT) :: coef_xyz
       REAL(KIND=dp), DIMENSION(lvab,hvab), INTENT(INOUT) :: vab
       REAL(KIND=dp), DIMENSION(3), INTENT(IN) :: rp, ra, rab
   
       ! LOCAL
       INTEGER :: ico,jco,lxa,lxb,lxp,lxyz,lyp,lzp, iaxis, l, k, lya,lyb,lza,lzb
       REAL(KIND=dp) :: a,b,binomial_k_lxa,binomial_l_lxb,const_rp,const_ra
 

    REAL(KIND=dp) :: alpha(0:(@<la_max_local>@+@<lb_max_local>@),0:@<la_max_local>@,0:@<lb_max_local>@,3)
    REAL(KIND=dp) :: coef_ttz(0:@<la_max_local>@,0:@<lb_max_local>@)
    REAL(KIND=dp) :: coef_tyz(0:@<la_max_local>@,0:@<lb_max_local>@,0:@<la_max_local>@,0:@<lb_max_local>@)
    
    coef_xyz=coef_xyz*prefactor

    alpha(:,:,:,:)=0.0_dp

    <$--(for iaxis in range(1,3+1))-->
    DO lxa=0,@<la_max_local>@
    DO lxb=0,@<lb_max_local>@
       binomial_k_lxa=1.0_dp
       a=1.0_dp
       DO k=0,lxa
        binomial_l_lxb=1.0_dp
        b=1.0_dp
        DO l=0,lxb
           alpha(lxa-l+lxb-k,lxa,lxb,@<iaxis>@)=alpha(lxa-l+lxb-k,lxa,lxb,@<iaxis>@)+ &
                             binomial_k_lxa*binomial_l_lxb*a*b
           binomial_l_lxb=binomial_l_lxb*REAL(lxb-l,dp)/REAL(l+1,dp)
           b=b*(rp(@<iaxis>@)-(ra(@<iaxis>@)+rab(@<iaxis>@)))
        ENDDO
        binomial_k_lxa=binomial_k_lxa*REAL(lxa-k,dp)/REAL(k+1,dp)
        a=a*(-ra(@<iaxis>@)+rp(@<iaxis>@))
       ENDDO
    ENDDO
    ENDDO
    <$--(end)-->

    lxyz=0
    DO lzp=0,@<la_max_local + lb_max_local>@
       coef_tyz=0.0_dp
       DO lyp=0,@<la_max_local + lb_max_local>@-lzp
          coef_ttz=0.0_dp
          DO lxp=0,@<la_max_local + lb_max_local>@-lzp-lyp
             lxyz=lxyz+1
             DO lxb=0,@<lb_max_local>@
             DO lxa=0,@<la_max_local>@
                coef_ttz(lxa,lxb)=coef_ttz(lxa,lxb)+coef_xyz(lxyz)*alpha(lxp,lxa,lxb,1)
             ENDDO
             ENDDO

          ENDDO ! lxp

          DO lyb=0,@<lb_max_local>@
          DO lya=0,@<la_max_local>@
             DO lxb=0,@<lb_max_local>@-lyb
             DO lxa=0,@<la_max_local>@-lya
                coef_tyz(lxa,lxb,lya,lyb)=coef_tyz(lxa,lxb,lya,lyb)+coef_ttz(lxa,lxb)*alpha(lyp,lya,lyb,2)
             ENDDO
             ENDDO
          ENDDO
          ENDDO

       ENDDO !lyp

       DO lzb=0,@<lb_max_local>@
       DO lza=0,@<la_max_local>@
          DO lyb=0,@<lb_max_local>@-lzb
          DO lya=0,@<la_max_local>@-lza
             DO lxb=MAX(lb_min_local-lzb-lyb,0),@<lb_max_local>@-lzb-lyb
             jco=coset(lxb,lyb,lzb)
             DO lxa=MAX(la_min_local-lza-lya,0),@<la_max_local>@-lza-lya
                ico=coset(lxa,lya,lza)
                vab(ico,jco)=vab(ico,jco)+coef_tyz(lxa,lxb,lya,lyb)*alpha(lzp,lza,lzb,3)
             ENDDO
             ENDDO
          ENDDO
          ENDDO
       ENDDO
       ENDDO

    ENDDO

    END SUBROUTINE xyz_to_vab_@<la_max_local>@_@<lb_max_local>@
