program f90_fig_gfrmwp
    implicit none
    real(8),parameter::pi=3.14159265358979323846D0
    integer::i,j,ne,kk
    character::strcom*50                !comment
    integer::NODT                       !Number of nodes
    integer::NELT                       !Number of elements
    integer::MATEL                      !Number of material sets
    integer::KOX                        !Number of nodes with given displacement in x-direction
    integer::KOY                        !Number of nodes with given displacement in y-direction
    integer::KOZ                        !Number of nodes with given rotation
    integer::NF                         !Number of nodes with given loads
    integer,allocatable::kakom(:,:)     !Element conectivity
    real(8),allocatable::x(:)           !x-coordinate of node
    real(8),allocatable::y(:)           !y-coordinate of node
    integer,allocatable::nokx(:)        !Node number with given displacement in x-direction
    integer,allocatable::noky(:)        !Node number with given displacement in y-direction
    integer,allocatable::nokz(:)        !Node number with given rotation
    real(8),allocatable::fx(:)          !External force increment in x-direction
    real(8),allocatable::fy(:)          !External force increment in y-direction
    real(8),allocatable::fz(:)          !External moment increment
    real(8),allocatable::ff(:)          !External force vector
    real(8),allocatable::dist(:)        !Node displacement vector
    integer::nt                         !Total degrees of freedom (NNODT x nhen)
    integer::nod=2                      !Number of nodes per element
    integer::nhen=3                     !Degree of freedom per node
    character::fnameR*50,fnameW*50      !Input data file, Output data file
    integer::nlmax,itmax,nload
    integer::itera         !Itereation number
    real(8)::lam           !lambda
    real(8)::deltaS        !deltaS
    integer::kku,kkv
    real(8)::Tpress        !Total water pressure

    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

    !Input from command line
    call getarg(1,fnameR) !Input file name
    call getarg(2,fnameW) !Output file name
    call getarg(3,dummy)  !string input
    read(dummy,*) kku     !Conversion from string to double
    call getarg(4,dummy)  !string input
    read(dummy,*) kkv     !Conversion from string to double

    !***************************
    !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')
    write(12,'(a)') '0,0,0'
    do nload=1,nlmax
        read(11,'(a)') linebuf
        if(linebuf(1:1)/='*')exit
        read(11,'()')
        read(11,*) iw,itera,Tpress,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,*) Tpress,',',dist(3*kku-2),',',dist(3*kkv-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_gfrmwp
