subroutine c_splot3d_animate(pname,X1,X2,Y,xmin,xmax,ymin,ymax)
integer :: i,j,m,Nx1,Nx2,Nt
character(len=*) :: pname
real(8),dimension(:) :: X1
real(8),dimension(:) :: X2
complex(8),dimension(:,:,:) :: Y
real(8) :: X1min,X1max
real(8) :: X2min,X2max
real(8),optional :: xmin,xmax,ymin,ymax
real(8) :: Rmin(3),Rmax(3),Zmin,Zmax
character(len=256) :: fname,dname
fname=get_filename(reg(pname))
dname=get_filepath(reg(pname))
Nx1=size(X1) ; Nx2=size(X2)
if(size(Y,1)/=Nx1) stop "Error Nx1"
if(size(Y,2)/=Nx2) stop "Error Nx2"
Nt=size(Y,3)
write(*,*) "splot3d_animate "//str(fname)//" ("//str(Nx1)//","//str(Nx2)//","//str(Nt)//")"
open(619,file=adjustl(trim(dname))//"re_"//adjustl(trim(fname)))
open(719,file=adjustl(trim(dname))//"im_"//adjustl(trim(fname)))
do m=1,Nt
do i=1,Nx1
do j=1,Nx2
write(619,*)X1(i),X2(j),real(Y(i,j,m),8)
write(719,*)X1(i),X2(j),aimag(Y(i,j,m))
enddo
write(619,*)""
write(719,*)""
enddo
enddo
close(619)
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
Rmin=minval(dreal(Y));Rmax=maxval(dreal(Y))
Zmin=minval(Rmin);Zmax=maxval(Rmax)
open(10,file=reg(pname)//"_re_map.gp")
! write(10,*)"gnuplot -persist << EOF"
write(10,*)"reset"
write(10,*)"#set term gif animate"
write(10,*)"#set output 're_"//trim(fname)//".gif'"
write(10,*)"set pm3d map"
write(10,*)"set size square"
write(10,*)"set xrange ["//trim(adjustl(trim(txtfy(X1min))))//":"//trim(adjustl(trim(txtfy(X1max))))//"]"
write(10,*)"set yrange ["//trim(adjustl(trim(txtfy(X2min))))//":"//trim(adjustl(trim(txtfy(X2max))))//"]"
write(10,*)"set cbrange ["//trim(adjustl(trim(txtfy(Zmin))))//":"//trim(adjustl(trim(txtfy(Zmax))))//"]"
write(10,*)"n="//trim(txtfy(Nt))
write(10,*)"do for [i=0:n-1]{"
write(10,*)"set title sprintf('Re("//trim(fname)//"); i=%i',i+1)"
write(10,*)"splot 're_"//trim(fname)//"' every :::("//trim(adjustl(trim(txtfy(Nx2))))//&
"*i)::("//trim(adjustl(trim(txtfy(Nx2))))//"*i+"//trim(adjustl(trim(txtfy(Nx1-1))))//") title '' "
write(10,*)"}"
write(10,*)"#set output"
! write(10,"(A)")"EOF"
close(10)
Rmin=minval(dimag(Y));Rmax=maxval(dimag(Y))
Zmin=minval(Rmin);Zmax=maxval(Rmax)
open(10,file=reg(pname)//"_im_map.gp")
! write(10,*)"gnuplot -persist << EOF"
write(10,*)"reset"
write(10,*)"#set term gif animate"
write(10,*)"#set output 'im_"//trim(fname)//".gif'"
write(10,*)"set pm3d map"
write(10,*)"set size square"
write(10,*)"set xrange ["//trim(adjustl(trim(txtfy(X1min))))//":"//trim(adjustl(trim(txtfy(X1max))))//"]"
write(10,*)"set yrange ["//trim(adjustl(trim(txtfy(X2min))))//":"//trim(adjustl(trim(txtfy(X2max))))//"]"
write(10,*)"set cbrange ["//trim(adjustl(trim(txtfy(Zmin))))//":"//trim(adjustl(trim(txtfy(Zmax))))//"]"
write(10,*)"n="//trim(txtfy(Nt))
write(10,*)"do for [i=0:n-1]{"
write(10,*)"set title sprintf('Im("//trim(fname)//"); i=%i',i+1)"
write(10,*)"splot 'im_"//trim(fname)//"' every :::("//trim(adjustl(trim(txtfy(Nx2))))//&
"*i)::("//trim(adjustl(trim(txtfy(Nx2))))//"*i+"//trim(adjustl(trim(txtfy(Nx1-1))))//") title '' "
write(10,*)"}"
write(10,*)"#set output"
! write(10,"(A)")"EOF"
close(10)
! call system("chmod +x "//reg(pname)//".gp")
end subroutine c_splot3d_animate