	SUBROUTINE AUTOLU
C*********************************************************************
C
C  SUBROUTINE TO ATTATCH A LOGICAL UNIT# TO "AUTO" DATA FILE AND THEN
C  TO READ RECORDS 1 THRU 6 SAVING ALL PARAMETERS IN LABELED COMMONS
C
C  FILE STRUCTURE DEFINED IN "AUTO - A REAL TIME DIFFRACTOMETER
C  CONTROL SYSTEM", R.L. SNYDER, C.R. HUBBARD AND N.C. PANAGIOTOPOULOS,
C  NBS INTERNAL REPORT, 1981.
C
C  FILE NAMING CONVENTION FOR UNIVAC 1108:
C     NAME IS FILE QUALIFIER (CHARACTER*6)
C     "RAW." IS FILE NAME.
C  FOR THE VAX THE NAME IS FOLLOWED BY .RAW
C
C  SPECIAL FLAGS PASSED OR RETURNED VIA COMMONS ARE:
C      ITYPE = 0 FOR PATTERN SPECTRUM DATA FILE
C            = 1 FOR QUANTITATIVE DATA FILE
C            = 3 FOR SPIKING METHOD OF ANALYSIS
C
C      IEND   = 0 DATA READ OK
C             = 1 END OF FILE ENCOUNTERED
C
C*********************************************************************
C
C  THE FOLLOWING PARAMETER STATMENT DEFINES THE DIMENSIONS FOR
C  THE QUANTATATIVE ANALYSIS SUBROUTINES.  TO REDIMENSION
C  SIMPLY CHANGE THIS STATMENT HERE AND IN EACH SUBROUTINE.
C      NF = THE NUMBER OF DATA FILES ALLOWED FOR INPUT
C      NR = THE MAXIMUM NUMBER OF REFLECTIONS WHICH CAN BE PROCESSED
C      NP = THE MAXIMUM NUMBER OF PHASES ALLOWED
C      NT = THE MAXIMUM NUMBER OF DATA POINTS FOR ANY REFLECTION
C
	PARAMETER (NF=30, NR=100, NP=10, NT=1250)
	COMMON/IO/ IRD,ITYP,IPRT,ISCRT,ICOBRG,LIMPRT,IDBUG,LAST
	COMMON /FILE/ LU,ITYPE,IEND
	COMMON /FILEC/ NAME
	COMMON /RECRD1/ IFLAG(10)
	COMMON /RECR1C/ TITLE,DATE,TIME,RNFLNM
	COMMON /RECRD2/ XLAMBD,TAKEOF,DIVANG,RADIUS,SOLLIN,
     1 SOLLOT,RECSLT,DEADTM,STABIL,TBASE
	COMMON /RECR2C/ UNIT,TARGET,LINE,FOCAL,DIVSLT,MONOCH,DETECT
	COMMON/RECRD3/ICALIB(6),TTHOFF,COEFF(5)
	COMMON/RECR45/ERRMTX(15)
	COMMON /RECRD6/ XWT(10),SIG(10)
	COMMON /RECR6C/ ELEMNT(10)
	COMMON/SVDAT1/ AINT(NR,NF),VINT(NR,NF),NREFL,IFILE
	LOGICAL LAST
	CHARACTER*100 TITLE
	CHARACTER*6 DATE,TIME,RNFLNM
	CHARACTER*20 NAME
	CHARACTER*4 UNIT
	CHARACTER*2 TARGET,LINE,FOCAL,DIVSLT,MONOCH,DETECT,ELEMNT
	CHARACTER*12 FILE(3)
	DATA FILE/'Orig. Sample','Orig.+ Spike','Background  '/
	IEND = 0
	J = IFILE
	IF(IABS(ITYPE).NE.3)GO TO 108
C
C  SPIKING METHOD  -ASK FOR FILES IN ORDER
C
	IF(LAST) THEN
	   IEND=-1
	   GOTO 999
	ENDIF
	IF(J.GT.2)J = 2
10	WRITE(ITYP,107)FILE(J)
107	FORMAT(/' Enter ',A12,' file name')
	GO TO 115
