      function bgetmax(nuGmax,HGMax,MGmax,JGmax,PGmax)

C***********************************************************
C
C This function takes a set of individual mode parameters
C for a given mode and returns the PEAK velocity
C amplitude which that mode will ever produde, for a mathematical
C amplitude of 1m/s. This peak is taken as MAX(radial velocity,
C horizontal velocity).
C
C  Input parameters:
C   
C    nuGmax 	:	mode rotation parameter
C    HGMax	:	mode K value
C    MGmax 	:	mode m value
C    JGmax	:	mode J value
C    PGmax	:       mode parity
C
C Started       :15/4/96
C Modified :
C
C  22/06/00 - Changed HGMax to HGmax (horizontal amplitude)
C
C  26/06/00 - Modified the code to check whether horizontal
C             velocity fields are being included
C
C (c) Richard Townsend 1996
C
C***********************************************************

C Turn off implicit typing

      implicit none

C Define the hard-wired parameters

      include "bruce.compar_define.inc"

C *** Start of Include block load ***

C Load in the stellar parameters common block

      include "bruce.stepar_common.inc"

C Load in the equilibrium Stellar grid common block

      include "bruce.eqstgr_common.inc"

C Load in the field switches common block

      include "bruce.fldswi_common.inc"

C LOad in the angles and constants include file

      include "angles.inc"

      include "constants.inc"

C *** End of Include Block Load ***

C Declare the passed variables
      
      double precision bgetmax	        ! The returned maximum velocity
      double precision nuGmax		! The rotation parameter of a given mode
      double precision HGMax		! The relative horizontal amplitude to use
      integer MGmax			! The m-value to find the velocity for
      integer JGmax			! The j-value to find the velocity for
      logical PGmax                	! The parity to find the velocity for

C Declare the variables used by the Lee & Saio routines

      double precision DVec(DimMax)	! The diagonal components of the W-matrix & eigenvalues
      double precision SVec(DimMax)     ! The off-diagonal components of the W-matrix
      double precision Emat(DimMax,DimMax) 	! Matrix to hold the eigenvectors found
      double precision RadVec(DimMax)		! Vector holding the radial expansion coeffs
      double precision HorVec(DimMax)		! Vector holding the horizontal expansion coeffs
      double precision TorVec(DimMax)		! Vector holding the toroidal expansion coeffs
      integer EgIndx(DimMax) 		! Array for sorting the eigenvalues
      integer ISign			! Counter for adjusting the sign of the eigenvectors
      double precision lsdFRad		! The function to return the Lee & Saio radial function
      double precision lsdFHor		! The function to return the Lee & Saio horizontal function
      double precision lsdFTor		! The function to return the Lee & Saio toroidal function
      double precision lsdFDRad		! The function to return d/dtheta of the Lee & Saio radial function
      double precision lsdFDHor		! The function to return d/dtheta of the Lee & Saio horizontal function
      double precision lsdFDTor		! The function to return d/dtheta of the Lee & Saio toroidal function
 

C Declare the variables used in the grid calculations

      double precision theta           ! The value of theta for some grid point
      double precision ctheta          ! Cosine of theta
      double precision stheta          ! Sine of theta
      double precision rtheta	       ! The reciprocal of stheta
      double precision phi	       ! The value of phi for some grid point
      double precision cosMP	       ! The cosine of m*phi
      double precision sinMP           ! The sine of m*phi
      double precision cnorm	       ! The colatitude normalisation factor (see later)
      double precision RBasis          ! The value of the radial basis function |lambda> at some theta
      double precision pVRad           ! The partial (theta-part) radial velocity for a given mode
      double precision pVThe           ! The partial (theta-part) theta velocity for a given mode
      double precision pVPhi           ! The partial (theta-part) phi velocity for a given mode
      double precision VRad	       ! The radial velocity
      double precision VThe	       ! The theta-velocity
      double precision VPhi            ! The phi velocity
      double precision VMod	       ! The modulus of the velocity

C Declare the variables used for general control

      logical ok                       ! Status flag for some of the subroutines called
      integer gcount                   ! Counter to step through the partial grid points (ie theta)
      integer pcount		       ! Counter to step through phi
      integer NGPhi	               ! The number of grid points at a given theta

C Depending on we are using Lee & Saio stuff, we find the W-matrix
C for the mode and diagonalise it etc...

      if(LSDEff) then

C Do matrix stuff...

