    program run_aermet
    
!   read existing stage input file, write new one with directories and run aermet for requested stage
    implicit none
    integer(kind=4):: istage,col,row,istat,iflag,eof,eof1,eof2,i,ii,ilen,col1,row1,idash(3),k
    character(len=1) :: astage
    character(len=4) :: domain
    character(len=3) :: acol,arow,ablock,ablockuse
    character(len=100) :: line1
    character(len=130) :: rptline
    character(len=200) :: infile1,infile2,fname,rptfile,junkfile,runline
    character(len=8) :: ftypes(8)
    character(len=10) :: pathid(6)
    character(len=30) :: lsline
    character(len=20) junkline
    logical lbad,lexist,lgood,lfound,lfound1
    
    data ftypes /'MESSAGES','REPORT','DATA','EXTRACT','QAOUT','AERSURF','OUTPUT','PROFILE'/
    data pathid /'JOB','UPPERAIR','SURFACE','ONSITE','METPREP','MERGE'/
    istage=0
    col=0
    row=0
    lbad=.false.
    domain=''
    
    
!   stage
    call getarg(1,astage,istat)
    read(astage,*,iostat=iflag)istage
    if (iflag /=0 .or. istage == 0 .or. istage > 3) then
        write(*,*)'Bad stage, stopping program'
        lbad=.true.
    endif
    
!   domain
    call getarg(2,domain,istat)
    if (domain /= '9AK' .and. domain /='3PR' .and. domain /='3HI') then
        write(*,*)'Bad domain,stopping program'
        lbad=.true.
    endif
    
!   column
    call getarg(3,acol,istat)
    read(acol,*,iostat=iflag)col
    if (iflag /= 0 .or. col < 1) then
        write(*,*)'bad column, stopping program'
        lbad=.true.
    endif
    
!   row
    call getarg(4,arow,istat)
    read(arow,*,iostat=iflag)row
    if (iflag /= 0 .or. row < 1) then
        write(*,*)'bad row, stopping program'
        lbad=.true.
    endif
    if (lbad) stop
    
!   check to see if the requested column  and row are in the cell list
    write(lsline,'(a,1x,a3,1x,a)')'ls',domain,'> junk1.txt'
    call system(lsline)
    open(unit=11,file='junk1.txt',status='old')
    eof1=0
    lfound=.false.
    do while(eof1 == 0 .and. .not. lfound)
        read(11,'(a3)',iostat=eof1)ablock
        write(lsline,'(a2,1x,a3,a1,a3,1x,a)')'ls',domain,'/',ablock,'> junk.txt'
        call system(lsline)
        eof2=0
        open(unit=12,file='junk.txt',status='old')
        read(12,'(a20)',iostat=eof2)junkline
        do while(eof2 ==0 .and. .not. lfound)
            if (eof2 == 0) then
                idash=0
                k=0
    l1:         do i=1,len_trim(junkline)
                    if (junkline(i:i)=='_') then
                        k=k+1
                        idash(k)=i
                    endif
                enddo l1
!               get column and row
                read(junkline(idash(2)+1:idash(3)-1),*)col1
                read(junkline(idash(3)+1:len_trim(junkline)),*)row1
                if (col1 == col .and. row1 == row) then
                    lfound=.true.
                    ablockuse=ablock
                endif
            endif
        enddo
        close(12,status='delete')
    enddo
    close(11,status='delete')
    
    if (.not. lfound) then
        write(*,'(2(a,i5),a)')'column ',col,' and row ',row,' not found'
        stop
    endif
    
!   text file to write screen output when running AERMET, deleted at end of program
    write(junkfile,'(a,i1,5(a))')'log_',istage,'_',trim(adjustl(acol)),'_',trim(adjustl(arow)),'.txt'
    open(unit=15,file=junkfile,status='unknown')
!   read original input file and write new temporary input file
    write(infile1,'(14(a),i1)')trim(adjustl(domain)),'/',ablockuse,'/MMIF_',trim(adjustl(domain)),'_',trim(adjustl(acol)),'_',&
        trim(adjustl(arow)),'/MMIF_AERMET_2018_',trim(adjustl(acol)),'_',trim(adjustl(arow)),'.IN',istage
    
    write(infile2,'(5(a),i1)')'MMIF_AERMET_2018_',trim(adjustl(acol)),'_',trim(adjustl(arow)),'.IN',istage
    
    open(unit=12,file=infile1,status='old')
    open(unit=13,file=infile2,status='unknown')
    
    eof=0
    do while(eof == 0) 
        read(12,'(a100)',iostat=eof)line1
        if (eof == 0) then 
            i=1
            lfound=.false.
            do while (i <= 8 .and. .not. lfound) !check for an input/output file
                if (index(line1,ftypes(i)) > 0)then
                    lfound=.true.
                    ii=index(line1,ftypes(i))
                else
                    i=i+1
                endif
            enddo
            if (lfound) then !put in relative directory path
                
                ilen=len_trim(ftypes(i))
                fname=line1(ii+ilen:100)
                write(13,'(4x,a,1x,12(a))')trim(adjustl(ftypes(i))),'./',trim(adjustl(domain)),'/',ablockuse,'/MMIF_',&
                    trim(adjustl(domain)),'_',trim(adjustl(acol)),'_',trim(adjustl(arow)),'/',trim(adjustl(fname))
                if (i==2) write(rptfile,'(12(a))')'./',trim(adjustl(domain)),'/',ablockuse,'/MMIF_',trim(adjustl(domain)),'_',&
                    trim(adjustl(acol)),'_',trim(adjustl(arow)),'/',trim(adjustl(fname))
            else
                i=1
                lfound1=.false.
                do while (i <= 6 .and. .not. lfound1) !check to see if a path ID
                    if (index(line1,pathid(i)) > 0) lfound1=.true.
                    i=i+1
                enddo
                if (lfound1) then
                    write(13,'(a)')trim(adjustl(line1)) !path id start at column 1
                else
!                   check location card for west longitude and negative GMT to LST, if so, make GMT positive
                    if (index(line1,'LOCATION') > 0 .and. index(line1,'W') > 0 .and. index(line1,'-') > 0) &
                        line1(index(line1,'-'):index(line1,'-'))=''
                    write(13,'(4x,a)')trim(adjustl(line1)) !not path id , start at column 5
                endif
            endif
        endif
    enddo
    close(12)
    close(13)
    
!   run AERMET
    write(runline,'(2(a))')'/home/jthurman/aermet_21112/aermet ',trim(adjustl(infile2))
    call system(runline)
    
!   now check the report file for successful run
    inquire(file=rptfile,exist=lexist)
    open(unit=14,file=rptfile,status='old')
    eof=0
    lgood=.false.
    
    do while(eof == 0 .and. .not. lgood .and. lexist)
        read(14,'(a130)',iostat=eof)rptline
        if (index(rptline,'AERMET Data Processing Finished Successfully')>0) lgood=.true.
    enddo
    close(14)
    if (lgood) then
        write(15,'(a,i1,a,2(1x,i3))')'Successful stage ',istage,' for',col,row
        open(13,file=infile2,status='old')
        close(13,status='delete')
    else
        write(15,'(a,i1,a,2(1x,i3))')'Un-successful stage ',istage,' for',col,row
    endif
    close(15) 
    end
    
        
    
        
    
    
