      subroutine search( mv_begin,mv_end,NumMinVols,
     &                   mv_start, max_batch_size, lu, ld, 
     &                   datalist, nreps, data, nobs, mv_done )
 
C------------------------------------------------------------------C
!                                                                  !
!   Program Name: Search              Date:  May 16, 1992          !
!   Programmer  : J. Pfaendtner                                    !
!   Modifications: M. Seablom, D. Lamich                           !
!                                                                  !
!   NASA / GMSB  Optimum Interpolation Version 1.3                 !
!   ----------------------------------------------                 !
!                                                                  !
!  Performs data selection for all volumes in a vertical column.   !
!  Selection algorithm is designed to search for mostly rawinsondes!
!  first, then other data types at mandatory levels, then left     !
!  over data at significant levels.                                !
!                                                                  !
!                                                                  !
C------------------------------------------------------------------C
C**********************************************************************
C  Data parallel implementation (CM Fortran) : M. Makivic (NPAC)      *
C  Date:       4/94                                                   *
C**********************************************************************

      include 'params.h'
      include 'processors.h'
      include 'templates.h'

      integer NumMinVols,mv_begin,mv_end
      real  alat(maxnrep), alon(maxnrep), data(nobs,NRECORDS), 
     &      datalist(NumMinVols,mxreps,50),
     &      dist(maxnrep),pres(maxnrep), trigtab(450*ntrig+1)
!hpf$ distribute alat(block) onto PROC_ARRAY_1D
!hpf$ align with alat:: alon,dist,pres
!hpf$ distribute datalist(block,*,*) onto PROC_ARRAY_1D
!hpf$ distribute data(block,*) onto PROC_ARRAY_1D

!hpf$ template, dimension(NumMinVols):: tempMV 
!hpf$ distribute tempMV(block) onto PROC_ARRAY_1D

      integer  indexd(maxnrep),
     &         itype(maxnrep), ixkxs(maxnrep),
     &         knt(NumMinVols,0:4), nreps(NumMinVols,50),
     &         num(NumMinVols,0:4)
!hpf$ align with alat:: indexd,itype,ixkxs

!hpf$ align knt(:,*) with tempMV
!hpf$ align nreps(:,*) with tempMV
!hpf$ align num(:,*) with tempMV

      logical  first, onestage, qprint

      character *60  message
      integer mv_start, mv_finish, max_batch_size, batch_size
      integer mv_new_start,start,finish
      logical mv_done
      integer F,L,N

 
      common /sparam/ lsolve, ns, nr, nv, nh(13104), nq,
     &                kq(3), volcentre_x(13104),
     &                ngv, volcentre_y(13104), glatup(13104),
     &                glonup(13104)
!hpf$ align with temp1D:: glatup,glonup,nh,volcentre_x,volcentre_y


      COMMON /DATAPTRS/ IKX, ILAT, ILON, IPRS, ITYP, IDEL, IFCE
 
      COMMON /scan/     maxobs, xscan, onestage, max_data  
 
      COMMON /GRIDXX/   IM,      JNP,     MLEV
 
      COMMON /OFFDATA/  KOFFLV(mxqobs)
 
      COMMON /PMH/      PMAND(18)
 
      COMMON /TRIGCOM/  NTRIGS, TRIGCON, TRIGTAB

      real, allocatable:: Tdist(:),Tixkxs(:),Tpres(:)
      integer, allocatable:: Titype(:),Tmarkoff(:),Tlabel(:)
      integer, allocatable:: Tknt(:,:),Tiflag(:,:),Tadd(:,:)
      integer, allocatable:: Tindexd(:)
      integer, allocatable:: Findx(:)
      integer, allocatable:: mv_mask(:),ind_mv(:)
