      subroutine bmakeg(ok)

C*******************************************************
C
C This subroutine constructs a partial stellar grid for
C use by BRUCE. The grid contains radius, area, temp and
C (vectorial) gravity for a number of points on the surface
C of a rotationally-distorted star with rotational angular
C omega. The grid is only calculated for colatitidue theta
C going from 0 to pi/2; this covers the 'northern' hemisphere,
C the southern hemisphere is just a mirror image of this.
C No phi dependance is included since all of the parameters
C in the grid are independant of phi.
C
C All variables are passed around using common blocks. A
C status variable is returned in ok - .true. indicates
C successful grid construction, .false. indicates an error
C 
C Started:18/9/95
C Modified:11/10/95
C
C  25/06/00 - Modified to calculate the disk area and the
C             the...
C
C (c) Richard Townsend 1995
C
C********************************************************

C Turn of implicit typing

      implicit none

C Define the hard-wired parameters

      include "bruce.compar_define.inc"

C Load in the stellar equilibrium grid include block

      include "bruce.eqstgr_common.inc"

C Load in the Stellar parameters include block

      include "bruce.stepar_common.inc"

C Declare the passed variables

      logical ok			! The returned status variable

C Declare the other parameters...

      double precision theta		! The colatitude theta
      double precision phi		! Guess
      double precision Radius		! The calculated radius at the current theta (using NR routine)
      double precision FRoot		! The function whose root is found to find the radius (bsically Ueff)
      double precision DFRoot		! The first derivative wrt Radius of DFRoot
      double precision GEffR		! The radial component of the effective gravity
      double precision GEffT		! The theta component of the effective gravity
      double precision ObsR		! The radial component of the observer vector for a given grid point
      double precision ObsT		! The theta component of the observer vector for a given grid point
      double precision ObsRC		! The radial component of the observer vector for a given conjugate grid point
      double precision ObsTC		! The theta component of the observer vector for a given conjugate grid point
      double precision cosMu		! The projected surface normal for a given grid point
      double precision cosMuC		! The projected surface normal for a given conjugate grid point
      integer gcount			! Counter to loop over the grid points (ie loop in theta)
      integer pcount			! Counter to loop over the phi points at some given theta (used for GrdVis/GrdTot)
      integer NGPhi			! The number of duplicate grid points needed in the phi direction at some theta

C Include the basic constants and angles

      include "constants.inc"
      include "angles.inc"

C Set up ok

      ok=.false.

C Set up DelThe

      DelThe=D90/dble(NGrid)

C Set up the initial value of Radius

      Radius=Rpole

C Also, work out TEq and GEq from TPole, Gpole, REq etc. Remember that GPole
C is the scalar (positive) polar gravity, so we must diddle the sign of GEq
C to be positive (this is what the initial - sign is for)

      GEq=-(Omega**2*REq-BigG*MStar/REq**2)

      TEq=TPole*(GEq/GPole)**Beta

C Initialise various calculated quantitites...

      LStar=0.D0
      AStar=0.D0
      VStar=0.D0

      ADisk=0.D0
      FDisk=0.D0

C Initialise GrdVis & GrdTot, the number of visible grid points and the total
C number of grid points respectively, to be zero

      GrdVis=0
      GrdTot=0

C Set up cDIncl and sDIncl for both bmakeg and bruce to use

      cDIncl=cos(DIncl)
      sDIncl=sin(DIncl)

C Write out a bit of info...

      write(chandl,
     : '('' Computing Stellar Grid...'')')

C Loop over the grid points, starting at the pole and working to the equator

      do gcount=1,NGrid,1

C Set up theta, making sure that the gcount=NGrid point is NOT on the pole (coz
C this leads to complications in BRUCE)

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

C Using a Newton-Raphson caculation, work out the radius of the star at this point. First
C get FRoot and DFRoot.

 1000   continue

        FRoot=0.5D0*(Omega*sin(theta))**2*Radius**2 
     :        -BigG*MStar/Rpole + BigG*MStar/Radius

        DFRoot=(Omega*sin(theta))**2*Radius - BigG*MStar/Radius**2

C Then check if the new radius differs from the old radius by less than the tolerance.
C If so, jump out of the loop

        if(abs((Radius-FRoot/DFRoot)/Radius-1.D0)
     :     .lt.RToler) goto 2000

C Otherwise update the radius and loop back

        Radius=Radius-FRoot/DFRoot
        goto 1000

C Jump in point for OK radius - do the last correction to radius		

 2000   continue

        Radius=Radius-FRoot/DFRoot

