program f90_fig_gfrm
    implicit none
    real(8),parameter::pi=3.14159265358979323846D0
    integer::i,j,ne,kk
    character::strcom*50                !
    integer::NODT                       !
    integer::NELT                       !
    integer::MATEL                      !
    integer::KOX                        !
    integer::KOY                        !
    integer::KOZ                        !
    integer::NF                         !
    integer,allocatable::kakom(:,:)     !
    real(8),allocatable::x(:)           !
    real(8),allocatable::y(:)           !
    integer,allocatable::nokx(:)        !
    integer,allocatable::noky(:)        !
    integer,allocatable::nokz(:)        !
    real(8),allocatable::fx(:)           !
    real(8),allocatable::fy(:)           !
    real(8),allocatable::fz(:)           !
    real(8),allocatable::ff(:)         !
    real(8),allocatable::dist(:)      !
    integer::nt                         !
    integer::nod=2                      !
    integer::nhen=3                     !
    character::fnameR*50,fnameW*50      !
    integer::nlmax,itmax,nload
    integer::itera
    real(8)::lam
    real(8)::deltaS
    integer::kkf,kki,kku

    character :: linebuf*1000,dummy*100,filename*100
    integer::iw,k1,k2,k3,n1,n2,n3
    real(8)::rw
    real(8)::dismax,coef,ddx,ddy,xmin,xmax,ymin,ymax,ddh,fmax,xx,yy

    call getarg(1,fnameR)
    call getarg(2,fnameW)
    call getarg(3,dummy)
    read(dummy,*) kkf
    call getarg(4,dummy)
    read(dummy,*) kki
    call getarg(5,dummy)
    read(dummy,*) kku

    !***************************
    !data input
    !***************************
    open(11,file=fnameR,status='old')
    read(11,'(a)') strcom
    read(11,'()')
    read(11,*) NODT,NELT,MATEL,KOX,KOY,KOZ,NF,nlmax,itmax
    nt=NODT*nhen
    allocate(kakom(1:NELT,1:nod))
    allocate(x(1:NODT))
    allocate(y(1:NODT))
    if(1<=KOX)allocate(nokx(1:KOX))
    if(1<=KOY)allocate(noky(1:KOY))
    if(1<=KOZ)allocate(nokz(1:KOZ))
    allocate(fx(1:NODT))
    allocate(fy(1:NODT))
    allocate(fz(1:NODT))
    allocate(ff(1:nt))
    allocate(dist(1:nt))
    read(11,'()')
    read(11,'()')
    n1=0
    n2=0
    n3=0
    do i=1,NODT
        read(11,*) iw,x(i),y(i),fx(i),fy(i),fz(i),k1,k2,k3
        if(0<KOX)then
            if(k1==1)then
                n1=n1+1
                nokx(n1)=i
            end if
        end if
        if(0<KOY)then
            if(k2==1)then
                n2=n2+1
                noky(n2)=i
            end if
        end if
        if(0<KOZ)then
            if(k3==1)then
                n3=n3+1
                nokz(n3)=i
            end if
        end if
    end do
    read(11,'()')
    read(11,'()')
    do ne=1,NELT
        read(11,*) iw,(kakom(ne,j),j=1,nod),rw,rw,rw,iw
    end do

    kk=11
    open(12, file=fnameW, status='replace')
    do nload=1,nlmax
        read(11,'(a)') linebuf
        if(linebuf(1:1)/='*')exit
        read(11,'()')
        read(11,*) iw,itera,lam,deltaS,rw
        if(itmax<itera)exit
        read(11,'()')
        read(11,'()')
        do i=1,NODT
            read(11,*) iw,rw,rw,dist(3*i-2),dist(3*i-1),dist(3*i),ff(3*i-2),ff(3*i-1),ff(3*i),rw,rw,rw
        end do
        read(11,'()')
        read(11,'()')
        do ne=1,NELT
            read(11,'()')
        end do
        write(linebuf,*) ff(3*(kkf-1)+kki),',',dist(3*kku-2),',',dist(3*kku-1),',',itera,',',lam,',',deltaS
        call del_spaces(linebuf)
        write(12,'(a)') trim(linebuf)
    end do
    close(12)
    close(11)

stop

contains


    subroutine del_spaces(s)
        character (*), intent (inout) :: s
        character (len=len(s)) tmp
        integer i, j
        j = 1
        do i = 1, len(s)
            if (s(i:i)==' ') cycle
            tmp(j:j) = s(i:i)
            j = j + 1
        end do
        s = tmp(1:j-1)
    end subroutine del_spaces


end program f90_fig_gfrm
