     	SUBROUTINE RRW5000
C
C  PROGRAMMER:  R.S. ZHOU
C		ALFRED UNIVERSITY   FEB-1987
C  MODIFIED: JDW 4-JUL-89 TO READ A PSD FILE.
C
C  MODIFIED: S.I. ZDZIESZYNSKI TO READ D5000 RAW DATA FILE
C                                              
C            OCT-18-1991
C
C
C	THIS ROUTINE READS FROM A   DIFFRAC-500 RAW DATA FILE
C	AND STORES IN THE CORRESPONDING COMMON BLOCKS
C
     	INCLUDE 'FILECONV.CBS'
C==================================================================

        INTEGER*2 DUMY,MESFLG,IOVER,MTHC
      	DIMENSION NRNS(20),NPT(100),KREC(100),LREC(100),MREC(100)
	REAL*4 SPCAN(4),START(20),STEP(20),END(20)
        CHARACTER DACOUT*1
       
     	CLOSE( UNIT=IINP )

     	OPEN( UNIT=IINP, FILE=FILINP, STATUS='OLD', ERR=888,
     1   ACCESS='DIRECT', RECL=12,  READONLY )
C
C  READ THE FILE HEADER BLOCK...
C
     	READ(IINP,REC= 1) DUMY,ITYP,NHDB,ISBF,LENG,MESFLG,IOVER

     	IF( ITYP.NE.1 ) GOTO 777     
     	IF( LENG.LT.3 ) GOTO 777
C
C  READ THE SPECIFICATION BLOCK REFERENCES...
C
     	IREC = ISBF                        !200
     
      READ(IINP,REC=IREC)(IRPR(I,1),IRPR(I,2),IRLR(I),I=1,4)
      READ(IINP,REC=IREC+1)(IRPR(I,1),IRPR(I,2),IRLR(I),I=5,8)
      READ(IINP,REC=IREC+2)(IRPR(I,1),IRPR(I,2),IRLR(I),I=9,12)

C
C  READ THE INSTRUMENTATION BLOCK...
C
     	IREC = IRPR(1,1)+4 

C  READ THE WAVE SOURCES

      READ(IINP,REC=IREC)DIFNUM,DIFVER,GONINT,ANODE(6),CHRLIN
     &                                 ,(WAVE(I,6),I=1,5)
     	IF( WAVE(1,6).LE.0.0 ) WAVE(1,6)=1.54184  !GOTO 777 !S.Z.
     	DO I=1, 5
C
     	   IF( ABS(WAVE(1,6)-WAVE(1,I)).LT.0.05 ) IELE = I
      
     	ENDDO

	IF(IELE.LE.0 .OR. IELE.GT.6 )IELE = 4   !S.Z.
C
C        READ(IINP,REC=IRPR(1,1)+6)DTIME,DIFGAS,RING,MAXXTR,MAXYTR
C     &  ,CHGTYP,MCATYP,MCAVER,MCACHA,MCAMOD 
C
C	READ(IINP,REC=IRPR(1,1)) PSDANG	!JDW

C
C  READ THE FILE DESCRIPTION BLOCK...
C
     	IREC = IRPR(3,1)
     	READ(IINP,REC= IREC ) (NAME(I),I=1,3),DATM,TIMM
     &  ,FTYP,MTYP,INTMAX,RES,NREP,MPRO,NRNG,SCAN
     	IF( NREP.LT.1 ) GOTO 777
C      	IF( NRNG.LT.1 .OR. SCAN.NE.'S' ) GOTO 777 ! removed for contignous 
C                                                 ! runs (S.Z.)
     	IF( NRNG.LT.1 ) GOTO 777	!JDW
      READ(IINP,REC=IREC+1)(SPCAN(I),I=3,4),STYP,MTHC
     &           ,DACOUT,(SLID(I),I=1,9)
     	READ(IINP,REC= IREC+2) (SLID(I),I=10,15)
C
C  READ THE LINE MEASUREMENT BLOCK...
C
     	IREC = IRPR(4,1)
C
C  SHOW ALL THE RANGES...
C
     	IF( NRNG.GT.1 ) WRITE(6,2000)
2000	FORMAT(/' RANGE  BEGIN  ENDING   STEP   SECS   MAXCOUNTS',
     	1'  MAX2THETA  MINCOUNTS  MIN2THETA'/,X,80('-'))
