subroutine d_splot3D(pname,X1,X2,Y,xmin,xmax,ymin,ymax,nosurface,wlines,nlines)
integer :: i,j,Nx1,Nx2,count,Nl
character(len=*) :: pname
real(8),dimension(:) :: X1
real(8),dimension(:) :: X2
real(8),dimension(size(X1),size(X2)) :: Y
real(8),optional :: xmin,xmax,ymin,ymax
integer,optional :: nlines
logical,optional :: wlines,nosurface
real(8) :: X1min,X1max
real(8) :: X2min,X2max
character(len=12) :: minx,miny,maxx,maxy
character(len=256) :: fname,dname
fname=get_filename(pname)
dname=get_filepath(pname)
Nx1=size(X1) ; Nx2=size(X2)
Nl=5; if(present(nlines))Nl=nlines
open(719,file=adjustl(trim(pname)))
if(present(wlines))open(720,file=adjustl(trim(pname))//"_withlines")
do i=1,Nx1
count=mod(i,Nl)
do j=1,Nx2
write(719,*)X1(i),X2(j),Y(i,j)
if(present(wlines).AND.count==0)write(720,*)X1(i),X2(j),Y(i,j)
enddo
write(719,*)""
if(present(wlines))write(720,*)""
if(present(wlines))write(720,*)""
enddo
close(719)
X1min=minval(X1);if(present(xmin))X1min=xmin
X1max=maxval(X1);if(present(xmax))X1max=xmax
X2min=minval(X2);if(present(ymin))X2min=ymin
X2max=maxval(X2);if(present(ymax))X2max=ymax
write(minx,"(f12.4)")X1min
write(maxx,"(f12.4)")X1max
write(miny,"(f12.4)")X2min
write(maxy,"(f12.4)")X2max
if(present(wlines))close(720)
open(10,file=adjustl(trim(pname))//"_map.gp")
write(10,*)"set term wxt"
write(10,*)"set title '"//trim(fname)//"'"
write(10,*)"set pm3d map"
write(10,*)"set size square"
write(10,*)"set xrange ["//trim(adjustl(trim(minx)))//":"//trim(adjustl(trim(maxx)))//"]"
write(10,*)"set yrange ["//trim(adjustl(trim(miny)))//":"//trim(adjustl(trim(maxy)))//"]"
write(10,*)"splot '"//trim(fname)//"'"
write(10,*)"#set term png size 1920,1280"
write(10,*)"#set out '"//adjustl(trim(fname))//".png'"
write(10,*)"#rep"
!
close(10)
if(present(nosurface).AND.nosurface)return
open(10,file=adjustl(trim(pname))//"_surface.gp")
write(10,*)"set term wxt"
write(10,*)"set title '"//trim(fname)//"'"
write(10,*)"set nokey"
write(10,*)"set grid"
write(10,*)"set view 50,10,1,1"
write(10,*)"splot '"//trim(fname)//"' with pm3d"
if(present(wlines))write(10,*)"rep '"//trim(pname)//"_withlines' with lines"
write(10,*)"#set term png size 1920,1280"
write(10,*)"#set out '"//adjustl(trim(fname))//".png'"
write(10,*)"#rep"
close(10)
end subroutine d_splot3D