      program split
      implicit none
      character inpfil*100,outtag*20,outfil*100,line*150
      integer eof,i,j,k
      real m
      logical lexist
      
      allocatable :: outfil(:)
      
      call getarg(1,inpfil)
      inquire(file=inpfil,exist=lexist)
      if (.not. lexist) then
          write(*,*)'file does not exist'
          stop
      endif
      call getarg(2,outtag)
      i=0
      open(unit=11,file=inpfil,status='old')
      read(11,5,iostat=eof)line
 10   if (eof .eq. 0) then
          i=i+1
          read(11,5,iostat=eof)line
          goto 10
      endif
      
      j=i/1000
      m=mod(real(i),1000.0)
      if (m .ne. 0) j=j+1
      
      allocate(outfil(j))
      do k=1,j
          write(outfil(k),'(2(a),i3.3,a4)')trim(adjustl(outtag)),'_',k,
     +    '.inp'
          write(*,*)outfil(k)
          open(unit=100+k,file=outfil(k),status='replace')
      enddo
      
      rewind(11)
      eof=0
      i=0
      k=1
      read(11,5,iostat=eof)line
 15   if (eof .eq. 0) then
          if (i .eq. 1000) then
              k=k+1
              i=0
          endif
          i=i+1
          write(100+k,5)line
          read(11,5,iostat=eof)line
          goto 15
      endif
      close(11)
      do k=1,j
          close(100+k)
      enddo
      deallocate(outfil)
      
 5    format(a150)
      end
          
              
          
      
