C  INTERACTIVE PROGRAM FOR THE XDS SIGMA 5
C  ADAPTED BY R. SNYDER 8/72
C
C  FILE 10 CONTAINS:
C     RECORD 1: TITLE
C     RECORD 2: A,B,C,COS(ALPHA),COS(BETA),COS(GAMMA)  ORFLS FORMAT
C     RECORD 3&ON:  ATOMIC COORDINATES IN ORFLS FORMAT
C
C  PROGRAM ADAPTED FROM:
C     7070 FORTRAN PROGRAM FOR LEAST SQUARES PLANE
C     MARCH, 1961                     BY SHIRLEY CHU
C     ADAPTED TO IBM 1130     JULY 1968
C     X-RAY CRYSTALLOGRAPHY LABORATORY,UNIVERSITY OF PITTSBURGH
C
C  INPUT SET UP
C  ITEM 1  NUMBER OF PLANES TO BE CALCULATED  (I2)
C  ITEM 2  TITLE CARD
C  ITEM 3  LATTICE PARAMETERS  ORFLS FORMAT (3F7.3,4F7.4)
C  ITEM 4    (2I4)
C        COL 1-4   NUMBER OF ATOMS IN THE PLANE
C        COL 5-8   NUMBER OF ATOMS NOT INCLUDED IN PLANE CALCULATION
C                  BUT WHOSE DISTANCES FROM THE PLANE IS TO BE CALCULATE
C  ITEM 5  ATOMIC PARAMETERS IN ORFLS FORMAT
C
C  NOTE  ATOMS IN THE PLANE MUST BE THE FIRST PARAMETER CARDS INPUT
C      FOR TWO OR MORE PLANES ITEMS 2 THROUGH 5 SHOULD BE REPEATED
C
	DIMENSION R1(3),R2(3),S1(3),S2(3),X(50),Y(50),Z(50),A(3,3),C(3,3),
	1G(3,3),P(6),U(9),B(3,3),D(3), N1(50),N2(50),N3(50)
	DIMENSION LS(50), NLS(50)
	DATA NO/'N'/
	I1 = 108
	ITAP = 10
	ICD=105
	DO 200 N = 1,50
200	LS(N) = 0
	WRITE(I1,100)
100	FORMAT(/' PROGRAM TO CALCULATE THE LEAST SQUARES PLANE'/
	1' THROUGH A SET OF (ATOMIC) COORDINATES'//
	2' DO YOU WANT DATA ON FILE 10 LISTED?')
	READ(ICD,101)NO1
101	FORMAT(A1)
	WRITE(I1,102)
