      program mkgrid
*
      implicit real*8 (a-h,l,m,o-z)
      parameter (nzd=7, nmd=25, ntfgb=70)
      integer cmax, czdat(3), cmdat(3), nmstd(nzd)
      real*8 zdat(nzd), mdat(nmd)
      character zfile(nzd)*5, grtype(3)*3, line*160
      logical thism
      data czdat /36, 57, 92/
      data cmdat /50, 87, 157/
      data grtype /'OVS', 'STD', 'HYB'/
      data zfile /'0001', '0003', '001', '004', '01', '02', '03'/
      data zdat /0.0001, 0.0003, 0.001, 0.004, 0.01, 0.02, 0.03/
      data nmstd /11, 11, 11, 11, 24, 24, 24/
      data mdat /0.5, 0.64, 0.8, 0.9, 1.0, 1.1, 1.25, 1.4, 1.6, 1.8,
     &     2.0, 2.5, 3.2, 4.0, 5.0, 6.35, 8.0, 10.0, 12.7, 16.0,
     &     20.0, 25.0, 32.0, 40.0, 50.0/
*
      write(*,'(a,$)') 'Type of grid: (1) OVS  (2) STD  (3) HYBrid : '
      read(*,*) ig
      write(*,'(a,$)') 'Zmin, Zmax (0.0001 - 0.03) : '
      read(*,*) z1, z2
      iz1 = indx(z1,zdat,nzd)
      if(z1.lt.zdat(iz1)) iz1 = max(1, iz1 - 1)
      iz2 = indx(z2,zdat,nzd)
      nz = iz2 - iz1 + 1
      write(*,'(a,$)') 'Mmin, Mmax (0.5 - 50) : '
      read(*,*) m1, m2
      im1 = indx(m1,mdat,nmd)
      if(m1.lt.mdat(im1)) im1 = max(1, im1 - 1)
      im2 = indx(m2,mdat,nmd)
      if(ig.eq.2 .and. im2.gt.nmstd(iz1)) then
         im2 = min(im2, nmstd(iz1))
         write(*,'(a,f6.2)') 'N.B. taking Mmax =', mdat(im2)
      end if
      nm = im2 - im1 + 1
      write(*,'(2a,$)') 'Grid details: (1) global  (2) incl. structure',
     &     '  (3) incl. composition : '
      read(*,*) ic
*
      cmax = czdat(ic)
      open(4,file='ZAMSGRID')
      write(4,'(a,i5,a)') ' ZAMS models',nz,' metallicities'
      do iz = iz1, iz2
         open(1,file='ZAMS'//zfile(iz),status='old')
         do i = 1, 3
            read(1,'(a)') line(1:92)
            write(4,'(a)') line(1:cmax)
         end do
         read(1,1001) z, nx
         write(4,1001) z, nx
 1001    format (/,' Z =',f8.4,i5,' models',/)
         do i = 1, nx
            read(1,'(a)') line(1:92)
            write(4,'(a)') line(1:cmax)
         end do
         close(1)
      end do
      close(4)
*
      cmax = cmdat(ic)
      open(4,file='EVOLGRID')
      write(4,'(1x,2a,i5,a)') grtype(ig),' models ',nz,' metallicities'
      igr = 1 + mod(ig-1,2)
      do iz = iz1, iz2
         open(1,file=grtype(igr)//zfile(iz),status='old')
         do i = 1, 3
            read(1,'(a)') line(1:157)
            write(4,'(a)') line(1:5)//line(11:cmax)
         end do
         read(1,2001) z, nf
         write(4,2001) z, nm
 2001    format (/,' Z =',f8.4,i5,' masses')
         k1 = 1
         if(ig.eq.3) then
            open(2,file=grtype(2)//zfile(iz),status='old')
            do i = 1, 3
               read(2,'(a)') line(1:157)
            end do
            read(2,2001) z, nf2
            do k = 1, 8
               k1 = k1 + 1
               thism = (k.ge.im1 .and. k.le.im2)
               read(1,2002) m, nx
               read(2,2002) ms, nxs
               if(nxs.le.ntfgb) then
                  nw = nxs
               else
                  nw = nx
               end if
               if(thism) write(4,2002) ms, nw
*  read models from STD grid and write up to He flash
 1             read(2,'(a)') line(1:157)
               read(line,'(i3)') im
               if(thism .and. im.le.ntfgb) then 
                  write(4,'(a)') line(1:5)//line(11:cmax)
                  if(im.eq.ntfgb) read(line,'(10x,1p,e16.9)') ajhef
               end if
               if(im.lt.nxs) goto 1
*  read models from OVS grid and write post He-flash, corrected for age
 2             read(1,'(a)') line(1:157)
               read(line,'(i3)') im
               if(thism) then
                  if(im.eq.ntfgb) then
                     read(line,'(10x,1p,e16.9)') aj
                     dajhef = ajhef - aj
                  else if(im.gt.ntfgb) then 
                     read(line,'(10x,1p,e16.9)') aj
                     aj = aj + dajhef
                     write(line(11:26),'(1p,e16.9)') aj
                     write(4,'(a)') line(1:5)//line(11:cmax)
                  end if
               end if
               if(im.lt.nx) goto 2
            end do
            close(2)
         end if
         do k = k1, nf
            thism = (k.ge.im1 .and. k.le.im2)
            read(1,2002) m, nx
            if(thism) write(4,2002) m, nx
 2002       format (/,' M =',f8.4,i5,' models',/)
 3          read(1,'(a)') line(1:157)
            if(thism) write(4,'(a)') line(1:5)//line(11:cmax)
            read(line,'(i3)') im
            if(im.lt.nx) goto 3
         end do
         close(1)
      end do
      close(4)
      end
