      subroutine BLDMOD(OFFSET,NOMOD)
      include 'SIZE.INC'
      include 'UNITS.INC'
      real VALUE
      integer CHUSED, OFFSET
      logical NOTNUM
      integer MODLEN(200), TS(200), TD(200), MODMIN(200)
      integer TCL1(200), TCL2(200), TCL3(200)
      integer IBUFF(VARIEC),COMVAR(50),NOCOM,START,STOPIT
      integer RECDAT(FILDAT), HRECS, MXMODS, NOMOD
      integer I,LENGTH,IN,IT,M,L,J,K
      character INPUT*80, MODS(1200)*1
      data MXMODS/1200/
      data NOCOM,COMVAR/18,6,15,6,6,5,5,6,6,4,9,6,10,6,6
     &,8,5,12,6,32*0/
      open (unit=AUDLUN(MACHNO),FILE='TYPE.DAT',status='OLD')
      if (.not.(TOPOPN(MACHNO))) rewind AUDLUN(MACHNO)
      NOMOD = 0
      CHUSED = 0
100   continue
      read (AUDLUN(MACHNO),5000,end=190,err=260) INPUT
5000  format (A)
      if (INPUT(1:1).eq.'*'.or.INPUT(1:1).eq.'C') go to 100
      NOMOD = NOMOD+1
      if (INPUT(1:1).eq.'K') then
         TD(NOMOD) = 5
      elseif (INPUT(1:1).eq.'I') then
         TD(NOMOD) = 4
      elseif (INPUT(1:1).eq.'R') then
         TD(NOMOD) = 3
      else
         write (PRINTR(MACHNO),5010) NOMOD
5010     format (/' Error while processing file TYPE--entry ',I3)
         write (PRINTR(MACHNO),5020) INPUT(1:1)
5020     format (' gives the data type as "',A1,'".')
         go to 280
      endif
      if (INPUT(3:3).eq.'0') then
         TS(NOMOD) = 1
      elseif (INPUT(3:3).eq.'1') then
         TS(NOMOD) = 2
      elseif (INPUT(3:3).eq.'2') then
         TS(NOMOD) = 3
      elseif (INPUT(3:3).eq.'3') then
         TS(NOMOD) = 4
      else
         write (PRINTR(MACHNO),5010) NOMOD
         write (PRINTR(MACHNO),5030) INPUT(3:3)
5030     format (' gives the storage type as "',A1,'".')
         go to 280
      endif
      MODLEN(NOMOD) = 0
      do 110 I = 5, 10
         if (INPUT(I:I).eq.' ') go to 120
         CHUSED = CHUSED+1
         if (CHUSED.gt.MXMODS) go to 270
         MODS(CHUSED) = INPUT(I:I)
         MODLEN(NOMOD) = MODLEN(NOMOD)+1
110   continue
120   continue
      TCL1(NOMOD) = 0
      TCL2(NOMOD) = 0
      TCL3(NOMOD) = 0
      if (TS(NOMOD).eq.1) go to 100
      STOPIT = 17
      do 130 I = 12, 17
         START = I
         if (INPUT(I:I).ne.' ') go to 140
130   continue
140   continue
      LENGTH = STOPIT-START+1
      call XVALUE(INPUT(START:START),LENGTH,VALUE,NOTNUM)
      if (.not.NOTNUM) then
         TCL1(NOMOD) = ifix(VALUE)
      elseif (INPUT(12:17).eq.'   NPX') then
         TCL1(NOMOD) = NPX
      elseif (INPUT(12:17).eq.' NCHEM') then
         TCL1(NOMOD) = NCHEM
      elseif (INPUT(12:17).eq.'  NCON') then
         TCL1(NOMOD) = NCON
      elseif (INPUT(12:17).eq.'MAXDAT') then
         TCL1(NOMOD) = MAXDAT
      elseif (INPUT(12:17).eq.'MAXMAS') then
         TCL1(NOMOD) = MAXMAS
      elseif (INPUT(12:17).eq.' NTRAN') then
         TCL1(NOMOD) = NTRAN
      else
         write (PRINTR(MACHNO),5010) NOMOD
         write (PRINTR(MACHNO),5040) INPUT(12:17)
         go to 280
