      SUBROUTINE profov(ifov, isite, cost, atime)
c============================================================================
c PROCEDURE NAME: 	_profov
c PROCEDURE TYPE: 	_subroutine
c VERSION DATE:		_9.III.98
c
c PURPOSE:
c
c CALLING PARAMETERS:
c   Name	Type	i/o	units	Description
c   --------	----	---	-----	--------------
c   ifov         i4      i              fov number in current level-2 frame
c   isite        i4      i              index number of site to match
c   cost         r8      i              cost function
c   atime        r8      i      julday  absolute time of scan.
c
c EXTERNAL ROUTINES REQUIRED:
c
c NOTES & CAVEATS:
c
c REVISION HISTORY:
c
c============================================================================
      IMPLICIT NONE
c----------------------------------------------------------------------------
c Compile parameters
c
      INCLUDE "ovpindex.prm"

c----------------------------------------------------------------------------
c Calling parameters
c
      INTEGER*4 ifov, isite
      REAL*8    cost, atime

c----------------------------------------------------------------------------
c Common blocks
c
      INCLUDE "ovx.cmn"

c----------------------------------------------------------------------------
c Local variables
c
      INTEGER*4 prev, pprev, curr, j, jj, k
      LOGICAL   l1

c============================================================================
c--If this is the first fov identified for this site, start the list
c   and return.

c--Note that ptr(0,isite) is the index of the largest value in the list.
c  ptr(i,isite) points to the next smaller value (That is, ptr is a one-
c  way linked list that keeps track of the order of the list by cost function.)
c  and the list is terminated by the value 0.
c-------------------------------------------------------------------------

c--If this is the first one for this site, start the list with it.

      IF(n_fov(isite) .EQ. 0) THEN
        n_fov(isite)= 1
        CALL stuff(ifov, isite, cost,atime, 0,1,0)
        RETURN
      END IF

c--See if we're really on the next day's data

c     write(96,*)'compared to time:',scantime(ptr(0,isite),isite)
      IF(atime - scantime(ptr(0, isite), isite) .GT. 0.5) THEN

c----Write out the current overpass data (also clears the list for the site)

        CALL writeout(isite)


c----Start the list with the point that was sent in

        n_fov(isite)= 1
        CALL stuff(ifov, isite, cost,atime, 0,1,0)
        RETURN

      END IF
      

c--If c.f. is greater than all the others, and there's still room on the list,
c--just add it

      IF(cost .GT. costfun(ptr(0, isite), isite)) THEN 
        IF(n_fov(isite) .EQ. max_vicin) RETURN
        n_fov(isite)= n_fov(isite) + 1
        k= ptr(0,isite)
        CALL stuff(ifov,isite,cost,atime, 0, n_fov(isite), k)
        RETURN
      END IF


c--All other cases

      pprev=0
      prev=0

  100 CONTINUE
        curr= ptr(prev, isite)
        l1= (curr .EQ. 0 )
        IF(.NOT.l1) l1= (cost .GT. costfun(curr,isite))
        IF(l1) THEN

          IF(n_fov(isite) .EQ. max_vicin) THEN 
            j=ptr(0, isite)
            IF(pprev .NE. 0) ptr(0, isite)= ptr(j,isite)
          ELSE
            j= n_fov(isite) + 1
            n_fov(isite)= j
          END IF

          CALL stuff(ifov,isite,cost,atime, prev, j, curr)
          RETURN
        END IF

        pprev=prev
        prev=curr
        GOTO 100
      END

      SUBROUTINE stuff(ifov, isite, cost,atime, i,j,k)

      IMPLICIT NONE

      INCLUDE "ovpindex.prm"
      INCLUDE "ovpindex.cmn"
      INCLUDE "ovx.cmn"
      INCLUDE "tomsframe.cmn"
      REAL*8 cost, atime
      INTEGER*4 ifov, isite, i,j,k

      filehndl(j, isite)= yeardoy(3:7)
      filerec(j, isite)= recno
      scanpos(j, isite)= ifov
      costfun(j, isite)= cost
      scantime(j, isite)= atime
      gmtsec(j,isite)= time
      ovplat(j,isite)= lat(ifov)
      ovplon(j,isite)= long(ifov)

      ptr(i,isite)= j
      ptr(j,isite)= k

      RETURN
      END
      
