! Born-Oppheneimer MC for Hydrogen
! bopimc3.09: BOMC with NPT-REPTATION-PIMC-TABC 13/3/2003
!             fully numerical and fully analitycal twfs
!	      old and new way of selecting phases
! 
      program mctas_wrapper 
      implicit none
      integer mnprm,mngrp
      parameter (mnprm=20,mngrp=10)

      character fs*20,qid(mngrp)*20,qid_global*20,p(mnprm)*16
      integer lpx,split_key,pnum(mngrp),j,n,cnt,procsum,tmp1,cnt1
      integer ipickoff,intread
      logical tmp
      include 'tas.cm'

      call parallel_init() 

      if (nproc_world.gt.maxproc_world) then
       if (root_world) write (*,*) 'nproc_world.gt.maxproc_world'
     . ,maxproc_world
       call parallel_end()
      endif
!      if (root) then
!       write (*,*) ' input run id (lt 12 characters)'
!      write (*,*) ' using bopimc.in by default.'
!       read (*,'(a)') qid_global
!      endif
! broadcast qid among processors
!      call bcast_char(qid_global,20,0)
      qid_global='bopimc'
      lpx=index(qid_global,' ')-1
      fs=qid_global(1:lpx)//'.in'
      if (root_world) write (*,*) ' trying to open file ',fs
      open(1,file=fs,status='old',form='formatted')

      tmp=.false. ! until I figure out how to avoid problems with 
                 ! error messages in ipickoff (open and close 6 on
                 ! root_world ???)
      cnt=0
      procsum=0
901    j=ipickoff(1,p,n,mnprm,tmp)
       if(j.ne.0) go to 902
       if(n.le.0) go to 901
   
       cnt=cnt+1 
       if(n.lt.2) then
        if(root_world) write(*,*)'insufficient parameters in input file'
        call parallel_end()
       endif   

       if(cnt.gt.mngrp) then !too many runs 
        if (root_world) write (*,*) 'Too many runs:',mngrp
        call parallel_end()
       endif

       qid(cnt)=p(1)
       pnum(cnt)=intread(p(2))

       if(pnum(cnt).gt.maxproc) then !too many procs in one run 
        if (root_world) write (*,*) 'Too many procs in a single run:'
     .  ,mngrp
        call parallel_end()
       endif

       procsum=procsum+pnum(cnt) 

       go to 901

! finished reading file,
! make sure nproc_world is equal to the sum of processors in each job
902    if(procsum.ne.nproc_world) then    
        if (root_world) write (*,*) 'Number of procs does not agree with
     .  number in input file' ,procsum,nproc_world
        call parallel_end()
       endif

! assign keys 
      split_key=0
      cnt1=1
      tmp1=0
903   if(cnt1.gt.cnt) then
       if (root_world) write (*,*) 'Problem assigning keys.'
       call parallel_end()
      endif 
      tmp1=tmp1+pnum(cnt1)
      if(myrank_world.lt.tmp1) then
       split_key=cnt1
       go to 904        
      else
       cnt1=cnt1+1
       go to 903        
      endif

904   call split_comm(split_key)   !creates local communicators

      call mctas(qid(split_key))   ! call mctas with corresponding qid

      call parallel_end()          !the code won't reach this point
                                   !but just in case

      end
