      PROGRAM RIGAKU

C       *****   PROGRAM TO READ THE RIGAKU FILES   *****
C       *****   24-JUNE-1988         MSU PHYSICS   *****

C       *****   THIS PROGRAM WORKS WITH RIGAKU     *****
C       *****   RAW TYPE AND PEAKS TYPE DATA FILES *****


C     *****************************************
C          DEFINE ALL VARIABLES AND ARRAYS  
C     *****************************************

      IMPLICIT NONE

      LOGICAL*1     CODEALPHA(256), CODEFWHM(256)

      CHARACTER     FILENAME*32
      CHARACTER*2   RADELEMENT
      CHARACTER*4   FILETYPE, FILEDATE(3), FILETIME(2)
      CHARACTER*4   SAMPLEID(40), TUBETYPE
      CHARACTER*8   SEARCHTIME, SCANTIME
      CHARACTER*12  SEARCHDATE, SCANDATE
      CHARACTER*16  LAST16ID
      CHARACTER*24  FIRST24ID

      INTEGER*2  INDEX, NUMBEROFPOINTS, AXISCODE, IOSTATUS
      INTEGER*2  NUMPEAKS
      INTEGER*2  INTERATIONLIMIT, ALPHA2CODE, CALIBRATIONCODE
      INTEGER*2  JCPDS, CARDNUMBER
      INTEGER*4  JUNKREAD
      INTEGER*4  SYSTEM, MODE1, MODE2, MODE3
      INTEGER*4  SCANSPEED, KV, MA, COUNTINGTIME
      INTEGER*4  FULLSCALEVALUE, STATUS, DATACSC
      INTEGER*4  NUMBERSOFAR, COLLECTIONMODE, NUMBEROFREPEATS

      REAL*4     XRAYDATA(100000)
      REAL*4     STOPANGLE, KA, KA1, KA2, KB1, KAVG
      REAL*4     STARTANGLE, STEPSIZE, MAXINTDATA, XPOSITION
      REAL*4     TWOTHETA(256), DSPACE(256) 
      REAL*4     INTREL(256),   INTCPS(256),  FWHM(256)
      REAL*4     DMAX, DMIN, STEPWIDTH, FLTSCANSPEED, FIXEDCOUNTTIME
      REAL*4     THRESHOLD1, THRESHOLD2, CUTOFFREL, TFWHM, MFWHM
      REAL*4     PERCENTCONV, ERRORTHETATWO


