	SUBROUTINE AUTOIN
	parameter (maxvol=30)
	include 'single.inc'
	DIMENSION X(3,3),XINV(3,3),HPHJ(3,30)
	1,Y(3,3),G(3,3),H0(3,3),A(3),H(3,30)
	2,ALPHA(3),S(3,2),HPHIP(3,30),H1(3,3),OLDVOL(maxvol)
	BYTE IRESP
C
	DATA RAD/57.2958/
C
c	TYPE 2
2	format('$Auto Indexing, Type Trial Limits[3]?')
c	read(5,4,end=9999)MAX
4	FORMAT(I10)
5	FORMAT(F10.0)
	DELTA=0.1
c	IF(MAX.LE.0)MAX=3
	max=3
	NUMVOL=0
	NOREF=0
	FACTML=1.0
c modified to use only reflections flagged with nuse=1
	DO I=1,NLST
	  if(iuse(i) .eq. 1)then
	        noref=noref+1
	  	CALL CALCH(ANGLST(1,I),HPHIP(1,noref))
	  endif
	enddo
	if(noref .lt. 3)then
	  type 199
199	  format(/'   *****Insufficient Reflns for AUTO****')
	  return
	endif
	MREF=3
201	DO I=1,3
	  DO J=1,3
	    X(J,I)=HPHIP(J,I)
	    XINV(J,I)=HPHIP(J,I)
	  enddo
	enddo
	CALL MINV(XINV,XINV,DET)
	TEMP=DET/FACTML
	IF(ABS(DET).GT.1.0E-6)GO TO 223
	  MREF=MREF+1
	  IF(MREF.LE.NOREF)GO TO 221
	    TYPE 6
6	    FORMAT(' UNABLE TO SOLVE PROBLEM')
	    RETURN
221	  DO 222 I=1,3
	    TEMP=HPHIP(I,3)
	    HPHIP(I,3)=HPHIP(I,MREF)
	    HPHIP(I,MREF)=TEMP
222	  CONTINUE
	  GO TO 201
223	DO 226 I=1,NOREF
	  DO 225 J=1,3
	    HPHJ(J,I)=0.0
	    DO 224 K=1,3
		HPHJ(J,I)=HPHJ(J,I)+XINV(J,K)*HPHIP(K,I)
224	    CONTINUE
225	  CONTINUE
226	CONTINUE
300	DO 2020 JMAX=1,MAX
	NSOL=0
	DO 2010 IH2=-JMAX,JMAX
	H0(1,2)=IH2
	DO 2010 IK2=-JMAX,JMAX
	H0(2,2)=IK2
	DO 2010 IL2=-JMAX,JMAX
	H0(3,2)=IL2
	DO 2010 IH3=-JMAX,JMAX
	H0(1,3)=IH3
	DO 2010 IK3=-JMAX,JMAX
	H0(2,3)=IK3
	DO 2010 IL3=-JMAX,JMAX
	H0(3,3)=IL3
	COF1=H0(2,2)*H0(3,3)-H0(2,3)*H0(3,2)
	COF2=H0(1,2)*H0(3,3)-H0(1,3)*H0(3,2)
	COF3=H0(1,2)*H0(2,3)-H0(2,2)*H0(1,3)
	IF(ABS(COF1)+ABS(COF2)+ABS(COF3).LT.1.0E-3)GO TO 2010
	DO 2000 IH1=0,JMAX
	H0(1,1)=IH1
	DO 2000 IK1=0,JMAX
	H0(2,1)=IK1
	H0(3,1)=JMAX
	DET=H0(1,1)*COF1-H0(2,1)*COF2+H0(3,1)*COF3
	IF(ABS(DET).LT.1.0E-6)GO TO 2000
	DO 330 I=4,NOREF
	DO 320 J=1,3
	H(J,I)=0.0
	DO 310 K=1,3
	H(J,I)=H(J,I)+H0(J,K)*HPHJ(K,I)
310	CONTINUE
	K=ANINT(H(J,I))
	IF(ABS(H(J,I)-K).GT.DELTA)GO TO 2000
320	CONTINUE
330	CONTINUE
	NSOL=1
	DO 333 I=1,3
	  DO 332 J=1,3
	    UB(J,I)=0.0
	    H1(J,I)=H0(J,I)
	    DO 331 K=1,3
	      UB(J,I)=UB(J,I)+H0(J,K)*XINV(K,I)
331	    CONTINUE
332	  CONTINUE
333	CONTINUE
	CALL MINV(UB,UB,DET)
C G = UB(T)*UB
	DO 336 I=1,3
	  DO 335 J=1,3
	    G(J,I)=0.0
	    DO 334 K=1,3
	      G(J,I)=G(J,I)+UB(K,J)*UB(K,I)
334	    CONTINUE
335	  CONTINUE
336	CONTINUE
	CALL MINV(G,G,DET)
	V=1.0/SQRT(DET)
	DO 337 I=1,NUMVOL
	  IF(ABS(OLDVOL(I)-V).LT.1.0)GO TO 2000
337	CONTINUE
	NUMVOL=NUMVOL+1
	if(numvol.gt.maxvol)numvol=maxvol
	OLDVOL(NUMVOL)=V
	CALL REDUCE(H1,G,NOREF)
	CALL MINV(H1,H1,DET)
	CALL MATMUL(X,H1,UB)
	DO 342 I=1,3
	  DO 341 J=1,3
	    H1(J,I)=UB(J,I)
341	  CONTINUE
342	CONTINUE
	CALL MINV(H1,H1,DET)
	DO 345 I=1,NOREF
	  DO 344 J=1,3
	    H(J,I)=0.0
	    DO 343 K=1,3
		H(J,I)=H(J,I)+H1(J,K)*HPHIP(K,I)
343	    CONTINUE
344	  CONTINUE
345	CONTINUE
	DO 360 I=1,3
	A(I)=SQRT(G(I,I))
360	S(I,1)=G(I,I)
	ALPHA(1)=ACOS(G(2,3)/(A(2)*A(3)))*RAD
	ALPHA(2)=ACOS(G(1,3)/(A(1)*A(3)))*RAD
	ALPHA(3)=ACOS(G(1,2)/(A(1)*A(2)))*RAD
	TYPE 10,A,ALPHA
10	FORMAT('0CELL PARAMETERS: ',3F8.4,3F8.3)
	TYPE 8,V
8	FORMAT(' VOLUME: ',F10.3)
	S(1,2)=G(2,3)
	S(2,2)=G(1,3)
	S(3,2)=G(1,2)
	TYPE 12,S
12	FORMAT(/' NIGGLI MATRIX:',3F12.4/15X,3F12.4)
13	TYPE 14
14	FORMAT(/'$SHOULD CALCULATIONS CONTINUE?')
	read(5,16,end=9999,err=13)IRESP
16	FORMAT(A1)
	IF(IRESP.NE.'Y'.AND.IRESP.NE.'y')GO TO 3000
2000	CONTINUE
2010	CONTINUE
	IF(NSOL.NE.0)GO TO 3000
2020	CONTINUE
	DELTA=2.0*DELTA
	GO TO 300
3000	DO 3020 I=1,NLST
	  call hcalc(anglst(1,i),ihlst(1,i))
3020	CONTINUE
	call omtob(1)
	CALL UPDATE
9999	RETURN
	END
