program f90_julia2_gmt
    implicit none
    integer,parameter::nmax=10000
    integer::ih,jw
    real(8)::x0,y0,rr
    real(8)::sr,si
    real(8)::a,b
    real(8)::xx,yy
    integer::i,j
    integer::col
    character(len=50)::dummy
    character(len=50)::fnameF
    character(len=20)::fout='_out.txt'

    call getarg(1,dummy) ; read(dummy,*) jw
    call getarg(2,dummy) ; read(dummy,*) ih
    call getarg(3,dummy) ; read(dummy,*) x0
    call getarg(4,dummy) ; read(dummy,*) y0
    call getarg(5,dummy) ; read(dummy,*) rr
    call getarg(6,dummy) ; read(dummy,*) sr
    call getarg(7,dummy) ; read(dummy,*) si
    call getarg(8,dummy) ; read(dummy,*) a
    call getarg(9,dummy) ; read(dummy,*) b
    call getarg(10,fnameF)

    write(6,'(a)') '* Job for "'//trim(adjustl(fnameF))//'" was started.'

    open(12,file=fout,status='replace')
    do i=0,ih
        write(12,'(a)') '>'
        yy=y0+rr-2.0D0*rr/dble(ih)*dble(i)
        do j=0,jw
            xx=x0-rr+2.0D0*rr/dble(jw)*dble(j)
            col=conv(xx,yy,sr,si,a,b,nmax)
            if(nmax<col)then
                col=99
            else
                col=mod(col,12)
            end if
            write(12,'(2(e15.7),i10)') xx,yy,col
        end do
    end do
    write(12,'()')
    close(12)

    write(6,'(a)') '** Calculation was completed.'
    call GMTSH(x0,y0,rr,fnameF,fout,jw,ih)
    call system('bash _gmt.sh')
    write(6,'(a)') '*** All works were completed.'

stop

contains

    integer function conv(xx,yy,sr,si,a,b,nmax)
        real(8),intent(in)::xx,yy
        real(8),intent(in)::sr,si,a,b
        integer,intent(in)::nmax
        complex(8)::z1,z2,zp,cc
        integer::i
        real(8),parameter::abslim=2.0D0

        conv=nmax+1
        zp=cmplx(sr,si)
        cc=cmplx(a,b)
        z1=cmplx(xx,yy)
        do i=1,nmax
            z2=z1**zp+cc
            if(abs(z2)>abslim)exit
            z1=z2
        end do
        if(abs(z2)>abslim)conv=i
    end function conv

    subroutine GMTSH(x0,y0,rr,fnameF,fout,jw,ih)
        real(8),intent(in)::x0,y0,rr
        character(len=50),intent(in)::fnameF
        character(len=20),intent(in)::fout
        integer,intent(in)::jw,ih
        character(len=50)::dummy1,dummy2,dummy3,dummy4
        character(len=50)::fnameW='_gmt.sh'
        character(len=200)::strdat

        write(dummy1,'(e15.7)') x0-rr
        write(dummy2,'(e15.7)') x0+rr
        write(dummy3,'(e15.7)') y0-rr
        write(dummy4,'(e15.7)') y0+rr
        strdat=trim(adjustl(dummy1))//'/'//trim(adjustl(dummy2))
        strdat=trim(adjustl(strdat))//'/'//trim(adjustl(dummy3))//'/'//trim(adjustl(dummy4))
        write(dummy1,'(e15.7)') dble(jw)*0.01D0
        write(dummy2,'(e15.7)') dble(ih)*0.01D0

        open(12,file=fnameW,status='replace')
        write(12,'(a)') 'range='//trim(adjustl(strdat))
        write(12,'(a)') 'scale='//trim(adjustl(dummy1))//'/'//trim(adjustl(dummy2))
        write(12,'(a)') 'inp_dat='//trim(adjustl(fout))
        write(12,'(a)') 'fig=_gmt.eps'
        write(12,'(a)') 'gmt psxy $inp_dat -R$range -JX$scale -SC0.01 -C_col.cpt -P > $fig'
        write(12,'(a)') 'convert -trim -density 300 $fig '//trim(adjustl(fnameF))
        close(12)
    end subroutine GMTSH
end program f90_julia2_gmt