
      SUBROUTINE mtchmd(tmodel, modlnm, modtyp, numpar, 
     &                  emxmod, emnmod, tblstr, tabtyp, qmerr, ierr)

      INTEGER modtyp, numpar, ierr
      REAL emxmod, emnmod

      CHARACTER*(*) tmodel, modlnm, tblstr, tabtyp
      LOGICAL qmerr 

c Routine to find a match to 'string' in the list of models and return
c the information about the model.

c Arguments :
c     string     c          i: Parse string
c     lenn       i          i: Parse position
c     tmodel     c          i: Model name entered by user
c     modlnm     c          r: Model name found
c     modtyp     i          r: Model type
c     numpar     i          r: Number of parameters
c     emxmod     r          r: Max valid energy for model
c     emnmod     r          r: Min valid energy for model
c     tblstr     c          r: Table model filename
c     tabtyp     c          r: Table model type
c     qmerr      l          r: true if model has associated error 
c     ierr       i          r: 0 = no problem

c   kaa   9/3/94
c   han   8/6/97  check user defined models first
c                 output model error indicator (qmerr) 


      INCLUDE '../../inc/xspec.inc'

      INCLUDE 'mtypes.inc'

      INTEGER ilun, i, lstr, tnmpar
      INTEGER j, lens
      INTEGER iparse, nret, iflag, idelim

      CHARACTER*255 filenm, wrtstr
      CHARACTER*255 instr
      CHARACTER*20 retstr(7), desc(7)

      LOGICAL qpart, qend, qfound, qaddtv

      INTEGER lenact
      LOGICAL xqmtch, xp_mtchmd
      CHARACTER cgmodd*128
      EXTERNAL lenact, xqmtch, cgmodd

      DATA desc /'model name', 'no. of parameters', 'low energy',
     &           'high energy', 'subroutine', 'model type', 
     &           'model error'/


      lstr = lenact(tmodel)
      qmerr = .false.        ! default is no model error 
c
c first, see if the model is a user-defined one, get the info is yes 
c
      if(xp_mtchmd(tmodel(:lstr), modtyp, numpar, emnmod, emxmod))then
         modlnm = tmodel
         tblstr=' '
         tabtyp=' '
         return
      end if
           

c  open the model data file

      filenm = cgmodd()
      lens = lenact(filenm)
*      filenm = filenm(1:lens)//'model_'//VERSION
      write(filenm,'(a,"model_",a6)') filenm(1:lens),version
      CALL getlun(ilun)
      CALL openwr(ilun, filenm, 'old', ' ', ' ', 0, 1, ierr)
      IF (ierr.NE.0) THEN
         CALL xaerror(
     &     ' MTCHMD: failed to open model definition file : ', 2)
         wrtstr = '    '//filenm(1:lenact(filenm))
         CALL xaerror(wrtstr, 2)
         RETURN
      ENDIF

      CALL xwrite(' Reading model data from the file : ', 25)
      wrtstr = '    '//filenm(1:lenact(filenm))
      CALL xwrite(wrtstr, 25)

c  loop through the model file finding the input model

      READ(ilun,'(a)') instr
      qend = .FALSE.
      qfound = .FALSE.

      DO WHILE ( .not. qend )

         iparse = 0
         CALL xgtstr(instr, iparse, 7, desc, 7, retstr, nret, iflag, 
     &               idelim)
         IF ( nret .LT. 7 ) CALL xwrite(
     & ' Failed to read list of models - check your model.dat file', 5)

         IF ( xqmtch(tmodel(1:lstr), retstr(1), qpart) ) THEN

            qend = .TRUE.
            qfound = .TRUE.

         ELSE

            READ(retstr(2),'(i8)') tnmpar
            DO i = 1, tnmpar+1
               READ(ilun, *)
            ENDDO

            READ(ilun, '(a)', iostat=ierr) instr
            IF ( ierr .NE. 0 ) qend = .TRUE.

         ENDIF

      ENDDO

      CLOSE(ilun)
      CALL frelun(ilun)

c  if the model was found then set the info and return

      IF ( qfound ) THEN

         CALL xwrite(instr, 25)
         modlnm = retstr(1)(1:8)
         IF ( retstr(6) .EQ. 'add' ) THEN
            modtyp = ADDMDL
         ELSEIF ( retstr(6) .EQ. 'mul' ) THEN
            modtyp = MULMDL
         ELSEIF ( retstr(6) .EQ. 'mix' ) THEN
            modtyp = MIXMDL
         ELSEIF ( retstr(6) .EQ. 'con' ) THEN
            modtyp = CONMDL
         ELSEIF ( retstr(6) .EQ. 'acn' ) THEN
            modtyp = ACNMDL
         ENDIF
         READ(retstr(2),*) numpar
         READ(retstr(3),*) emnmod
         READ(retstr(4),*) emxmod
         READ(retstr(7),*) i 
         qmerr = i.gt.0  
         tblstr = ' '
         tabtyp = ' '
         RETURN

      ENDIF

c  do the special cases of table models. Read the table model
c  file to get the model info.

      ierr = 0
      lens = 0
      IF( index(tmodel(1:lstr),'{') .NE. 0 ) THEN
         lstr = index(tmodel(1:lstr),'{') - 1
         lens = index(tmodel(:lenact(tmodel)),'}')
      ENDIF

      IF ( lstr .GT. 1 .AND. 
     &     xqmtch(tmodel(2:lstr), 'table', qpart) ) THEN
         IF ( xqmtch(tmodel(1:1), 'a', qpart) ) THEN
            modtyp = ADDMDL
            tabtyp = 'add'
            qaddtv = .TRUE.
         ELSEIF ( xqmtch(tmodel(1:1), 'm', qpart) ) THEN
            modtyp = MULMDL
            tabtyp = 'mul'
            qaddtv = .FALSE.
         ELSEIF ( xqmtch(tmodel(1:1), 'e', qpart) ) THEN
            modtyp = MULMDL
            tabtyp = 'exp'
            qaddtv = .FALSE.
         ELSE
            WRITE(wrtstr,'(2a)') tmodel(1:lstr), 
     &                           ' is an unknown type of table model'
            CALL xwrite(wrtstr, 10)
            RETURN
         ENDIF
         IF ( lens .GT. 0 ) THEN
            filenm = tmodel(lstr+2:lens-1)
            lstr = 0
            CALL motble(filenm, lstr, numpar, qaddtv, emxmod, emnmod, 
     &               tblstr, modlnm, qmerr, ierr)
         ELSE
            CALL xwrite(
     &       'Please give a filename within {} for the table model', 5)
         ENDIF
         RETURN
      ENDIF

c  could not find the requested model so give the user a list to choose
c  from.


      IF (tmodel(1:lstr).NE.'?') THEN
         WRITE (wrtstr, '(3a)') ' The instr ', tmodel(1:lstr),
     &                          ' is not a recognized model'
         CALL xwrite(wrtstr,2)
      ENDIF

      CALL lstmod(ierr)

      RETURN
      END