108	WRITE(ITYP,110) IFILE
110	FORMAT(/' Enter name for file #',I3)
115	READ(5,120) NAME
120	FORMAT(A20)
C-----ARE WE THROUGH READING DATA?
	IF(NAME.NE.'      ') GOTO 140
	IF(ITYPE.EQ.-3 .AND. J.EQ.2) THEN
	   J = 3
	   LAST = .TRUE.
	   GOTO 10
	ENDIF
	IEND = -1
	GO TO 999
140	IF(NAME(7:7).NE.' ')GO TO 145
	NAME(7:11) = '.RAW '
C
C  ATTACH THE INPUT FILE TO LOGICAL UNIT LU
C
145	OPEN(UNIT=LU,ERR=80,FILE=NAME,STATUS='OLD',ACCESS='SEQUENTIAL',
     1 FORM='FORMATTED',IOSTAT=IOS)
	GO TO 90
C
C  ILLEGAL FILE NAME - RETRY
C
80	WRITE(ITYP,85)NAME,IOS
85	FORMAT(/' **Error**File name= ',A11,' IOSTAT=',I10,
     * ' **try again**')
	GO TO 115
C
C-----READ RECORD 1 - PROBLEM SPECIFICATIONS
C
90	READ(LU,100,END=900) TITLE,DATE,TIME,IFLAG,RNFLNM
100	FORMAT(A100,2A6,10I1,A6)
C
C-----TEST IFLAG(3) FOR PROPER CODE
C        ITYPE=0 FOR PATTERN DATA FILE.
C                 IFLAG(3)=0 REQUIRED.
C        ITYPE>1 FOR QUANTITATIVE ANALYSIS DATA FILE.
C                 IFLAG(3)=1 FOR INTERNAL STANDARD METHOD
C                 IFLAG(3)=2 FOR INTENSITY RATIO METHOD
C                 IFLAG(3)=3 FOR SPIKING METHOD
C                 IFLAG(3)=4 FOR RELATIVE INTENSITY ANALYSIS
C                 IFLAG(3)=5 FOR PROFILE ANALYSIS
C
	IF(ITYPE.EQ.0.AND.IFLAG(3).EQ.0) GOTO 200
	IF(ITYPE.NE.0.AND.IFLAG(3).GE.1) GOTO 200
	WRITE(ITYP,150) ITYPE,IFLAG(3)
	IF(IPRT.NE.ITYP) WRITE(IPRT,150) ITYPE,IFLAG(3)
150	FORMAT(/' Warning**ITYPE=',I2,' is not consistent with IFLAG(3)=
     *',I2)
C
C-----READ RECORD 2 - INSTRUMENT SPECIFICATIONS
C
200	READ(LU,201,END=900) UNIT,TARGET,LINE,XLAMBD,FOCAL,TAKEOF,DIVSLT,
     * DIVANG,RADIUS,SOLLIN,SOLLOT,RECSLT,MONOCH,DETECT,DEADTM,STABIL,
     * TBASE
201	FORMAT(A4,8X,A2,A2,F6.4,A2,F4.1,A2,2F6.2,3F4.2,2A2,3F4.2)
C
C-----16 NOV 1981  TO HANDEL OLD FILES WHERE TBASE WAS NOT RECORDER
C-----DEFAULT TBASE TO 0.1 SEC
C
	IF(TBASE.LT.0.00001) TBASE = 0.1
C
C-----READ RECORD 3 - INTRUMENT CALIBRATION INFO
C
	READ(LU,300,END=900) ICALIB,TTHOFF,COEFF
300	FORMAT(6I1,F10.3,5E16.8)
C
C-----READ RECORDS 4 AND 5 - INVERSE ERROR MATRIX FOR CALIBRATION
C
	READ(LU,400,END=900) ERRMTX
400	FORMAT(8E16.6/7E16.6)
C
C-----IF PATTERN SPECTRUM FILE THEN ALL RECORDS HAVE BEEN READ
C
	IF(ITYPE.EQ.0) RETURN
C
C-----READ RECORD 6  - ELEMENTAL COMPOSITION - QUANT DATA FILE ONLY
C
	READ(LU,600,END=900)(ELEMNT(I),XWT(I),SIG(I),I=1,10)
600	FORMAT(10(A2,2F5.2))
	RETURN
C
C-----END OF FILE ENCOUNTERED
C
900	IEND=1
	CLOSE(UNIT=LU,STATUS='KEEP')
999	RETURN
	END
