      subroutine DECOD4(OPTION,IREC,KT,KCHEM,CC)
      include 'INPAR.INC'
      include 'UNITS.INC'
      real RESULT
      integer IMBED,MATCH,IHELP,THREE,NUMAS,NUMIN
      integer IT,KT,IREC,KCHEM
      integer INUNIT, WHICH, ERROR, CC, OPTION, EOF
      integer ASLEN(3), MINAS(3), INLEN(3), MININ(3)
      character BLANK*1, AS(10)*1, IN(10)*1, TEXT1(2)*10
      character REQFMT*18
      data THREE,NUMAS,NUMIN/3,10,10/
      data REQFMT/'(1X''or Quit-> ''  )'/
      data ASLEN,MINAS,INLEN,MININ/2,4,4, 1,1,1, 2,4,4, 1,1,1/
      data BLANK,AS,IN/' ','A','S','H','E','L','P','Q','U','I','T'
     &, 'I','N','H','E','L','P','Q','U','I','T'/
      data TEXT1/'stored.   ','retrieved.'/
      CC = 1
      INUNIT = TTYIN(MACHNO)
      START = IMBED(INPUT,STOPIT)
      if (START.eq.-999) go to 140
      call SKAN (INPUT,START,STOPIT,TYPE,BLANK,1)
      if (TYPE.eq.100) STOPIT = 81
      if (OPTION.eq.1) WHICH = MATCH(THREE,NUMIN,INLEN,IN,MININ)
      if (OPTION.eq.2) WHICH = MATCH(THREE,NUMAS,ASLEN,AS,MINAS)
      if (WHICH.eq.0) then
         write (TTYOUT(MACHNO),5000)
5000     format (/1X,'Response not recognized.')
         go to 170
      endif
      go to (110,100,170), WHICH
100   continue
      if (OPTION.eq.1) write (TTYOUT(MACHNO),5010)
5010  format (/1X,'Message unknown.')
      if (OPTION.eq.2) write (TTYOUT(MACHNO),5020) KCHEM
5020  format (/                                                         
     &' Enter a number between 1 and ',I3,'.'/                          
     &' The database from the chemical UDB will'/                       
     &' be transferred to that sector of active chemical'/              
     &' memory, and will then be available for simulation.')
      go to 120
110   continue
      call GETNUM (ERROR,RESULT)
      if (ERROR.eq.0) go to 130
      if (ERROR.eq.1) go to 120
      IT = IHELP(IT)
      if (IT.eq.2) go to 170
      if (IT.eq.1) go to 100
      if (OPTION.eq.1) write (TTYOUT(MACHNO),5030)
5030  format (/1X,'Invalid chemical record number specified.',' Nothing 
     &stored.')
      if (OPTION.eq.2) write (TTYOUT(MACHNO),5040)
5040  format (/1X,'Invalid chemical catalog number specified.',' Nothing
     & retrieved.')
      go to 170
120   continue
      REQFMT(16:17) = CRSTOP(MACHNO)
      if (OPTION.eq.1) then
         write (TTYOUT(MACHNO),5050)
         write (TTYOUT(MACHNO),REQFMT)
      elseif (OPTION.eq.2) then
         write (TTYOUT(MACHNO),5060)
         write (TTYOUT(MACHNO),REQFMT)
      endif
5050  format (/,1X,'Enter chemical catalog  number, HELP,')
5060  format (/,1X,'Enter activity database number, HELP,')
      call INREC (EOF,INUNIT,INPUT)
      if (EOF.eq.1) go to 160
      STOPIT = 0
      IT = IHELP(IT)
      STOPIT = 0
      if (IT.eq.1) go to 100
      if (IT.eq.2) go to 170
      go to 110
130   continue
      if (OPTION.eq.2) then
         KT = RESULT
      else
         KT = IREC
         IREC = RESULT
      endif
140   continue
      if (KT.le.KCHEM) go to 150
      write (TTYOUT(MACHNO),5070) KCHEM,TEXT1(OPTION)
5070  format (/1X,'Chemical catalog number greater than the',' maximum a
     &llowable: ',I3,'.'/' Nothing ',A10)
      go to 170
150   continue
      return
160   continue
      CC = 2
      go to 150
170   continue
      CC = 3
      go to 150
      end
