      SUBROUTINE UNIQTOORDER(nmask,ifn,ipro2,maxmask,m,npro,jp,
     * mskcode)

C     UNIQTOORDER TAKES ALL MASK FILES AND CONVERTS THE NUMBERS FROM
C     UNIQUE STATION NUMBER TO THE POSITION NUMBER OF THE STATION
C     IN THE FILE AND WRITES THIS NUMBER TO TEMPORARY MASKS

c     nmask - number of each type of mask
c     ifn - file ID number for each mask
c     ipro2 - probe type of each mask (negative=unique stat#s
c             instead of order numbers)
c     maxmask - maximum number of masks
c     m - probe type order 
c     npro - number of probes
c     jp - probe code

      character*17 charu
      dimension ifn(maxmask),ipro2(maxmask),nmask(5)

c     mskcode - Code of what type of mask each present mask is
c                1 - (.s,index,.p) sequential mask
c                2 - (.d, .2) two integer mask
c                3 - (.3) three integer
c                4 - (.4) four integer
c                5 - (.c) one string
c                6 - (.c2) two string
c                7 - (2ri) two reals, one integer
c                8 - (2r2i) two reals, two integers
c                9 - (c2i) one string, two integers

      dimension mskcode(maxmask)

      do 50 mm=m,nmask(5),npro
       if ( ipro2(mm) .lt. 0 ) then

        if ( ipro2(mm) .eq. -1000 ) then
         ipro2(mm)=0
        else
         ipro2(mm)=-ipro2(mm)
        endif

        itemp=-1

        call tempfile(itemp)

        idone=0
        icount=0
        do 55 nn=1,200000000

         if ( mskcode(mm) .eq. 1) then
          call readmask(ju,ifn(mm),idone)
         elseif ( mskcode(mm) .eq. 2 ) then
          call readmask2(ju,ifn(mm),idone,isecond)
         elseif ( mskcode(mm) .eq. 3 ) then
          call readmask3(ju,ifn(mm),idone,isecond,ithird)
         elseif ( mskcode(mm) .eq. 4 ) then
          call readmask4(ju,ifn(mm),idone,isecond,ithird,ifourth)
         elseif ( mskcode(mm) .eq. 5 ) then
          call readcmask(ju,charu,ifn(mm),idone)
         endif

         if ( idone .gt. 0 ) then
          call onefilecloseft(ifn(mm))
          ifn(mm)=itemp
          idone=0
          call filerewind(itemp)
          goto 50
         endif

         call uniqthere(jj,ju,jp)
         if ( jj .gt. -1 ) then
          if ( mskcode(mm) .eq. 1 ) then
           call writetomask(jj,icount,itemp)
          elseif ( mskcode(mm) .eq. 2 ) then
           call writetomask2(jj,icount,itemp,isecond)
          elseif ( mskcode(mm) .eq. 3 ) then
           call writetomask3(jj,icount,itemp,isecond,ithird)
          elseif ( mskcode(mm) .eq. 4 ) then
           call writetomask4(jj,icount,itemp,isecond,ithird,ifourth)
          elseif ( mskcode(mm) .eq. 5 ) then
           call writecmask(jj,charu,itemp)
           icount=icount+1
          endif
         endif

55      continue

       endif
50    continue

      return
      end
