C-----------------------------------------------------------------------
	SUBROUTINE READDATA(infile,nfil,STATUS,peakflag,dataflag,
	1	dataform,peakform,warn)
C-----------------------------------------------------------------------
C	USED TO READ A DATA & PEAK FILE IN ONE OF A NUMBER OF FORMATS
C
C  INFILE    the name of file to read, file extension optional (input)
C	      if the format is known, it may be included following the 
C	      file name using FILENAME/FORM=format or FILENAME/FORM=format
C            the name of the peak file, if one was read (output)
C  NFIL      is the array number to use for storage (input)
C  STATUS    is returned as .TRUE. if the requested information is located
C		if both PEAKFLAG and DATAFLAG are .TRUE., STATUS will be set
C		to .FALSE. unless both a peak and a data file have been read
C  PEAKFORM   is set as the format used for the peakfile (output)
C  DATAFORM   is set as the format used for the datafile (output)
C  PEAKFLAG  should be set to .TRUE. to read a peak file (output)
C  DATAFLAG  should be set to .TRUE. to read a peak file (output)
C  WARN      if .TRUE. warning messages will be displayed
C The name of the data file is stored in OFILNAM(nfil) & CFILNAM(nfil)
C If a peak file is read, the number of peaks, NPKS(nfil), is set
C If a data file is read, the number of data points, NPOINT(nfil), is set
C-----------------------------------------------------------------------
C
	include 'shared$common'
	include '[-.shared]flabel.common/list'
C
	character*(*) infile,peakform,dataform
	LOGICAL status,peakflag,dataflag,warn
	INTEGER nfil

	character*80 dname,pname
	INTEGER ln
C---------------------------------------------------------------------------
	STATUS = .FALSE.
	IF (.not. (dataflag .or. peakflag)) RETURN

C parse the file name to remove the format, if any
	dataform = ' '

	K1 = LENCH(INFILE)+1
	K2 = LENCH(INFILE)+1
	DO I=1,LENCH(INFILE)
	  IF (INFILE(I:I) .eq. '/') K1 = I
	ENDDO
C was a format command found?
	IF (K1 .lt. LENCH(INFILE)) THEN
	  K2 = K1 + 1
	  DO I=K1+1,LENCH(INFILE)
	    IF (INFILE(I:I) .eq. '=') K2 = I+1
	  ENDDO
	  IF (k2 .le. LENCH(INFILE)) THEN 
	    dataform = INFILE(K2:)
C convert format command to upper case
	    CALL STR$UPCASE(dataform,dataform)
	  ENDIF
	ENDIF

	IF (peakflag) THEN
	  pname = INFILE(:k1-1)
	  peakform = dataform
	ELSE
	  pname = ' '
	ENDIF
	IF (dataflag) THEN
	  dname = INFILE(:k1-1)
	ELSE
	  dataform = ' '
	  dname = ' '
	ENDIF

	ln = 0
C Clear data arrays
	CALL ZERODAT(nfil)
C Clear arrays of peak information
	CALL ZEROPK(nfil,1,M$pks)
C
C attempt to read in file in DuPont format
	CALL READDU(dname,pname,dataform,nfil,STATUS)
	IF (STATUS) GOTO 1000
C how about Scintag raw data file format?
!	CALL READSN(dname,pname,dataform,nfil,STATUS)
!	IF (STATUS) GOTO 1000
C no luck so far, lets try reading it as an ARGONNE file
	CALL READIPNS(dname,pname,dataform,nfil,STATUS)
	IF (STATUS) GOTO 1000
C----------------------------------------------------------------------
C lets try reading the file as an ASCII formatted file -- check the
C file header when reading to insure the file matches the format
C----------------------------------------------------------------------
C Penn State 
	CALL READPSU(dname,pname,dataform,nfil,STATUS)
	IF (STATUS) GOTO 1000
c Philips old (NOVA) APD
!	CALL READPHN(dname,pname,dataform,nfil,STATUS)
!	IF (STATUS) GOTO 1000
c Philips new (VAX) APD
	CALL READPHV(dname,pname,dataform,nfil,STATUS)
	IF (STATUS) GOTO 1000
C NIST (NBS)
	CALL READNIST(dname,pname,dataform,nfil,STATUS)
	IF (STATUS) GOTO 1000
C Brookhaven X7A
	CALL READX7A(dname,pname,dataform,nfil,STATUS)
	IF (STATUS) GOTO 1000
C Jones
	CALL READJNS(dname,pname,dataform,nfil,STATUS)
	IF (STATUS) GOTO 1000
C Energy Dispersive -- MCA
C	CALL READHU(dname,pname,dataform,nfil,STATUS)
C	IF (STATUS) GOTO 1000
C GSAS
	CALL READGSAS1(dname,pname,dataform,nfil,STATUS)
	IF (STATUS) GOTO 1000
C subroutine GSAS2 must be linked to the GSAS library
	CALL READGSAS2(dname,pname,dataform,nfil,STATUS)
	IF (STATUS) GOTO 1000
C----------------------------------------------------------------------
C end of data file formats and no success -- print a warning & exit 
C if we had wanted to read data only
C----------------------------------------------------------------------
	IF (DATAFLAG .AND. WARN) 
	1	write (lo,919) 
	1  'Unable to locate or read data file ',dname(:lench(dname))
