      function lsdfrad(m,ctheta,n,np,vrad,parity)

C********************************************************
C
C This function returns the function Psquiggle(m,lj)
C for use in the new Lambda Basis of Lee & Saio (1990)
C 349, 570-579. This function is a sum over n Associated
C Legendre polynomials of constant m-value and and l value
C given by lj=|m|+2(j-1) for parity=.true. (even modes)
C and by lj=|m|+1+2(j-1) for parity=.false. (odd modes),
C where j goes from 1 to d. The sum is weighted by coefficents
C vrad which are held in an array of physical size np and
C logical size n. This array is previously calculated by the
C funtion lsdvrad. Since Psquiggle is a function of the associated
C Legendre polynomials, it also obviously depends upon theta,
C the 'colatitude'.
C
C Note that cos theta MUST NOT EXCEED +/-1 or this routine will
C bum out.
C
C The m=0 screw-up experienced previously should be fixed by now...
C
C This routine now adheres to the Condon & Shortley phase convention
C
C Started:29/3/95
C Updated:6/12/95
C
C (c) Richard Townsend 1995
C
C************************************************************

      implicit none		! Turn off implicit typing

C Declare the passed variables

      double precision lsdfrad	! The returned function
      double precision ctheta	! The cosine of the 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 vrad
      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 am		! The absoulte value of m
      integer CSphas		! The Condon & Shortley 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 Pml2	! The value of the Assoc. Leg. Poly, l=lj+2
      double precision stheta	! Sine of theta
      double precision oddfac	! The product of all odd integers lt (2m-1)

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

C Set up CSPhas

      CSPhas=(-1)**((am-m)/2)

C Initialise the staring values of Pml and Pml1
C oddfac has to be used along the way for Pml - 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 to that of Condon & Shortley

      Pml=Pml*CSPhas

C Set up Pml1

      Pml1=ctheta*(2*am+1)*Pml

C First initialise the total sum.

      if(parity) then
        lsdfrad=vrad(1)*Pml
      else
        lsdfrad=vrad(1)*Pml1
      endif

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

      do j=2,n

C Update Pml - first get the l-value for this update,
C and then bung its new value into Pml. 

        lj=am+2*(j-1)

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

C Now update the value of Pml1, again doing lj

        lj=am+1+2*(j-1)

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

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

        if(parity) then
          lsdfrad=lsdfrad+vrad(j)*Pml
        else
          lsdfrad=lsdfrad+vrad(j)*Pml1
        endif

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

      enddo

C Thats all folks - return...

      end

