    PROGRAM g13dlfe

!      G13DLF Example Program Text

!      Mark 23 Release. NAG Copyright 2011.

!      .. Use Statements ..
       USE nag_library, ONLY : g13dlf, nag_wp
!      .. Implicit None Statement ..
       IMPLICIT NONE
!      .. Parameters ..
       INTEGER, PARAMETER              :: nin = 5, nout = 6
!      .. Local Scalars ..
       INTEGER                         :: d, i, ifail, k, kmax, n, nd, tddelta
!      .. Local Arrays ..
       REAL (KIND=nag_wp), ALLOCATABLE :: delta(:,:), w(:,:), work(:), z(:,:)
       INTEGER, ALLOCATABLE            :: id(:)
       CHARACTER (1), ALLOCATABLE      :: tr(:)
!      .. Intrinsic Functions ..
       INTRINSIC                          max, maxval
!      .. Executable Statements ..
       WRITE (nout,*) 'G13DLF Example Program Results'
       WRITE (nout,*)

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

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

       ALLOCATE (id(k))

!      Read in differencing
       READ (nin,*) id(1:k)

       d = maxval(id(1:k))
       tddelta = max(d,1)
       nd = n - d
       kmax = k
       ALLOCATE (z(kmax,n),tr(k),delta(kmax,tddelta),w(kmax,nd),work(k*n))

!      Read in series and the transformation flag
       READ (nin,*) (z(i,1:n),i=1,k)
       READ (nin,*) tr(1:k)

!      If required, read in delta
       IF (d>0) THEN
          READ (nin,*) (delta(i,1:id(i)),i=1,k)
       END IF

!      Difference and / or transform series
       ifail = 0
       CALL g13dlf(k,n,z,kmax,tr,id,delta,w,nd,work,ifail)

!      Display results
       WRITE (nout,*) ' Transformed/Differenced series'
       WRITE (nout,*) ' ------------------------------'
       DO i = 1, k
          WRITE (nout,*)
          WRITE (nout,99999) ' Series ', i
          WRITE (nout,*) ' -----------'
          WRITE (nout,*)
          WRITE (nout,99998) ' Number of differenced values = ', nd
          WRITE (nout,*)
          WRITE (nout,99997) w(i,1:nd)
       END DO

99999  FORMAT (1X,A,I2)
99998  FORMAT (1X,A,I6)
99997  FORMAT (1X,8F9.3)
    END PROGRAM g13dlfe