!hpf$ distribute Tdist(block) onto PROC_ARRAY_1D
!hpf$ distribute Tixkxs(block) onto PROC_ARRAY_1D
!hpf$ distribute Tpres(block) onto PROC_ARRAY_1D
!hpf$ distribute Titype(block) onto PROC_ARRAY_1D
!hpf$ distribute Tmarkoff(block) onto PROC_ARRAY_1D
!hpf$ distribute Tlabel(block) onto PROC_ARRAY_1D
!hpf$ distribute Tknt(block,*) onto PROC_ARRAY_1D
!hpf$ distribute Tiflag(block,*) onto PROC_ARRAY_1D
!hpf$ distribute Findx(block) onto PROC_ARRAY_1D
!hpf$ distribute Tindexd(block) onto PROC_ARRAY_1D
!hpf$ distribute mv_mask(block) onto PROC_ARRAY_1D
!hpf$ distribute ind_mv(block) onto PROC_ARRAY_1D
!hpf$ distribute Tadd(block,*) onto PROC_ARRAY_1D

      integer, dimension(1:NumMinVols):: maxp,maxsn
      integer, dimension(1:NumMinVols):: ngot,need
!hpf$ align with tempMV:: maxp,maxsn,ngot,need

      data first/.true./
 
      real,allocatable:: Fdist(:),Frdist(:),Fldist(:)
      integer,allocatable:: Findexd(:),Frindexd(:),Flindexd(:)
      integer,allocatable:: Fitype(:),Fritype(:),Flitype(:)
      integer,allocatable:: Fsave(:),Flabel(:),Fmv_mask(:)
      integer,allocatable:: Fsegment(:),even(:),odd(:)
      integer,allocatable:: mask1(:),irep(:),imv(:)
      integer,allocatable:: accept(:),ncat(:)

!hpf$ distribute Fdist(block) onto PROC_ARRAY_1D
!hpf$ distribute Frdist(block) onto PROC_ARRAY_1D
!hpf$ distribute Fldist(block) onto PROC_ARRAY_1D
!hpf$ distribute Findexd(block) onto PROC_ARRAY_1D
!hpf$ distribute Frindexd(block) onto PROC_ARRAY_1D
!hpf$ distribute Flindexd(block) onto PROC_ARRAY_1D
!hpf$ distribute Fitype(block) onto PROC_ARRAY_1D
!hpf$ distribute Fritype(block) onto PROC_ARRAY_1D
!hpf$ distribute Flitype(block) onto PROC_ARRAY_1D
!hpf$ distribute Fsave(block) onto PROC_ARRAY_1D
!hpf$ distribute Flabel(block) onto PROC_ARRAY_1D
!hpf$ distribute Fmv_mask(block) onto PROC_ARRAY_1D
!hpf$ distribute Fsegment(block) onto PROC_ARRAY_1D
!hpf$ distribute even(block) onto PROC_ARRAY_1D
!hpf$ distribute odd(block) onto PROC_ARRAY_1D
!hpf$ distribute mask1(block) onto PROC_ARRAY_1D
!hpf$ distribute irep(block) onto PROC_ARRAY_1D
!hpf$ distribute imv(block) onto PROC_ARRAY_1D
!hpf$ distribute accept(block) onto PROC_ARRAY_1D
!hpf$ distribute ncat(block) onto PROC_ARRAY_1D


      message(1:60) = ' '
      write(6,*)'from search ',max_batch_size, ' mv_start= ',mv_start
      if(mv_end.ne.(mv_begin+NumMinVols-1)) then
         print *, 'MV indices messed up!'
         stop
      end if
 
         if ( ntrig .ne. ntrigs ) then
           message = 'SEARCH: NTRIG does not equal NTRIGS.'
           call flash ( 5,message,1 )
         end if

         call getconh( 'RSCAN   ', RSCAN )
         call getconh( 'RADE    ', RADE )
         call getconi( 'MAXREP  ', MAXREP )

      allocate(Tdist(max_batch_size))
      Tdist=0
      allocate(Tixkxs(max_batch_size))
      Tixkxs=0
      allocate(Tpres(max_batch_size))
      Tpres=0
      allocate(Titype(max_batch_size))
      Titype=0
      allocate(Tmarkoff(max_batch_size))
      Tmarkoff=0
      allocate(Tlabel(max_batch_size))
      Tlabel=0
      allocate(Tindexd(max_batch_size))
      Tindexd=0

c***********************************************
c      Begin serial DO loop over mini-volumes
c***********************************************
      finish=0
      batch_size=0

      do igp=mv_start,NumMinVols

      start=finish+1
      print *,'doing dgsort for igp= ',igp
 