C     **********************************************
C          GET THE NAME OF THE RIGAKU DATA FILE
C     **********************************************
      WRITE ( 6, 30 )
   30 FORMAT (                        //////////////////////////
     &           '  ****************************************** '/
     &           '  *                                        * '/
     &           '  *      THIS IS THE RIGAKU DATA FILE      * '/
     &           '  *             READER PROGRAM             * '/
     &           '  *                                        * '/
     &           '  *    REVISION 2             JULY 1988    * '/
     &           '  ****************************************** '// )

   40 WRITE ( 6, 50 )
   50 FORMAT ( //
     &   '  TO QUIT THIS PROGRAM ENTER JUST A CARRIAGE RETURN ', // 
     &   '  OR ELSE, TO PROCESS A RIGAKU DATA FILE            ', /
     &   '  ENTER THE FILENAME OF THAT RIGAKU DATA FILE       ', /
     &   '  DEVC:[DIR]NAME.EXT;VER:    ',                        $ )

      READ ( 5, 55 ) FILENAME
   55 FORMAT ( A )

      IF ( FILENAME .EQ. ' ' )     THEN
      GOTO 998
      END IF

      WRITE ( 6, 60 ) FILENAME
   60 FORMAT ( //
     &            '  RIGAKU DATA FILENAME ' /
     &            '  SPECIFICATION IS:  ', A
     &         // )



C     ******************************************************
C        OPEN THE FILE AND TEST WHAT TYPE OF FILE THIS IS
C     ******************************************************

      OPEN ( UNIT=10, FILE=FILENAME, STATUS='OLD',
     &       FORM='UNFORMATTED',  READONLY,  ACCESS='SEQUENTIAL',
     &       ERR=950,  IOSTAT=IOSTATUS                           )

      READ ( 10, ERR=970, IOSTAT=IOSTATUS ) FILETYPE

      IF      ( FILETYPE .EQ. '.RAW' )     THEN
      GOTO 100

      ELSE IF ( FILETYPE .EQ. '.PKS' )     THEN
      GOTO 500

      ELSE
      WRITE ( 6, 70 )
   70 FORMAT ( / '  ***************************************** '/
     &           '  *                                       * '/
     &           '  *       THIS FILE IS NOT A RIGAKU       * '/
     &           '  *        RAW OR PEAKS DATA FILE.        * '/
     &           '  *                                       * '/
     &           '  ***************************************** '/ )
      CLOSE ( UNIT=10 )
      WRITE ( 6, 80 )
   80 FORMAT ( / '  ***************************************** '/
     &           '  *                                       * '/
     &           '  *    RESTARTING THE RIGAKU DATA FILE    * '/
     &           '  *            READER PROGRAM.            * '/
     &           '  *                                       * '/
     &           '  ***************************************** '// )
      GOTO 40

      END IF




C     *********************************************************************
C       THIS IS THE START OF THE SECTION FOR HANDLING RAW TYPE DATA FILES
C     **************-----*****************************---******************

  100 CONTINUE

      REWIND ( UNIT=10 )

      WRITE ( 6, 101 )
  101 FORMAT (  '  ***************************************** '/
     &          '  *                                       * '/
     &          '  *  THIS FILE IS A RIGAKU RAW DATA FILE. * '/
     &          '  *                        ---            * '/
     &          '  ***************************************** '//
     &          '  ***************************************** '/
     &          '  *                                       * '/
     &          '  *  THE FOLLOWING DATA IS READ FROM THE  * '/
     &          '  *   HEADER AREA OF THE RAW DATA FILE.   * '/
     &          '  *                                       * '/
     &          '  ***************************************** '// )



C       *****     INPUT FILE RECORD NUMBERS: 1 THROUGH 16     *****
C       *****  GET THE FILE: TYPE, DATE, TIME, AND SAMPLE ID  *****

      READ ( 10 ) FILETYPE

      DO INDEX=1,3
        READ ( 10 ) FILEDATE(INDEX)
      END DO

      DO INDEX=1,2
        READ ( 10 ) FILETIME(INDEX)
      END DO

      DO INDEX=1,10
        READ ( 10 ) SAMPLEID(INDEX)
      END DO


      WRITE ( 6, 105 ) FILETYPE
  105 FORMAT ( '   FILE TYPE IS:  ', A / )

      WRITE ( 6, 110 ) FILEDATE(1), FILEDATE(2), FILEDATE(3)
  110 FORMAT ( '   FILE DATE IS:  ', 3A / )

      WRITE ( 6, 115 ) FILETIME(1), FILETIME(2)
  115 FORMAT ( '   FILE TIME IS:  ', 2A / )

      WRITE ( 6, 120 ) ( SAMPLEID(INDEX), INDEX=1, 10 )
  120 FORMAT ( '   SAMPLE ID IS:  ', 10A / )



C       *****  INPUT FILE RECORD NUMBERS: 17 THROUGH 20  *****
C       *****  GET AND PRINT SYSTEM AND MODE LONGWORDS   *****

      READ ( 10 ) SYSTEM
      READ ( 10 ) MODE1
      READ ( 10 ) MODE2
      READ ( 10 ) MODE3

      IF      ( SYSTEM .EQ. 1 )   THEN
        WRITE ( 6, 134 ) SYSTEM
  134   FORMAT ( '  SYSTEM BYTE = ', I2, '   ===> RIGHT SYSTEM ' / )
      ELSE IF ( SYSTEM .EQ. 2 )  THEN
        WRITE ( 6, 135 ) SYSTEM
  135   FORMAT ( '  SYSTEM BYTE = ', I2, '   ===> LEFT SYSTEM ' / )
      END IF

      IF      ( MODE1 .EQ. 1 )   THEN
        WRITE ( 6, 140 ) MODE1
  140   FORMAT ( '   MODE1 BYTE = ', I2, '   ===> CONTINUOUS SCAN '/ )
      ELSE IF ( MODE1 .EQ. 2 )  THEN
        WRITE ( 6, 141 ) MODE1
  141   FORMAT ( '   MODE1 BYTE = ', I2, '   ===> STEP SCAN  ' / )
      END IF

      IF      ( MODE2 .EQ. 1 )   THEN
        WRITE ( 6, 145 ) MODE2, MODE3
  145   FORMAT ( '   MODE2 BYTE = ', I2, '   ===> STANDARD MODE ' //
     &           '   MODE3 BYTE = ', I4, '   = NUMBER OF REPEATS' / )
      ELSE IF ( MODE2 .EQ. 2 )  THEN
        WRITE ( 6, 146 ) MODE2, MODE3
  146   FORMAT ( '   MODE2 BYTE = ', I2, '  ===> INTEGRAL MODE ' //
     &           '   MODE3 BYTE = ', I4, '  = NUMBER OF INTERGARTIONS'/)
      ELSE IF ( MODE2 .EQ. 3 )  THEN
        WRITE ( 6, 147 ) MODE2, MODE3
  147   FORMAT ( '   MODE2 BYTE = ', I2, '   ===> SKIP MODE ' //
     &           '   MODE3 BYTE = ', I4, '   = THRESHOLD VALUE ' / )
      END IF



C       *****    INPUT FILE RECORD NUMBERS: 21 THROUGH 24    *****
C       *****  GET AND PRINT STOP ANGLE, SCAN SPEED, KV, MA  *****

      READ ( 10 ) STOPANGLE
      READ ( 10 ) SCANSPEED
      READ ( 10 ) KV
      READ ( 10 ) MA

      WRITE ( 6, 150 ) STOPANGLE, SCANSPEED
  150 FORMAT ( '   STOP ANGLE = ', F7.2, '  DEGREES '  //
     &         '   SCAN SPEED = ', I6   / )

      WRITE ( 6, 155 ) KV, MA
  155 FORMAT ( '   TUBE KV IN THIS SCAN = ', I3, ' KV '  //
     &         '   TUBE MA IN THIS SCAN = ', I3, ' MA ' / )



C       *****       INPUT FILE RECORD NUMBERS: 25 THROUGH 28       *****
C       *****      GET AND PRINT TUBE TYPE, FULL SCALE VALUE,      *****
C       *****  SPECTROMETER STATUS,  DATA IN COUNTS/SEC OR COUNTS  *****

      READ ( 10 ) TUBETYPE
      READ ( 10 ) FULLSCALEVALUE
      READ ( 10 ) STATUS
      READ ( 10 ) DATACSC

      WRITE ( 6, 160 ) TUBETYPE
  160 FORMAT ( '   TUBE TYPE IS:  ', A  / )

      WRITE ( 6, 165 ) FULLSCALEVALUE
  165 FORMAT ( '   FULL SCALE VALUE FOR THIS SCAN IS:  ', I7 / )

      IF      ( STATUS .EQ. 0 )   THEN
        WRITE ( 6, 170 ) STATUS
  170   FORMAT ( '   STATUS BYTE = ', I2, '   ===> SCAN NOT STARTED '/)
      ELSE IF ( STATUS .EQ. 1 )  THEN
        WRITE ( 6, 171 ) STATUS
  171   FORMAT ( '   STATUS BYTE = ', I2, '   ===> SCAN IN PROGRESS '/)
      ELSE IF ( STATUS .EQ. 2 )  THEN
        WRITE ( 6, 172 ) STATUS
  172   FORMAT ( '   STATUS BYTE = ', I2, '   ===> SCAN COMPLETED ' / )
      ELSE IF ( STATUS .EQ. 3 )  THEN
        WRITE ( 6, 173 ) STATUS
  173   FORMAT ( '   STATUS BYTE = ', I2, '   ===> SCAN IS STOPED ' / )
      END IF

      IF      ( DATACSC .EQ. 1 )  THEN
        WRITE ( 6, 175 ) DATACSC
  175   FORMAT ( '   DATA FORMAT BYTE = ', I2, '   ====> COUNTS/SEC '/)
      ELSE IF ( DATACSC .EQ. 2 )  THEN
        WRITE ( 6, 176 ) DATACSC
  176   FORMAT ( '   DATA FORMAT BYTE = ', I2, '   ====> COUNTS ' / )
      END IF



C       *****     INPUT FILE RECORD NUMBERS: 29 THROUGH 34    *****
C       *****      GET AND PRINT:  K ALPHA, KA1, KA2, KB1     *****

      READ ( 10 ) JUNKREAD
      READ ( 10 ) JUNKREAD
      READ ( 10 ) KA
      READ ( 10 ) KA1
      READ ( 10 ) KA2
      READ ( 10 ) KB1

      WRITE ( 6, 180 ) KA, KA1, KA2, KB1
  180 FORMAT (   '   K ALPHA = ', F11.7 /
     &           '       KA1 = ', F11.7 /
     &           '       KA2 = ', F11.7 /
     &           '       KB1 = ', F11.7 / )



C       *****      INPUT FILE RECORD NUMBERS: 35 THROUGH 38      *****
C       *****  GET AND PRINT: START ANGLE, STEP SIZE, NUMBER OF  *****
C       *****    POINTS, AXIS CODE, COUNTING TIME IN SECONDS     *****

      READ ( 10 ) STARTANGLE
      READ ( 10 ) STEPSIZE
      READ ( 10 ) NUMBEROFPOINTS, AXISCODE
      READ ( 10 ) COUNTINGTIME

      WRITE ( 6, 185 ) STARTANGLE, STEPSIZE
  185 FORMAT ( '   STARTING ANGLE IS:  ', F9.4, '  DEGREES ' //
     &         '   STEP SIZE IS:       ', F9.4, '  DEGREES '  / )

      WRITE ( 6, 190 ) NUMBEROFPOINTS, AXISCODE
  190 FORMAT ( '   NUMBER OF POINTS IN THIS SCAN IS:  ', I4  //
     &         '   AXIS CODE FOR THIS SCAN IS:  ',       I4   / )

      WRITE ( 6, 195 ) COUNTINGTIME
  195 FORMAT ( '   COUNTING TIME IN SECONDS FOR THIS SCAN IS:  ', I5 /)



C       *****       INPUT FILE RECORD NUMBERS: 39 THROUGH 47        *****
C       *****  GET AND PRINT: MAXIMUM INTENSITY OF THE INPUT DATA,  *****
C       *****  NUMBER OF POINTS COLLECTED SO FAR, COLLECTION MODE,  *****
C       *****  AND NUMBER OF REPEATS                                *****

      DO INDEX=39, 43
        READ ( 10 ) JUNKREAD
      END DO

      READ ( 10 ) MAXINTDATA
      READ ( 10 ) NUMBERSOFAR
      READ ( 10 ) COLLECTIONMODE
      READ ( 10 ) NUMBEROFREPEATS

      WRITE ( 6, 200 ) MAXINTDATA, NUMBERSOFAR
  200 FORMAT ( '   MAXIMUM DATA POINT IN THIS SCAN IS:  ',F11.2  //
     &         '   NUMBER OF POINTS COLLECTED SO FAR:  ', I4  / )

      WRITE ( 6, 205 ) COLLECTIONMODE
  205 FORMAT (   '   THE COLLECTION MODE BYTE IS:  ', I2 /
     &           '          -2  ===>  DATA COLLECTION NOT STARTED '/
     &           '          -1  ===>  BACKWARD DATA COLLECTION    '/
     &           '           0  ===>  ?????                       '/
     &           '           1  ===>  FORWARD DATA COLLECTION     '/ )


      WRITE ( 6, 210 ) NUMBEROFREPEATS
  210 FORMAT ( '   THE NUMBER OF REPEATS OF THIS SCAN IS:  ', I2 / )



C       *****  INPUT FILE RECORD NUMBERS: 48 THROUGH 50 + NUMBER    *****
C       *****                           OF POINTS COLLECTED SO FAR  *****
C       *****                                                       *****
C       *****   READ THE X-RAY DATA INTO THE ARRAY                  *****

      DO INDEX=48,50
        READ ( 10 ) JUNKREAD
      END DO

      WRITE ( 6, 215 )
  215 FORMAT ( //
     &     '  *****  NOW READING THE X-RAY DATA FROM THE FILE  ***** '
     &         // )

      DO INDEX=51, ( 50 + NUMBERSOFAR )
        READ ( 10 ) XRAYDATA(INDEX)
      END DO

      CLOSE ( UNIT=10 )



C       *****  LIST FIRST 10 POINTS OF X-RAY DATA  *****

      WRITE ( 6, 220 )
  220 FORMAT (  /
     &     '   *****  THE FIRST 10 POINTS OF X-RAY DATA ARE:  ***** '
     &           / )

      DO INDEX=51, 60
        WRITE ( 6, 230 ) XRAYDATA(INDEX)
      END DO

  230 FORMAT ( '   XRAYDATA =  ', F11.2 )



C     *****  FINDOUT IF THERE SHOULD BE AN OUTPUT FILE  *****
C     *****             AND GET ITS FILE NAME           *****

      WRITE ( 6, 250 )
  250 FORMAT ( // '  TO BUILD AN ASCII FILE OF X-Y PAIRS   '  /
     &            '  ENTER THE FILENAME OF THE OUTPUT FILE '  /
     &            '              OR ELSE                   '  /
     &            '  TO QUIT ENTER JUST A CARRIAGE RETURN  '  // )
      WRITE ( 6, 251 )
  251 FORMAT (    '  ENTER CARRIAGE RETURN OR              '  /
     $            '  DEVC:[DIR]NAME.EXT;VER:   ',$ )
      READ ( 5, 255 ) FILENAME
  255 FORMAT ( A )

      IF ( FILENAME .EQ. ' ' )     THEN
      WRITE ( 6, 257 )
  257 FORMAT (// '   NO ASCII OUTPUT FILE WILL BE WRITTEN ' // )
      WRITE ( 6, 80 )
      GOTO 40
      END IF

      WRITE ( 6, 270 ) FILENAME
  270 FORMAT (// '  THE SPECIFICATION OF THE OUTPUT FILE IS:   ', A //)

      OPEN ( UNIT=15, FILE=FILENAME, STATUS='NEW',
     &      FORM='FORMATTED', CARRIAGECONTROL='LIST',
     &      ACCESS='SEQUENTIAL',  ERR=960,  IOSTAT=IOSTATUS )



C     *****  NOW WRITE THE ASCII X-Y PAIR DATA IN THE OUTPUT FILE  *****

      IF ( STARTANGLE .LT. STOPANGLE )    THEN

      XPOSITION = STARTANGLE - STEPSIZE
      DO INDEX=51, ( 50 + NUMBERSOFAR )
         XPOSITION = XPOSITION + STEPSIZE
         WRITE ( 15, 300 ) XPOSITION, XRAYDATA(INDEX)
      END DO

      ELSE IF ( STARTANGLE .GT. STOPANGLE )   THEN

      XPOSITION = STARTANGLE + STEPSIZE
      DO INDEX=51, ( 50 + NUMBERSOFAR )
         XPOSITION = XPOSITION - STEPSIZE
         WRITE ( 15, 300 ) XPOSITION, XRAYDATA(INDEX)
      END DO

      END IF

  300 FORMAT ( ' ', F6.3, '  ', F11.1 )

      CLOSE ( UNIT = 15 )

      WRITE ( 6, 280 )
  280 FORMAT ( '  THE ASCII OUTPUT FILE ', 
     &           'HAS BEEN SUCCESSFULLY WRITTEN ', // )

      WRITE ( 6, 80 )
      GOTO 40

C     *******************************************************************
C       THIS IS THE END OF THE SECTION FOR HANDLING RAW TYPE DATA FILES
C     **************---*****************************---******************




C     **********************************************************************
C       THIS IS THE START OF THE SECTION FOR HANDLING PEAK TYPE DATA FILES
C     **************-----*****************************----******************

  500 CONTINUE

      REWIND ( UNIT=10 )

      WRITE ( 6, 501 )
  501 FORMAT (  '  ******************************************* ' /
     &          '  *                                         * ' /
     &          '  *  THIS FILE IS A RIGAKU PEAKS DATA FILE. * ' /
     &          '  *                        -----            * ' /
     &          '  ******************************************* ' //
     &          '  ******************************************* ' /
     &          '  *                                         * ' /
     &          '  *   THE FOLLOWING DATA IS READ FROM THE   * ' /
     &          '  *   HEADER AREA OF THE PEAKS DATA FILE.   * ' /
     &          '  *                                         * ' /
     &          '  ******************************************* ' // )

      READ ( 10 )      FILETYPE, SEARCHDATE, SEARCHTIME
      WRITE ( 6, 510 ) FILETYPE, SEARCHDATE, SEARCHTIME
  510 FORMAT ( '  THE FILE TYPE OF THIS RIGAKU FILE IS:     ', A /
     &         '  THE DATE THAT THE PEAKS SEARCH WAS MADE:  ', A /
     &         '  THE TIME THAT THE PEAKS SEARCH WAS MADE:  ', A //  ) 

      READ ( 10 ) FIRST24ID
      READ ( 10 ) LAST16ID
      WRITE ( 6, 520 ) FIRST24ID, LAST16ID
  520 FORMAT ( '  THE SAMPLE IDENTIFICATION IS:  ', A, A // )

      READ ( 10 )       DMAX, DMIN, STARTANGLE, STOPANGLE, STEPWIDTH,
     &             FLTSCANSPEED, FIXEDCOUNTTIME
      WRITE ( 6, 525 )  DMAX, DMIN, STARTANGLE, STOPANGLE, STEPWIDTH,
     &             FLTSCANSPEED, FIXEDCOUNTTIME
  525 FORMAT ( '  DMAX IN THIS RUN IS: ', F8.3,     /
     &         '  DMIN IN THIS RUN IS: ', F8.3,     /
     &         '  THE START ANGLE IS: ', F7.3,      /
     &         '  THE STOP ANGLE IS: ' F7.3,        /
     &         '  THE STEP WIDTH IS: ' F7.4,        /
     &         '  THE SCAN SPEED IS: ' F7.4,        /
     &         '  THE FIXED COUNT TIME IS: ', F8.2, // )

      READ ( 10 )       RADELEMENT, KA1, KA2, KB1, KAVG
      WRITE ( 6, 530 )  RADELEMENT, KA1, KA2, KB1, KAVG
  530 FORMAT ( '  THE RADIATION ELEMENT IS: ', A,     /
     &         '              K ALPHA 1 IS: ', F11.7, /
     &         '              K ALPHA 2 IS: ', F11.7, /
     &         '              K BETA 1  IS: ', F11.7, /
     &         '              K AVERAGE IS: ', F11.7, // )

      READ ( 10 )      THRESHOLD1, THRESHOLD2, CUTOFFREL, TFWHM, MFWHM
      WRITE ( 6, 535 ) THRESHOLD1, THRESHOLD2, CUTOFFREL, TFWHM, MFWHM
  535 FORMAT ( '  THRESHOLD NUMBER 1  IS: ', F7.3, /
     &         '  THRESHOLD NUMBER 2  IS: ', F7.3, /
     &         '  THE RELATIVE CUTOFF IS: ', F7.3, /
     &         '  TFWHM   IN THIS RUN IS: ', F7.3, /
     &         '  MFWHM   IN THIS RUN IS: ', F7.3, // )

      READ ( 10 )       INTERATIONLIMIT, PERCENTCONV, ALPHA2CODE,
     &             CALIBRATIONCODE, JCPDS, CARDNUMBER, ERRORTHETATWO
      WRITE ( 6, 540 )  INTERATIONLIMIT, PERCENTCONV, ALPHA2CODE,
     &             CALIBRATIONCODE, JCPDS, CARDNUMBER, ERRORTHETATWO
  540 FORMAT ( '  THE INTERATION LIMIT IS: ', I5, /
     &         '  THE PERCENT CONVERGENCE IS: ', F6.3, /
     &         '  THE ALPHA 2 CODE IS: ', I5,          /
     &         '  THE CALIBRATION CODE IS: ', I5,      /
     &         '  THE JCPDS SET NUMBER IS: ', I5,      /
     &         '  THE CARD NUMBER IS: ', I5,           /
     &         '  THE 2-THETA ERROR IS: ', F8.4,       // )

      READ ( 10 )      SCANDATE, SCANTIME
      WRITE ( 6, 545 ) SCANDATE, SCANTIME
  545 FORMAT ( '  DATE THAT THE COLLECTION SCAN WAS MADE: ', A, /
     &         '  TIME THAT THE COLLECTION SCAN WAS MADE: ', A, // )

      READ ( 10 ) NUMPEAKS
      WRITE ( 6, 550 ) NUMPEAKS
  550 FORMAT ( '  THE NUMBER OF PEAKS IN THIS PEAK FILE IS: ', I3 // )


      DO INDEX = 1, NUMPEAKS
      READ ( 10 )  CODEALPHA(INDEX), TWOTHETA(INDEX), DSPACE(INDEX), 
     &                INTREL(INDEX),   INTCPS(INDEX),   FWHM(INDEX),
     &              CODEFWHM(INDEX)
      END DO

      WRITE ( 6, 555 )
  555 FORMAT ( /,
     & '    TWO         D      INTENSITY   INTENSITY     FULL WIDTH  ' /
     & '   THETA      SPACE     RELATIVE   COUNTS/SEC   HALF MAXIMUM ' /
     & '  -------  ----------  ----------  ----------   ------------ ' )

      DO INDEX = 1, NUMPEAKS
      WRITE ( 6,560 )  TWOTHETA(INDEX), DSPACE(INDEX), 
     &                  INTREL(INDEX), INTCPS(INDEX), FWHM(INDEX)
      END DO
  560 FORMAT (  F8.3, F11.4, F12.4, F14.4, F12.4 )

      CLOSE ( UNIT = 10 )

      WRITE ( 6, 570 )
  570 FORMAT ( // )

      WRITE ( 6, 80 )
      GOTO 40

C     ********************************************************************
C       THIS IS THE END OF THE SECTION FOR HANDLING PEAK TYPE DATA FILES
C     **************---*****************************----******************




C     ************************************************************
C       THIS IS THE SECTION FOR HANDLING IO ERRORS AND ALL EXITS 
C     ***********************************---------*********-----**

  950 WRITE ( 6, 951 )  IOSTATUS
  951 FORMAT ( //, '  THERE HAS BEEN AN IO SYSTEM ERROR TRYING ', /
     &             '  TO OPEN THE SPECIFIED RIGAKU DATA FILE.  ', //
     &             '  DID YOU TYPE THE FILENAME CORRECTLY?     ', //
     &             '  FORTRAN OPEN IOSTAT = ', I3,                / )
      WRITE ( 6, 80 )
      GOTO 40


  960 WRITE ( 6, 961 )  IOSTATUS
  961 FORMAT ( //, '  THERE HAS BEEN AN IO SYSTEM ERROR TRYING ', /
     &             '  TO OPEN THE SPECIFIED ASCII OUTPUT FILE. ', //
     &             '  DID YOU TYPE THE FILENAME CORRECTLY?     ', //
     &             '  FORTRAN OPEN IOSTAT = ', I3,                / )
      WRITE ( 6, 80 )
      GOTO 40


  970 WRITE ( 6, 971 )  IOSTATUS
  971 FORMAT ( //, '  THERE HAS BEEN AN IO SYSTEM ERROR TRYING ', /
     &             '  TO READ THE DATA FILE TYPE FROM THE      ', /
     &             '  SPECIFIED RIGAKU DATA FILE.              ', //
     &             '  DID YOU TYPE THE FILENAME CORRECTLY?     ', //
     &             '  FORTRAN READ IOSTAT = ', I3,                / )
      WRITE ( 6, 80 )
      GOTO 40

  998 WRITE ( 6, 999 )
  999 FORMAT ( // '  RIGAKU DATA FILE READER PROGRAM COMPLETED ', // )
      STOP '  '

      END
