C----------------------------------------------------------------------
	SUBROUTINE READSN(dname,pname,filform,nfil,STATUS)
C----------------------------------------------------------------------
C Read in a raw data file and/or a peak file in Scintag format
C  assume the raw data has been collected with Cu K alpha 1 and 2 radiation
C----------------------------------------------------------------------
	LOGICAL status
	INTEGER nfil
	character*(*) dname,pname,filform
	CHARACTER*7 thisform/'SCINTAG'/

	include 'shared$common'
	include '[-.shared]flabel.common'
C
	INTEGER ln,npnt
	character temp*80
	INTEGER*2 IH2,IK2,IL2

	LOGICAL exist

	INTEGER ITIM(3)

C Assume that the read was not successful
	STATUS = .false.

C check the file format qualifier, if one exists
	IF (filform .ne. ' ') THEN
	  I = LENCH(filform)
	  IF (filform(:I) .ne. thisform(:I)) RETURN
	ENDIF
C-------------------------------------------------------------------
C Is there a datafile name?
	IF (dname .eq. ' ') GOTO 201
C   First, Does the file exist?
	INQUIRE(FILE=dname,EXIST=exist,NAME=cfilnam(nfil),
	1	defaultfile='.rd')
	IF (.not. exist) goto 111
C if the file can be opened with recl=1 it is very likely to be Scintag
	OPEN(UNIT=IN,FILE=cfilnam(nfil),TYPE='OLD',ERR=111,
	1	FORM='UNFORMATTED',ACCESS='DIRECT',RECL=1,
	2	RECORDTYPE='FIXED',readonly,shared)

C read the title
	do i=1,20
	  j = 1+4*(i-1)
	  READ(IN,rec=i,err=111) temp(j:j+3)
	enddo

	READ (IN,REC=21,err=111) IBEG
	READ (IN,REC=22,err=111) IEND
	READ (IN,REC=23,err=111) ISTEP
	READ (IN,REC=27,err=111) MSTEP
	IF (MSTEP .eq. 0) GOTO 111
	IF (ISTEP .eq. 0) GOTO 111
	NPNT = (IEND - IBEG)/(1.*ISTEP) + 1.
	IF (NPNT.gt.M$PTS) then
	  WRITE (LI,'(A,i6)') 
	1	' WARNING -- NUMBER OF DATA POINTS WILL BE LIMITED TO ',
	2	M$PTS
	  NPNT = M$PTS
	ENDIF

C	READ (IN,REC=25,err=111) PSCAL
C	READ (IN,REC=26,err=111) XLAM
C	READ (IN,REC=28,err=111) ISPIN
C	READ (IN,REC=29,err=111) IOSC
	READ (IN,REC=30,err=111) NDAT1
	READ (IN,REC=31,err=111) NDAT2
	READ (IN,REC=32,err=111) NDAT3

C	READ (IN,REC=33,err=111) ITIM(1)
C	READ (IN,REC=34,err=111) ITIM(2)
C	READ (IN,REC=35,err=111) ITIM(3)
	READ (IN,REC=36,err=111) MAXREC
C	READ (IN,REC=37,err=111) ITOP
C	READ (IN,REC=38,err=111) NTOP

C Check if internally consistent
	IF (NPNT.gt.(MAXREC-49)/2) then
	  WRITE (LI,'(2A)') 
	1	' SCINTAG WARNING -- # of records is inconsistent',
	1	' with # of data points'
	  WRITE (LI,'(17x,A)') '(assuming invalid SCINTAG .RD format)'
	  GOTO 111
!	  WRITE (LI,'(A,i6,a,i6,a)') 
!	1	' WARNING -- Too few records, truncating from',npnt,' to',
!	2	  (MAXREC-49)/2,' points'
!	  NPNT = (MAXREC-49)/2
	ENDIF

C	
C counts are in units of CPM, change to CPS
	READ (IN,REC=24,err=111) TIME(nfil)
	DO N=1,NPNT
	  READ (IN,REC=2*n+49,err=111) IRAT
	  B(N,nfil) = IRAT/60.
	  READ (IN,REC=2*n+50,err=111) ISIG
	  BESD(N,nfil) = ISIG/60.
	ENDDO


	INST(nfil) ='DI'
	SOURCE(nfil) = 'SN'
	IRAD(nfil) = 'CU'
	CALL WAVESET(IRAD(nfil),WAVE(nfil),WKA1(nfil),WKA2(nfil),*33)
33	MONO(nfil) = .false.
	NPKS(nfil) = 0
	NBKGP(nfil) = 0

	DATE(1,nfil) =  NDAT2
	DATE(2,nfil) =  NDAT3
	DATE(3,nfil) =  mod(NDAT1,100)
	ATITLE(nfil) = temp
	START(nfil) = FLOAT(IBEG)/FLOAT(MSTEP)
	DELTA(nfil) = FLOAT(ISTEP)/FLOAT(MSTEP)
	FINISH(nfil) = START(nfil) + (NPNT-1)*DELTA(nfil)
	NPOINT(nfil) = npnt

C Compute X values and insure that ESD's are non-zero
	DO i=1,npnt
	  bx(i,nfil) = START(nfil) + (I-1.)*DELTA(nfil)
	  IF (BESD(i,nfil) .eq. 0) BESD(i,nfil) = 1.0
	ENDDO

	ln = lench(cfilnam(nfil))
	write (LO,'(A)') ' File '//cfilnam(nfil)(:ln)
	1		//' read in Scintag raw data format'

	FXTYP(nfil) = XTYP_TT
	FYTYP(nfil) = 2
	filform = thisform
	STATUS = .true.
