      subroutine incom(p,n,nsets,ndone,qid)
c     on input:p contains parameters,n the number
c     nsets number of sets and ndone the number of blocks
      include 'upi.cm'

! janor add
      include 'cpbc.cm'
! end janor add

      integer n,nsets,ndone,ifres,ifav,noblk,iflag,ifirst
      integer iblk,intread,i,l
      character p(n)*(*),qid*(*)

! janor add
      integer ijar,jjar,kjar
      
      integer nbinjar(3),nbinr
      real*8 rbinjar
      real*8 xtmp,ytmp,ztmp,rtmp,Vtmp
      real*8 rxtmp,rytmp,rztmp,rrtmp
      logical b1jar,b2jar,b3jar
! end janor add

c     set up flags and input for each method

      ifres=1
      ifgofr=1
      ifav=1

      if(n.lt.2)stop
      noblk=intread(p(2))
      write (6,*) 'starting new set with ',noblk,' blocks'

! janor add
      dxdistjar=ell(1)/nxdistjar
      dydistjar=ell(2)/nydistjar
      dzdistjar=ell(3)/nzdistjar
      ellrjar=min(ell(1),ell(2))
      drdistjar=ellrjar/nrdistjar
! end janor add



! janor add
      if(write_config_file.eq.1)then
         write(52,520)nsets,noblk
      end if
 520  format(2(i8))
! end janor add

c     here we do the work
      iflag=0
      ifirst=ndone+1
c     zerov if we start Monte Carlo
      if(ifirst.eq.1.and.ifav.eq.1)call zerov

! janor add
      if(write_config_file.eq.1)then
         write(51,510)ndim,nparts,nslices
      end if
 510  format(3(i8))
! end janor add

      do iblk=ifirst,noblk


        if(p(1).eq.'OMOVE') then
            call omove(iflag,p,n)
         elseif(p(1).eq.'SELECT') then
            call select(iflag,p,n)
         elseif(p(1).eq.'PERMUTE') then
            call permute(iflag,p,n)
         elseif(p(1).eq.'READPC') then
            call readpc(iflag,p,n,qid)
            if(iflag.eq.-1) return
         elseif(p(1).eq.'REACT') then
            call react(iflag,p,n,qid,1)
            if(iflag.eq.-1) return
         elseif(p(1).eq.'ACT') then
            call react(iflag,p,n,qid,0)
            if(iflag.eq.-1) return
         elseif(p(1).eq.'TESTER') then
            call tester(iflag,p,n,qid)
            if(iflag.eq.-1) return
         else
            write (*,*) 'Unknown driver command in incom'
            stop
         endif
         write (83,*)' nsets=',nsets,' nblocks ',iblk ! acceptance rates
         call flush (83)

         if(effmss.ne.0) call weffmss(qid)
         if(ifkt.ne.0) call wfkt(qid)
         if(nstimes.gt.0)call wfself
         call pcycle(qid)
         if(ifgofr.ne.0) then
            call wsofk(qid)
            call wgvector(qid)
            call wgofr(qid)
         endif
         if(jcm.gt.0) call wcm(qid)

         if(ifav.eq.1) then
            call etot(qid)
            call sumavv(1)      ! no prints-data is in .bsc

!     do i=1,40  ! this is 'angle distribution'
!     write (44,'(f7.3,10i7)')(i-.5)*.05-1.,(cdist(i,l),l=1,10) ! CHANGE
!     enddo
         endif
         if(ifres.eq.1)  then
            call spill(qid,iblk,nsets)
            ndone=iblk
         endif

         iflag= 1
 
! janor add
!      if(write_config_file.eq.1)then
!         do kjar=1,nslices
!            do jjar=1,nparts
!               do ijar=1,ndim
!                  write(50,500)r(ijar,jjar,kjar)
!               end do
!            end do
!         end do
!      end if
! 500  format(1pe12.4)
! end janor add


       
      enddo

c     check whether the permutation table is dimensioned large enough
      call mcheck(nprmx,mspermt,'nprmx','mspermt','incom')
      if(nwrapx.gt.0)call writewr
      return
      end
