C
C**********************************************************************
C
C     THIS PROGRAM WORKS WITH TEMPERATURE, PRESSURE, AND YIELD DATA
C     COLLECTED FROM THE ANALOG SIMULATOR VIA A REALTIME DATA COLLECT-
C     ING PROGRAM.  SOME STATISTICAL CALCULATIONS ARE DONE ON THE DATA
C     AND AN ESTIMATE OF THE TOTAL YIELD FROM THE SIMULATED EXPERIMENT
C     IS CALCULATED. AN ARRAY E IS INCLUDED TO COMPUTE AN ERROR TERM
C     FOR THE LINEAR MODEL DETERMINED IN THIS PROGRAM.
C
C**********************************************************************
C
	DIMENSION T(1810),P(1810),Y(1810),E(1810)
	LOGICAL*1 FNAME(50)
	LOGICAL*1 X(3)
C
C*********  L IS A TEST VALUE TO BE USED FOR DETERMINING THE APPROPRIATE
C*********  WRITE STATEMENT (TO TERMINAL AND/OR FILE).  I IS A COUNTER
C*********  FOR THE NUMBER OF DATA POINTS IN EACH FILE.
C*********  AN IS THE MEAN OF ARRAY VALUES COMPUTED IN SUBROUTINE SUB.
C
	COMMON L
	COMMON AN
	L=1
	I=1
C
C*********  INITIALIZE ALL SUMS TO ZERO.
C
	XX=0
	YY=0
	XY=0
	XZ=0
	YZ=0
	SST=0
	SSE=0
	AR=0
C
C*********  ASSIGN A UNIT NUMBER TO THE DATA FILES CREATED FROM THE 
C*********  REAL TIME PROGRAM.
C
	CALL ASSIGN(1,'TEMP.DAT')
	CALL ASSIGN(2,'PRESS.DAT')
	CALL ASSIGN(3,'YIELD.DAT')
C
C*********  READ THESE FILES INTO THREE ARRAYS. 
C
1	READ(1,2,END=3)ID
2	FORMAT(I6)
C
C*********  THE DIGITAL NUMBER, ID, IS PROPERLY SCALED TO REPRESENT
C*********  A TEMPERATURE <DEGREES CENTIGRADE> AND STORED IN ARRAY T.
C
	T(I)=ID*70./4095.+80.
	I=I+1
	GOTO 1
3	I=1
33	READ(2,4,END=5)ID
4	FORMAT(I6)
C
C******  THE DIGITAL NUMBER, ID, IS PROPERLY SCALED TO REPRESENT A
C******  PRESSURE <PSI> AND STORED IN ARRAY P.
C
	P(I)=ID*15./4095.
	I=I+1
	GOTO 33
5	I=1
54	READ(3,22,END=7)ID
22	FORMAT(I6)
C
C******  THE DIGITAL NUMBER, ID, IS PROPERLY SCALED TO REPRESENT A
C******  YIELD RATE <GRAMS/SEC> AND STORED IN ARRAY Y.
C
	Y(I)=ID*1000./4095.
	I=I+1
	GOTO 54
C
C*********  SUBRTACT ONE FROM I TO OBTAIN THE CORRECT NUMBER OF DATA
C*********  POINTS.
C
7	I=I-1
C
C*********  THE DATA FILES ARE NOW CLOSED.
C
	CALL CLOSE(1)
	CALL CLOSE(2)
	CALL CLOSE(3)
