      program splitfiles
      implicit none
      integer nsrc,nsrc1,nruns,irun,eof,isrc,isrc1,isrc2,n
      character srcid*12,facid*12,aruns*3,infile*250,infile1*250,
     +line*200,src*12,j1*2,j2*8,sgrp*8
      logical lexist,lfound,lkeep
      
      allocatable :: srcid(:)
      
      call getarg(1,facid)
      call getarg(2,aruns)
      
      read(aruns,*)nruns
      
      write(infile,'(3(a))')'aermod_',trim(adjustl(facid)),'.inp'
      inquire(file=infile,exist=lexist)
      if (.not. lexist) then
          write(*,'(2(a))')trim(adjustl(infile)),'does not exist'
          stop
      endif
      open(unit=11,file=infile,status='old')
      eof=0
      isrc=0
      read(11,5,iostat=eof)line
 10   if (eof .eq. 0) then
          if (index(line,'SO LOCATION') .gt. 0) isrc=isrc+1
          read(11,5,iostat=eof)line
          goto 10
      endif
      rewind(11)
      nsrc=isrc
      allocate(srcid(nsrc))
      
c     get source IDS
      eof=0
      isrc=0
      read(11,5,iostat=eof)line
 15   if (eof .eq. 0) then
          if (index(line,'SO LOCATION') .gt. 0) then
              isrc=isrc+1
              srcid(isrc)=line(13:24)
          endif
          read(11,5,iostat=eof)line
          goto 15
      endif
      rewind(11)      
      
      
c     break up 
      nsrc1=nsrc/nruns
      if (mod(real(nsrc),real(nruns)) .ne. 0.0) nsrc1=nsrc1+1
      
c     write(*,*)nsrc1
     
c     write new input file
      isrc1=0
      isrc2=0
      
c      do irun=1,nruns
c          isrc1=isrc2+1
c          isrc2=isrc1+nsrc1-1
c          write(*,*)irun,isrc1,isrc2
c          pause
c      enddo
   
      do irun=1,nruns
          lkeep=.false.
          isrc1=isrc2+1
          isrc2=isrc1+nsrc1-1
          eof=0
          write(infile1,'(3(a),i2.2,a)')'aermod_',trim(adjustl(facid)),
     +    '_',irun,'.inp'
          open(unit=12,file=infile1,status='replace')
          read(11,5,iostat=eof)line
 20       if (eof .eq. 0) then
              if (index(line,'SO LOCATION') .gt. 0 .or. 
     +        index(line,'SO SRCPARAM') .gt. 0 .or. 
     +        index(line,'SO EMISFACT') .gt. 0 .or. 
     +        index(line,'SO SRCGROUP') .gt. 0 .or. 
     +        index(line,'OU PLOTFILE') .gt. 0) then
                  if (index(line,'OU PLOTFILE') .gt. 0) then
                      src=trim(adjustl(line(20:27)))
                  elseif (index(line,'SO SRCGROUP') .gt. 0) then
                      read(line,*)j1,j2,sgrp,src
                  else
                      src=trim(adjustl(line(13:24)))
                  endif
                  isrc=1
                  lfound=.false.
                  do while(isrc .le. nsrc .and. .not. lfound)
                      if (trim(adjustl(src)) .eq. 
     +                trim(adjustl(srcid(isrc)))) then
                          lfound=.true.
                      else
                          isrc=isrc+1
                      endif
                  enddo
                  if (isrc .ge. isrc1 .and. isrc .le. isrc2) then
                      if (index(line,'OU PLOTFILE') .gt. 0) then
                          lkeep=.true.
                          n=index(line,'.plt')
                          write(12,'(2(a),i2.2,a)')line(1:n-1),'_',irun,
     +                    '.plt 35'
                      else
                          write(12,5)line
                      endif
                  else
                      line(1:2)='**'
                      write(12,5)line
                  endif
              elseif (index(line,'CO ERRORFIL') .gt. 0) then
                  n=index(line,'.out')
                  write(12,'(2(a),i2.2,a)')line(1:n-1),'_',irun,'.out'
              else
                  write(12,5)line
              endif
              read(11,5,iostat=eof)line
              goto 20
          endif
          if (lkeep) then ! this file has some source(s)
            close(12)
          else !no sources, this file goes past the cut-off
            close(12,status='delete')
          endif
          rewind(11)
      enddo
      close(11)
      write(*,'(2(a))')'Done with ',trim(adjustl(facid))
 5    format(a200)
      end
      
                  
      
      
      
      
      
