    program batch_files
!   create the batch files needed for running AERMET on atmos
    implicit none
    integer(kind=4):: istage,col,row,istat,eof,i,k,nblocks,nfiles,iblock,ifile,icells,idash(3)
    character(len=3) :: domain
    character(len=3) :: anum
    character(len=100), allocatable,dimension(:,:) :: outfil
    character(len=30) lsline
    character(len=20) junkline
    logical lbad
    
!   domain
    call getarg(1,domain,istat)
    call getarg(2,anum,istat)
    read(anum,*)nblocks
    if (domain /= '9AK' .and. domain /='3PR' .and. domain /='3HI') then
        write(*,*)'Bad domain,stopping program'
        lbad=.true.
    endif
    
!   number of job files
    nfiles=int(nblocks/8)
    if (mod(nblocks,8) /= 0) nfiles=nfiles+1
    allocate(outfil(3,nfiles))
    l1: do ifile=1,nfiles
    s1:     do istage=1,3
                write(outfil(istage,ifile),'(a,a3,a,i1,a1,i2.2,a)')'run_mmif_',domain,'_stage_',istage,'_',ifile,'.job'
                open(unit=ifile*100+istage,file=outfil(istage,ifile),status='replace')
!               write job header
                write(ifile*100+istage,'(a)')'!/bin/bash -l'
                write(ifile*100+istage,'(a)')'#SBATCH --gid=romo'
                write(ifile*100+istage,'(a)')'#SBATCH -p singlepe'
                write(ifile*100+istage,'(a)')'#SBATCH -t 24:00:00'
                write(ifile*100+istage,'(a)')'#SBATCH --ntasks 1'
                write(ifile*100+istage,'(a,a3,a1,i1,a1,i2.2,a)')'#SBATCH -o ',domain,'_',istage,'_',ifile,'.txt'
                write(ifile*100+istage,'(a,a3,a1,i1,a1,i2.2,a)')'#SBATCH -e ',domain,'_',istage,'_',ifile,'_err.txt'
                write(ifile*100+istage,'(a)')'#SBATCH --mail-type=BEGIN,END'
                write(ifile*100+istage,'(a)')'#SBATCH --mail-user=jthurman'
                write(ifile*100+istage,'(a)')'echo starting'
                write(ifile*100+istage,'(a)')'cd /work/ROMO/2018_MP_AERMOD/mmif_output/'
            enddo s1
    enddo l1
    
    icells=0
    l2: do iblock=1,nblocks
!       get cells in the block
        write(lsline,'(a2,1x,a3,a1,i3.3,1x,a)')'ls',domain,'/',iblock,'> junk.txt'
        call system(lsline)
        open(unit=11,file='junk.txt',status='old')
        do while (eof==0)
            read(11,'(a20)',iostat=eof)junkline
            if (eof == 0) then
                idash=0
                k=0
    l3:         do i=1,len_trim(junkline)
                    if (junkline(i:i)=='_') then
                        k=k+1
                        idash(k)=i
                    endif
                enddo l3
!               get column and row
                read(junkline(idash(2)+1:idash(3)-1),*)col
                read(junkline(idash(3)+1:len_trim(junkline)),*)row
                icells=icells+1
                ifile=int(icells/8000)
                if (mod(icells,8000) /= 0) ifile=ifile+1
    s2:         do istage=1,3
                    write(ifile*100+istage,'(a,1x,i1,1x,a3,2(1x,i3))')'/home/jthurman/mp_2018_programs/run_aermet_nonconus',&
                        istage,domain,col,row
                enddo s2
            endif
        enddo
        close(11,status='delete')
    enddo l2
    
!   write end of job file and close
    l4: do ifile=1,nfiles
    s3: do istage=1,3
            write(ifile*100+istage,'(a)')'exit'
            close(ifile*100+istage)
        enddo s3
    enddo l4
    deallocate(outfil)
    end
    
