      SUBROUTINE select_sites(lat, lon, atime, nsel, selist, success)
c============================================================================
c PROCEDURE NAME: 	_select_sites
c PROCEDURE TYPE: 	_subroutine
c VERSION DATE:		_10.II.98
c
c PURPOSE:
c
c CALLING PARAMETERS:
c   Name	Type	i/o	units	Description
c   --------	----	---	-----	--------------
c   lat          r4      i       deg    Latitude of FOV
c   lon          r4      i       deg    Logitude of FOV
c   atime        r8      i       days   Absolute time for FOV
c   nsel         i4      o              Number of selected sites
c   selist       i4      o              List of selected sites
c   success      l4      o              Successful completion flag.
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
      REAL*4    lat, lon
      REAL*8    atime
      INTEGER*4 nsel, selist(max_slct)
      LOGICAL   success

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

c----------------------------------------------------------------------------
c Local variables
c
      INTEGER*4 isite
      LOGICAL   nonpolar, grenight
      REAL*4 dlat, dlon, par
      REAL*8 radeg      /57.295779513082323/    !deg-rad conversion

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

      nsel= 0
      isite= 1

c--loop through the sites on the sitelist

  100 CONTINUE

c--check against quasi-rectangle

        IF(lat .GE. lat_min(isite) .AND. lat .LE. lat_max(isite)) THEN
          dlon= abs( lon_site(isite) - lon )
          IF (dlon .GT. 180) dlon= 360. - dlon
          IF (dlon .LE. lon_range(isite)) THEN

c--check against circle

            dlat= lat - lat_site(isite)
            par= SIN(dlat/2./radeg)**2 + SIN(dlon/2./radeg)**2 * 
     &              COS(lat_site(isite)/radeg) * COS(lat/radeg)
            IF(par .LE. acceptpar) THEN 

c--for polar sites, check time

              nonpolar= (90. - ABS(lat_site(isite)) .GT. 3.0 )
              grenight= ( DMOD(atime+0.25d0,1.d0) .LT. 0.5d0 )
              IF( nonpolar .OR. grenight )THEN

c--if we've gotten through all that. add this site to the list.

                IF(nsel .GT. max_slct) GOTO 901
                nsel= nsel + 1
                selist(nsel)= isite

              END IF
            END IF
          END IF
        END IF

        IF (isite .EQ. n_sites) GOTO 200
        isite= isite + 1
        IF (lat .GE. lat_min(isite)) GOTO 100

  200 CONTINUE
      success= .TRUE.
      RETURN

  901 CONTINUE
      WRITE(6,*)'*** SUBROUTINE select_sites ***'
      WRITE(6,*)'  selected more than the permitted number of matches'
      WRITE(6,*)'  for lon=',lon,', lat=',lat
      WRITE(6,*)'  The maximum is currently set to ',max_slct
      success= .FALSE.
      RETURN

      END
