      function lsdfdrad(m,ctheta,n,np,
     :                  vrad,parity)

C***************************************************
C
C This function returns the derivative wrt theta of
C the radial velocity function lsdfrad
C of Lee & Saio's Lambda basis (ApJ 1990)
C This routine calculates the derivative of the eigenstate
C for an azimuthal quantum number m and a corresponding
C set of expansion coefficients vrad(1:n) which has physical dimension np.
C These coeffs are previously calculated using lsdvrad.
C parity defines whether this is done for odd or even modes.
C
C This function now adheres to the Condon & Shortley phase convention
C
C Stated:30/3/95
C Last Modified:
C
C   29/11/00 - Fixed problems calculating m=0 and j>1 modes
C
C
C (c) Richard Townsend 1995,2000
C
C*****************************************************

      implicit none		! Turn off implicit typing

C Declare the passed variables

      double precision lsdfdrad	! The returned function
      double precision ctheta	! The cos(theta) value to use
      integer m			! The m-value to use
      integer n			! The number of terms to sum over
      integer np		! The total size of vhor
      double precision vrad(np)	! The column vector containg the coefficients
      logical parity		! The parity to be used - .true.=even

C Declare the in-program variables

      integer j			! The summation counter
      integer lj    		! Holds l-values for Pml's
      integer lj1		! Holds l-values for Pml1's
      integer am		! The absoulte value of m
      integer am1		! am+1
      integer CSPhas		! The Condon & Shortley phase adjustor
      integer CSPha2		! A second phase adjustor
      double precision Pml	! The value of the Assoc. Leg. Poly, l=lj
      double precision Pml1	! The value of the Assoc. Leg. Poly, l=lj+1
      double precision Pm1l	! The value of the Pml for m=m+1
      double precision Pm1l1    ! The value of the Pml1 for m=m+1
      double precision stheta	! Sine of theta
      double precision rtheta   ! value of 1/sin(theta) ; =1.D0 if theta=0
      double precision oddfac	! The product of all odd integers lt (2m-1)

C Set up am=|m| and am1
                             
      am=iabs(m)      
      am1=am+1

C Set up CSPhas & CSPha2

      CSPhas=(-1)**((am-m)/2)
      CSPha2=-sign(1,m)
                       	
C Initialise the staring values of Pml,Pm1l, Pml1 and Pm1l1.
C oddfac has to be used along the way for Pml,Pm1l - see
C Numerical Recipies 2nd Ed p247

      stheta=dsqrt((1.D0-ctheta)*(1.D0+ctheta))
      oddfac=1.D0

      Pml=1.D0

      if (am.gt.0) then
        do j=1,am
          Pml=-Pml*oddfac*stheta
          oddfac=oddfac+2.D0
        enddo

      endif

C Fix the phase of Pml to that of Condon & Shortley

      Pml=Pml*CSPhas

C Get Pm1l1 from Pml. Note that Pm1l1's initial l value is l=m+1 and not l=m,
C and that there is another phase-shift here

      Pm1l1=CSPha2*Pml*oddfac*stheta

C Set up Pml1 and Pm1l

      Pml1=ctheta*(2*am+1)*Pml
      Pm1l=0.D0

C Set up rtheta

      if(stheta.eq.0.D0) then
       rtheta=1.D0
      else
       rtheta=1/stheta
      endif

C First initialise the total sum, including the extra
C eigval and 1/(lj(lj+1)) bits.

      if(parity) then
        lsdfdrad=vrad(1)*am*Pml*rtheta*ctheta
      else
        lsdfdrad=vrad(1)*(am*Pml1*rtheta*ctheta-CSPha2*Pm1l1)
      endif

C And now loop over j to get the rest of the sum...

      do j=2,n

C Update Pml & Pm1l - first get the l-values for these updates,
C and then bung the new values into Pml & Pm1l 

        lj=am+2*(j-1)

        Pml=(ctheta*(2*lj-1)*Pml1
     :      -(lj+am-1)*Pml)/(lj-am)
        Pm1l=(ctheta*(2*lj-1)*Pm1l1
     :      -(lj+am1-1)*Pm1l)/(lj-am1)

C Now update the values of Pml1 & Pm1l1 , again doing lj's
 
        lj1=am+1+2*(j-1)

        Pml1=(ctheta*(2*lj1-1)*Pml
     :       -(lj1+am-1)*Pml1)/(lj1-am)
        Pm1l1=(ctheta*(2*lj1-1)*Pm1l
     :       -(lj1+am1-1)*Pm1l1)/(lj1-am1)

C Now we add in the appropiate term to the sum, depending
C on the parity...

        if(parity) then
          lsdfdrad=lsdfdrad+vrad(j)*
     :     (am*Pml*rtheta*ctheta-CSPha2*Pm1l)
        else
          lsdfdrad=lsdfdrad+vrad(j)*
     :     (am*Pml1*rtheta*ctheta-CSPha2*Pm1l1)
        endif

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

      enddo

C Thats all folks - return...

      return

      end
