C******************** START FILE UFSCAV.FOR ; GROUP UFILS0 ******************
C---------------------------------------------------------------
C  UFSCAV
C  AVERAGE TOGETHER TWO SCALAR LISTS
C  LIST1 CONTAINS CUMMULATIVE AVG OF SCALARS FROM NSHOTS SHOTS
C   ACCUMULATED "AVERAGING WEIGHT" ZWGHT1
C  LIST2 CONTAINS SCALARS FROM A NEW SHOT TO BE AVERAGED IN
C   INCREMENTAL NEW WEIGHT ZWGHT2
C  ANY SCALAR QUANTITY OCCURRING IN LIST2 BUT NOT IN LIST1 IS
C  INSERTED IN LIST1
C  IF THERE ARE SCALAR QUANTITIES IN LIST1 BUT NOT IN LIST2
C  THOSE QUANTITIES ARE NOT AFFECTED
C
C--------------------------------------------
C  MOD DMC - WEIGHTING SCHEME GENERALIZED
C   CALLING ARGUMENTS CHANGED 16 JULY 1982
C   FIND ARGUMENTS ZWGHT1,ZWGHT2
C    IN PLACE OF OLD ARGUMENT NSHOTS
C--------------------------------------------
C
      SUBROUTINE UFSCAV(ZLIS1,ZVAL1,IORD1,NL1,
     >     ZLIS2,ZVAL2,IORD2,NL2,ZWGHT1,ZWGHT2,NDIM)
C
      USE UFILES
C
      CHARACTER*30 ZLIS1(NDIM),ZLIS2(NDIM)
      REAL ZVAL1(NDIM),ZVAL2(NDIM)
      INTEGER IORD1(NDIM),IORD2(NDIM)
C
C  CALCULATE ORDER ON EACH LIST
C
      IF(NL2.EQ.0) RETURN
      IF(NL1.GT.0) THEN
        CALL UFSCOR(NL1,ZLIS1,IORD1)
        CALL UFSCOR(NL2,ZLIS2,IORD2)
      ELSE
        DO 40 I=1,NL2
C  IF NO PRIOR AVG DATA DON'T CHANGE ORDER OF INPUT DATA
        IORD2(I)=I
 40     CONTINUE
      ENDIF
C
C  SCAN THRU LIST 2.  CHECK FOR OCCURANCE OF EACH KEY IN LIST 2 IN
C  LIST 1.  IF THE KEY OCCURS AVG THE TWO CORRESPONDING VALUES
C  WEIGHTING BY NSHOTS.  IF THE KEY DOES NOT OCCUR IN LIST 1 ADD THE
C  KEY AND VALUE TO LIST 1.
C
      DO 100 ISC2=1,NL2
        IP2=IORD2(ISC2)
        IF(NL1.EQ.0) GO TO 80
        ILOC1=IUFSLO(ZLIS2(IP2),NL1,ZLIS1,IORD1,IEQ)
        IP1=IORD1(ILOC1)
        IF(IEQ.NE.1) GO TO 80
C  ELEMENT FOUND
        ZW1=ZWGHT1/(ZWGHT1+ZWGHT2)
        ZW2=1.0-ZW1
        ZVAL1(IP1)=ZW1*ZVAL1(IP1)+ZW2*ZVAL2(IP2)
        GO TO 100
C  ELEMENT NOT FOUND:  ADD IT TO THE LIST
 80     CONTINUE
        IL1=NL1
        NL1=NL1+1
        IF(NL1.GT.NDIM) GO TO 9000
        ZVAL1(NL1)=ZVAL2(IP2)
        ZLIS1(NL1)=ZLIS2(IP2)
        IF(NL1.EQ.1) IORD1(NL1)=NL1
        IF(NL1.GT.1) CALL UFSCIN(ILOC1,IL1,NDIM,IORD1)
 100  CONTINUE
      RETURN
C  ERROR-- NOT ENOUGH ARRAY SPACE
 9000 CONTINUE
      WRITE(UFELUN,9001) NDIM
 9001 FORMAT(' % UFSCAV WARNING-- RAN OUT OF ARRAY SPACE TRYING'/
     >'   TO ADD ELEMENT "',A10,'" TO AVERAGED DATA SCALAR LIST'/
     >'   ARRAY DIMENSION NDIM= ',I3,' NOT ADEQUATE; SCALAR DATA LOST'/
     >'   HIT CARRIAGE RETURN--')
      CALL UFPAUS(1)
      NL1=NL1-1
      RETURN
      END
C******************** END FILE UFSCAV.FOR ; GROUP UFILS0 ******************