!  Gather all data within RSCAN kilometres of this data volume.

      vollat = volcentre_y( mv_begin + igp -1 )
      vollon = volcentre_x( mv_begin + igp -1 )

      call dgsort (data(:,ind_lat),  
     &             data(:,ind_lon),  
     &             data(:,ind_type), 
     &             data(:,ind_pr),
     &             data(:,ind_kx),   
     &             nobs,        nrep,        maxnrep,
     &             indexd,      pres,        alat,        alon,
     &             ixkxs,       itype,       rscan,       rade,
     &             vollat,      vollon,
     &             ntrigs,      trigcon,     trigtab,     dist        )
 
      if ( nrep .eq. 0 ) then
         nreps ( igp, lu : ld : nv ) = 0

         if(igp.eq.NumMinVols) then
           mv_done=.true.
           write(6,*) 'Doing last mini-volume in the batch '
           mv_finish=NumMinVols
         end if

      else
 
         batch_size=batch_size+nrep
         print *, 'batch_size= ',batch_size, igp, nrep
         mv_done=.false.

         if ( batch_size .gt. max_batch_size ) then
           mv_new_start=igp
         print *, 'mv_new_start= ',mv_new_start
           mv_done=.false.
           mv_finish=igp-1
           batch_size=batch_size-nrep
           write(6,*) 'batch_size exceeded with MV # ',igp
           go to 200
         end if

         if ( nrep .gt. maxnrep ) then
           write(6,'(" NREP:",i10,3x," MAXNREP:",i10,"  (Fatal)")')
     &       nrep, maxnrep
           message = 'SEARCH: NREP exceeds MAXNREP.'
           mv_done=.false.
           call flash ( 5,message,1 )
         end if
   
         if(igp.eq.NumMinVols) then
           mv_done=.true.
           write(6,*) 'Doing last mini-volume in the batch '
           mv_finish=NumMinVols
         end if


!  Set MARKOFF = 1 for off-level and other secondary data types,
!  otherwise zero.
 
          finish=start+nrep-1
          Tdist(start:finish)=dist(1:nrep)
          Tixkxs(start:finish)=ixkxs(1:nrep)
          Tindexd(start:finish)=indexd(1:nrep)
          Tpres(start:finish)=pres(1:nrep)
          Titype(start:finish)=itype(1:nrep)
          Tmarkoff (start:finish) = kofflv( ixkxs( 1:nrep ))
          Tlabel   (start:finish) = igp
    
      end if
c********************************************************
c     Finish serial Do loop over mini-volumes
c********************************************************
      end do
 
200   continue
      if(batch_size.eq.0) then
            mv_done=.true.
            mv_finish=igp
            go to 1000
      end if
      allocate(Tknt(batch_size,0:4))
      Tknt=0
      allocate(Tiflag(batch_size,0:4))
      Tiflag=0
      allocate(Tadd(batch_size,0:4))
      Tadd=0
      allocate(Findx(batch_size))
      Findx=0
      allocate(mv_mask(batch_size))
      mv_mask=Tlabel(1:batch_size)
      where(mv_mask.eq.cshift(mv_mask,dim=1,shift=-1))
           mv_mask=0
      else where
           mv_mask=1
      end where
      allocate(ind_mv(batch_size))
      ind_mv=mv_mask*Tlabel(1:batch_size)

C  LOOP OVER THE GRID VOLUME LEVELS
      DO 300 LEV = LU, LD, NV
 
         IF( LEV.LE.MLEV ) THEN
 
C  START UPPER AIR DATA SELECTION
            NUMCAT = 4
            PABOVE = 0.0
            IF( LEV.GT.1 ) PABOVE = PMAND(LEV-1)
            PBELOW = 2000.0
            IF( (LEV+NV).LE.MLEV ) PBELOW = PMAND(LEV+NV)
 
!  Initialize flags for selecting upper air data.
!
!    0    for rawinsonde data
!    1    for data at pres = pabove with markoff = 0
!    2    for data at pres = pbelow with markoff = 0
!    3    for data between pres = pabove and  pres = pbelow   with
!         markoff = 0
!    4    for data between pres = pabove and  pres = pbelow   with
!         markoff = 1
 
 
            Tiflag  = 0
            N=batch_size
 
 
