ed getopt.for
cccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccccc
c      program psort

c     Sorts a post file by one post option.

      parameter (MAXDIM=60000)
      character order*1
      character*8 id
      real post(8),  array(MAXDIM)                        
      integer ipoint(60000)
  
      call getfile2(21,' Infile ','in','unformatted','.pos')
      call getfile2(22,' Outfile ','out','unformatted','.srt') 
c       make da file for use in sort
      lrec=10
      open(11, file='tmp1', form='unformatted',status='unknown',
     1 access='direct',recl=4*lrec)

      print *, ' '
      print *, ' To sort on Longitude, answer -1 ...'
      print *, ' To sort on Latitude,  answer  0 ....'
      print *, ' To sort on one of the fields answer 1 to 6'
      iasc = 1
      call getint (' Give post option to sort on (-1 to 6)', 1, iopt)
      iopt = iopt + 2

      call getopt (' Start with low or high value?','l/h','l', order)
      if (order .eq.'h') iasc = -1

c     Read observations.
      n = 0
 10   n = n + 1
      if (n.gt.MAXDIM) then
        print *, ' ****  ERROR sort array not big enough...'
        stop
      endif
      call getpost (21, id, post, *50)
c      idall (n) = id
c      do 20 i=1,8
c 20   postall(i,n) = post(i)
      array(n) = iasc * post(iopt)   
c     write d.a. file of records
        write(11,rec=n) id,post
        ipoint(n)=n

      go to 10

 50   close (21)
      ntot = n - 1
      print *, ' '
      print *, '    Number of records to sort = ', ntot

c     Sort
        call rosort(array,ipoint,ntot)
c       generate sorted data file
        do 100 i=1,ntot
        irec=ipoint(i)
        read(11,rec=irec) id,post
100     call putpost(22,id,post)
        close(11,status='delete')

 90   call closeblp (22)

      stop ' '
      end


 
