Program g05nefe

!     G05NEF Example Program Text

!     Mark 25 Release. NAG Copyright 2014.

!     .. Use Statements ..
      Use nag_library, Only: g05kff, g05nef, nag_wp
!     .. Implicit None Statement ..
      Implicit None
!     .. Parameters ..
      Integer, Parameter               :: lseed = 1, nin = 5, nout = 6
!     .. Local Scalars ..
      Integer                          :: genid, i, ifail, lipop, lstate, m,   &
                                          n, subid
      Character (1)                    :: order, pop
!     .. Local Arrays ..
      Real (Kind=nag_wp), Allocatable  :: wt(:)
      Integer, Allocatable             :: ipop(:), isampl(:), state(:)
      Integer                          :: seed(lseed)
!     .. Executable Statements ..
      Write (nout,*) 'G05NEF Example Program Results'
      Write (nout,*)

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

!     Read in the base generator information and seed
      Read (nin,*) genid, subid, seed(1)

!     Initial call to initialiser to get size of STATE array
      lstate = 0
      Allocate (state(lstate))
      ifail = 0
      Call g05kff(genid,subid,seed,lseed,state,lstate,ifail)

!     Reallocate STATE
      Deallocate (state)
      Allocate (state(lstate))

!     Initialize the generator to a repeatable sequence
      ifail = 0
      Call g05kff(genid,subid,seed,lseed,state,lstate,ifail)

!     Read in population size, sample size and order
      Read (nin,*) n, m, pop
      Read (nin,*) order

      Select Case (pop)
      Case ('S','s')
        lipop = n
      Case Default
        lipop = 0
      End Select

      Allocate (ipop(lipop),wt(n),isampl(m))

      If (lipop==n) Then
!       Read in the population and weights
        Do i = 1, n
          Read (nin,*) ipop(i), wt(i)
        End Do
      Else
!       Read in just the weights
        Do i = 1, n
          Read (nin,*) wt(i)
        End Do
      End If

!     Generate the sample without replacement, unequal weights
      Call g05nef(order,wt,pop,ipop,n,isampl,m,state,ifail)

!     Display the results
      Write (nout,99999)(isampl(i),i=1,m)

99999 Format (10(1X,I4))
    End Program g05nefe