!   G02HFF Example Program Text
!   Mark 23 Release. NAG Copyright 2011.

    MODULE g02hffe_mod

!      G02HFF Example Program Module:
!             Parameters and User-defined Routines

!      .. Use Statements ..
       USE nag_library, ONLY : nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       REAL (KIND=nag_wp), PARAMETER       :: tc = 1.5_nag_wp
       INTEGER, PARAMETER                  :: nin = 5, nout = 6
    CONTAINS
!dec$ attributes offload : mic :: psi
       FUNCTION psi(t)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: psi
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: t
!         .. Intrinsic Functions ..
          INTRINSIC                              abs
!         .. Executable Statements ..
          IF (t<=-tc) THEN
             psi = -tc
          ELSE IF (abs(t)<tc) THEN
             psi = t
          ELSE
             psi = tc
          END IF
          RETURN
!dec$ attributes offload : mic :: psi
       END FUNCTION psi

!dec$ attributes offload : mic :: psp
       FUNCTION psp(t)

!         .. Implicit None Statement ..
          IMPLICIT NONE
!         .. Function Return Value ..
          REAL (KIND=nag_wp)                  :: psp
!         .. Scalar Arguments ..
          REAL (KIND=nag_wp), INTENT (IN)     :: t
!         .. Intrinsic Functions ..
          INTRINSIC                              abs
!         .. Executable Statements ..
          psp = 0.0_nag_wp
          IF (abs(t)<tc) THEN
             psp = 1.0_nag_wp
          END IF
          RETURN
!dec$ attributes offload : mic :: psp
       END FUNCTION psp
    END MODULE g02hffe_mod
    PROGRAM g02hffe

!      G02HFF Example Main Program

!      .. Use Statements ..
       USE nag_library, ONLY : g02hff, x04caf
       USE g02hffe_mod, ONLY : nag_wp, nin, nout, psi, psp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Local Scalars ..
       REAL (KIND=nag_wp)                  :: sigma
       INTEGER                             :: i, ifail, indc, indw, ldc, ldx,  &
                                              m, n
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE     :: c(:,:), rs(:), wgt(:), wk(:),    &
                                              x(:,:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G02HFF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)

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

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

       ldx = n
       ldc = m
       ALLOCATE (x(ldx,m),wgt(n),rs(n),wk(m*(n+m+1)+2*n),c(ldc,m))

!      Read in the data
       READ (nin,*) (x(i,1:m),i=1,n)

!      Read in SIGMA
       READ (nin,*) sigma

!      Read in weights and residuals
       READ (nin,*) (wgt(i),rs(i),i=1,n)

!      Read in control parameters
       READ (nin,*) indw, indc

!      Estimate variance-covariance matrix
       ifail = 0
       CALL g02hff(psi,psp,indw,indc,sigma,n,m,x,ldx,rs,wgt,c,ldc,wk,ifail)

!      Display results
       ifail = 0
       CALL x04caf('General',' ',m,m,c,ldc,'Covariance matrix',ifail)

    END PROGRAM g02hffe
