      SUBROUTINE VAVG(VECR, NVEC, VMEAN, VSTDEV)
C
C***********************************************************************
C
C   NAME- VAVG
C
C   LANGUAGE- FORTRAN 77     TYPE- SUBROUTINE        COMPUTER- CRAY     
C
C   VERSION- 1.0    DATE- 07/13/89   PROGRAMMER- S.SWEETLAND (SMSRC)
C   VERSION- 3.0    DATE- 05/13/96   PROGRAMMER- A.POZDNYAK (FDC)   
C     FOR REHOSTING TO THE CRAY, CHANGED ALL I*4 DECLARATIONS TO
C     INTEGER AND ALL R*4 DECLARATIONS TO REAL.
C   VERSION- 4.0    DATE- 02/27/03   PROGRAMMER- Rickey Rollins (CSC)
C        Replace R4SPV equivalence test with ISR4SPV function
C
C   FUNCTION- COMPUTES THE MEAN AND STANDARD DEVIATION OF THE ELEMENTS
C             IN A VECTOR.
C
C   CALLING SEQUENCE PARAMETERS-
C
C     NAME   TYPE   I/O    DESCRIPTION
C     ----   ----   ---    ------------
C     VECR   REAL   I      REAL VECTOR OF DIMENSION NVEC.
C
C     NVEC   INT    I      NUMBER OF ELEMENTS IN VECR.
C
C     VMEAN  REAL   O      VECTOR MEAN.
C
C     VSTDEV REAL   O      VECTOR STANDARD DEVIATION.
C
C   LOCAL PARAMETERS-
C
C     NAME   TYPE         DESCRIPTION
C     ----   ----         ------------
C     TEMP   REAL         DIFFERENCE OF AN ELEMENT FROM THE MEAN.
C
C     NVALOK  INT         NUMBER OF NVEC VALUES OK.
C
C     VARANC REAL         VARIANCE.
C
C     J       INT         DO LOOP VARIABLE.
C
C   SUBROUTINES CALLED - NONE
C
C   FUNCTIONS USED - SQRT
C
C   EXIT STATES - NONE
C
C   NOTES-
C     1) INPUT DATA ASSUMED TO CONTAIN VALID DATA.
C     2) PRIOR TO CALLING VAVG, NVEC MUST BE CHECKED TO BE
C            SURE THAT ITS VALUE IS > 1 FOR THE CALCULATION TO
C            HAVE MEANINGFUL RESULTS.
C     3) THE ROUTINE WILL NOT PERFORM THE STANDARD DEVIATION
C        IF LESS THAN TWO GOOD VALUES ARE FOUND IN THE VECTOR.
C
C***********************************************************************
C
      INTEGER   NVEC, NVALOK,J
      REAL      VMEAN, VARANC, VSTDEV, VECR(NVEC)
      LOGICAL   ISR4SPV
C
C   HOLD AREA FOR INTERMEDIATE COMPUTATION.
C
      REAL   TEMP
C
      INCLUDE 'SPVAL'
C
      VMEAN = 0.
      NVALOK = 0
      VSTDEV = R4SPV
C
C   COMPUTE THE MEAN.
      DO 10 J = 1,NVEC
        IF(.NOT. ISR4SPV(VECR(J))) THEN
            VMEAN = VMEAN + VECR(J)
            NVALOK = NVALOK +1
        END IF
   10 CONTINUE
C
       IF (NVALOK .GE. 1) THEN
          VMEAN = VMEAN / NVALOK
          IF (NVALOK .EQ. 1) RETURN
       ELSE
          VMEAN = R4SPV
          RETURN
       ENDIF
C
C   COMPUTE THE VARIANCE AS A PRE-STEP TO COMPUTE THE STANDARD
C   DEVIATION.
C
      VARANC = 0.
      DO 20 J = 1,NVEC
         IF (.NOT. ISR4SPV(VECR(J))) THEN
             TEMP = VECR(J) - VMEAN
             VARANC = VARANC + (TEMP * TEMP)/(NVALOK - 1)
         END IF
   20 CONTINUE
C
C   COMPUTE THE STANDARD DEVIATION FOR THE NUMBER OF ELEMENTS
C   OF THE INPUT VECTOR VECR. NOTE, IN ORDER TO COMPUTE THE
C   STANDARD DEVIATION, NUMBER OF ELEMENTS (NVEC) OF THE INPUT
C   VECTOR (VECR) SHOULD BE EQUAL TO OR GREATER THAN 2.
C
       IF(NVALOK .GE. 2) VSTDEV=SQRT(VARANC)
C
      RETURN
      END
