Program g11safe

!     G11SAF Example Program Text

!     Mark 26.1 Release. NAG Copyright 2016.

!     .. Use Statements ..
      Use nag_library, Only: g11saf, nag_wp, x04abf
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: iset = 1, nin = 5, nout = 6
!     .. Local Scalars ..
      Real (Kind=nag_wp)               :: cgetol, chi, rlogl, siglev
      Integer                          :: i, idf, ifail, ip, ip2, iprint,      &
                                          ishow, ldcm, ldexpp, ldx, lw, maxit, &
                                          n, nadv, niter, ns
      Logical                          :: chisqr, gprob
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: a(:), alpha(:), c(:), cm(:,:),       &
                                          exf(:), expp(:,:), g(:), obs(:,:),   &
                                          pigam(:), w(:), xl(:), y(:)
      Integer, Allocatable             :: iob(:), irl(:)
      Logical, Allocatable             :: x(:,:)
!     .. Executable Statements ..
      Write (nout,*) 'G11SAF Example Program Results'
      Write (nout,*)
      Flush (nout)

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

!     Read in problem size
      Read (nin,*) ip, n, ns

!     Read in control parameters
      Read (nin,*) gprob, iprint, cgetol, maxit, chisqr, ishow

!     Set the advisory channel to NOUT for monitoring information
      If (iprint>0 .Or. ishow/=0) Then
        nadv = nout
        Call x04abf(iset,nadv)
      End If

      ip2 = 2*ip
      ldx = ns
      ldcm = ip2
      ldexpp = ip
      lw = 4*ip*(ip+16)
      Allocate (x(ldx,ip),irl(ns),a(ip),c(ip),alpha(ip),pigam(ip),             &
        cm(ldcm,ip2),g(ip2),expp(ldexpp,ip),obs(ldexpp,ip),exf(ns),y(ns),      &
        xl(ns),iob(ns),w(lw))

!     Read in data
      Read (nin,*)(irl(i),x(i,1:ip),i=1,ns)

!     Read in initial values
      Read (nin,*) a(1:ip)
      Read (nin,*) c(1:ip)

!     Fit a latent variable model
      ifail = 0
      Call g11saf(ip,n,gprob,ns,x,ldx,irl,a,c,iprint,cgetol,maxit,chisqr,      &
        ishow,niter,alpha,pigam,cm,ldcm,g,expp,ldexpp,obs,exf,y,xl,iob,rlogl,  &
        chi,idf,siglev,w,lw,ifail)

    End Program g11safe