program dcl_plot
!  テキストデータから直線のグラフを作成する gnuplot dcl 版
  use dcl
  use Dcl_Automatic
  use file_operate

  implicit none

  integer :: nx, ny, line, ln, xlnum, i, j
  integer, allocatable, dimension(:) :: cn
  real, allocatable, dimension(:,:) :: val, dval, xval
  character(100), allocatable, dimension(:,:) :: cval
  character(100) :: fname, title, xa, ya
  character(1) :: cont
  integer, external :: iargc
  character(80) :: argv
  character(80), allocatable :: argar(:)
  integer :: nargc
  integer, allocatable, dimension(:) :: nval

!  write(*,*) "input the file name."
!  read(*,*) fname

  nargc=iargc()

  if(nargc==0)then
     write(*,*) "[usage] : dcl_plot [input file] [column number]"
     stop
  end if

  allocate(argar(nargc))
  allocate(nval(nargc))

  do i=1,nargc
     call getarg(i,argv)
     argar(i)=argv
  end do

  read(argar(2),*) nx

  ny=line_number_counter( trim(fname) )

!  write(*,*) "input data colum."
!  read(*,*) nx

  allocate(val(nx,ny))
  allocate(cval(nx,ny))
  allocate(dval(ny,nx))
  allocate(xval(ny,nx))

  call read_file_text( trim(fname), nx, ny, cval(1:nx,1:ny) )

  do j=1,ny
     do i=1,nx
        read(cval(i,j),*) val(i,j)
     end do
  end do

!-- DCL

  cont='Y'

  call DclOpenGraphics(1)

  do while(cont/='n')

     write(*,*) "input the title, x-axis name, y-axis name."
     read(*,*) title, xa, ya

     write(*,*) "input the colum muber of x-axis data."
     read(*,*) xlnum

     write(*,*) "input the line numbers."
     read(*,*) ln

     allocate(cn(ln))

     write(*,*) "input the each colum number."
     read(*,*) cn(1:ln)

     do j=1,ny
        do i=1,ln
           xval(j,i)=val(xlnum,j)
           dval(j,i)=val(cn(i),j)
        end do
     end do

     call Dcl_PL( 'l', trim(title), xval, dval, xval, dval, (/trim(xa), trim(ya)/) )

     write(*,*) "plot continue ?? [Y/n]"
     read(*,*) cont

     deallocate(cn)

  end do

  call DclCloseGraphics

end program
