*      SUBROUTINE RRWDPC
*
* This subroutine reads a raw file from Diffrac / PC software
* for PSD data collection written by Gerhard Zorn
* 
*  This subroutine written for vaxconv convertion routine
*   By D. P. MAtheis           September 1990
*
*
C==================================================================
C
C	COMMON BLOCKS FOR PROGRAM FILECONV
C
C==================================================================
C
C  GENERAL BLOCKS...
C
     	PARAMETER DTOR= 0.8726646259E-2, RTOD= 114.591559
     	COMMON /GENRAL/ IREC
C
C	IREC --- READ RECORD POINTER (FOR DEBUGING PURPOSE)
C==================================================================
C
C	INPUT/OUTPUT BLOCKS...
C
     	PARAMETER IINP= 1, IOUT= 2, NTYP= 60
     	CHARACTER FILTYP(NTYP)*24
     	CHARACTER FILINP*32, FILOUT*32
     	COMMON/IOFILE/IERR,JFIL,IFLI,IFLO,ITYI,ITYO,FILINP,FILOUT,FILTYP
C
C	IERR --- ERROR FLAG:
C	     FROM  READ: 0=NO ERROR, 1=ERROR DURING READ, 2=OPEN FAILURE
C			 3=BAIL OUT
C	     FROM WRITE: 0=NO ERROR, 1=ERROR DURING WRITE OR OPEN FAILURE
C			 3=BAIL OUT
C	JFIL ----- FILE TYPE NOT SUPPORTED YET
C	IFLI ----- INPUT FILE TYPE
C	IFLO ----- OUTPUT FILE TYPE
C	ITYI ----- INPUT FILE CATEGORY
C		   1=RAW, 2=DIF, 3=PLT, 4=DAT
C	ITYO ----- OUTPUT FILE CATEGORY
C		   1=RAW, 2=DIF, 3=PLT, 4=DAT
C	IINP ----- LOGICAL UNIT NUMBER FOR INPUT DATA FILE
C	IOUT ----- LOGICAL UNIT NUMBER FOR OUTPUT DATA FILE
C	FILINP --- INPUT DATA FILENAME
C	FILOUT --- OUTPUT DATA FILENAME
C	NTYP ----- MAX NUMBER OF FILE TYPES CONVERTABLE
C	FILTYP --- CHARACTER ARRAY THAT STORES THE FILE TYPE NAME
C==================================================================
C
C	FILE HEADER BLOCK...
C
     	INTEGER*2 ITYP
     	COMMON /FILHED/ ITYP, NHDB, ISBF, LENG
C
C	ITYP --- DATA FILE TYPE
C	NHDB --- NUMBER OF RECORDS IN THE HEADER BLOCK
C	ISBF --- STARTING RECORD OF SPECIFICATION BLOCK REFERENCES
C	LENG --- LENGTH OF SPECIFICATION BLOCK REFERENCES
C==================================================================
C
C	FILE BLOCK REFERENCES...
C
     	PARAMETER NSBR= 12
     	CHARACTER IRLR(NSBR)*4
     	COMMON /FILRFR/ IRPR(NSBR,2), IRLR
C
C	NSBR --- NUMBER OF SPECIFICATION BLOCK REFERENCES
C	IRPR --- SPECIFICATION BLOCK REFERENCES (POINTERS AND LENGTH)
C	IRLR --- LABEL OF SPECIFICATION BLOCK REFERENCES
C==================================================================
C
C	FILE DESCRIPTION BLOCK...
C
     	INTEGER*2 FTYP, MTYP, NREP, MPRO, NRNG
     	CHARACTER NAME(3)*4, SCAN*1, STYP*1, SLID(20)*4
     	COMMON /FILDES/ NAME, DATM, TIMM, FTYP, MTYP, NREP,
     	1 MPRO, NRNG, SCAN, STYP, SLID
C
C	NAME --- OPERATOR NAME
C	DATM --- ENCODED DATE
C	TIMM --- ENCODED TIME
C	FTYP --- FILE TYPE
C	MTYP --- METHOD OF ANALYSIS
C	NREP --- NUMBER OF SERIES OF MEASUREMENTS
C	MPRO --- MEASUREMENT PROCEDURE
C	NRNG --- NUMBER OF SCAN RANGES
C	SCAN --- SCAN MODE
C	STYP --- SERIES TYPE
C	SLID --- SAMPEL ID
C==================================================================
C
C	LINE MEASUREMENT BLOCK...
C
     	COMMON /LINEMM/ BANG(14), DANG(14)
C
C	BANG(1) --- STARTING ANGLE
C	BANG(2) --- ENDING ANGLE
C	BANG(3) --- STEP WIDTH
C	BANG(4) --- MAX COUNTS
C	BANG(5) --- TWO-THETA OF MAX COUNTS
C	BANG(6) --- MIN COUNTS
C	BANG(7) --- TWO-THETA OF MIN COUNTS
C	BANG(8) --- RESERVED
C	BANG(9) --- NUMBER OF OSCILLATIONS
C	BANG(10)--- COUNT TIME (SEC) FOR PEAK
C	BANG(11)--- COUNT TIME FOR BACKGROUND
C	BANG(12)--- RESERVED
C	BANG(13)--- DELAY TIME
C	BANG(14)--- FIXED ANGLE OF MEASUREMENT
C	DANG(*) --- USER MODIFIABLE SET OF BANG(*)
C==================================================================
C
C	INSTRUMENTATION BLOCK...
C
     	CHARACTER ANODE(6)*2
     	COMMON /INSTRU/ IELE, WAVE(5,6), ANODE
