C
C   PROGRAM TO READ EW MASTER DATASETS AND PRODUCE A FILE WITH THE DATA
C   FOR A SINGLE STAR
C
C   R. E. LUCK  -  1/90
C                  VAX/VMS 5.3
C                  FORTRAN 5.3
C
      DOUBLE PRECISION WAVE
      DIMENSION IEW(100)
      LOGICAL USED
      CHARACTER *   1 ANS,FLG(100)
      CHARACTER *   5 SP,SPECA(51),SPECB(51)
      CHARACTER *  15 STEM
      CHARACTER *  25 TITLE(100)
      CHARACTER * 100 FLN
      DIMENSION ATOM(51)
      DATA SPECA /'Li I ','C I  ','N I  ','O I  ','Na I ','Mg I ',
     $            'Mg II','Al I ','Si I ','Si II','S I  ','Ca I ',
     $            'Sc I ','Sc II','Ti I ','Ti II','V I  ','V II ',
     $            'Cr I ','Cr II','Mn I ','Fe I ','Fe II','Co I ',
     $            'Ni I ','Cu I ','Zn I ','Sr I ','Sr II','Y I  ',
     $            'Y II ','Zr I ','Zr II','Mo I ','Ba II','La II',
     $            'Ce II','Pr II','Nd II','Sm II','Eu II','(O I)',
     $            '(C I)','P I  ','Th II','Ba I ','Ca II','Eu I ',
     $            'K I  ','(O I)','(C I)'/
      DATA SPECB /'LI I ','C I  ','N I  ','O I  ','NA I ','MG I ',
     $            'MG II','AL I ','SI I ','SI II','S I  ','CA I ',
     $            'SC I ','SC II','TI I ','TI II','V I  ','V II ',
     $            'CR I ','CR II','MN I ','FE I ','FE II','CO I ',
     $            'NI I ','CU I ','ZN I ','SR I ','SR II','Y I  ',
     $            'Y II ','ZR I ','ZR II','MO I ','BA II','LA II',
     $            'CE II','PR II','ND II','SM II','EU II','(O I)',
     $            '(C I)','P I  ','TH II','BA I ','CA II','EU I ',
     $            'K I  ','(O I)','(C I)'/
      DATA ISPEC /51/
      DATA ATOM /3.0,6.0,7.0,8.0,11.0,12.0,12.1,13.0,14.0,14.1,16.0,
     $ 20.0,21.0,21.1,22.0,22.1,23.0,23.1,24.0,24.1,25.0,26.0,26.1,27.0,
     $ 28.0,29.0,30.0,38.0,38.1,39.0,39.1,40.0,40.1,42.0,56.1,57.1,58.1,
     $ 59.1,60.1,62.1,63.1,8.0,6.0,15.0,90.0,56.0,20.1,63.0,15.0,8.0,
     $ 6.0/
C
C   GET THE NAME OF THE EQUIVALENT WIDTH DATASET:
C
      WRITE (6,*) 'ENTER NAME OF MASTER EW DATA FILE:'
      READ (5,9000) FLN
      OPEN (11,FILE=FLN,STATUS='OLD',READONLY)
      READ (11,*) NSTAR, NLINE
      IF (NSTAR .GT. 100) STOP '*** TOO MANY STARS ***'
      DO 10 K = 1,NSTAR
           READ (11,9010) TITLE(K)
   10 CONTINUE
C
C     WRITE OUT THE NAMES OF THE STARS:
C
      NN       = NINT(FLOAT(NSTAR)/3.0)
      IF (NN .EQ. 0) NN = 1
   20 DO 30 K = 1,NN
           I1       = (3*K) - 2
           I2       = I1 + 2
           IF (I2 .GT. NSTAR) I2 = NSTAR
           WRITE (6,9040) (M,TITLE(M)(1:15),M=I1,I2)
   30 CONTINUE
C
C    WHICH ONE DO YOU WANT?
C
      WRITE (6,*) 'Enter index of star desired:'
      READ (5,*,END=90) ISTAR
      WRITE (6,*) 'Star selected is ', TITLE(ISTAR), '  Correct (Y/N)?'
      READ (5,9030) ANS
      IF (ANS .EQ. 'y') ANS = 'Y'
      IF (ANS .NE. 'Y') GO TO 20
C
C     DO WE WANT ALL LINES OR JUST THOSE USED IN THE ANALYSIS?
C            USED = F FOR ALL LINES
C            USED = T FOR THOSE USED
C
      WRITE (6,*) 'Do you want only the subset of used lines?'
      READ (5,9030) ANS
      IF (ANS .EQ. 'y') ANS = 'Y'
      USED     = ANS .EQ. 'Y'
      WRITE (6,*) 'Enter name for output dataset:'
      READ (5,9000) FLN
      OPEN (21,FILE=FLN,STATUS='NEW')
C
C   NOW READ THE TABLE DATA
C
C   AN ADDED COMPLICATION:  MASTER DATASETS DO NOT INCLUDE ATOM!
C
      NGET     = 0
      DO 60 M = 1,NLINE
           READ (11,9020,END=70) SP, WAVE, CHX, GF, (IEW(L),FLG(L),L=1,
     $     NSTAR)
           IF (IEW(ISTAR) .GT. 0) THEN
                IF (USED) THEN
                     IF (FLG(ISTAR) .NE. '*') GO TO 60
                END IF
                NGET     = NGET + 1
                DO 40 KK = 1,ISPEC
                     IF (SP .EQ. SPECB(KK)) GO TO 50
                     IF (SP .EQ. SPECA(KK)) GO TO 50
   40           CONTINUE
                WRITE (6,*) 'Problem : ', SP
                SP       = 'XXXXX'
                KK       =  1
   50           XEW      = FLOAT(IEW(ISTAR))
                GF       = 10.0**GF
                WRITE (21,9050) SP, WAVE, ATOM(KK), CHX, GF, XEW
           END IF
   60 CONTINUE
   70 CLOSE (21)
      WRITE (6,*) NGET-1, ' Lines written'
      REWIND (11)
      READ (11,*) NSTAR, NLINE
      DO 80 K = 1,NSTAR
           READ (11,9010) TITLE(K)
   80 CONTINUE
      GO TO 20
   90 STOP
C
 9000 FORMAT(A100)
 9010 FORMAT(A25)
 9020 FORMAT(A5,3F10.3,100(I5,A1))
 9030 FORMAT(A1)
 9040 FORMAT(' ',5X,3(I2,1X,A15,5X))
 9050 FORMAT(A5,F10.3,F10.1,F10.3,1PE10.3,0PF10.1)
      END
