C  LOOP TO CHOOSE THE DATA FROM EACH CATEGORY
 
      do 280 icat = 0, numcat    
 
C  CHECK TO SEE IF THERE ARE ANY USABLE OBSERVATIONS IN CATEGORY ICAT

         forall(i=1:batch_size,Tiflag(i,icat).eq.1)
     &     Findx(i)=SUM(Tiflag(1:i))

         nflag=maxval(Findx)
         Fdist(Findx(:))=Tdist(:)
         Findexd(Findx(:))=Tindexd(:)
         Flabel(Findx(:))=Tlabel(:)
         where(Flabel.eq.cshift(Flabel,shift=-1))
           Fmv_mask=0
         else where
           Fmv_mask=1
         end where

         where(Flabel.eq.cshift(Flabel,shift=+1))
           Fsegment=.true.
         else where
           Fsegment=.false.
         end where

         even=.false.
         odd=.false.
         even(2:NFLAG:2)=.true.
         odd(1:NFLAG:2) =.true.

c         forall(i=1:batch_size,Tiflag(i,icat).eq.1)
c           Fdist(Findx(i))=Tdist(i)
c         forall(i=1:batch_size,Tiflag(i,icat).eq.1)
c           Findexd(Findx(i))=Tindexd(i)
c         forall(i=1:batch_size,Tiflag(i,icat).eq.1)
c           Flabel(Findx(i))=Tlabel(i)

100      continue
         Fsave=Findexd

         Frdist=eoshift(Fdist,shift=1)
         Frindexd=eoshift(Findexd,shift=1)
         Fritype=eoshift(Fitype,shift=1)
         Fldist=eoshift(Fdist,shift=-1)
         Flindexd=eoshift(Findexd,shift=-1)
         Flitype=eoshift(Fitype,shift=-1)

         mask1=(even.eq.1).and.Fsegment.and.(Fdist.lt.Frdist)
         mask1=mask1.or.eoshift(mask1,shift=-1)
         where(mask1)
                Fdist=Frdist*even + Fldist*odd
                Findexd=Frindexd*even + Flindexd*odd
                Fitype=Fritype*even + Flitype*odd
         end where

         Frdist=eoshift(Fdist,shift=1)
         Frindexd=eoshift(Findexd,shift=1)
         Fritype=eoshift(Fitype,shift=1)
         Fldist=eoshift(Fdist,shift=-1)
         Flindexd=eoshift(Findexd,shift=-1)
         Flitype=eoshift(Fitype,shift=-1)

         mask1=(odd.eq.1).and.Fsegment.and.(Fdist.lt.Frdist)
         mask1=mask1.or.eoshift(mask1,shift=-1)
         where(mask1)
                Fdist=Frdist*odd + Fldist*even
                Findexd=Frindexd*odd+ Flindexd*even
                Fitype=Fritype*odd + Flitype*even
         end where

         if(any(Findexd.ne.Fsave)) goto 100
         
         forall(i=1:nflag)
     &      ncat(i)=sum(Fitype(1:i),mask=Flabel(i).eq.Flabel(1:i))

         where(ncat.le.Fnum(:,icat)) 
             accept=1
         else where
             accept=0
         end where

         irep=0
         imv=0
         forall(i=1:nflag,accept(i).ne.0)
     &   irep(i)=sum(accept(1:i),mask=Flabel(i).eq.Flabel(1:i))
         forall(i=1:nflag,accept(i).ne.0)
     &   imv(i)=Flabel(i)


         if(maxval(irep)).gt.maxreps) then
            write(6,*) 'SEARCH: ',lev,maxval(irep),mxreps
            message='SEARCH: MXREPS has been exceeded - see message'
            call flash(5,message,1)
         endif

         forall(i=1:nflag,accept(i).ne.0)
     &   datalist(imv(i),irep(i),lev)=Findexd(i)
         forall(igp=start:finish)
     &   nrep(igp)=sum(accept,mask=Flabel.eq.igp)
         

280   CONTINUE
 
      RETURN
      END
