C******************** START FILE FILFAS.FOR ; GROUP FILFAS ******************
C---------------------------------------------------------------
C  FILFAS   D. Mc Cune  pppl 4 dec 1981
C  A FAST MOVING TRIANGULAR WEIGHTED AVERAGE SMOOTHING ROUTINE
C
        SUBROUTINE FILFAS(F,FSM,N,NWA)
C
        DIMENSION F(N),FSM(N)
C
C  INPUT:
C   F(N) DATA TO BE SMOOTHED
C   NWA-- NUMBER OF POINTS ON EITHER SIDE OF F(J) TO AVERAGE
C         WITH TRIANGULAR WEIGHTING TO COMPUTE FSM(J)
C
C  OUTPUT:
C   FSM(N): SMOOTHED DATA
C
C
C  ALGORITHM:
C  FSM(J) IS A WEIGHTED AVERAGE OF F(J-NWA),F(J-NWA+1),...,
C                                  F(J),...,F(J+NWA-1),F(J+NWA)
C  THE WEIGHTING FACTOR FOR
C  F(J+K) IS W(K)=(NWA+1-IABS(K))/(NWA+1)**2
C   NOTE THAT SUM[K= -NWA TO +NWA]W(K) = 1.0 EXACTLY
C
C  W(K) IS A STEP FUNCTION APPROXIMATION TO A TRIANGULAR WEIGHTING
C  FUNCTION
C
C  END CONDITIONS:  IF THE INDEX (J+K) IS .LT. 1 IT IS
C   REPLACED BY 1; IF (J+K) .GT. N IT IS REPLACED BY N.
C  THUS THE END POINTS ARE MORE HEAVILY WEIGHTED IN THE REGIONS NEAR THE
C  ENDPOINTS OF THE DATA BEING SMOOTHED
C
C  EXECUTABLE CODE
C
        IF(NWA.LE.0) GO TO 200
        INWA=NWA+1
        FAC=1.0/(INWA**2)
C  START LOOP OVER DATA POINTS
        DO 100 J=1,N
C  INITIATE SUM FOR POINT J
        SUM=F(J)*INWA
C  LOOP OVER POINTS TO AVERAGE
        DO 90 K=1,NWA
        IW=INWA-K
        IPT1=MAX0(1,(J-K))
        IPT2=MIN0(N,(J+K))
        SUM=SUM+IW*(F(IPT1)+F(IPT2))
 90     CONTINUE
        FSM(J)=SUM*FAC
 100    CONTINUE
        RETURN
C  NWA < 0: JUST COPY DATA
 200    CONTINUE
        DO 210 J=1,N
        FSM(J)=F(J)
 210    CONTINUE
        RETURN
        END
C******************** END FILE FILFAS.FOR ; GROUP FILFAS ******************