102	FORMAT(/' ARE TEMPERATURE PARAMETERS IN WITH COORDINATES ON FILE 1
	10?'/)
	READ(ICD,101)NO2
	READ(ITAP,32)
	READ(ITAP,33) R1, S1
	IF(NO1.EQ.NO) GO TO 104
	WRITE(I1,32)
	WRITE(I1,103)R1,S1
103	FORMAT(/' A = ',F7.3,' B = ',F7.3,' C = ',F7.3/' COS(ALPHA) = ',
	1F7.4,' COS(BETA) = ',F7.4,' COS(GAMMA) = ',F7.4)
104	L = 0
	IF(NO1.NE.NO) WRITE(I1,106)
106	FORMAT(//' SEQ. NO.  ATOM      A         B         C')
105	L = L + 1
	READ(ITAP,34,END=107)N1(L),N2(L),N3(L),X(L),Y(L),Z(L)
	IF(X(L).EQ.999.) GO TO 107
	IF(NO1.EQ.NO) GO TO 110
	WRITE(I1,108)L, N1(L),N2(L),N3(L),X(L),Y(L),Z(L)
108	FORMAT(2XI4,5X3A2,3F10.6)
110	IF(NO2.EQ.NO) GO TO 105
	READ(ITAP,34,END=107)
	GO TO 105
107	NC = L - 1
120	WRITE(I1,130)
130	FORMAT(//' TYPE THE SEQUENCE NUMBERS OF THE ATOMS TO BE '/
	1' INCLUDED IN CALCULATION OF PLANE'/
	2'  ENTER ALL NUMBERS SEPERATED BY COMMAS'/
	3/' THE DISTANCES FROM ALL OTHER ATOMS TO THE GENERATED PLANE'/
	4' WILL BE CALCULATED AND OUTPUT'/)
	READ(ICD,135)LS
135	FORMAT(50G)
	DO 145 N = 1,50
	IF(LS(N).EQ.0) GO TO 150
145	CONTINUE
150	LSN = N - 1
	NLSN = 0
	DO 160 L = 1,NC
	DO 155 N = 1,LSN
	IF(LS(N)-L)155,160,155
155	CONTINUE
	NLSN = NLSN + 1
	NLS(NLSN) = L
160	CONTINUE
	KLINE=0
	SUMX=0.0
	SUMY=0.0
	SUMZ=0.0
	SUMSX=0.0
	SUMSY=0.0
	SUMSZ=0.0
	SUMXY=0.0
	SUMYZ=0.0
	SUMXZ=0.0
	DO 4 IL = 1,LSN
	L = LS(IL)
	SUMX=SUMX+X(L)
	SUMY=SUMY+Y(L)
	SUMZ=SUMZ+Z(L)
	SUMSX=SUMSX+X(L)**2
	SUMSY=SUMSY+Y(L)**2
	SUMSZ=SUMSZ+Z(L)**2
	SUMXY=SUMXY+X(L)*Y(L)
	SUMYZ=SUMYZ+Y(L)*Z(L)
4	SUMXZ=SUMXZ+X(L)*Z(L)
	FN=LSN
	AVGX=SUMX/FN
	AVGY=SUMY/FN
	AVGZ=SUMZ/FN
	A(1,1)=SUMSX-SUMX*AVGX
	A(2,2)=SUMSY-SUMY*AVGY
	A(3,3)=SUMSZ-SUMZ*AVGZ
	A(1,2)=SUMXY-SUMX*AVGY
	A(1,3)=SUMXZ-SUMX*AVGZ
	A(2,3)=SUMYZ-SUMY*AVGZ
	C(1,1)=A(2,2)*A(3,3)-A(2,3)**2
	C(2,2)=A(1,1)*A(3,3)-A(1,3)**2
	C(3,3)=A(1,1)*A(2,2)-A(1,2)**2
	C(1,2)=A(1,3)*A(2,3)-A(1,2)*A(3,3)
	C(1,3)=A(1,2)*A(2,3)-A(1,3)*A(2,2)
	C(2,3)=A(1,2)*A(1,3)-A(2,3)*A(1,1)
	C(2,1)=C(1,2)
	C(3,1)=C(1,3)
	C(3,2)=C(2,3)
	V=R1(1)*R1(2)*R1(3)*(SQRT (1.0+2.0*S1(1)*S1(2)*S1(3)-S1(1)**2-
	1S1(2)**2-S1(3)**2))
	T1=SQRT (1.0-S1(1)**2)
	T2=SQRT (1.0-S1(2)**2)
	T3=SQRT (1.0-S1(3)**2)
	R2(1)=R1(2)*R1(3)*T1/V
	R2(2)=        CUSTODIA
N	
	F
	
	qDESAIN
N	ANCY           R.D. 1
	110ALMOND N Y      14804
607 5	87 8533GEORGE      
8	71 2144
H	MRS     MR & MR
	  SUP UNIV
RELA 	TIONS RECORDS OFF
	                                  
	  F                               rDIC
ENS	          DONALD A.
3	1 PINE HILL DR     A
ED	Y      14802   607 5
4702M	ARIA
	
871 2	465
E	  MR
MR	MRS
	                                  ASST PROF OF
CER.	ENGINEERING   EMERITUS
	          
M	
	  DICKINSON
L	ILLIAN E.      84 W UN
VERSI	TY     AL
ED	Y
14802	   607 587 8375HAROL
	x    871 2154
I	  MRS
MR	MRS
CUSTODIAN
	              
	      F
	          DIX
	      FRED W.
41 SA	YLES ST        ALFRE
Y	  14802   607 587 2132ROSALIE     
8	71 2208        A   DR      DR & MRS
	                              ASSISTANT PR
OFESS	OR OF PHYSICS
	              
	
	              
DODGE	               ROBERT E.       R.D.
1	          AL
ED ST	A N Y  14803
607 4	78 8292CHRYSTAL    i1-6 87
2482 	OR 2484D   MR      MR &
	      RT (1.0-S1(3)**2)
	U(8)=(U(4)-U(5)*S1(3))/SN3
	U(9)=-(U(4)*S1(2))/(SN2*SN3)+(U(5)*(SN1*S1(2)*S1(3)-S1(1)*SN3))/
	1(SN1*SN2*SN3)+U(6)/(SN1*SN2)
	WRITE(I1,49)
	WRITE(I1,50)
	WRITE(I1,36)U(8),U(5),U(9),U(7)
	GO TO 23
20	P(4)=B(1,1)*P(1)+B(1,2)*P(2)+B(1,3)*P(3)
	P(5)=B(2,1)*P(1)+B(2,2)*P(2)+B(2,3)*P(3)
	P(6)=B(3,1)*P(1)+B(3,2)*P(2)+B(3,3)*P(3)
	IF (P(1)) 71,74,71
71	E1=(P(4)/P(1))*10.0
	GO TO 75
74	E1=0.0
75	IF (P(2)) 72,76,72
72	E2=(P(5)/P(2))*10.0
	GO TO 77
76	E2=0.0
77	IF (P(3)) 73,78,73
73	E3=(P(6)/P(3))*10.0
	GO TO 79
78	E3=0.0
79	P(1)=P(4)
	P(2)=P(5)
	P(3)=P(6)
	GO TO 16
23	DZERO=ABS (U(7))
	WRITE(I1, 47)DZERO
	WRITE(I1,45)
	WRITE(I1,37)
	KLINE=KLINE+21
	DO 26 IL = 1,LSN
	L = LS(IL)
	DIST=U(1)*X(L)+U(2)*Y(L)+U(3)*Z(L)-U(7)
	IF (KLINE-56) 24,24,25
24	WRITE(I1,38)L,X(L),Y(L),Z(L),DIST,N1(L),N2(L),N3(L)
	KLINE=KLINE+1
	GO TO 26
25	WRITE(I1,39)
	KLINE=0
	GO TO 24
26	CONTINUE
	IF (NLSN) 43,43,22
22	IF (KLINE-50) 29,30,30
30	WRITE(I1,39)
	KLINE=0
29	WRITE(I1,46)
	WRITE(I1,37)
	KLINE=KLINE+6
	DO 42 I=1,NLSN
	L = NLS(I)
	DIST=U(1)*X(L)+U(2)*Y(L)+U(3)*Z(L)-U(7)
	IF (KLINE-56) 40,40,41
40	WRITE(I1,38)L,X(L),Y(L),Z(L),DIST,N1(L),N2(L),N3(L)
	KLINE=KLINE+1
	GO TO 42
41	WRITE(I1,39)
	KLINE=0
	GO TO 40
42	CONTINUE
43	WRITE(I1,167)
167	FORMAT(//' DO YOU WISH ANOTHER PLANE CALCULATED?'/)
	READ(ICD,101)NO3
	IF( NO3.EQ.NO ) GO TO 28
27	WRITE(I1,39)
	GO TO 120
28	CALL EXIT
31	FORMAT (I2)
32	FORMAT (72H
	1                 )
33	FORMAT (3F7.3,3F7.4)
34	FORMAT(3A2,21X,3F9.6)
35	FORMAT (//54HPLANE CONSTANTS WITH RESPECT TO CRYSTALLOGRAPHIC AXES
	1 )
36	FORMAT (1X,                    4F15.8)
37	FORMAT (/80H ATOM    FRACTIONAL COORDINATE      DISTANCE TO THE PL
	1ANE                       /)
38	FORMAT(I4,3F10.5,F16.5,7X,3A2)
39	FORMAT(////1H ///)
45	FORMAT (//' DISTANCE  TO THE PLANE FROM THE ATOMS WHICH ARE INCLUD
	1ED IN THE'/' CALCULATION OF THE PLANE' )
46	FORMAT (//90HDISTANCE TO THE PLANE FROM THE ATOMS WHICH ARE NOT FO
	1RMING THE PLANE                      )
47	FORMAT (//35HDISTANCE TO THE PLANE FROM ORIGIN  F15.8)
48	FORMAT(//13X,'A',12X,'B',12X,'C',12X,'D')
49	FORMAT (//72HPLANE CONSTANTS WITH RESPECT TO ORTHOGONAL CRYSTALLOG
	1RAPHIC AXES        )
50	FORMAT(//13X,'L',12X,'M',12X,'N',12X,'D')
	END
