! ------------------------------------------------------------ !
      program gq 
! ------------------------------------------------------------ !
! Purpose: This is the main program of the grand que.
! ------------------------------------------------------------ !
      implicit none
     
      integer :: i,j,k,ict,jred,njred
      integer :: plsmzns = 10         ! Plasma zones used to compute snd_c only
      integer, dimension(40) :: a1,b1     ! rray containing sequences
      integer :: maxgen
      integer :: nin, nout
      integer :: ipopsize
      real(8), dimension(10) :: densarray ! Density array (mapping)
      real(8), dimension(10) :: temparray
      real(8), dimension(10000) :: plistin 
      real(8), dimension(10000) :: plistout 
      external fbuff
      nin  = plsmzns*2 ! 2={t,dna} Amount of data sent to each plasma (ct)
      open(unit=20,file='seq_d.in')  
      open(unit=21,file='seq_t.in')
      ipopsize=0
      ict=0
      read(20,*) njred
      read(20,*) (densarray(jred),jred=1,njred)
      print*,'densarray=',(densarray(jred),jred=1,10)
      read(21,*) njred
      read(21,*) (temparray(jred),jred=1,njred)
      print*,'temparray=',(temparray(jred),jred=1,10)
      do 
         ict=ict+1
         print*,'ict=',ict
! -- Temp array
         j=0
         read(21,*,end=75) (b1(i),i=1,10)
         do i=ipopsize*nin+1,ipopsize*nin+20,2
            j=j+1
            plistin(i)=temparray(b1(j))
         enddo
75       continue
         write(6,'(10(1x,i3))')b1(1),b1(2),b1(3),b1(4),b1(5),&
    &           b1(6),b1(7),b1(8),b1(9),b1(10)
! -- Den. array
         j=0
         read(20,*,end=80) (a1(i),i=1,10)
         do i=ipopsize*nin+2,ipopsize*nin+20,2
            j=j+1
            plistin(i)=densarray(a1(j))
         enddo
         ipopsize=ipopsize+1
         write(6,'(10(1x,i3))')a1(1),a1(2),a1(3),a1(4),a1(5),&
    &           a1(6),a1(7),a1(8),a1(9),a1(10) 
      enddo
80    continue
         write(6,'(10(1x,i3))')a1(1),a1(2),a1(3),a1(4),a1(5),&
    &           a1(6),a1(7),a1(8),a1(9),a1(10)
      close(20)
      close(21)

      print*,'ipopsize=',ipopsize
      do k=1,ipopsize*nin
         print*,'plistin(',k,')=',plistin(k)
      enddo 
      call subq(maxgen,ipopsize,nin,nout,plistin,plistout)
      do k=1,ipopsize*nout
         print*,'plistout(',k,')=',plistout(k)
      enddo

      end

! ------------------------------------------------------------------- !
      subroutine subq(maxgen,ipopsize,nin,nout,plistin,plistout)
! ------------------------------------------------------------------- !
      use communicate
      implicit none
      integer :: nin,nout
      integer :: ipopsize
      integer :: nproc = 5
      integer :: strt  = 0
      integer :: shft  = 1
      integer :: maxgen
      integer :: ipsz
      real(8),dimension(10000) :: plistin
      real(8),dimension(10000) :: plistout
      character(len=30) :: exec ="ct" ! A single plasma configuration

      ipsz=ipopsize
      snd_c = nin
      r8_frm_p=plistin

      call x_sp_c( exec, nproc, strt, shft )
      call x_stids( 1, exec, exec )
      call x_cque_c( 2, exec, ipsz )
      call x_skflg_c( 2 ,exec )

      nout = rcv_c
      plistout=r8_frm_c
!     do i=1,rcv_c*ipsz
!        print*,'r8_frm_c(',i,')=',r8_frm_c(i) 
!     enddo

      end 