!  Count and flag the available data.

      where(( Tdist(1:N) .le. rscan )
     &                 .and.
     &        ( Tixkxs(1:N) .eq. 7 )
     &                 .and.
     &      (Tpres(1:N).ge.pabove .and. Tpres(1:N).le.pbelow))
                           Tiflag( 1:N,0 ) = 1 - Tmarkoff(1:N)
      end where
 
      where (( Tdist(1:N) .le. rscan )
     &              .and.
     &      (.not.( Tixkxs(1:N) .eq. 7 )) 
     &              .and.
     &      ( Tpres(1:N).eq.pabove ))
 
                            Tiflag(1:N,1) = 1 - Tmarkoff(1:N)
      endwhere

      where (( Tdist(1:N) .le. rscan )
     &              .and.
     &      (.not.( Tixkxs(1:N) .eq. 7 )) 
     &              .and.
     &      ( Tpres(1:N).eq.pbelow ))
                            Tiflag(1:N,2) = 1 - Tmarkoff(1:N)
      end where
 
      where (( Tdist(1:N) .le. rscan )
     &              .and.
     &      (.not.( Tixkxs(1:N) .eq. 7 )) 
     &              .and.
     $ ((Tpres(1:N).gt.pabove).and.(Tpres(1:N).lt.pbelow)))
                     Tiflag(1:N,3) = 1 - Tmarkoff(1:N)
                     Tiflag(1:N,4) = Tmarkoff(1:N)
      end where
 

 
      do j=0,4
       Tadd(1:N,j) = Tiflag(1:N,j)*Titype(1:N)
      end do
      
c      forall(i=1:N)  Tknt(i,0)= 
c     &  SUM(Tadd(i:N,0),mask=Tlabel(i:N).eq.Tlabel(i),dim=1)
c      forall(i=1:N)  Tknt(i,1)= 
c     &  SUM(Tadd(i:N,1),mask=Tlabel(i:N).eq.Tlabel(i),dim=1)
c      forall(i=1:N)  Tknt(i,2)= 
c     &  SUM(Tadd(i:N,2),mask=Tlabel(i:N).eq.Tlabel(i),dim=1)
c      forall(i=1:N)  Tknt(i,3)= 
c     &  SUM(Tadd(i:N,3),mask=Tlabel(i:N).eq.Tlabel(i),dim=1)
c      forall(i=1:N)  Tknt(i,4)= 
c     &  SUM(Tadd(i:N,4),mask=Tlabel(i:N).eq.Tlabel(i),dim=1)
 
      do i=1,N  
        Tknt(i,0)= 
     &  SUM(Tadd(i:N,0),mask=Tlabel(i:N).eq.Tlabel(i),dim=1)
        Tknt(i,1)= 
     &  SUM(Tadd(i:N,1),mask=Tlabel(i:N).eq.Tlabel(i),dim=1)
        Tknt(i,2)= 
     &  SUM(Tadd(i:N,2),mask=Tlabel(i:N).eq.Tlabel(i),dim=1)
        Tknt(i,3)= 
     &  SUM(Tadd(i:N,3),mask=Tlabel(i:N).eq.Tlabel(i),dim=1)
        Tknt(i,4)= 
     &  SUM(Tadd(i:N,4),mask=Tlabel(i:N).eq.Tlabel(i),dim=1)
      end do

      forall(i=1:N,ind_mv(i).ne.0) knt(ind_mv(i),:)=Tknt(i,:)
 
            maxdat  = maxrep * 3
 
! Initially request 60% of the data to be rawinsondes.
            F=mv_start
            L=mv_finish
 
            num(F:L,0)  = min0( knt(F:L,0), maxdat )
            num(F:L,0)  = num(F:L,0) * .6
 
            maxp   = (maxdat-num(F:L,0)) * percenta
            maxsn = -maxp
            num(F:L,1) = min0( knt(F:L,1), maxp )
            maxp   = (maxdat-num(F:L,0)) * percentb
            maxsn = maxsn-maxp
            num(F:L,2) = min0( knt(F:L,2), maxp )
            maxp   = (maxdat-num(F:L,0)) * percents
            maxsn = maxsn-maxp
            num(F:L,3) = min0( knt(F:L,3), maxp )
            maxsn   = maxsn+(maxdat-num(F:L,0)) 
            num(F:L,4) = min0( knt(F:L,4), maxsn )
 
            ngot(F:L) = 
     &      num(F:L,1)+num(F:L,2)+num(F:L,3)+num(F:L,4)+num(F:L,0)
 
 
            WHERE( NGOT(F:L).LT.MAXDAT )
