C
C     ..................................................................
C
C        SUBROUTINE AVDAT
C
C        PURPOSE
C           PLACE DATA FOR ANALYSIS OF VARIANCE IN PROPERLY DISTRIBUTED
C           POSITIONS OF STORAGE.  THIS SUBROUTINE IS NORMALLY FOLLOWED
C           BY CALLS TO AVCAL AND MEANQ SUBROUTINES IN THE PERFORMANCE
C           OF ANALYSIS OF VARIANCE FOR A COMPLETE FACTORIAL DESIGN.
C
C        USAGE
C           CALL AVDAT (K,LEVEL,N,X,L,ISTEP,KOUNT)
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           N     - TOTAL NUMBER OF DATA POINTS READ IN.
C           X     - WHEN THE SUBROUTINE IS CALLED, THIS VECTOR CONTAINS
C                   DATA IN LOCATIONS X(1) THROUGH X(N).  UPON RETURNING
C                   TO THE CALLING ROUTINE, THE VECTOR CONTAINS THE DATA
C                   IN PROPERLY REDISTRIBUTED LOCATIONS OF VECTOR X.
C                   THE LENGTH OF VECTOR X IS CALCULATED BY (1) ADDING
C                   ONE TO EACH LEVEL OF VARIABLE AND (2) OBTAINING THE
C                   CUMULATIVE PRODUCT OF ALL LEVELS.  (THE LENGTH OF
C                   X = (LEVEL(1)+1)*(LEVEL(2)+1)*...*(LEVEL(K)+1).)
C           L     - OUTPUT VARIABLE CONTAINING THE POSITION IN VECTOR X
C                   WHERE THE LAST INPUT DATA IS STORED.
C           ISTEP - OUTPUT VECTOR OF LENGTH K CONTAINING CONTROL STEPS
C                   WHICH ARE USED TO LOCATE DATA IN PROPER POSITIONS
C                   OF VECTOR X.
C           KOUNT - WORKING VECTOR OF LENGTH K.
C
C        REMARKS
C           INPUT DATA MUST BE ARRANGED IN THE FOLLOWING MANNER.
C           CONSIDER THE 3-VARIABLE ANALYSIS OF VARIANCE DESIGN, WHERE
C           ONE VARIABLE HAS 3 LEVELS AND THE OTHER TWO VARIABLES HAVE
C           2 LEVELS.  THE DATA MAY BE REPRESENTED IN THE FORM X(I,J,K),
C           I=1,2,3  J=1,2  K=1,2.  IN ARRANGING DATA, THE INNER
C           SUBSCRIPT, NAMELY I, CHANGES FIRST.  WHEN I=3, THE NEXT
C           INNER SUBSCRIPT, J, CHANGES AND SO ON UNTIL I=3, J=2, AND
C           K=2.
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 AVDAT (K,LEVEL,N,X,L,ISTEP,KOUNT)
      DIMENSION LEVEL(1),X(1),ISTEP(1),KOUNT(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
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 TOTAL DATA AREA REQUIRED
C
      M=LEVEL(1)+1
      DO 105 I=2,K
  105 M=M*(LEVEL(I)+1)
C
C     MOVE DATA TO THE UPPER PART OF THE ARRAY X
C     FOR THE PURPOSE OF REARRANGEMENT
C
      N1=M+1
      N2=N+1
      DO 107 I=1,N
      N1=N1-1
      N2=N2-1
  107 X(N1)=X(N2)
C
C     CALCULATE MULTIPLIERS TO BE USED IN FINDING STORAGE LOCATIONS FOR
C     INPUT DATA
C
      ISTEP(1)=1
      DO 110 I=2,K
  110 ISTEP(I)=ISTEP(I-1)*(LEVEL(I-1)+1)
      DO 115 I=1,K
  115 KOUNT(I)=1
C
C     PLACE DATA IN PROPER LOCATIONS
C
      N1=N1-1
      DO 135 I=1,N
      L=KOUNT(1)
      DO 120 J=2,K
  120 L=L+ISTEP(J)*(KOUNT(J)-1)
      N1=N1+1
      X(L)=X(N1)
      DO 130 J=1,K
      IF(KOUNT(J)-LEVEL(J)) 124, 125, 124
  124 KOUNT(J)=KOUNT(J)+1
      GO TO 135
  125 KOUNT(J)=1
  130 CONTINUE
  135 CONTINUE
      RETURN
      END
