     	SUBROUTINE RRWRIG
C
C  PROGRAMMER:  R. L. SNYDER
C		ALFRED UNIVERSITY,  JUNE-1987
C
C	THIS ROUTINE READS FROM A RIGAKU RAW DATA FILE
C	AND STORES IN THE CORRESPONDING COMMON BLOCKS

c
c	Updated to read unformatted Rigaku raw data files.
c	Martin McMillan: March 9,1988
c	


     	INCLUDE 'FILECONV.CBS'
	character ans*4,time*8,date*12
	integer*2 nnpts

     	CLOSE( UNIT=IINP )

c	old stuff

CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC
c     	INCLUDE 'FILECONV.CBS'


c     	INTEGER*2 ITIT(40)
c     	EQUIVALENCE (ITIT,SLID)

c     	CLOSE( UNIT=IINP )
c     	OPEN( UNIT=IINP, FILE=FILINP, STATUS='OLD',
c     	1 FORM='FORMATTED', READONLY, ERR=888 )


CC
CC  READ THE FILE HEADER INFORMATION...
CC


C     	READ(IINP,1,ERR=777,END=777)
C     	READ(IINP,1,ERR=777,END=777) ITIT
C1	FORMAT(40A2)
C     	READ(IINP,2,ERR=777,END=777) BANG(1)
C2	FORMAT(G)
C     	READ(IINP,2,ERR=777,END=777) BANG(2)
C     	READ(IINP,2,ERR=777,END=777) BANG(3)
C     	READ(IINP,2,ERR=777,END=777) BANG(10)
C     	READ(IINP,2,ERR=777,END=777) DUMMY
C     	READ(IINP,3,ERR=777,END=777) NPTS
C3	format(i5)
CC
CC  READ THE INTENSITY DATA...
CC
C     	DO I=1, NPTS
C     	   READ(IINP,2,ERR=777,END=666) YOBS(I)
C     	ENDDO
c666	NPTS = I-1
c     	IF( NPTS.GT.0 ) GOTO 999
C     	GOTO 999

CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

c
c	update: March 9,1988
c
     	OPEN( UNIT=IINP, FILE=FILINP, ACCESS ='DIRECT', STATUS='OLD',
     	1 FORM='UNFORMATTED', RECL=1, READONLY, ERR=888 )

C
C  READ THE FILE HEADER INFORMATION...
C
	read(iinp,rec=1,err=777)ans
		if((ans(1:4).eq.'.RAW').or.(ans(1:4).eq.'.raw'))then
			ityi=1
		else
			type *,'file is not .RAW type'
			goto 777
		end if
	read(iinp,rec=2,err=777)date(1:4)
	read(iinp,rec=3,err=777)date(5:8)
	read(iinp,rec=4,err=777)date(9:12)
	read(iinp,rec=5,err=777)time(1:4)
	read(iinp,rec=6,err=777)time(5:8)
	datm=datcod(date(1:9))
	timm=timcod(time)

	do i=1,10
		read(iinp,rec=6+i,err=777)slid(i)
	enddo

	read(iinp,rec=17,err=777)junk
		if(junk.eq.1)slid(11)=' R  '
		if(junk.eq.2)slid(11)=' L  '

	read(iinp,rec=18,err=777)junk
		if(junk.eq.1)scan='C'
		if(junk.eq.2)scan='S'

	read(iinp,rec=19,err=777)junk
		if(junk.eq.1)slid(12)='Stnd'
		if(junk.eq.2)slid(12)='Int '
		if(junk.eq.3)slid(12)='Skip'

	read(iinp,rec=20,err=777)junk2
		encode(4,1000,ans)junk2
		if(junk.eq.1)slid(13)=ans
		if(junk.eq.1)slid(14)='#Rep'
		if(junk.eq.2)slid(13)=ans
		if(junk.eq.2)slid(14)='#Int'
		if(junk.eq.3)slid(13)=ans
		if(junk.eq.3)slid(14)='TVC'

	read(iinp,rec=21,err=777)bang(2)

	read(iinp,rec=23,err=777)junk
		encode(4,1000,ans)junk
		slid(15)=ans
		slid(16)=' KV '

	read(iinp,rec=24,err=777)junk
		encode(4,1000,ans)junk
		slid(17)=ans
		slid(18)=' MA '

	read(iinp,rec=25,err=777)ans(1:2)
		if((ans(1:2).eq.'Cr').or.(ans(1:2).eq.'CR'))then
			iele=1
		else if((ans(1:2).eq.'Fe').or.(ans(1:2).eq.'FE'))then
			iele=2
		else if((ans(1:2).eq.'Co').or.(ans(1:2).eq.'CO'))then
			iele=3
		else if((ans(1:2).eq.'Cu').or.(ans(1:2).eq.'CU'))then
			iele=4
		else if((ans(1:2).eq.'Mo').or.(ans(1:2).eq.'MO'))then
			iele=5
		else
			iele=6
		endif

	if(iele.eq.6)then
		read(iinp,rec=31,err=777)wave(3,iele)
		read(iinp,rec=32,err=777)wave(1,iele)
		read(iinp,rec=33,err=777)wave(2,iele)
		read(iinp,rec=34,err=777)wave(4,iele)
	endif

	read(iinp,rec=35,err=777)bang(1)
	read(iinp,rec=36,err=777)bang(3)
	read(iinp,rec=37,err=777)nnpts,mpro
		npts=nnpts
		if(mpro.eq.1)mpro=1
		if(mpro.eq.2)mpro=4
		if(mpro.eq.3)mpro=3
	read(iinp,rec=38,err=777)bang(10)
	read(iinp,rec=44,err=777)bang(4)
	

C
C  READ THE INTENSITY DATA...
	do i=1,npts
		read(iinp,rec=50+i,err=777)yobs(i)
	enddo
	goto 999

C

c	old stuff retained:

777	IERR = 1
     	GOTO 999
888	IERR = 2
999	RETURN
1000	FORMAT(I4)
     	END
