      PROGRAM CAD4ASCII
C
C ROBERT H. BLESSING
C MEDICAL FOUNDATION OF BUFFALO
C 73 HIGH STREET
C BUFFALO, NEW YORK 14203, USA
C TELEPHONE (716)856-9600
C
C JUNE 1994
C
C RUNS INTERACTIVELY.
C
C DECODES A FILE CONTAINING THETA/TWO-THETA OR OMEGA SCAN PROFILE DATA
C WRITTEN BY THE ENRAF-NONIUS CAD4 DIFFRACTOMETER.
C
C WRITES A BINARY FILE NAME.RAW CONTAINING THE RAW REFLECTION DATA
C RECORDS:
C
C   JREF      SERIAL NUMBER
C
C   JH
C   JK        MILLER INDICES
C   JL
C
C   THETA
C   PHIK      DIFFRACTOMETER
C   OMEGAK    SETTING ANGLES
C   KAPPA
C
C   WIDTH     SCAN WIDTH (DEGREES OMEGA)
C   SPEED     SCAN SPEED (DEGREES OMEGA PER MINUTE)
C
C   BGL       LEFT BACKGROUND  (SUM STEPS  1-16)
C   PK        PEAK             (SUM STEPS 17-80)
C   BGR       RIGHT BACKGROUND (SUM STEPS 81-96)
C
C   RNET      NET REFLECTION INTENSITY
C   SIGR      ESD ON COUNTING STATISTICS ALONE
C
C   XTIME     X-RAY EXPOSURE TIME (HOURS)
C
C   JD(96)    ARRAY OF RAW STEP SCAN COUNTS
C
C JREF IS AN INTEGER*4 WORD; JH, JK, JL, AND JD(96) ARE INTEGER*2 WORDS,
C AND ALL THE OTHER WORDS ARE REAL*4.
C
C JREF IS NEGATIVE FOR STANDARD REFERENCE INTENSITY REFLECTIONS.
C
C WIDTH IS NEGATIVE IF THE BEAM ATTENUATOR WAS USED.
C
C RNET = SPEED*(PK - 2*(BGL + BGR))
C SIGR = SPEED*(SQRT(PK + 4*(BGL + BGR)))
C
C NOTE THAT RNET AND SIGR AS READ FROM THE DIFFRACTOMETER TAPE ARE
C RNET/SPEED AND SIGR/SPEED, WHICH ARE NOT NORMALIZED TO ACCOUNT FOR
C VARIABLE SCAN SPEEDS OR FOR THE BEAM ATTENUATOR FACTOR.
C
C-----------------------------------------------------------------------
C DATA ON CAD4 ASCII FILE ARE ORGANIZED IN 64-BYTE RECORDS.
C
C BYTE 1 IS A LINE FEED CHARACTER; BYTE 64 IS A CARRIAGE RETURN
C CHARACTER.
C
C 64 BYTES PER RECORD
C
C 8 BITS PER BYTE
C 1 BYTE PER ASCII CHARACTER
C 2 CHARACTERS PER INTEGER*2 WORD
C-----------------------------------------------------------------------
      CHARACTER ADATA*64,ACODE(6)*1,AD(96)*4
      REAL KAPPA
      INTEGER*2 JREF
      INTEGER*2 JH,JK,JL,JD(96)
      WRITE (6,999)
 999  FORMAT ('0TYPE IN NAME FOR INPUT FILE.')
      READ (5,998) ADATA
 998  FORMAT (A)
      OPEN (UNIT=1,FILE=ADATA,STATUS='OLD',FORM='FORMATTED')
      WRITE (6,997)
 997  FORMAT ('0TYPE IN NAME FOR OUTPUT FILE.')
      READ (5,998) ADATA
      OPEN (UNIT=2,FILE=ADATA,STATUS='NEW',FORM='UNFORMATTED')
      NREF=0
 1    READ (1,100,END=9) ADATA
 100  FORMAT (A)
C
C DETERMINE RECORD TYPE.
C
      JTYPE=0
      READ (ADATA,1400,ERR=1) JTYPE
 1400 FORMAT (1X,I2)
      IF (JTYPE.LT. 1) GO TO 1
      IF (JTYPE.GT.10) GO TO 1
      IF (JTYPE.EQ. 1) GO TO 401
      IF (JTYPE.EQ. 2) GO TO 402
      IF (JTYPE.LE.10) GO TO 403
C
C TYPE 1 RECORD
C
  401 READ (ADATA,1401,ERR=1) JREF,JH,JK,JL,(ACODE(J),J=1,6),
     & PSI,NPI,JBGL,JPK,JBGR
 1401 FORMAT (3X,I6,3I5,1X,6A1,F7.2,I4,I6,I7,I6)
      GO TO 1
