    PROGRAM g02lcfe

!      G02LCF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g02lcf, nag_wp, x04caf
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       REAL (KIND=nag_wp)              :: rcond
       INTEGER                         :: i, ifail, ip, iscale, ldb, ldc,      &
                                          ldob, ldp, ldvip, ldw, ldycv,        &
                                          maxfac, my, nfact, orig, vipopt
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: b(:,:), c(:,:), ob(:,:), p(:,:),     &
                                          vip(:,:), w(:,:), xbar(:), xstd(:),  &
                                          ybar(:), ycv(:,:), ystd(:)
!      .. Executable Statements ..
       WRITE (nout,*) 'G02LCF Example Program Results'
       WRITE (nout,*)
       FLUSH (nout)

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

!      Read problem size
       READ (nin,*) ip, my, maxfac, nfact, orig, iscale, vipopt

       ldp = ip
       ldc = my
       ldw = ip
       ldb = ip
       IF (orig==1) THEN
          ldob = ip + 1
       ELSE
          ldob = 1
       END IF
       IF (vipopt/=0) THEN
          ldycv = nfact
          ldvip = ip
       ELSE
          ldycv = 0
          ldvip = 0
       END IF
       ALLOCATE (p(ldp,maxfac),c(ldc,maxfac),w(ldw,maxfac),b(ldb,my),xbar(ip), &
          ybar(my),xstd(ip),ystd(my),ob(ldob,my),ycv(ldycv,my), &
          vip(ldvip,vipopt))

!      Read in data
       READ (nin,*) (p(i,1:maxfac),i=1,ip)
       READ (nin,*) (c(i,1:maxfac),i=1,my)
       READ (nin,*) (w(i,1:maxfac),i=1,ip)
       IF (vipopt/=0) THEN
          READ (nin,*) (ycv(i,1:my),i=1,nfact)
       END IF

!      Read means and scalings
       IF (orig==1) THEN
          READ (nin,*) xbar(1:ip)
          READ (nin,*) ybar(1:my)
          IF (iscale/=-1) THEN
             READ (nin,*) xstd(1:ip)
             READ (nin,*) ystd(1:my)
          END IF
       END IF

!      Calculate predictions
       rcond = -1.0E0_nag_wp
       ifail = 0
       CALL g02lcf(ip,my,maxfac,nfact,p,ldp,c,ldc,w,ldw,rcond,b,ldb,orig,xbar, &
          ybar,iscale,xstd,ystd,ob,ldob,vipopt,ycv,ldycv,vip,ldvip,ifail)

!      Display results
       ifail = 0
       CALL x04caf('General',' ',ip,my,b,ldb,'B',ifail)
       IF (orig==1) THEN
          WRITE (nout,*)
          FLUSH (nout)
          ifail = 0
          CALL x04caf('General',' ',ip+1,my,ob,ldob,'OB',ifail)
       END IF
       IF (vipopt/=0) THEN
          WRITE (nout,*)
          FLUSH (nout)
          ifail = 0
          CALL x04caf('General',' ',ip,vipopt,vip,ldvip,'VIP',ifail)
       END IF

    END PROGRAM g02lcfe