5040     format (' gives the first dimension as "',A6,'".')
      endif
      if (TS(NOMOD).eq.2) go to 100
      STOPIT = 24
      do 150 I = 19, 24
         START = I
         if (INPUT(I:I).ne.' ') go to 160
150   continue
160   continue
      LENGTH = STOPIT-START+1
      call XVALUE(INPUT(START:START),LENGTH,VALUE,NOTNUM)
      if (.not.NOTNUM) then
         TCL2(NOMOD) = ifix(VALUE)
      elseif (INPUT(19:24).eq.'   NPX') then
         TCL2(NOMOD) = NPX
      elseif (INPUT(19:24).eq.' NCHEM') then
         TCL2(NOMOD) = NCHEM
      elseif (INPUT(19:24).eq.'  NCON') then
         TCL2(NOMOD) = NCON
      elseif (INPUT(19:24).eq.'MAXDAT') then
         TCL2(NOMOD) = MAXDAT
      elseif (INPUT(19:24).eq.'MAXMAS') then
         TCL2(NOMOD) = MAXMAS
      elseif (INPUT(19:24).eq.' NTRAN') then
         TCL2(NOMOD) = NTRAN
      else
         write (PRINTR(MACHNO),5010) NOMOD
         write (PRINTR(MACHNO),5050) INPUT(19:24)
5050     format (' gives the second dimension as "',A6,'".')
         go to 280
      endif
      if (TS(NOMOD).eq.3) go to 100
      STOPIT = 31
      do 170 I = 26, 31
         START = I
         if (INPUT(I:I).ne.' ') go to 180
170   continue
180   continue
      LENGTH = STOPIT-START+1
      call XVALUE(INPUT(START:START),LENGTH,VALUE,NOTNUM)
      if (.not.NOTNUM) then
         TCL3(NOMOD) = ifix(VALUE)
      elseif (INPUT(26:31).eq.'   NPX') then
         TCL3(NOMOD) = NPX
      elseif (INPUT(26:31).eq.' NCHEM') then
         TCL3(NOMOD) = NCHEM
      elseif (INPUT(26:31).eq.'  NCON') then
         TCL3(NOMOD) = NCON
      elseif (INPUT(26:31).eq.'MAXDAT') then
         TCL3(NOMOD) = MAXDAT
      elseif (INPUT(26:31).eq.'MAXMAS') then
         TCL3(NOMOD) = MAXMAS
      elseif (INPUT(26:31).eq.' NTRAN') then
         TCL3(NOMOD) = NTRAN
      else
         write (PRINTR(MACHNO),5010) NOMOD
         write (PRINTR(MACHNO),5060) INPUT(25:30)
5060     format (' gives the third dimension as "',A6,'".')
         go to 280
      endif
      go to 100
190   continue
      call MNLN (NOMOD,MODLEN,MODMIN,MODS,MXMODS)
      write (PRINTR(MACHNO),5070)