C
C	IELE -------- TARGET ELEMENT (DEFAULT=4,Cu)
C	WAVE(*,1) --- Cr
C	WAVE(*,2) --- Fe
C	WAVE(*,3) --- Co
C	WAVE(*,4) --- Cu
C	WAVE(*,5) --- Mo
C	WAVE(*,6) --- USER SPECIFIED
C	WAVE(1,*) --- WAVELENGTH OF K-ALPHA 1
C	WAVE(2,*) --- WAVELENGTH OF K-ALPHA 2
C	WAVE(3,*) --- WAVELENGTH OF K-ALPHA
C	WAVE(4,*) --- WAVELENGTH OF K-BETA
C	WAVE(5,*) --- KA1/KA2 RATIO
C==================================================================
C
C	INTENSITY DATA BLOCK...
C
     	PARAMETER MNP= 10000
     	REAL*4 YOBS(MNP)
     	COMMON /COUNTS/ MODE, NPTS, XOBS(MNP), YOBS
C
C	MNP ---- MAXIMUM NUMBER OF DATA POINTS ALLOWED
C	MODE --- 0 = X --- TWO-THETA
C		 1 = X --- D-SPACING
C		 2 = X --- Q-SCAN (Q=2PI/D=4PI*SIN(THETA)/LAMDA)
C	NPTS --- TOTAL NUMBER OF DATA POINTS STORED IN YOBS
C	XOBS --- ARRAY TO STORE THE X-VALUES
C	YOBS --- ARRAY TO STORE THE RAW COUNTS
C==================================================================
C
C	DIFILE BLOCK...
C
     	PARAMETER MNK= 500
     	CHARACTER PEAK(MNK)*4
     	INTEGER*4 PHKL(3,MNK)
     	COMMON /DIFILE/ NPKS, IRES, JRES, CUTI(2), CUTF(2),
     	1 F2TH(MNK), FDSP(MNK), C2TH(MNK), CDSP(MNK),
     	1 PHGH(MNK), AREA(MNK,2), RELA(MNK),
     	1 FWHM(MNK), PHKL, PEAK
C
C	MNK ---- MAXIMUM NUMBER OF PEAKS ALLOWED
C	NPKS --- TOTAL NUMBER OF PEAKS STORED
C	IRES --- RESIDUAL PATTERN FLAG: 0=CONTAINED, 1=NOT CONTAINED
C	JRES --- USE THE RESIDUAL PATTERN: 1=NO, 2=YES
C	CUTI --- INTENSITY CUTOFFS (%)
C	CUTF --- FWHM CUTOFFS (D)
C	F2TH --- OBSERVED TWO-THETA
C	FDSP --- OBSERVED D-SPACING
C	C2TH --- CORRECTED TWO-THETA
C	CDSP --- CORRECTED D-SPACING
C	PHGH --- PEAK HEIGHT (CPS)
C	AREA(*,1) --- INTEGRATED INTENSITY
C	AREA(*,2) --- RESIDUAL INTENSITY
C	RELA --- RELATIVE INTENSITY
C	FWHM --- FULL WIDTH AT HALF MAXIMUM
C	PHKL --- REFLECTION HKL
C	PEAK --- PEAK TYPE LABEL
C==================================================================
      character*4 key
      REAL*4 LAMDA1, lambda2
      INTEGER*4 ITIT(20), flag, samp
      EQUIVALENCE (ITIT,SLID)
*
      close(unit=iinp)
      open(unit=iinp, file=filinp,  status='old',
     1  access='direct', form='unformatted', recl=4, err=888)
*
* read scan parameters
*
      read(iinp,rec=1,err=777)key
      read(iinp,rec=2,err=777)npts
      read(iinp,rec=3,err=777)bang(10)
      read(iinp,rec=4,err=777)bang(3)
      read(iinp,rec=5,err=777)flag
      read(iinp,rec=6,err=777)samp
      read(iinp,rec=7,err=777)bang(1)
*
* read sample info
* 
      do i=11,18
        read(iinp,rec=i,err=777)itit(I-10)
      enddo
*
* read wavelength
*
      read(iinp,rec=19,err=777)lambda1
      read(iinp,rec=20,err=777)lambda2
      if(lambda1.eq.0.0.and.lambda2.eq.0.0)then
        iele = 4
      else
        wave(1,6) = lambda1
        wave(2,6) = lambda2
      endif
*
* read number of ranges
*
      read(iinp,rec=39,err=777)irngs
*
* read the intensity data
*
      do i=40,39+npts
        read(iinp,rec=i,err=777)yobs(i-39)
      enddo
*
* check for another range or end of file
*
      read(iinp,rec=40+npts,err=200)npts2
      print*,'This program will not yet read multiple ranges'
      go to 999
 777  print*,'Error reading from file'
      GOTO 999
 888  Print*,'Cannot find/open file'
      goto 999
C
C  WRITE OUT AN ASCII FILE...
C
 200	CLOSE( UNIT=IOUT )
     	OPEN( UNIT=IOUT, FILE=FILOUT, STATUS='NEW', ERR=888,
     	1 FORM='FORMATTED', CARRIAGECONTROL='LIST' )

     	WRITE(IOUT,1000,ERR=888) SLID
1000	FORMAT(20A4)
     	WRITE(IOUT,1002,ERR=888) bang(1),bang(2),bang(3),bang(10)
1002	FORMAT(4F10.4)
     	WRITE(IOUT,1003,ERR=888) (YOBS(I), I=1, NPTS)
1003	FORMAT(10F7.0)
     	GOTO 999
 999  stop
      END
