      SUBROUTINE writeout(isite)
c============================================================================
c PROCEDURE NAME: 	_writeout
c PROCEDURE TYPE: 	_subroutine
c VERSION DATE:		_10.III.98
c
c PURPOSE:
c
c CALLING PARAMETERS:
c   Name	Type	i/o	units	Description
c   --------	----	---	-----	--------------
c   isite        i4      i              Site number
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 isite

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

c----------------------------------------------------------------------------
c Local variables
c
      INTEGER*4 ipos, ilist, stick(max_vicin), jpos, j
      INTEGER*4 ovplun /8/, str_1st_blank, l1
      REAL*8 oldtime
      CHARACTER*3 stn
      CHARACTER*60 basename

c============================================================================


c--Compose the list in the right order

      ipos=0
      ilist= ptr(0,isite)
  100 CONTINUE
        ipos= ipos+1
        stick(ipos)= ilist
        ilist= ptr(ilist,isite)
        if(ilist .NE. 0) GOTO 100


c--Write out the list

        basename= ovxdir(1:str_1st_blank(ovxdir)-1)//'/'//
     &                            ovxname(1:str_1st_blank(ovxname)-1)
        l1= str_1st_blank(basename)-1
        WRITE(stn,2) siteno(isite)
        OPEN(UNIT=ovplun,
     &       FILE=basename(1:l1) // stn,
     &       FORM='formatted',
     &       ACCESS='append')

      oldtime= scantime(stick(n_fov(isite)),isite)
c
c=======================================================================


      DO ipos= n_fov(isite), 1, -1
        jpos= stick(ipos)

        WRITE(ovplun,3)  oldtime,
     &                   n_fov(isite)-ipos +1,
     &                   filehndl(jpos, isite),
     &                   filerec(jpos, isite),
     &                   scanpos(jpos, isite),
     &                   gmtsec(jpos,isite)


      END DO

      CLOSE(ovplun)

c--Clear the lists for the overpass data

      n_fov(isite)= 0
      ptr(0,isite)= 0
      DO j= 1, max_vicin
        filehndl(j,isite)= '     '
        filerec(j,isite)= 0
        scanpos(j,isite)= 0
        gmtsec(j,isite)= 0
        costfun(j,isite)= 0.0
        scantime(j,isite)= 0.0
        ptr(j,isite)= 0
        ovplat(j,isite)= 0.0
        ovplon(j,isite)= 0.0
      END DO

      RETURN 
    1 FORMAT(I3.3,            !site no
     &       1X,F7.1,         !scan time
     &       1X,I2.2,         !order
     &       1X,A5,           !file handle
     &       1x,I4,           !file record
     &       1x,i2,           !scan position
     &       1x,i5.5,         !gmt seconds
     &       1x,f6.2,         !lat
     &       1x,f7.2,         !lon
     &       1x,1pe8.2)       !cost function

    2 FORMAT(I3.3)

    3 FORMAT(   F7.1,         !scan time
     &       1X,I2.2,         !order
     &       1X,A5,           !file handle
     &       1x,I4,           !file record
     &       1x,i2,           !scan position
     &       1x,i5.5)         !gmt seconds

      END
