subroutine viz_plot(val,nx,ny,max)
real, intent(in) :: val(:,:)
integer, intent(in) :: nx,ny
real, intent(in),optional :: max
integer :: i, j, k, l, m, n, dx, dy
real :: vmax, scalef, tmp
! set blocksize for averaging
dx = nx / cols
if (dx < 1) dx = 1
dy = ny / rows
if (dy < 1) dy = 1
! set or determine scaling factor for data points
vmax=1.0e-30
if (present(max)) then
vmax = abs(max)
else
! find absolute maximum value for scaling
do j=1,rows
do i=1,cols
! average over cells
tmp = 0.0
n = 0
do k=(j-1)*dy+1,j*dy
do l=(i-1)*dx+1,i*dx
tmp = tmp + val(l,k)
n = n + 1
enddo
enddo
tmp = ABS(tmp)/REAL(n)
if (vmax < tmp) vmax = tmp
enddo
enddo
endif
scalef = real(maxplot)/vmax
! now plot
do j=1,rows
call viz_pos(1,j)
do i=1,cols
! average over cells
tmp = 0.0
n = 0
do k=(j-1)*dy+1,j*dy
do l=(i-1)*dx+1,i*dx
tmp = tmp + val(l,k)
n = n + 1
enddo
enddo
! convert absolute value into character
m = int(scalef*abs(tmp)/real(n)+0.5)
if (m > maxplot) m = 10
if (tmp < 0.0) then
if (m > 5) then
call viz_put(cli//'36m'//plot(m))
else
call viz_put(cli//'34m'//plot(m))
endif
else
if (m > 5) then
call viz_put(cli//'31m'//plot(m))
else
call viz_put(cli//'30m'//plot(m))
endif
endif
enddo
call viz_put(cli//'30m|')
enddo
call viz_pos(1,rows)
call viz_put(cli//'30m')
DO i=1,cols+1
call viz_put('-')
enddo
end subroutine viz_plot