C  ADD SOME MORE DATA WITH MARKOFF=0 BETWEEN PABOVE AND PBELOW
               NEED(F:L)   = MAXDAT - NGOT(F:L)
               NGOT(F:L)   = NGOT(F:L) - NUM(F:L,3)
               NUM(F:L,3)=MIN0(KNT(F:L,3),NEED(F:L)+NUM(F:L,3))
               NGOT(F:L)   = NGOT(F:L) + NUM(F:L,3)
            END WHERE
 
            WHERE( NGOT(F:L).LT.MAXDAT ) 
C  ADD SOME MORE DATA WITH MARKOFF=0 AT PABOVE
               NEED(F:L)   = MAXDAT - NGOT(F:L)
               NGOT(F:L)   = NGOT(F:L) - NUM(F:L,1)
               NUM(F:L,1)=MIN0(KNT(F:L,1),NEED(F:L)+NUM(F:L,1))
               NGOT(F:L)   = NGOT(F:L) + NUM(F:L,1)
            END WHERE
 
            WHERE( NGOT(F:L).LT.MAXDAT )
C  ADD SOME MORE DATA WITH MARKOFF=0 AT BBELOW
               NEED(F:L)   = MAXDAT - NGOT(F:L)
               NGOT(F:L)   = NGOT(F:L) - NUM(F:L,2)
               NUM(F:L,2)=MIN0(KNT(F:L,2),NEED(F:L)+NUM(F:L,2))
               NGOT(F:L)   = NGOT(F:L) + NUM(F:L,2)
            END WHERE
 
            WHERE( NGOT(F:L).LT.MAXDAT )
C  ADD SOME MORE DATA WITH MARKOFF=1 BETWEEN PABOVE AND PBELOW
               NEED(F:L)   = MAXDAT - NGOT(F:L)
               NUM(F:L,4)=MIN0(KNT(F:L,4),NEED(F:L)+NUM(F:L,4))
            END WHERE

 
      ELSE
 
C  SEA LEVEL DATA SELECTION

       NUMCAT = 1
       Tiflag = 0
       maxdat  = maxrep * 3
       where(Tdist(1:batch_size).le.rscan) Tiflag(:,1)=1
       Tadd(:,1)=Tiflag(:,1)*Titype(1:batch_size)
       N=batch_size
c       forall(i=1:N)  Tknt(i,1)= 
c     & SUM(Tadd(i:N,1),mask=Tlabel(i:N).eq.Tlabel(i),dim=1)

       do i=1,N
         Tknt(i,1)= 
     &   SUM(Tadd(i:N,1),mask=Tlabel(i:N).eq.Tlabel(i),dim=1)
       end do

       forall(i=1:N,ind_mv(i).ne.0) knt(ind_mv(i),1)=Tknt(i,1)

       F=mv_start
       L=mv_finish
       NUM(F:L,1) = MIN0( KNT(F:L,1), MAXDAT )



      ENDIF
 
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

