    PROGRAM g01hbfe

!      G01HBF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g01hbf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: prob, tol
       INTEGER                         :: i, ifail, ldsig, lwk, n
       CHARACTER (1)                   :: tail
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: a(:), b(:), sig(:,:), wk(:), xmu(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max
!      .. Executable Statements ..
       WRITE (nout,*) 'G01HBF Example Program Results'
       WRITE (nout,*)

!      Skip heading in data file
       READ (nin,*)

!      Read in the problem size
       READ (nin,*) n, tol, tail

!      Use more workspace, unless N is very large
!      as LWK also defines the number of sub-intervals
       lwk = max(2000,4*n)

       ldsig = n
       ALLOCATE (a(n),b(n),xmu(n),sig(ldsig,n),wk(lwk))

!      Read in the means
       READ (nin,*) xmu(1:n)

!      Read in the variance covariance matrix
       READ (nin,*) (sig(i,1:n),i=1,n)

!      Read in bounds
       IF (tail=='C' .OR. tail=='c' .OR. tail=='U' .OR. tail=='u') THEN
          READ (nin,*) a(1:n)
       END IF
       IF (tail=='C' .OR. tail=='c' .OR. tail=='L' .OR. tail=='l') THEN
          READ (nin,*) b(1:n)
       END IF

!      Calculate probability
       ifail = -1
       prob = g01hbf(tail,n,a,b,xmu,sig,ldsig,tol,wk,lwk,ifail)
       IF (ifail/=0) THEN
          IF (ifail<=3) THEN
             GO TO 20
          END IF
       END IF

!      Display results
       WRITE (nout,99999) 'Multivariate Normal probability =', prob

20     CONTINUE

99999  FORMAT (1X,A,F7.4)
    END PROGRAM g01hbfe
