C  NSLS X14 DAS output ascii files data to newest MDI format for use 
C  with Jade + for WINDOWS or Shadow.   Modified 6/96 by Scott Misture.  
C      
C     ************ VARIABLE DECLARATION **********
      CHARACTER*70 TITLE
      REAL*4 ANG(20000), SIG(20000), INTEN(20000)
      REAL*4 SANG, EANG, STEP, CTIM,LAMDA,ENERGY
      INTEGER*4 ENDFLG
      CHARACTER*4 SKIP
      CHARACTER*60 x14FILE, MDIFILE
      CHARACTER*2 RAD
      INTEGER IO, COUNT,DAY,MONTH,YEAR,NPTS
C
C     *************  GET FILENAMES  ***************
C
      TYPE *, 'THIS PROGRAM CONVERTS ASCII FILES FROM NSLS X14'
      TYPE *, 'TO *.MDI FILES THAT CAN BE PROCESSED BY MDI SOFTWARE'
      TYPE *, ' '
      TYPE *, 'ENTER ENERGY IN keV (DEFAULT IS CU K-ALPHA)'
      READ(*,12)ENERGY
12    FORMAT(F8.5)
      IF(ENERGY.EQ.0.0) ENERGY=8.04
13    TYPE *, 'ENTER THE X14 FILENAME WITHOUT EXTENSION'
      READ (*,5) x14FILE
5     FORMAT (A75)
      IF(X14FILE(1:1).eq.' ')goto 999
      do k=1,75
        if(X14FILE(k:k).eq.' ') GO TO 14
      enddo
14    X14FILE(k:k+3)='.ASC'
       MDIFILE(1:K-1) = X14FILE(1:K-1)
       MDIFILE(K:K+3) = '.MDI'
C
C  *************  OPEN FILES  *******************
C
      CLOSE(UNIT=1)
      OPEN (UNIT=1,FILE=x14FILE,ERR=888,STATUS='OLD',FORM='FORMATTED')
      CLOSE(UNIT=2)
      OPEN (UNIT=2, FILE=MDIFILE, STATUS='NEW',FORM='FORMATTED')

C  ******* READ ASCII FILE  HEADER  *********

      READ (1,10) TITLE1
      READ (1,10) TITLE1
      READ (1,10) TITLE1
      READ (1,10) TITLE1
10    FORMAT (A)
C
C ********    READ INTENSITY DATA **********  
C
      DO I=1,20000
        READ(1,1002,ERR=111)ANG(I),INTEN(I)
1002    FORMAT(F14.4,1X,F12.1)
      NPTS = I
      EANG = ANG(I)
      ENDDO
111   STEP = ANG(2) - ANG(1)
      SANG = ANG(1)
      CTIM = 1.0
C
C  *********** CALCULATE WAVELENGTH ********
C
      LAMDA=12.39842435/ENERGY
C
C  ********** WRITE MDI FILE *********
C
      DAY=01
      MONTH=01
      YEAR=01
      RAD ='US' 
      TITLE = MDIFILE(1:K-1)
          WRITE (2,100,ERR=890)DAY,MONTH,YEAR,TITLE
100       FORMAT (7X,I2,'/',I2,'/',I2,4X,A60)
          WRITE (2,101,ERR=890) SANG,STEP,CTIM,RAD,LAMDA,EANG,NPTS
101       FORMAT(F7.3,F6.3,1X,F5.1,1X,A2,F10.6,F8.3,I7)
          WRITE (2,102,ERR=890) (INTEN(I),I=1,NPTS)
102       FORMAT(8F8.0)
C
C ************* CLOSE AND ERRORS *****************
C
          CLOSE(UNIT=1)
          CLOSE(UNIT=2)
       GO TO 1111
888   PRINT*, 'CAN NOT FIND/OPEN FILE'     
       GO TO 999
889   print*,'ERROR READING INPUT FILE'
       GO TO 999
890   PRINT*,'ERROR WRITING FILE'            
       GO TO 999
1111  TYPE *, '******** CONVERSION COMPLETE *********'
      GO TO 13
999   STOP
200   END

