! *****************************************************************************
    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
       REAL(KIND=dp), INTENT(IN) :: prefactor
       INTEGER, INTENT(IN) :: lp, la_max_local, lb_max_local, &
                              lb_min_local, la_min_local, maxl, &
                              lvab,hvab

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

       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_1(0:(@<la_max_local>@+@<lb_max_local>@),0:@<la_max_local>@,0:@<lb_max_local>@)
    REAL(KIND=dp) :: alpha_2(0:(@<la_max_local>@+@<lb_max_local>@),0:@<la_max_local>@,0:@<lb_max_local>@)
    REAL(KIND=dp) :: alpha_3(0:(@<la_max_local>@+@<lb_max_local>@),0:@<la_max_local>@,0:@<lb_max_local>@)
<$--(if (la_max_local == 0) and (lb_max_local==0))-->
<$--(else)-->
    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>@)
<$--(end)-->
    coef_xyz=coef_xyz*prefactor

<$--(if (la_max_local == 0) and (lb_max_local==0))-->
<$--(else)-->
  alpha_1(:,:,:)=0.0_dp
  alpha_2(:,:,:)=0.0_dp
  alpha_3(:,:,:)=0.0_dp

  <$--(for iaxis in range(1,3+1))-->
    <$--(if (lb_max_local>0))-->
         const_rp = rp(@<iaxis>@)-(ra(@<iaxis>@)+rab(@<iaxis>@))
    <$--(end)-->
    <$--(if (la_max_local>0))-->
         const_ra = (-ra(@<iaxis>@)+rp(@<iaxis>@))
    <$--(end)-->
   <$--(for lxa in range(0,la_max_local+1))-->
     <$--(for lxb in range(0,lb_max_local+1))-->
         binomial_k_lxa=1.0_dp
         a=1.0_dp
       <$--(for k in range(0,lxa+1))-->
          binomial_l_lxb=1.0_dp
          b=1.0_dp
         <$--(for l in range(0,lxb+1))-->
             alpha_@<iaxis>@(@<lxa-l+lxb-k>@,@<lxa>@,@<lxb>@)=alpha_@<iaxis>@(@<lxa-l+lxb-k>@,@<lxa>@,@<lxb>@)+ &
                               binomial_k_lxa*binomial_l_lxb*a*b
             <$--(if (lxb-l) == 0)-->
                 ! Removed here as lxb - l = 0
             <$--(elif (lxb-l) == (l+1))-->
                 ! binomial_l_lxb=binomial_l_lxb*1
                 b=b*const_rp
             <$--(elif (l+1) == 1)-->
                 binomial_l_lxb=binomial_l_lxb*REAL(@<lxb-l>@,dp)
                 b=b*const_rp
             <$--(else)-->
                 binomial_l_lxb=binomial_l_lxb*REAL(@<lxb-l>@,dp)/REAL(@<l+1>@,dp)
                 b=b*const_rp
             <$--(end)-->
         <$--(end)-->
          <$--(if (lxa-k) == 0)-->
             ! Removed  here as lxa-k == 0
          <$--(elif (lxa-k) == (k+1))-->
             a=a*const_ra
          <$--(elif (k+1) == 1)-->
             binomial_k_lxa=binomial_k_lxa*REAL(@<lxa-k>@,dp)
             a=a*const_ra
          <$--(else)-->
             binomial_k_lxa=binomial_k_lxa*REAL(@<lxa-k>@,dp)/REAL(@<k+1>@,dp)
             a=a*const_ra
          <$--(end)-->
       <$--(end)-->
     <$--(end)-->
   <$--(end)-->
  <$--(end)-->
<$--(end)-->

<$--(if (la_max_local+lb_max_local) == 0)-->
             jco=1 ! coset(0,0,0)
                ico=1 ! coset(0,0,0)
                vab(ico,jco)=vab(ico,jco)+coef_xyz(1)