2001	FORMAT(X,I3,2X,4(F7.3,X),X,2(G11.4,X,F8.3))
     	NPT(0) = 0
        MREC(0)= IRPR(6,1)
        LREC(0)= 0
     	DO J=1, NRNG
              READ(IINP,REC= IREC) (BANG(I),I=1,12)
     	      READ(IINP,REC= IREC+1) (BANG(I),I=13,14)
              START(J)=BANG(1)
              STEP(J)=BANG(3)
              END(J)=BANG(2)


     	NPT(J) = INT( (BANG(2)-BANG(1))/BANG(3)+0.5 ) + 1

     	   IF( BANG(3).LE.0.0 .OR. BANG(10).LE.0.0 ) GOTO 777
     	   IF( NRNG.GT.1 ) WRITE(6,2001) J, BANG(1),BANG(2),
     1  BANG(3),BANG(10),BANG(4),BANG(5),BANG(6),BANG(7)
       
        IREC=IREC+2 
C
C  CALCULATE RECORD LENGHT FOR EACH RANGE   S.Z.
C
        REC=0
        L=0
        DO K=1,NPT(J)
        L=L+1
        IF(L.EQ.12. OR .K.EQ.NPT(J)) THEN
        REC=REC+1
        L=0
        ENDIF
        ENDDO
        LREC(J)=REC                    ! RECORD LENGHT (J) RANGE
        MREC(J)=MREC(J-1)+LREC(J-1)    ! STARTING RECORD OF (J) RANGE
    	ENDDO
     	IRNG = NRNG
     	IF( NRNG.GT.1 ) THEN
C
C  GET A SPECIFIC RANGE...
C
2004	    WRITE(6,2005)
     	    READ(5,1001,ERR=2004,END=2004) DUMY
            IF(DUMY.EQ.0) THEN
            CALL RFULL(START,END,STEP,MREC,LREC,NPT)
            GO TO 999
            ENDIF

     	    IF( DUMY.LT.0 .OR. DUMY.GT.NRNG ) DUMY = 1
     	    IRNG = DUMY
     	    IREC = IRPR(4,1) + 2*(IRNG-1)
     	       READ(IINP,REC= IREC) (BANG(I),I=1,12)
               READ(IINP,REC= IREC+1) (BANG(I),I=13,14)
C     	NPTS = INT( (BANG(2)-BANG(1))/BANG(3)+0.5 ) + 1
        IF(SCAN.EQ.'S') NPTS=MCACHA

2005	FORMAT(/,' Which range do you want to convert ? <1> ',/,
     &         $,' Enter 0 for all ranges:')
     	ENDIF
        NPTS = NPT(IRNG)      ! TAKE NUMBER OF POINTS FOR SPECIF. RANGE
        IF(NPTS.EQ.0) GO TO 999
C
C  READ THE INTENSITY DATA BLOCK...
C
     	IF( NREP.GT.1 ) THEN
C     	    DUMY = LDIGIT( NREP )
1002	    WRITE(6,1000) NREP
     	    READ(5,1001,ERR=1002,END=1002) DUMY
     	    IF( DUMY.LT.1 .OR. DUMY.GT.NREP ) DUMY = 1
     	    NREP = DUMY
     	ENDIF
1000	FORMAT(/
     	1' Input file contains ',I2,' series of measurements'/
     	1' but only one measurement can be converted at a time.'/
     	1'$Which measurement to use? <1> ')
1001	FORMAT(I)
C
C  JUMP TO THE SPECIF. RANGE AND SERIES  S.Z.
C
        IREC = MREC(IRNG)      ! JUMP TO THE RANGE
C  CALCULATE RECORD FOR SPECIF. SERIES   S.Z.
        KREC(0)=0
        DO K=1,NRNG
        KREC(K)=LREC(K)
        KREC(K)=KREC(K-1)+KREC(K)
        ENDDO
        IREC=IREC + (NREP-1)*KREC(IRNG)   ! JUMP TO THE MEASUREMENT

        K=1
        FIRST=1
        LAST=12
        DO J=0,LREC(IRNG)-1
        READ(IINP,REC=IREC+J,err=1111)(YOBS(K),K=FIRST,LAST)

        FIRST=FIRST+12
        LAST=LAST+12
        IF(NPTS-LAST.LT.0) LAST=NPTS
10      ENDDO
1111    npts=k-1

C        DO I=1,NPTS
C        TYPE*,'YOBS(',I,')=',YOBS(I)
C        ENDDO
    	GOTO 999



777	IERR = 1
     	GOTO 999
888	IERR = 2
999	RETURN    
    	END
