C
C     ..................................................................
C
C        SUBROUTINE AVCAL
C
C        PURPOSE
C           PERFORM THE CALCULUS OF A FACTORIAL EXPERIMENT USING
C           OPERATOR SIGMA AND OPERATOR DELTA.  THIS SUBROUTINE IS
C           PRECEDED BY SUBROUTINE ADVAT AND FOLLOWED BY SUBROUTINE
C           MEANQ IN THE PERFORMANCE OF ANALYSIS OF VARIANCE FOR A
C           COMPLETE FACTORIAL DESIGN.
C
C        USAGE
C           CALL AVCAL (K,LEVEL,X,L,ISTEP,LASTS)
C
C        DESCRIPTION OF PARAMETERS
C           K     - NUMBER OF VARIABLES (FACTORS). K MUST BE .GT. ONE.
C           LEVEL - INPUT VECTOR OF LENGTH K CONTAINING LEVELS (CATE-
C                   GORIES) WITHIN EACH VARIABLE.
C           X     - INPUT VECTOR CONTAINING DATA.  DATA HAVE BEEN PLACED
C                   IN VECTOR X BY SUBROUTINE AVDAT.  THE LENGTH OF X
C                   IS (LEVEL(1)+1)*(LEVEL(2)+1)*...*(LEVEL(K)+1).
C           L     - THE POSITION IN VECTOR X WHERE THE LAST INPUT DATA
C                   IS LOCATED.  L HAS BEEN CALCULATED BY SUBROUTINE
C                   AVDAT.
C           ISTEP - INPUT VECTOR OF LENGTH K CONTAINING STORAGE CONTROL
C                   STEPS WHICH HAVE BEEN CALCULATED BY SUBROUTINE
C                   AVDAT.
C           LASTS - WORKING VECTOR OF LENGTH K.
C
C        REMARKS
C           THIS SUBROUTINE MUST FOLLOW SUBROUTINE AVDAT.
C
C        SUBROUTINES AND FUNCTION SUBPROGRAMS REQUIRED
C           NONE
C
C        METHOD
C           THE METHOD IS BASED ON THE TECHNIQUE DISCUSSED BY H. O.
C           HARTLEY IN 'MATHEMATICAL METHODS FOR DIGITAL COMPUTERS',
C           EDITED BY A. RALSTON AND H. WILF, JOHN WILEY AND SONS,
C           1962, CHAPTER 20.
C
C     ..................................................................
C
      SUBROUTINE AVCAL (K,LEVEL,X,L,ISTEP,LASTS)
      DIMENSION LEVEL(1),X(1),ISTEP(1),LASTS(1)
C
C        ...............................................................
C
C        IF A DOUBLE PRECISION VERSION OF THIS ROUTINE IS DESIRED, THE
C        C IN COLUMN 1 SHOULD BE REMOVED FROM THE DOUBLE PRECISION
C        STATEMENT WHICH FOLLOWS.
C
C     DOUBLE PRECISION X,SUM
C
C        THE C MUST ALSO BE REMOVED FROM DOUBLE PRECISION STATEMENTS
C        APPEARING IN OTHER ROUTINES USED IN CONJUNCTION WITH THIS
C        ROUTINE.
C
C        ...............................................................
C
C     CALCULATE THE LAST DATA POSITION OF EACH FACTOR
C
      LASTS(1)=L+1
      DO 145 I=2,K
  145 LASTS(I)=LASTS(I-1)+ISTEP(I)
C
C     PERFORM CALCULUS OF OPERATION
C
  150 DO 175 I=1,K
      L=1
      LL=1
      SUM=0.0
      NN=LEVEL(I)
      FN=NN
      INCRE=ISTEP(I)
      LAST=LASTS(I)
C
C     SIGMA OPERATION
C
  155 DO 160 J=1,NN
      SUM=SUM+X(L)
  160 L=L+INCRE
      X(L)=SUM
C
C     DELTA OPERATION
C
      DO 165 J=1,NN
      X(LL)=FN*X(LL)-SUM
  165 LL=LL+INCRE
      SUM=0.0
      IF(L-LAST) 167, 175, 175
  167 IF(L-LAST+INCRE) 168, 168, 170
  168 L=L+INCRE
      LL=LL+INCRE
      GO TO 155
  170 L=L+INCRE+1-LAST
      LL=LL+INCRE+1-LAST
      GO TO 155
  175 CONTINUE
      RETURN
      END
