      SUBROUTINE CHKCAND(NPR,IPR,LOCPR,NDATP,NAMB,IAMB,IAMBN,CAMBP,
     2                    NCAND,LCAND,R,IOFFC)

C     Check satellite pairs to see which qualify as candidates for
C     integer-cycle ambiguity resolution.  Unlike CHKCAND, CHKCAND2
C     bases its criterion on the probability accumulated in CAMBP.
C     Also, CHKCAND2 allows no more than 4 candidates.

C     Arguments:
C        NPR             [I*4]: Number of pairs of satellites that may
C                               qualify for ambiguity resolution.
C        IPR(2,*)        [I*4]: PRN's of satellites in the current pairs.
C                               For each pair the lower PRN comes first.
C        LOCPR(*)        [I*4]: Location in pairs database of corresponding
C                               satellite pair in IPR.
C        NDATP           [I*4]: Number of epochs processed for each pair.
C                               Index is location in pairs database.
C        NAMB(*)         [I*4]: Number of different ambiguities found for each
C                               pair.  Index in location in pairs database.
C        IAMB(MAXAMB,*)  [I*4]: Array of calculated integer-cycle ambiguities
C                               for each pair.  Second index is location in
C                               the pairs database.
C        IAMBN(MAXAMB,*) [I*4]: Number of epochs for which the corresponding
C                               ambiguity in IAMB is found most probable.
C        CAMBP(MAXAMB,*) [R*8]: Accumulated probability for each ambiguity.
C        NCAND           [I*4]: Number of pairs that have been identified as
C                               candidates for ambiguity resolution.
C        LCAND(*)        [I*4]: True for a pair iff that pair is a candidate
C                               for ambiguity resolution.  Index is pair no.
C        R(*)            [R*8]: For each pair, the difference between the
C                               numbers of times that the two most frequently
C                               calculated ambiguities appear, divided by the
C                               uncertainty in that difference.
C        IOFFC(*)        [I*4]: Proposed integer-cycle offset for each
C                               candidate pair. Index is pair number.


      IMPLICIT REAL*8 (A-H,O-Z)
      LOGICAL*1 LCAND

      PARAMETER (MAXAMB = 50, MAXSAT = 10, MXXSAT = 32)
      PARAMETER (MAXPRS = MAXSAT - 1)
      PARAMETER (MAXGRPS = MAXPRS)

      DIMENSION CAMBP(MAXAMB,*),IAMB(MAXAMB,*),IAMBN(MAXAMB,*),
     2   IOFFC(*),IORDER(MAXAMB),IPR(2,*),LCAND(*),LOCPR(*),NAMB(*),
     3   NDATP(*),R(*)
                                 ! SPECIAL FOR TESTING
      DATA NDMIN /6/             ! Minimum number of identical measurements
      DATA RMIN /2.8D0/          ! Significance threshold for candidate pairs


      NCAND = 0                              ! Init. number of candidate pairs
      DO  JPR=1,NPR                          ! Try each pair
         LOCDB = LOCPR(JPR)                  ! Location in pairs database
         IF (NDATP(LOCDB).GT.0)  THEN        ! Normal case: have some results
            NEP = NDATP(LOCDB)               ! Number of epochs, this pair
            NAMBP = NAMB(LOCDB)              ! No. of ambiguities, this pair
            CALL DSORT(CAMBP(1,LOCDB),NAMBP,2, IORDER)   ! Sort probs.
            IOFFC(JPR) = IAMB(IORDER(NAMBP),LOCDB)       ! Save offset
            R(JPR) =
     2       8.0D1*CAMBP(IORDER(NAMBP),LOCDB)/IAMBN(IORDER(NAMBP),LOCDB)
            WRITE(6,'(T6,''PR'',I2,'' ('',I2.2,''-'',I2.2,''):'',I5,
     2         '' EPOCHS,'',I5,'' AT'',I10,''; R ='',F5.2)' )
     3            JPR,IPR(1,JPR),IPR(2,JPR),NEP,               ! Simulate R in
     4               IAMBN(IORDER(NAMBP),LOCDB), IOFFC(JPR), R(JPR)  ! CHKCAND
            LCAND(JPR) = (NEP.GE.NDMIN).AND.(R(JPR).GE.RMIN)
            IF (LCAND(JPR))  NCAND = NCAND + 1
         ELSE                    ! All epochs rejected for this pair, so far
            R(JPR) = 0.0D0
            LCAND(JPR) = .FALSE.
            WRITE(6,'(T6,''PR'',I2,'' ('',I2.2,''-'',I2.2,
     2         ''): 0 EPOCHS'')' )  JPR,IPR(1,JPR),IPR(2,JPR)
         END IF                        ! Abbreviated output line
      END DO
                        ! TRIM LIST OF CANDIDATES TO 4 IF NECESSARY
      DO WHILE (NCAND.GT.4)         ! Eliminate one pair each iteration
         RLOW = 1.0D10                 ! Init. lowest R-value
         DO  JPR=1,NPR              ! If so, see which pair it's in
            IF (LCAND(JPR).AND.(R(JPR).LT.RLOW))  THEN
               IF (R(JPR).LT.RLOW)  THEN     ! Lowest R so far
                  JPRX = JPR                 ! Remember the pair number
                  RLOW = R(JPR)              ! and its R-value
               END IF
            END IF
         END DO
         LCAND(JPRX) = .FALSE.                     ! Delete the candidate
         NCAND = NCAND - 1                         ! having the lowest R-value
      END DO
      WRITE(6,'(T6,''CHKCAND: RETURNING'',I2,'' CANDIDATE PAIRS ['',
     2   <NPR>L2,'']'')' )  NCAND, (LCAND(I), I=1,NPR)

      RETURN
      END