c         forall(i=1:batch_size,Tiflag(i,icat).eq.1)
c     &     Findx(i)=
c     &   SUM(Tiflag(1:i,icat),dim=1,mask=Tiflag(1:i,icat).eq.1)
         do i=1,batch_size 
           if(Tiflag(i,icat).eq.1) then
             Findx(i)=
     &       SUM(Tiflag(1:i,icat),dim=1,mask=Tiflag(1:i,icat).eq.1)
           end if
         end do

         NFLAG=maxval(Findx)

         if(nflag.eq.0) go to 280
         if(nflag.gt.batch_size) then
             write(6,*) 'FORALL messed up again'
         endif

         allocate(Fdist(nflag))
         Fdist=0
         allocate(Frdist(nflag))
         Frdist=0
         allocate(Fldist(nflag))
         Fldist=0
         allocate(Fsave(nflag))
         Fsave=0
         allocate(Findexd(nflag))
         Findexd=0
         allocate(Frindexd(nflag))
         Frindexd=0
         allocate(Flindexd(nflag))
         Flindexd=0
         allocate(Fitype(nflag))
         Fitype=0
         allocate(Fritype(nflag))
         Fritype=0
         allocate(Flitype(nflag))
         Flitype=0
         allocate(Flabel(nflag))
         Flabel=0
         allocate(Fmv_mask(nflag))
         Fmv_mask=0
         allocate(Fsegment(nflag))
         Fsegment=0
         allocate(even(nflag))
         even=0
         allocate(odd(nflag))
         odd=0
         allocate(mask1(nflag))
         mask1=0
         allocate(irep(nflag))
         irep=0
         allocate(imv(nflag))
         imv=0
         allocate(accept(nflag))
         accept=0
         allocate(ncat(nflag))
         ncat=0

         forall(i=1:batch_size,Findx(i).ne.0)
     &      Fdist(Findx(i))=Tdist(i)
         forall(i=1:batch_size,Findx(i).ne.0)
     &      Findexd(Findx(i))=Tindexd(i)
         forall(i=1:batch_size,Findx(i).ne.0)
     &      Flabel(Findx(i))=Tlabel(i)
         forall(i=1:batch_size,Findx(i).ne.0)
     &      Fitype(Findx(i))=Titype(i)

         where(Flabel.eq.cshift(Flabel,dim=1,shift=-1))
           Fmv_mask=0
         else where
           Fmv_mask=1
         end where

         where(Flabel.eq.cshift(Flabel,dim=1,shift=+1))
           Fsegment=1
         else where
           Fsegment=0
         end where

         even=0
         odd=0
         even(2:NFLAG:2)=1
         odd(1:NFLAG:2) =1


100      continue
         Fsave=Findexd

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

         where((even.eq.1).and.(Fsegment.eq.1).and.
     &         (Fdist.gt.Frdist))
            mask1=1
         else where
            mask1=0
         end where
         mask1=mask1+eoshift(mask1,dim=1,shift=-1)

         where(mask1.eq.1)
                Fdist=Frdist*even + Fldist*odd
                Findexd=Frindexd*even + Flindexd*odd
                Fitype=Fritype*even + Flitype*odd
         end where

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

         where((odd.eq.1).and.(Fsegment.eq.1).and.
     &         (Fdist.gt.Frdist))
            mask1=1
         else where
            mask1=0
         end where
         mask1=mask1+eoshift(mask1,dim=1,shift=-1)

         where(mask1.eq.1)
                Fdist=Frdist*odd + Fldist*even
                Findexd=Frindexd*odd+ Flindexd*even
                Fitype=Fritype*odd + Flitype*even
         end where

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

         accept=0
         forall(i=1:nflag,ncat(i).le.num(Flabel(i),icat))
     &    accept(i)=1

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

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


         if(maxval(irep).gt.mxreps) 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)
c         forall(igp=mv_start:mv_finish)
c     &   nreps(igp,lev)=sum(accept,mask=Flabel.eq.igp)
         do igp=mv_start,mv_finish
           nreps(igp,lev)=sum(accept,mask=Flabel.eq.igp)
         end do
         

         deallocate(Fdist)
         deallocate(Frdist)
         deallocate(Fldist)
         deallocate(Fsave)
         deallocate(Findexd)
         deallocate(Frindexd)
         deallocate(Flindexd)
         deallocate(Fitype)
         deallocate(Fritype)
         deallocate(Flitype)
         deallocate(Flabel)
         deallocate(Fmv_mask)
         deallocate(Fsegment)
         deallocate(even)
         deallocate(odd)
         deallocate(mask1)
         deallocate(irep)
         deallocate(imv)
         deallocate(accept)
         deallocate(ncat)

280    CONTINUE
300   continue
 
1000  continue
      deallocate(Findx)
      deallocate(mv_mask)
      deallocate(ind_mv)
      mv_start=mv_new_start

      deallocate(Tdist)
      deallocate(Tixkxs)
      deallocate(Tpres)
      deallocate(Titype)
      deallocate(Tmarkoff)
      deallocate(Tlabel)
      deallocate(Tindexd)
      deallocate(Tknt)
      deallocate(Tiflag)
      deallocate(Tadd)

      RETURN
      END
