program f90_gmt_mesh
    implicit none
    integer::NODT
    integer::NELT
    integer::nod
    integer,allocatable::kakom(:,:)
    real(4),allocatable::x(:),y(:)
    character(len=50)::fnameR
    character(len=50)::fnameW
    character(len=10)::dum
    integer::i,j,k1,k2,k3,k4
    real(4)::xg,yg

    call getarg(1,dum)    ;read(dum,*) nod
    call getarg(2,fnameR)
    call getarg(3,fnameW)

    open(11,file=fnameR,status='old')
    read(11,*) NODT,NELT
    allocate(kakom(1:NELT,1:nod))
    allocate(x(1:NODT),y(1:NODT))
    do i = 1,NELT
        read(11,*) (kakom(i,j),j=1,nod)
    end do
    do i = 1,NODT
        read(11,*) x(i),y(i)
    end do
    close(11)

    open(12, file=fnameW, status='replace')
    do i=1,NELT
        write(12,'(a)') '>'
        do j = 1,nod
            write(12,*) x(kakom(i,j)),y(kakom(i,j))
        end do
    end do
    close(12)

    fnameW='_inp_node.txt'
    open(12, file=fnameW, status='replace')
    do i=1,NODT
        write(dum,'(i5)') i
        write(12,*) x(i),y(i),'7p,blue 0 TR ',trim(adjustl(dum))
    end do
    close(12)

    fnameW='_inp_nelt.txt'
    open(12, file=fnameW, status='replace')
    do i=1,NELT
        write(dum,'(i5)') i
        k1=kakom(i,1)
        k2=kakom(i,2)
        k3=kakom(i,3)
        if(nod==3)then
        xg=(x(k1)+x(k2)+x(k3))/3.0
        yg=(y(k1)+y(k2)+y(k3))/3.0
        end if
        if(nod==4)then
            k4=kakom(i,4)
            xg=(x(k1)+x(k2)+x(k3)+x(k4))/4.0
            yg=(y(k1)+y(k2)+y(k3)+y(k4))/4.0
        end if
        write(12,*) xg,yg,'6p,red 0 MC ',trim(adjustl(dum))
    end do
    close(12)

stop

end program f90_gmt_mesh
