      subroutine lsdvtor(vtor,m,nu,eigval,n,np,
     :                  bvec,parity)

C***************************************************
C
C This subroutine sets up a vector vhor which is used as
C a set of expansion coefficients in the calculation
C of returns the toriodal velocity function of
C Lee * Saio's Lambda basis (ApJ 1990). The toroidal velocity field
C of an NRP eigenstate is then the curl
C of this function.
C The routine uses the eigenvector for the mode in question bvec,
C and also the eigenvector eigval.
C See lsdftor for details...
C
C Started:3/7/95
C Modified:4/12/95
C
C (c) Richard Townsend 1995
C
C*****************************************************

      implicit none		! Turn off implicit typing

C Declare the passed variables

      double precision eigval	! The eigenvalue corresponding to bvec
      double precision nu	! The roation parameter to use
      integer m			! The m-value to use
      integer n			! The number of terms to sum over
      integer np		! The total size of bvec
      double precision bvec(np)	! The column vector containg the eigenvector
      double precision vtor(np) ! The column vector to contain the expansion coefficients
      logical parity		! The parity to be used - .true.=even

C Declare the in-program variables

      integer j			! The coefficient index counter
      integer kj		! =|m|+2(j-1), used to simplify stuff
      integer am		! The absoulte value of m
      double precision parfac	! The partial factorial (l+|m|)...(l-|m|+1)
      double precision lsdJml	! The external Jml function

C Set up am=|m|
                             
      am=iabs(m)      

C First initialise the coefficients. We have to do the
C j=1 case separately because the general term for the coeffs
C involves bvec(j-1), which is undefined for j=1.

      if(parity) then
        vtor(1)=eigval*(bvec(1)/(am*(am+1))*nu*am/(am+1)*
     :          lsdJml(m,am+1) + bvec(2)/((am+2)*(am+3))*nu*
     :          (am+3)/(am+2)*lsdJml(m,am+2) )/ 
     :          (1-m*nu/((am+1)*(am+2)) )*
     :          dsqrt((am+1.5D0)/
     :          parfac(am+1,am))
      else
       vtor(1)=eigval*(bvec(1)/((am+1)*(am+2))*nu*
     :          (am+2)/(am+1)*lsdJml(m,am+1) )/
     :          (1-m*nu/(am*(am+1)) )*
     :          dsqrt((am+0.5D0)/
     :          parfac(am,am))
      endif

C And now loop over j to get the other coeffs (except for the last bit)

      do j=2,n-1

        kj=am+2*(j-1)

       if(parity) then
         vtor(j)=eigval*( bvec(j)/((am+2*(j-1))*
     :    (am+2*(j-1)+1))*nu*kj/(kj+1)*
     :    lsdJml(m,kj+1) + bvec(j+1)/
     :    ((am+2*j)*(am+2*j+1))*nu*(kj+3)/
     :    (kj+2)*lsdJml(m,kj+2) )/
     :    (1-m*nu/((am+1+2*(j-1))*
     :    (am+2+2*(j-1))) )*
     :    dsqrt((am+2*(j-1)+1.5D0)/
     :    parfac(am+1+2*(j-1),am))
        else
          vtor(j)=eigval*( bvec(j)/((am+1+2*(j-1))*
     :     (am+2+2*(j-1)))*nu*(kj+2)/(kj+1)*
     :     lsdJml(m,kj+1) + bvec(j-1)/
     :     ((am+2*(j-2)+1)*(am+2*(j-2)+2))*nu*(kj-1)/
     :     kj*lsdJml(m,kj) )/
     :     (1-m*nu/((am+2*(j-1))*
     :     (am+1+2*(j-1))) )*
     :     dsqrt((am+2*(j-1)+0.5D0)/
     :     parfac(am+2*(j-1),am))
        endif

C Its as simple as that - now loop back around....

      enddo

C Now do the final bit of the sum - check that j=n first

      j=n

C Do the stuff, cutting out the bit with bvec(j+1)

        kj=am+2*(j-1)

       if(parity) then
         vtor(j)=eigval*( bvec(j)/((am+2*(j-1))*
     :    (am+2*(j-1)+1))*nu*kj/(kj+1)*
     :    lsdJml(m,kj+1) )/
     :    (1-m*nu/((am+1+2*(j-1))*
     :    (am+2+2*(j-1))) )*
     :    dsqrt((am+2*(j-1)+1.5D0)/
     :    parfac(am+1+2*(j-1),am))
        else
          vtor(j)=eigval*( bvec(j)/((am+1+2*(j-1))*
     :     (am+2+2*(j-1)))*nu*(kj+2)/(kj+1)*
     :     lsdJml(m,kj+1) + bvec(j-1)/
     :     ((am+2*(j-2)+1)*(am+2*(j-2)+2))*nu*(kj-1)/
     :     kj*lsdJml(m,kj) )/
     :     (1-m*nu/((am+2*(j-1))*
     :     (am+1+2*(j-1))) )*
     :     dsqrt((am+2*(j-1)+0.5D0)/
     :     parfac(am+2*(j-1),am))
        endif

C As a final check, we zero all the other coeffs..

        do j=n+1,np
          vtor(j)=0.D0
        enddo

C Thats all folks - return...

      return

      end