C
C*********  USER IS GIVEN THE OPTION TO CREATE AN OUTPUT FILE TO STORE 
C*********  THE RESULTS OF THE ANALYSIS.
C
55	WRITE(5,6)
6	FORMAT(1X,' DO YOU WANT TO STORE THE RESULTS IN AN OUTPUT FILE
     C? YES/NO   ',$)
	READ(5,91)(X(J),J=1,3)
91	FORMAT(3A1)
	IF(X(1).EQ.'Y')GOTO 14
	IF(X(1).EQ.'N')GOTO 56
	WRITE(5,26)
26	FORMAT(1X,' YOU HAVE MADE AN ERROR, PLEASE TRY AGAIN.')
	GOTO 55
14	WRITE(5,36)
36	FORMAT(1X,' ENTER NAME OF DATA FILE   '$)
	READ(5,46)(FNAME(LL),LL=1,50)
46	FORMAT(50A1)
	CALL ASSIGN(1,FNAME)
	GOTO 57
56	L=0
C
C*********  THE SAMPLING RATE IS EQUAL TO THE NUMBER OF DATA POINTS
C*********  SAMPLED, DIVIDED BY SIXTY SECONDS.
C
57	SR=I/60.
C
C*********  SUBROUTINE SUB IS CALLED SEVERAL TIMES AND THE RESULTS OF
C*********  THE ANALYSIS ARE DISPLAYED.
C
	IF(L.EQ.0)GOTO 23
	WRITE(1,10)SR
23	WRITE(5,10)SR
10      FORMAT(1X,' SAMPLING RATE=',F4.0,1X,' <CYCLES/SEC>')
	IF(L.EQ.0)GOTO 24
	WRITE(1,25)I
24	WRITE(5,25)I
25	FORMAT(1X,' NUMBER OF DATA POINTS=',I4,/)
	IF(L.EQ.O)GOTO 27
	WRITE(1,8)
27	WRITE(5,8)
8	FORMAT(1X,' SUMMARY STATISTICS FOR TEMPERATURE <DEGREES CENT
     CIGRADE>')
	CALL  SUB(I,T)
	IF(L.EQ.0)GOTO 29
	WRITE(1,9)
29	WRITE(5,9)
9       FORMAT(1X,' SUMMARY STATISTICS FOR PRESSURE <PSI>')
	CALL  SUB(I,P)
	IF(L.EQ.0)GOTO 32
	WRITE(1,20)
32	WRITE(5,20)
20      FORMAT(1X,' SUMMARY STATISTICS FOR YIELD RATE <GRAMS/SEC>')
	CALL  SUB(I,Y)
C
C*********  SUM OF SQUARES AND CROSS PRODUCT VALUES ARE COMPUTED.
C
	DO 11 N=1,I
	XX=XX+T(N)**2
	YY=YY+P(N)**2
	XY=XY+T(N)*P(N)
	XZ=XZ+T(N)*Y(N)
	YZ=YZ+P(N)*Y(N)
11	CONTINUE
C
C*********  D IS THE DETERMINANT VALUE USED IN CALCULATING THE LINEAR
C*********  REGRESSION COEFICIENTS.  IF D IS EQUAL TO ZERO, THE LINEAR
C*********  MODEL CANNOT BE DETERMINED.
C
	D=XX*YY-XY**2
        IF(D.EQ.0)GOTO 13
C
C*********  THE COEFICIENTS OF THE LINEAR MODEL ARE CALCULATED.
C
	A=(YY*XZ-XY*YZ)/D
	B=(XX*YZ-XY*XZ)/D
	IF(L.EQ.0)GOTO 39
	WRITE(1,12)A,B
39	WRITE(5,12)A,B
12      FORMAT(1X,/,'  THE LEAST SQUARES MODEL IS:',3X,'Y=',F5.3,'X1+',
     CF6.3,'X2',/,15X,'X1=TEMPERATURE   X2=PRESSURE',/)
	IF(L.EQ.0)GOTO 97
	WRITE(1,98)
97	WRITE(5,98)
C
C*********  THE ERROR TERM E IS THE SUM OF THE DIFFERENCES BETWEEN
C*********  THE ACTUAL AND THE ESTIMATED YIELD VALUES.
C*********  AN F VALUE IS COMPUTED FOR AN F TEST ON THE REGRESSION.
C
98	FORMAT(1X,' STATISTICS FOR ERROR TERM OF LINEAR MODEL:')
	DO 66 N=1,I
	E(N)=Y(N)-(A*T(N)+B*P(N))
	SSE=SSE+E(N)**2
	SST=SST+(AN-Y(N))**2
66	CONTINUE
	CALL SUB(I,E)
	GOTO 50
13	IF(L.EQ.0)GOTO 41
	WRITE(1,59)
41	WRITE(5,59)
59	FORMAT(1X,' THE LEAST SQUARES MODEL CANNOT BE DETERMINED',/)
50	CONTINUE
	SSR=SST-SSE
	AMSR=SSR
	AMSE=SSE/(I-2)
	F0=AMSR/AMSE
	IF(L.EQ.0)GOTO 121
	WRITE(1,122)F0,I
121	WRITE(5,122)F0,I
122	FORMAT(1X,' F0=',F13.5,4X,'THE MODEL IS A GOOD FIT IF
     C F0 > F(ALPHA,1,',I4,'-2)')
C
C*********  DT IS THE TIME INTERVAL BETWEEN COLLECTING EACH DATA VALUE.
C*********  AR IS AN ESTIMATE OF THE TOTAL AREA UNDER THE YIELD RATE    
C*********  -VS- TIME CURVE.  THE TRAPEZOIDAL RULE IS USED TO COMPUTE AR
C
	DT=60./I
	DO 15 N=1,I
	M=N+1
	AR=AR+((Y(N)+Y(M))/2.)*DT
15	CONTINUE
	IF(L.EQ.0)GOTO 43
	WRITE(1,16)AR
43	WRITE(5,16)AR
16	FORMAT(1X,/,' THE TOTAL YIELD=',F10.3,1X,'GRAMS')
	CALL CLOSE(1)
	STOP
	END
