      subroutine lsdfindE(m,nu,matdim,dimmax,
     :                 eigval,tmpind,egindx,parity)

C**************************************************************
C
C This subroutine is passed a vector containing the reciprocal eigenvalues
C of Lee & Saio's W-Matrix (MNRAS (1987) 221). The vector is
C of physical dimension dimmax and of logical length matdim, so the W-Matrix
C is implied to be matdim*matdim in logical dimension. Given these eigenvalues, and
C the values of the azimuthal quantum number m & the rotation 
C parameter nu, an index list is constructed whose j'th element corresponds
C to the position in eigval which the j'th eigenvalue appears at.
C By j'th eigenvalue, I mean the eigenvalue which approaches lj(lj+1)
C in the zero-rotation(nu=0) limit, where lj=|m|+2(j-1) for even modes
C and lj=|m|+1+2(j-1) for odd modes. The subroutine needs to know
C the parity of the modes it is dealing with - these are passed in
C the variable parity, with parity=.true. for even modes, and parity=
C .false. for odd modes.
C
C Started:23/3/95
C Updated:27/3/95
C
C (c) Richard Townsend 1995
C
C*********************************************************************

      implicit none		! Turn off implicit typing

C Declare the passed variables

      integer m		   		! The m value of the modes
      double precision nu  		! The rotation parameter
      integer matdim	    		! The number of eigenvalues to sort
      integer dimmax	   		! The physical dimension of eigval, tmpind and eigval
      double precision eigval(dimmax)	! The vector containing the eigenvalues
      integer tmpind(dimmax)		! Temporary indexing array for sorting...
      integer egindx(dimmax) 		! The index list
      logical parity	     		! The parity of the set of modes in question

C Declare the in-program variables

      integer i		 		! Looping variable...
      integer j		       		! Looping variable...
      integer lj	       		! lj value for finding nosing...
      integer temp	       		! Temporary variable for permuting...
      integer nosing	       		! The number of matrix singularities between
			       		! rotation parmeter=nu and rotation parameter=0

C The sorting basically works in the following way: At nu=0, the seqence
C of the reciprocal eigenvalues is that the smallest lj eigenvalue is the
C most positive, the largest lj eigenvalue is the most negative (although
C both are greater than zero), and the eigenvalues are sequenced regularly
C between these two limits. As nu is moved away from zero, every time the 
C matrix passes through a singularity, this ordering is permuted cyclicaly once


C First we work out how many singularities we have crossed since 
C nu was at zero. First see if m*nu is greater than zero, if not 
C nosing must be zero.

      if(m*nu.lt.0.D0) then
        nosing=0
      else

C Now we loop through all the j values, counting the number of dooh-dads which have
C gone singular...

        nosing=0

        do j=1,matdim

          if(parity) then
            lj=abs(m)+2*(j-1)
          else
            lj=abs(m)+1+2*(j-1)
          endif

        if (m*nu.gt.(lj+1)*(lj+2)) nosing=nosing+1

        enddo

      endif

C Singularities occur also for l(l-1)=m*nu, but this just duplicates the ones
C above. However, for odd modes, the method above does not include the l=|m|+1
C singularity of l(l-1)=m*nu, so we have to explicitly count it. We do
C not do this for even modes because this particular singluarity occurs with
C a zero value of Jml and so doesn't matter....

      if(.not.parity.and.m*nu.gt.abs(m)*(abs(m)+1))
     : nosing=nosing+1

C Now call the numerical recipies indexing routine, so that the j'th
C element of tmpind is the index into eigval of the j'th most negative eigenvalue.
C For instance, egindx(5)=4 means that eigval(4) is the 5th most negative eigenvalue.

      call lsdindexx(matdim,eigval,tmpind)

C Now we invert the elements of tmpind (ie reverse the order) to get the
C positive-to-negative indexing rather than the negative-to-positive indexing

      do j=1,matdim
        egindx(j)=tmpind(matdim+1-j)
      enddo

C Now, since at nu=0, the most positive eigenvalue is the j=1 one, the next-most-
C positive is j=2, and this ordering is premuted cyclicaly nosing times, at
C nu=nu, all we have to do to now get the correct values (as mentioned at the
C top of this prog) into eigindx is permute egindx to the left
C nosing times to get what the indexing array would be like at nu=0, and then the j'th
C element of egindx array will be the index of the jth eigenvalue. We permute to the
C left becuase, if for instance nosing was 1, then the eigenvalue index array would
C hold the j=matdim eigenvalue index in the first element, the j=1 index in the 2nd
C element, the j=2 in the third, and so on, so permuting to the left gives the j=1
C index in the first element, the j=2 index in the second etc.

      do j=1,nosing
        temp=egindx(1)
        do i=2,matdim
          egindx(i-1)=egindx(i)
        enddo
        egindx(matdim)=temp
      enddo

C That's all folks....

      return

      end  