C Do a bounds check on the radius

        if(Radius.gt.Veq/Omega.or.Radius.lt.Rpole) then
          write(chandl,
     :    '(''*** There was a problem finding the stellar radius - '',
     :    ''Please request a recompile'')')
          goto 9000
        endif

C Bung it into the grid...

        RGrid(gcount)=Radius

C Work out the the radial and theta-components of the gravity vector, and get GGrid and NRGrid/NTGrid from
C them. Remember again that GEffR/GEffT are vector gravities, and can be negative, but GGrid must be
C positive since it is the scalar gravity.

        GEffR=Omega**2*Radius*sin(theta)-BigG*MStar/Radius**2
        GEffT=Omega**2*Radius*sin(theta)*cos(theta)

        GGrid(gcount)=sqrt(GEffR**2+GEffT**2)
        
        NRGrid(gcount)=-GEffR/GGrid(gcount)
        NTGrid(gcount)=-GEffT/GGrid(gcount)

C Set up the effective temperature using the gravity-darkening law  (von Zeipel)

        TGrid(gcount)=TPole*(GGrid(gcount)/GPole)**Beta

C Now we have to work out the area of the element associated with the grid point. First
C we work out the number of duplicate phi grid points we want to have at this colatitude
C so that each grid point has approx the same area. Make sure NGPhi is always greater than
C 1.

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

C Now work out what range in phi each grid point will cover

        DelPhi=D360/NGPhi

C OK, now we can work out the area, which is given by the modulus of dA:
C
C                                     (       1        )
C    dA = R**2 sin(theta) dtheta dphi ( -1/R dR/dtheta )
C                                     (                )
C First work out dR/dtheta

        DRGrid(gcount)=(Omega**2*Radius**3*sin(theta)*cos(theta))/
     :      (BigG*Mstar/RPole-1.5D0*Omega**2*Radius**2*(sin(theta))**2)

C Now get the area...

        AGrid(gcount)=Radius**2*sin(theta)*DelThe*DelPhi*
     :                sqrt(1+(DRGrid(gcount)/Radius)**2)

C Now we do a loop over the number of phi grid points at this theta value, working out
C from the surface normal of each grid point whether this grid point is visible - we
C then update GrdVis (and GrdTot) accordingly, for both the northern and southern hemisphere
C points. First things first, though, we must work out the radial and theta components of
C this grid point and of the conjugate (southern-hemisphere) grid point.
C
C Also, update the various integrated quantities for the star...

        do pcount=1,NGPhi,1

          phi=(pcount-0.5D0)/dble(NGPhi)*D360

          ObsR= cDincl*cos(theta)+sDIncl*sin(theta)*cos(phi)
          ObsT=-cDIncl*sin(theta)+sDIncl*cos(theta)*cos(phi)

          ObsRC=-cDincl*cos(theta)+sDIncl*sin(theta)*cos(phi)
          ObsTC=-cDIncl*sin(theta)-sDIncl*cos(theta)*cos(phi)

          cosMu=ObsR*NRGrid(gcount)+ObsT*NTGrid(gcount)
          cosMuC=ObsRC*NRGrid(gcount)-ObsTC*NTGrid(gcount)

          LStar = LStar+2*AGrid(gcount)*TGrid(gcount)**4*BSigma
          AStar = AStar+2*AGrid(gcount)
          VStar = VStar+2*RGrid(gcount)**3*sin(theta)*DelThe*DelPhi/3.D0

          if(cosMu.ge.0.D0) then
             GrdVis=GrdVis+1
             ADisk = ADisk+cosMu*AGrid(gcount)
             FDisk = FDisk+cosMu*AGrid(gcount)*(1.-LIMBOL*(1.-cosMu))*
     :               TGrid(gcount)**4*BSigma/((1.-LIMBOL)/2.+LIMBOL/3.)
          endif

          if(cosMuC.ge.0.D0) then
             GrdVis=GrdVis+1
             ADisk = ADisk+cosMuC*AGrid(gcount)
             FDisk = FDisk+cosMuC*AGrid(gcount)*(1.-LIMBOL*(1.-cosMuC))*
     :               TGrid(gcount)**4*BSigma/((1.-LIMBOL)/2.+LIMBOL/3.)
          endif
 
          GrdTot=GrdTot+2

        enddo

C OK, that seems to be all, so loop back around...

      enddo

C Set up ok to be .true.

      ok=.true.

C This is the jump -in point for returning

 9000 continue

      return

      end