C
C TYPE 2 RECORD
C
  402 READ (ADATA,1402,ERR=1) JREF,THETA,PHIK,OMEGAK,KAPPA,
     & WIDTH,JXTIME,JFRIDL
 1402 FORMAT (3X,I6,F8.3,3F9.3,F7.3,I7,I3)
      GO TO 1
C
C TYPES 3 TO 10 RECORDS
C
  403 N=12*(JTYPE-2)
      M=N-11
      READ (ADATA,1403,ERR=1) JREF,(AD(J),J=M,N)
 1403 FORMAT (3X,I6,4(1X,3A4))
      IF (N.LT.96) GO TO 1
C
C RECORDS FOR ONE ENTIRE REFLECTION ARE DECODED.
C
      NREF=NREF+1
C
C FLAG STANDARD REFERENCE INTENSITY REFLECTIONS BY SETTING THEIR SERIAL
C NUMBER NEGATIVE.
C
      IF (ACODE(1).EQ.'I') JREF=-JREF
C
C STORE THE COUNT ARRAY, AFTER TESTING FOR COUNT STEPS THAT OVERFLOWED
C THE I4 FORMAT.
C
      NERR=0
      NSUM=0
      DO J=1,96
        IF (AD(J).EQ.'****') THEN
          NERR=NERR+1
        ELSE
          READ (AD(J),500) JD(J)
          NSUM=NSUM+JD(J)
        END IF
      END DO
 500  FORMAT (I4)
C
C FIX ANY FORMAT I4 OVERFLOW ERRORS BY SUPPLYING THE AVERAGE OVERFLOW
C COUNT.
C
      IF (NERR.GT.0) THEN
        N=NINT(FLOAT(JBGL+JPK+JBGR-NSUM)/NERR)
        DO J=1,96
          IF (AD(J).EQ.'****') JD(J)=N
        END DO
      END IF
C
C ESTIMATE NET INTEGRATED INTENSITY.
C
      RNET=JPK-2*(JBGL+JBGR)
      VARR=JPK+4*(JBGL+JBGR)
      SIGR=SQRT(VARR)
C
C EVALUATE SCAN SPEED.
C
      SPEED=20.1166/NPI
C
C NEGATIVE SCAN SPEED IS A FLAG THAT THE BEAM ATTENUATOR WAS USED.
C NEGATIVE WIDTH IS A FLAG THAT THE SCAN WAS DONE IN NEGATIVE, I.E.,
C POSITIVE TO NEGATIVE, DIRECTION.  (NOTE, HOWEVER, THAT THE PROFILE IS
C ALWAYS WRITTEN IN THE POSITIVE, I.E., NEGATIVE TO POSITIVE, DIRECTION.)
C
C TRANSFER ATTENUATOR FLAG FROM SCAN SPEED TO SCAN WIDTH.
C
      WIDTH=ABS(WIDTH)
      IF (SPEED.LE.0.0) THEN
        SPEED=-SPEED
        WIDTH=-WIDTH
      END IF
C
C NORMALIZE NET INTENSITY FOR VARIABLE SCAN SPEED.
C
      RNET=SPEED*RNET
      SIGR=SPEED*SIGR
C
C CONVERT X-RAY EXPOSURE TIME FROM SECONDS TO HOURS.
C
      XTIME=JXTIME/3600.
C
C REVERSE ORDER OF PROFILE ARRAY FOR REFLECTIONS MEASURED AT NEGATIVE
C TWO-THETA.
C
      IF (THETA.LT.0) THEN
        DO J=1,48
          K=96-J+1
          JT=JD(J)
          JD(J)=JD(K)
          JD(K)=JT
        END DO
      END IF
C
C REVERSE SIGNS OF INDICES FOR PSI SCAN REFLECTIONS MEASURED AS FRIEDEL
C EQUIVALENTS BUT AT NEGATIVE TWO-THETA.
C
      IF (PSI.NE.0.AND.THETA.LT.0.AND.JFRIDL.LT.0) THEN
        JH=-JH
        JK=-JK
        JL=-JL
      END IF
C
C WRITE REFLECTION RECORD TO OUTPUT FILE.
C
      WRITE (2) JREF,JH,JK,JL,THETA,PHIK,OMEGAK,KAPPA,WIDTH,SPEED,
     & FLOAT(JBGL),FLOAT(JPK),FLOAT(JBGR),RNET,SIGR,XTIME,JD
      GO TO 1
 9    CONTINUE
C
C END OUTPUT FILE AND EXIT.
C
      ENDFILE 2
      WRITE (6,990)
 990  FORMAT ('0NREF = ',I5,' REFLECTIONS TRANSFERRED TO OUTPUT FILE')
      STOP 'PROGRAM CAD4TAPE FINIS!'
      END