<$--(elif (la_max_local+lb_max_local) == 1)-->
    lxyz=0
 <$--(for lzp in range(0,la_max_local+lb_max_local+1))-->
       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
    <$--(for lxb in range(0,lb_max_local+1))-->
     <$--(for lxa in range(0,la_max_local+1))-->
                coef_ttz(@<lxa>@,@<lxb>@)=coef_ttz(@<lxa>@,@<lxb>@)+coef_xyz(lxyz)*alpha_1(lxp,@<lxa>@,@<lxb>@)
     <$--(end)-->
    <$--(end)-->
          ENDDO

  <$--(for lyb in range(0,lb_max_local+1))-->
    <$--(for lya in range(0,la_max_local+1))-->
      <$--(for lxb in range(0,lb_max_local-lyb+1))-->
         <$--(for lxa in range(0,la_max_local-lya+1))-->
                coef_tyz(@<lxa>@,@<lxb>@,@<lya>@,@<lyb>@)=coef_tyz(@<lxa>@,@<lxb>@,@<lya>@,@<lyb>@)+coef_ttz(@<lxa>@,@<lxb>@)*alpha_2(lyp,@<lya>@,@<lyb>@)
         <$--(end)-->
      <$--(end)-->
    <$--(end)-->

  <$--(end)-->
       ENDDO


  <$--(for lzb in range(0,lb_max_local+1))-->
     <$--(for lza in range(0,la_max_local+1))-->
        <$--(for lyb in range(0,lb_max_local-lzb+1))-->
         <$--(for lya in range(0,la_max_local-lza+1))-->
            <$--(if (lb_max_local-lzb-lyb) > 0)-->
             <$--(if (lzb+lyb)==0)-->
              DO lxb=lb_min_local,@<lb_max_local>@
             <$--(else)-->
              DO lxb=MAX(lb_min_local-@<lzb>@-@<lyb>@,0),@<lb_max_local-lzb-lyb>@
             <$--(end)-->
             jco=coset(lxb,@<lyb>@,@<lzb>@)
            <$--(else)-->
             lxb = 0
             !jco=coset(0,@<lyb>@,@<lzb>@)
             jco=@<(lyb+lzb)*(lyb+lzb+1)*(lyb+lzb+2)/6+1+(lyb+lzb)*(lyb+lzb+1)/2+lzb>@
            <$--(end)-->

            <$--(if (la_max_local-lza-lya) > 0)-->
             <$--(if (lza+lya == 0))-->
              DO lxa=la_min_local,@<la_max_local>@
             <$--(else)-->
              DO lxa=MAX(la_min_local-@<lza>@-@<lya>@,0),@<la_max_local - lza - lya>@
             <$--(end)-->
                ico=coset(lxa,@<lya>@,@<lza>@)
            <$--(else)-->
                lxa = 0
                !ico=coset(0,@<lya>@,@<lza>@)
                ico=@<(lya+lza)*(lya+lza+1)*(lya+lza+2)/6+1+(lya+lza)*(lya+lza+1)/2+lza>@
            <$--(end)-->

                vab(ico,jco)=vab(ico,jco)+coef_tyz(lxa,lxb,@<lya>@,@<lyb>@)*alpha_3(@<lzp>@,@<lza>@,@<lzb>@)

            <$--(if (la_max_local-lza-lya) > 0)-->
             ENDDO
            <$--(end)-->

            <$--(if (lb_max_local-lzb-lyb) > 0)-->
             ENDDO
            <$--(end)-->
         <$--(end)-->
        <$--(end)-->
     <$--(end)-->
  <$--(end)-->

 <$--(end)-->
<$--(else)-->
    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
    <$--(for lxb in range(0,lb_max_local+1))-->
     <$--(for lxa in range(0,la_max_local+1))-->
                coef_ttz(@<lxa>@,@<lxb>@)=coef_ttz(@<lxa>@,@<lxb>@)+coef_xyz(lxyz)*alpha_1(lxp,@<lxa>@,@<lxb>@)
     <$--(end)-->
    <$--(end)-->
          ENDDO

  <$--(for lyb in range(0,lb_max_local+1))-->
    <$--(for lya in range(0,la_max_local+1))-->
      <$--(for lxb in range(0,lb_max_local-lyb+1))-->
         <$--(for lxa in range(0,la_max_local-lya+1))-->
                coef_tyz(@<lxa>@,@<lxb>@,@<lya>@,@<lyb>@)=coef_tyz(@<lxa>@,@<lxb>@,@<lya>@,@<lyb>@)+coef_ttz(@<lxa>@,@<lxb>@)*alpha_2(lyp,@<lya>@,@<lyb>@)
         <$--(end)-->
      <$--(end)-->
    <$--(end)-->
  <$--(end)-->
       ENDDO


  <$--(for lzb in range(0,lb_max_local+1))-->
     <$--(for lza in range(0,la_max_local+1))-->
        <$--(for lyb in range(0,lb_max_local-lzb+1))-->
         <$--(for lya in range(0,la_max_local-lza+1))-->
            <$--(if (lb_max_local-lzb-lyb) > 0)-->
             <$--(if (lzb+lyb)==0)-->
              DO lxb=lb_min_local,@<lb_max_local>@
             <$--(else)-->
              DO lxb=MAX(lb_min_local-@<lzb>@-@<lyb>@,0),@<lb_max_local-lzb-lyb>@
             <$--(end)-->
             jco=coset(lxb,@<lyb>@,@<lzb>@)
            <$--(else)-->
             lxb = 0
             !jco=coset(0,@<lyb>@,@<lzb>@)
             jco=@<(lyb+lzb)*(lyb+lzb+1)*(lyb+lzb+2)/6+1+(lyb+lzb)*(lyb+lzb+1)/2+lzb>@
            <$--(end)-->

            <$--(if (la_max_local-lza-lya) > 0)-->
             <$--(if (lza+lya == 0))-->
              DO lxa=la_min_local,@<la_max_local>@
             <$--(else)-->
              DO lxa=MAX(la_min_local-@<lza>@-@<lya>@,0),@<la_max_local - lza - lya>@
             <$--(end)-->

                ico=coset(lxa,@<lya>@,@<lza>@)
            <$--(else)-->
                lxa = 0
                !ico=coset(0,@<lya>@,@<lza>@)
                ico=@<(lya+lza)*(lya+lza+1)*(lya+lza+2)/6+1+(lya+lza)*(lya+lza+1)/2+lza>@
            <$--(end)-->

                vab(ico,jco)=vab(ico,jco)+coef_tyz(lxa,lxb,@<lya>@,@<lyb>@)*alpha_3(lzp,@<lza>@,@<lzb>@)

            <$--(if (la_max_local-lza-lya) > 0)-->
             ENDDO
            <$--(end)-->

            <$--(if (lb_max_local-lzb-lyb) > 0)-->
             ENDDO
            <$--(end)-->
         <$--(end)-->
        <$--(end)-->
     <$--(end)-->
  <$--(end)-->

   ENDDO
<$--(end)-->

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