919	FORMAT(' READDATA WARNING -- ',2a)
C Try peaks?
	IF (peakflag) goto 2000
	goto 4000
C-----------------------------------------------------------------------
C data read was successful, set flag that file exists on disk
C-----------------------------------------------------------------------
1000	CLOSE (UNIT=IN)
C warn if no points were read!
	IF (dataflag .and. npoint(nfil) .le. 0 .and. WARN) 
	1	write (lo,919) 
	1	'No Data points on file ',dname(:lench(dname))
C   If data but no peaks were read, try reading the peak file formats
C   remove the file extension 1st, though
	IF (dataflag .and. npks(nfil) .le. 0 .and. peakflag) THEN
	  pname = dname
	  call remext(pname)
C   if peaks were read prior, set the peak format to the data format
	ELSEIF (npks(nfil) .gt. 0) THEN
	  peakform = dataform
	  goto 4000
C   no peaks needed ... nothing more to do
	ELSEIF (.not. peakflag) THEN
	  goto 4000
	ENDIF
C----------------------------------------------------------------------
C Peak file formats
C----------------------------------------------------------------------
2000	call READPEAK(pname,nfil,STATUS,peakform)
	IF (STATUS) GOTO 3000
C----------------------------------------------------------------------
C end of peak file formats and no success -- print a warning & exit 
C----------------------------------------------------------------------
	IF (.not. DATAFLAG .AND. WARN) 
	1	write (lo,919) 
	1  'Unable to locate or read peak file ',pname(:lench(pname))
C-----------------------------------------------------------------------
C peak read was successful
C-----------------------------------------------------------------------
3000	CLOSE (UNIT=IN)
C If only peaks were requested, were any any peaks read?
	IF (.not. DATAFLAG .and. npks(nfil) .eq. 0 .and. WARN)
	1	write (lo,919) 
	1	'No peaks on file ',pname(:lench(pname))
C-----------------------------------------------------------------------
C all done -- something was successful
C-----------------------------------------------------------------------
4000	IF (dataflag .and. npoint(nfil) .gt. 0) then
	  ofilnam(nfil) = cfilnam(nfil)
	  FEXISTS(nfil)=.true.
	  yscale = 1
	  xscale = 1
	ENDIF
C set INFILE as the name of the peak file, if one was read
	IF (npks(nfil) .gt. 0 .and. peakflag) infile = pname
C If no peaks/data were read, clear the PEAK/DATA format
	IF (npks(nfil) .le. 0) peakform = ' '
	IF (npoint(nfil) .le. 0) dataform = ' ' 
C set status of read
	STATUS = .TRUE.
	IF (npks(nfil) .le. 0 .and. peakflag) status = .false.
	IF (npoint(nfil) .le. 0 .and. dataflag) status = .false.
	RETURN
	END
C!**************************************************************************
	SUBROUTINE ZERODAT(nfil)
C This subroutine is used to zero out a file in the data array
C***************************************************************************
	include 'shared$common'
C reset the number of background points, peaks & data points
	nbkgp(nfil)  = 0
	NPKS(nfil)   = 0
	npoint(nfil) = 0
C clear data arrays
	DO I=1,M$PTS
	  B(I,nfil)=0.0
	  BESD(I,nfil)=1.0
	  BX(I,nfil)=0.0
	ENDDO
	DATE(1,nfil) = 0
	DATE(2,nfil) = 0
	DATE(3,nfil) = 0
	TIME(nfil)= 1.0
	TEMP1(nfil)= 0.0
	TEMP2(nfil)= 0.0
	REFINT(nfil)=1.0
	ofilnam(nfil) = ' '
	cfilnam(nfil) = ' '
	FXTYP(nfil) = 0
	FYTYP(nfil) = 0
	RETURN
	END
C!**************************************************************************
	SUBROUTINE ZEROPK(nfil,n1,n2)
C This subroutine is used to zero out elements in the peak array
C***************************************************************************
	include 'shared$common'
C
	NPKS(nfil) = n1-1
	DO J=n1,n2
	  TT(J,nfil)= 0.0
	  TTCOR(J,nfil)= 0.0
	  DSPACE(J,nfil)= 0.0
	  IWAVE(J,nfil)= 0
	  IH(J,nfil)= 0
	  IK(J,nfil)= 0
	  IL(J,nfil)= 0
	  PKHT(J,nfil)= 0.0
	  BKG(J,nfil)= 0.0
	  FINTEG(J,nfil)= 0.0
	  FWHM(J,nfil)= 0.0
	  FLPCOR(J,nfil)= 0.0
	  ABSCOR(J,nfil)= 0.0
	  CORINT(J,nfil)= 0.0
	  RELINT(J,nfil)= 0.0
	  ESDINT(J,nfil)= 0.0
	  ESDDSP(J,nfil)= 0.0
	  COMMT1(J,nfil)= '    '
	  COMMT2(J,nfil)= '    '
	  DELTT(J,nfil)= 0.0
	  WTINT(J,nfil)= 0.0
	  WTHKL(J,nfil)= 0.0
	ENDDO
	RETURN
	END
