    PROGRAM nag_tsa_spectral_ex09

      ! Example Program Text for nag_tsa_spectral
      ! NAG fl90, Release 4. NAG Copyright 2000.

      ! .. Use Statements ..
      USE nag_tsa_spectral, ONLY : nag_bivar_spectral_coh
      USE nag_examples_io, ONLY : nag_std_out, nag_std_in
      ! .. Implicit None Statement ..
      IMPLICIT NONE
      ! .. Intrinsic Functions ..
      INTRINSIC KIND
      ! .. Parameters ..
      INTEGER, PARAMETER :: wp = KIND(1.0D0)
      ! .. Local Scalars ..
      INTEGER :: i, l
      REAL (wp) :: t
      ! .. Local Arrays ..
      REAL (wp), ALLOCATABLE :: cross_ampl(:), cross_ampl_bounds(:,:), f_x(:), &
       f_y(:), gain(:), gain_bounds(:,:), phase(:), phase_bounds(:,:), &
       sqrd_coh(:), sqrd_coh_bounds(:,:)
      REAL (wp) :: stats(4)
      COMPLEX (wp), ALLOCATABLE :: f_xy(:)
      ! .. Executable Statements ..
      WRITE (nag_std_out,*) &
       'Example Program Results for nag_tsa_spectral_ex09'

      READ (nag_std_in,*)          ! Skip heading in data file
      READ (nag_std_in,*) l
      READ (nag_std_in,*) stats

      ALLOCATE (f_x(l),f_y(l),f_xy(l),sqrd_coh(l),sqrd_coh_bounds(l,2), &
       cross_ampl(l),cross_ampl_bounds(l,2),gain(l),gain_bounds(l,2),phase(l), &
       phase_bounds(l,2))          ! Allocate storage

      READ (nag_std_in,*) f_x
      READ (nag_std_in,*) f_y
      READ (nag_std_in,*) f_xy

      CALL nag_bivar_spectral_coh(f_x,f_y,f_xy,stats,sqrd_coh, &
       sqrd_coh_bounds=sqrd_coh_bounds,t=t,cross_ampl=cross_ampl, &
       cross_ampl_bounds=cross_ampl_bounds,gain=gain,gain_bounds=gain_bounds, &
       phase=phase,phase_bounds=phase_bounds)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) '      Cross amplitude spectrum'
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) '                    Lower     Upper'
      WRITE (nag_std_out,*) '          Value     bound     bound'
      WRITE (nag_std_out,'(1X,I5,3F10.4)') (i-1,cross_ampl(i), &
       cross_ampl_bounds(i,:),i=1,l)
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,'(1X,A,F12.4)') 'Squared coherency test statistic =', &
       t
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) '         Squared coherency'
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) '                    Lower     Upper'
      WRITE (nag_std_out,*) '          Value     bound     bound'
      WRITE (nag_std_out,'(1X,I5,3F10.4)') (i-1,sqrd_coh(i), &
       sqrd_coh_bounds(i,:),i=1,l)

      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) '             The gain'
      WRITE (nag_std_out,*) '                    Lower     Upper'
      WRITE (nag_std_out,*) '          Value     bound     bound'
      WRITE (nag_std_out,'(1X,I5,3F10.4)') (i-1,gain(i),gain_bounds(i,:),i=1, &
       l)
      WRITE (nag_std_out,*)
      WRITE (nag_std_out,*) '             The phase'
      WRITE (nag_std_out,*) '                    Lower     Upper'
      WRITE (nag_std_out,*) '          Value     bound     bound'
      WRITE (nag_std_out,'(1X,I5,3F10.4)') (i-1,phase(i),phase_bounds(i,:), &
       i=1,l)

      DEALLOCATE (f_x,f_y,f_xy,sqrd_coh,sqrd_coh_bounds,cross_ampl, &
       cross_ampl_bounds,gain,gain_bounds,phase, &
       phase_bounds)               ! Deallocate storage

    END PROGRAM nag_tsa_spectral_ex09