C remove the file extension from the peak name, if one was specified
	call remext(pname)
111	close(unit=IN)
C----------------------------------------------------------------------------
C is there a peak file name?
201	IF (pname .eq. ' ') RETURN
C now try to read a SCINTAG binary format peak file
	INQUIRE(FILE=pname,EXIST=exist,defaultfile='.pk')
	NPKS(nfil) = 0
	IF (.not. exist) goto 211
C if the file can be opened with recl=1 it is very likely to be Scintag
	OPEN(UNIT=IN,FILE=pname,TYPE='OLD',ERR=211,FORM='UNFORMATTED'
	1	,readonly,shared,ACCESS='DIRECT',RECL=1,RECORDTYPE='FIXED'
	2	,defaultfile='.pk')
C
C  READ THE FILE HEADER INFORMATION...
C
C read the title
	do i=1,20
	  j = 1+4*(i-1)
	  READ(IN,rec=i,err=211) ATITLE(nfil)(j:j+3)
	enddo

	READ (IN,REC=27) MSTEP
	READ (IN,REC=36) Mrec
C compute # of peaks from number of records (yes, it seems strange)
	MPK = (MREC - 51)/6 + 1

	do j=1,MIN(M$PKS,MPK)
	  READ(IN,rec=50+(j-1)*6+1,err=211) TT(J,nfil) 
	  READ(IN,rec=50+(j-1)*6+2,err=211) DSPACE(j,nfil)
	  READ(IN,rec=50+(j-1)*6+3,err=211) mpk
	  READ(IN,rec=50+(j-1)*6+4,err=211) FINTEG(J,nfil)
C counts per minute to counts per second
	  PKHT(J,nfil) = mpk/60.
	  FINTEG(J,nfil) = FINTEG(J,nfil)/60.
	  READ(IN,rec=50+(j-1)*6+5,err=211) FWHM(J,nfil)
	  READ(IN,rec=50+(j-1)*6+6,err=211) ihkl
	  IH(J,nfil) = IHKL/1024
	  IK(J,nfil) = mod(IHKL/32,32)
	  IL(J,nfil) = mod(IHKL,32)
	  IWAVE(J,nfil) = 1
	  NPKS(nfil) = J
	enddo
C
C  CALCULATE THE RELATIVE INTENSITY BASED ON AREAS
C
     	DUMY = FINTEG(1,nfil)
     	DO J=2,NPKS(nfil)
     	  DUMY = MAX(FINTEG(J,nfil),DUMY)
     	ENDDO
     	DO J=1, NPKS(nfil)
	  RELINT(J,nfil) = 100.0*FINTEG(J,nfil)/DUMY
     	ENDDO
211    	IF (NPKS(nfil) .gt. 0) then
	  INQUIRE(UNIT=IN,NAME=pname)
	  ln = lench(pname)
	  IF (FYTYP(nfil) .eq. 0) FYTYP(nfil) = 2
	  IF (FXTYP(nfil) .eq. 0) FXTYP(nfil) = XTYP_TT
	  write (LO,'(I5,A)') NPKS(nfil),
	1	' Peaks read from Scintag binary file '//pname(:ln)
	  filform = thisform
	  STATUS = .true.
	  close(unit=IN)
	  RETURN
	ENDIF
	close(unit=IN)
C----------------------------------------------------------------------------
C now try to read a SCINTAG ASCII format peak file
	INQUIRE(FILE=pname,EXIST=exist,defaultfile='.pklst')
	IF (.not. exist) goto 311
	OPEN(UNIT=IN,FILE=pname,TYPE='OLD',ERR=311,FORM='FORMATTED'
	1	,readonly,shared,defaultfile='.pklst')
C
C  READ THE FILE HEADER INFORMATION...
C
	READ(IN,'(/,32x,a)',ERR=311) ATITLE(nfil)
	READ(IN,'(/,1x,a5,3(1x,i2))',ERR=311) temp,
	1	DATE(1,nfil),DATE(2,nfil),DATE(3,nfil)
	IF (temp .ne. 'DATE:') goto 311
	READ(IN,'(//,a5)',ERR=311) temp
	IF (temp .ne. '     ') goto 311
C
C  READ THE PEAK LIST...
C
     	DO J=1, M$PKS
     	  READ(IN,'(F5.0,f8.0,f9.0,5X,f7.0,f9.0)',ERR=311,END=301)
	1	X,TT(J,nfil),DSPACE(J,nfil),PKHT(J,nfil),FWHM(J,nfil)
	  IWAVE(J,nfil) = 1
	  NPKS(nfil) = J
     	ENDDO

301	IF (NPKS(nfil).LE.0) GOTO 311
C
C  CALCULATE THE RELATIVE INTENSITY BASED ON PEAK HEIGHTS
C
     	DUMY = PKHT(1,nfil)
     	DO J=2,NPKS(nfil)
     	  DUMY = MAX(PKHT(J,nfil),DUMY)
     	ENDDO
     	DO J=1, NPKS(nfil)
	  RELINT(J,nfil) = 100.0*PKHT(J,nfil)/DUMY
     	ENDDO
	INQUIRE(UNIT=IN,NAME=pname)
	ln = lench(pname)
	write (LO,'(I5,A)') NPKS(nfil),
	1	' Peaks read from Scintag ASCII peak file '//pname(:ln)
	filform = thisform
	STATUS = .true.
311	close(unit=IN)
	RETURN
C--------------------------------------------------------------------
	END
