      SUBROUTINE MASKSIFT(ity,ifilein,itemp,jp,iparm,cmask)

C     MASKSIFT SORTS A UNIQUE STATION NUMBER MASK BY
C     SEQUENTIAL ORDER

c***************************************************
c
c     Passed Variables:
c
c      itype - type of mask: 1 - (.s), 2 - (.d) , 3 - (.3), 5 - (.c)
c      ifilein - original mask file identification number
c      itemp - temporary file id number 
c      jp - probe code number for mask
c      iparm - parameter code number for mask
c      cmask - mask name
c
c**************************************************

      parameter (maxlevel=6000)

      character*17 charu,charulast
      character*80 filename
      character*(*) cmask
  
c     idlev - second and third mask values for
c             given station
c     levsort - second mask value (for sorting)

      dimension idlev(4*maxlevel,2),levsort(4*maxlevel)

      common /silent/ isilent

      isilent=1
      idone=0
      icount=0
      itx=0

c***************************************************
c
c    Set sort code from type code
c
c***************************************************

      itype=ity
      if ( itype .lt. 0 ) itype=-itype
      if ( itype .eq. 1 ) then
       isort=3
      elseif ( itype .eq. 2 ) then
       isort=1
      elseif ( itype .eq. 3 ) then
       isort=4
      elseif ( itype .eq. 5 ) then
       isort=5
      endif

c**************************************************
c
c    Replace unique station numbers with sequential order
c
c**************************************************

      if ( ity .gt. 0 ) then
       call tempfile(itemp)

       do 50 nn=1,50000000

        if ( itype .eq. 1 ) then
         call readmask(ju,ifilein,idone)
        elseif ( itype .eq. 2 ) then
         call readmask2(ju,ifilein,idone,isecond)
        elseif ( itype .eq. 3 ) then
         call readmask3(ju,ifilein,idone,isecond,ithird)
        elseif ( itype .eq. 5 ) then
         call readcmask(ju,charu,ifilein,idone)
        endif

        if ( idone .gt. 0 ) then
         call onefileclose(ifilein)
         call filerewind(itemp)
         goto 4
        endif

c       if ( ifilein .eq. 420 ) write(6,*) idone,nn,ju
        call uniqthere(jj,ju,jp)

        if ( jj .gt. -1 ) then
         if ( itype .eq. 1 )  then
          call writetomask(jj,icount,itemp)
         elseif ( itype .eq. 2 ) then
          call writetomask2(jj,icount,itemp,isecond)
         elseif ( itype .eq. 3 ) then
          call writetomask3(jj,icount,itemp,isecond,ithird)
         elseif ( itype .eq. 5 ) then
          call writecmask(jj,charu,itemp)
          icount=icount+1
         endif
        endif

50     continue
4      continue

       else
        itemp=ifilein
        ifilein=-1
        call fileend(itemp)
        if ( itype .lt. 5 ) then
         call filepos(itemp,0,itype,icount)
        else
         call filepos(itemp,4,0,icount)
        endif
        call filerewind(itemp)
       endif

c****************************************************
c
c   Sort for sequential order
c
c****************************************************

      if ( itype .eq. 5 ) then
       itemp2=itemp+1
       call tempfile(itemp2)
      else
       itemp2=itemp
      endif
      write(6,*) 'isort',isort,itemp,icount
      call sortfile(isort,itemp,icount)

      call filerewind(itemp2)

c****************************************************
c
c   Reopen unique station mask as new mask
c
c****************************************************

      call maskname(cmask,iparm,filename,jp)
      call fileopen(filename,'w+'//CHAR(0),ifilein)

c****************************************************
c  
c   Place unique station numbers back in mask,
c   excluding removed stations and duplicate entries
c
c****************************************************

      jjlast=0
      idnum=0

      jj=0
      isec=0
      ithird=0
      idtot=0

      iend=0

      do 70 j=1,icount+1

       if ( j .le. icount ) then
        if ( itype .eq. 1 ) then
         call readmask(jj,itemp2,iend)
        elseif ( itype .eq. 2 ) then
         call readmask2(jj,itemp2,iend,isec)
        elseif ( itype .eq. 3 ) then
         call readmask3(jj,itemp2,iend,isec,ithird)
        elseif ( itype .eq. 5 ) then
         call readcmask(jj,charu,itemp2,iend)
        endif
       endif

       ilast=0
       if ( j .eq. icount + 1 ) jj=-1
       if ( j .eq. 1 ) then
        jjlast=jj
        if ( itype .eq. 5 ) charulast=charu
        ilast=1
       endif

       if ( jj .eq. jjlast ) then

        ilast=1
        do 505 n3=1,idnum
         if ( isec .eq. idlev(n3,1) .and. ithird .eq.
     *        idlev(n3,2) ) then
          goto 70
         endif
505     continue

        idnum=idnum+1
        idlev(idnum,1)=isec
        idlev(idnum,2)=ithird
        levsort(idnum)=isec

       endif

       if ( ilast .eq. 0 ) then

        call uniqfind(jjlast,ju,jp,itx)
        jjlast=jj

c     sort secondary portion of array

        if ( itype .gt. 1 .and. itype .ne. 5 ) 
     * call sortarray(levsort,idnum)

        if ( ju .gt. 0 ) then

         icount2=0
         do 200 n=1,idnum

          if ( itype .eq. 1 ) then
           call writetomask(ju,icount2,ifilein)
          elseif ( itype .eq. 2 ) then
          call writetomask2(ju,icount2,ifilein,levsort(n))
          elseif ( itype .eq. 3 ) then

           do 201 nn=1,idnum

            if ( idlev(nn,1) .eq. levsort(n) ) then
             call writetomask3(ju,icount2,ifilein,idlev(nn,1),
     *                         idlev(nn,2))
             idlev(nn,1)=-1
            endif

201        continue

          elseif ( itype .eq. 5 ) then
           call writecmask(ju,charulast,ifilein)
           charulast=charu
          endif

200      continue

        endif

        idnum = 1
        idlev(idnum,1)=isec
        idlev(idnum,2)=ithird
        levsort(idnum)=isec

       endif

70    continue

c*********************************************************
c
c     Close temporary file, rewind unique station mask
c
c*********************************************************

      call onefilecloseft(itemp2)
      if ( itype .eq. 5 ) call onefilecloseft(itemp)
      call filerewind(ifilein)

      isilent=0

      return
      end
