      subroutine writer
      include 'SIZE.INC'
      include 'GLOBAL.INC'
      include 'LOCAL.INC'
      include 'UNITS.INC'
      include 'INPAR.INC'
      integer EOF,NORESP(4),MNRESP(4),NUMANS(2),MINANS(2),WHICH
      integer INUNIT,IMBED,MATCH,TWO,FOUR,LENRES,LENANS
      logical CHEM, ENV
      character ANSWER(8)*1, RESP(27)*1, BLANK*1
      character REQFMT(2)*53
      data TWO,FOUR,LENRES,LENANS/2, 4, 27, 8/
      data REQFMT(1)
     &/'(/'' Enter Environment, Chemical, Help, or Quit-> ''  )'/
        data REQFMT(2)
     &/'(/'' Enter name of file, Help, or Quit-> ''           )'/
        data RESP  /'C','H','E','M','I','C','A','L'
     &,'E','N','V','I','R','O','N','M','E','N','T'
     &,'H','E','L','P', 'Q','U','I','T'/
        data BLANK, NORESP, MNRESP /' ', 8,11,4,4, 1,1,1,1/
        data ANSWER/'H','E','L','P', 'Q','U','I','T'/
        data NUMANS, MINANS /4,4, 1,1/
      CHEM = .false.
      ENV = .false.
      INUNIT = TTYIN(MACHNO)
      REQFMT(1)(51:52) = CRSTOP(MACHNO)
      REQFMT(2)(42:43) = CRSTOP(MACHNO)
      START = IMBED(INPUT,STOPIT)
      if (START.ne.-999) go to 110
100   continue
      write (TTYOUT(MACHNO),REQFMT(1))
      call INREC (EOF,INUNIT,INPUT)
      if (EOF.eq.1) go to 180
      START = IMBED(INPUT,0)
      if (START.eq.-999) go to 100
110   continue
      call SKAN (INPUT,START,STOPIT,TYPE,BLANK,1)
      if (TYPE.eq.100) STOPIT = 81
      WHICH = MATCH(FOUR,LENRES,NORESP,RESP,MNRESP)
      if (WHICH.eq.0) then
         write (TTYOUT(MACHNO),5000)
5000     format (/' Response not recognized, please try again:')
         go to 100
      endif
      go to (130,130,120,200), WHICH
120   continue
      write (TTYOUT(MACHNO),5010)
5010  format (/' The WRITE command is used to download the ADB to'/' a s
     &eparate datafile on disk.  In response to the prompt,'/' indicate 
     &your choice of Chemical or Environmental data,'/' or you can type 
     &"Quit" to return to the EXAMS prompt.')
      go to 100
130   continue
      if (WHICH.eq.1) then
         CHEM = .true.
      else
         ENV = .true.
      endif
      START = IMBED(INPUT,STOPIT)
      if (START.ne.-999) go to 150
140   continue
      write (TTYOUT(MACHNO),REQFMT(2))
      call INREC (EOF,INUNIT,INPUT)
      if (EOF.eq.1) go to 180
      START = IMBED(INPUT,0)
      if (START.eq.-999) go to 140
150   continue
      call SKAN (INPUT,START,STOPIT,TYPE,BLANK,1)
      if (TYPE.eq.100) STOPIT = 81
      WHICH = 1+MATCH(TWO,LENANS,NUMANS,ANSWER,MINANS)
      go to (170,160,200), WHICH
160   continue
      write (TTYOUT(MACHNO),5020)
5020  format (/' In reponse to the prompt, enter the full name of the'/'
     & file you want to receive the ADB data.  If you want to'/' termina
     &te the WRITE and return to the EXAMS prompt,'/' instead respond to
     & the prompt with "Quit".')
      if (MACHIN.eq.'IBM') then
         if (CHEM) write (TTYOUT(MACHNO),5030)
         if (ENV) write (TTYOUT(MACHNO),5040)
      endif
5030  format (' You can only create files that have been allocated in yo
     &ur'/' JCL.  If you are using the JCL supplied with the program,'/'
     & files of chemical data should be named CHEMn,'/' where "n" is any
     & integer.')
5040  format (' You can only create files that have been allocated in yo
     &ur'/' JCL.  If you are using the JCL supplied with the program,'/'
     & files of environmental data should be named ENVIRn,'/' where "n" 
     &is any integer.')
      go to 140
170   continue
      FILNAM(2) = INPUT(START:STOPIT-1)
      open (unit=RWLUN(MACHNO),status='UNKNOWN',access='SEQUENTIAL',form
     &   ='FORMATTED',err=190,FILE=FILNAM(2))
      if (.not.(TOPOPN(MACHNO))) rewind RWLUN(MACHNO)
      if (CHEM) call CHMOUT
      if (ENV) call ENVOUT
      close (unit=RWLUN(MACHNO),err=200)
      go to 200
180   continue
      write (TTYOUT(MACHNO),5050)
5050  format (/' End-of-file marker encountered while reading input.')
      go to 200
190   continue
      write (TTYOUT(MACHNO),5060)
5060  format (/' Error opening disk file, command aborted.')
200   continue
      return
      end