5070  format (///,'1',15X,'+**********************+'/16X,'|  EXAMS Versi
     &on 2.96  |'/16X,'|    BLDMOD Utility    |'/16X,'|      02-Nov-1988
     &     |'/16X,'+**********************+')
      write (PRINTR(MACHNO),5080) VARIEC,VARCEC,NOMOD,NOCOM
5080  format (//' Building the random access input variable',' definitio
     &n file'/' from a ',' sequential input file. Each of the input reco
     &rds is'/' 80 characters long and each of the output records has a'
     &/' record length of',I5,' INTEGER values or',I5,' CHARACTERs.'//' 
     &Number of user-specifiable parameters: ',I5,'.'/' (Enter as dimens
     &ion of MODLEN etc. in MODNAM.INC)'/' Number of Named Commons: ',I5
     &,'.'/' (Enter as dimension of COMVAR in MODNAM.INC)')
      IN = OFFSET
      IT = IN+1
      call RECPAK (IN,MODLEN,NOMOD)
      write (PRINTR(MACHNO),5090)
5090  format (//' Lengths of the names of the Model Parameters are')
      if (IT.ne.IN) then
         write (PRINTR(MACHNO),5100) IT,IN
5100     format (' stored in records ',I5,' through ',I5,'.')
      else
         write (PRINTR(MACHNO),5110) IT
5110     format (' stored in record ',I5,'.')
      endif
      IT = IN+1
      call RECPAK (IN,MODMIN,NOMOD)
      write (PRINTR(MACHNO),5120)
5120  format (//' Lengths required for unique identification are')
      if (IT.ne.IN) then
         write (PRINTR(MACHNO),5100) IT,IN
      else
         write (PRINTR(MACHNO),5110) IT
      endif
      IT = IN+1
      call RECPAK (IN,TS,NOMOD)
      write (PRINTR(MACHNO),5130)
5130  format (//' Storage mode of the Model Parameters')
      if (IT.ne.IN) then
         write (PRINTR(MACHNO),5100) IT,IN
      else
         write (PRINTR(MACHNO),5110) IT
      endif
      IT = IN+1
      call RECPAK (IN,TD,NOMOD)
      write (PRINTR(MACHNO),5140)
5140  format (//' Data type of the Model Parameters')
      if (IT.ne.IN) then
         write (PRINTR(MACHNO),5100) IT,IN
      else
         write (PRINTR(MACHNO),5110) IT
      endif
      IT = IN+1
      call RECPAK (IN,TCL1,NOMOD)
      write (PRINTR(MACHNO),5150)
5150  format (//,' Vector lengths of the Model Parameters')
      if (IT.ne.IN) then
         write (PRINTR(MACHNO),5100) IT,IN
      else
         write (PRINTR(MACHNO),5110) IT
      endif
      IT = IN+1
      call RECPAK (IN,TCL2,NOMOD)
      write (PRINTR(MACHNO),5160)
5160  format (//' Number of columns of the Model Parameters')
      if (IT.ne.IN) then
         write (PRINTR(MACHNO),5100) IT,IN
      else
         write (PRINTR(MACHNO),5110) IT
      endif
      IT = IN+1
      call RECPAK (IN,TCL3,NOMOD)
      write (PRINTR(MACHNO),5170)
5170  format (//' Number of tables of the Model Parameters')
      if (IT.ne.IN) then
         write (PRINTR(MACHNO),5100) IT,IN
      else
         write (PRINTR(MACHNO),5110) IT
      endif
      IT = IN+1
      call RECPAK (IN,COMVAR,NOCOM)
      write (PRINTR(MACHNO),5180)
5180  format (//' Number of labeled COMMONs in the Parameters dataset')
      if (IT.ne.IN) then
         write (PRINTR(MACHNO),5100) IT,IN
      else
         write (PRINTR(MACHNO),5110) IT
      endif
      IT = IN+1
      call PKCREC (IN,MODS,CHUSED)
      write (PRINTR(MACHNO),5190)
5190  format (//' Model Parameter names are')
      if (IT.ne.IN) then
         write (PRINTR(MACHNO),5100) IT,IN
      else
         write (PRINTR(MACHNO),5110) IT
      endif
      write (PRINTR(MACHNO),5200) CHUSED
5200  format (//' Number of characters used for Parameter Names: ',I5/' 
     &(Enter as dimension of MODS in MODNAM.INC).')
      call DMPTAB (NOMOD,MODLEN,MODS,MXMODS)
      HRECS = FILDAT/VARIEC
      if (FILDAT-VARIEC*HRECS.ne.0) HRECS = HRECS+1
      M = 0
      do 210 I = 1, HRECS
         read (RANUNT(MACHNO),rec=I) IBUFF
         do 200 L = 1, VARIEC
            M = M+1
            if (M.gt.FILDAT) go to 220
            RECDAT(M) = IBUFF(L)
200      continue
210   continue
220   continue
      RECDAT(5) = OFFSET+1
      RECDAT(6) = IN-OFFSET
      do 250 I = 1, HRECS
         J = (I-1)*VARIEC
         K = J+VARIEC
         J = J+1
         M = 0
         do 230 L = J, K
            M = M+1
            if (L.gt.FILDAT) go to 240
            IBUFF(M) = RECDAT(L)
230      continue
240      continue
         write (RANUNT(MACHNO),rec=I) IBUFF
250   continue
      OFFSET = IN
      go to 280
260   continue
      write (PRINTR(MACHNO),5210)
5210  format (/' Error while building the modify table.')
      stop
270   continue
      write (PRINTR(MACHNO),5220) MXMODS
5220  format (/' MODS array full: ',I4)
      stop
280   continue
      close (unit=AUDLUN(MACHNO))
      return
      end