C Call the first of the Lee & Saio LSD routines to generate the W-matrix.
C If the generation is sucessful, continue, otherwise write out an error
C message.

        call lsdMakeW(MGmax,nuGmax,PGmax,
     :                MatDim,DimMax,DVec,SVec,ok)

        call lsdTQLI(DVec,SVec,MatDim,DimMax,EMat)

        call lsdFindE(MGmax,nuGmax,MatDim,
     :                DimMax,DVec,SVec,EgIndx,
     :                PGmax)

        if(EMat(JGmax,EgIndx(JGmax)).lt.0.D0) then
            do ISign=1,MatDim
              EMat(ISign,EgIndx(JGmax))=
     :       -EMat(ISign,EgIndx(JGmax))
            enddo
        endif
 
        call lsdVRad(RadVec,MGmax,MatDim,DimMax,
     :               EMat(1,EgIndx(JGmax)),
     :               PGmax)

        call lsdVHor(HorVec,MGmax,
     :               1.D0/DVec(EgIndx(JGmax)),
     :               MatDim,DimMax,
     :               EMat(1,EgIndx(JGmax)),
     :               PGmax)

        call lsdVTor(TorVec,MGmax,nuGmax,
     :               1.D0/DVec(EgIndx(JGmax)),
     :               MatDim,DimMax,
     :               EMat(1,EgIndx(JGmax)),
     :               PGmax)

      else

        call lsdVharm(MGmax,JGmax,MatDim,DimMax,
     :      RadVec,HorVec,TorVec,
     :      PGmax)

      endif

C OK, now we loop over theta (ie the stellar grid) to find the maximum velocity. First
C we initialise the max velocity to be zero:

      bgetmax=-1.D20

C Now we can loop through the theta-part of the stellar grid to find the maximum and
C minimum velocity amplitude for this mode.

      do gcount=1,NGrid,1

C Work out theta for this grid point

        theta=(gcount-0.5D0)/dble(NGrid)*D90

C Get the theta-part of Vr, Vtheta and Vphi for this mode. To do the calculations, we first set up
C ctheta with the value of cos(theta), & stheta with sin(theta). This part is lifted straight
C from the main routine bruce.f; see the corresponding bit in bruce.f for a bit more documentation.

        ctheta=cos(theta)
        stheta=sin(theta)
        rtheta=1.D0/stheta

        if (ColNrm) then
          cnorm=RGrid(gcount)/Rpole
        else
          cnorm=1.D0
        endif

C Set up the theta part of the radial basis state in RBasis

        RBasis=RecpPi*
     :        lsdFRad(MGmax,ctheta,MatDim,DimMax,
     :         RadVec,PGmax)

C Use RBasis to get the radial velocity VRad (amplitude 1)

        pVRad=RBasis*cnorm

C Get the horizontal fields

        pVThe=RecpPi*HGMax*(
     :         lsdFDHor(MGmax,ctheta,MatDim,DimMax,
     :          HorVec,PGmax) +
     :         lsdFTor(MGmax,ctheta,MatDim,DimMax,
     :          TorVec,PGmax)*MGmax*rtheta )*
     :         cnorm

        pVPhi=-RecpPi*HGMax*(
     :         lsdFHor(MGmax,ctheta,MatDim,DimMax,
     :          HorVec,PGmax)*MGmax*rtheta +
     :         lsdFDTor(MGmax,ctheta,MatDim,DimMax,
     :          TorVec,PGmax) )*
     :          cnorm


C Work out the number of phi grid points at this latitude...

        NGPhi=int(NEqtr*sin(theta))+1

C Loop over phi...

        do pcount=1,NGPhi,1

C Work out the phi of this point, and trig functions associated with it

          phi=(pcount-0.5D0)/dble(NGPhi)*D360
        
C Work out the cos and sine bitz

          cosMP=cos(MGmax*phi)
                        
          sinMP=sin(MGmax*phi)

C Update the net values defined above for the grid point

          VRad=pVRad*cosMP
          VThe=pVThe*cosMP
          VPhi=pVPhi*sinMP

C Work out the velocity modulus...

          if(HorEff) then
             VMod=sqrt(VRad**2+VThe**2+VPhi**2)
          else
             VMod=abs(VRad)
          endif

C Update the maximum velocity
         
          bgetmax=max(bgetmax,VMod)

C Loop back around....

        enddo
           
      enddo


C Return to the caller...

       return

       end

