program ZOF2PRE
  !! Compute image quality maps
  !! Paul Sava (paul@sep.stanford.edu)
  !! 10/14/02

  !! input:  z-ax-mx-ro (ADCIG)
  !! output: z-   mx-ro (image quality map)

  use sep
  implicit none

  integer :: n1,n2,n3
  real    :: o1,o2,o3
  real    :: d1,d2,d3
  real    :: din(:,:), dou(:,:)
  integer :: win2

  integer :: i2,i3, j2,k2

  call sep_init()

  call from_history("n1",n1,1)
  call from_history("n2",n2,1)
  call from_history("n3",n3,1)
  call from_history("o1",o1,0.)
  call from_history("o2",o2,0.)
  call from_history("o3",o3,0.)
  call from_history("d1",d1,1.)
  call from_history("d2",d2,1.)
  call from_history("d3",d3,1.)

  call from_param("win2",win2,0)

  call to_history("n2",2*win2)
  call to_history("o2",-win2*d2)
  call to_history("d2",d2)

  call to_history("n3",n2)
  call to_history("o3",o2)
  call to_history("d3",d2)
  
  call to_history("n4",n3)
  call to_history("o4",o3)
  call to_history("d4",d3)

  call sep_close()

  allocate(din(n1,n2))
  allocate(dou(n1,2*win2))

  do i3=1,n3
     write(0,*) i3,n3
     call sep_read(din)
     do i2=1,n2
        j2=i2-win2
        k2=i2+win2-1
!        write(0,*) i2,j2,k2
        if(j2<1 .or. k2>n2) then
           dou=0
        else
           dou = din(:,j2:k2)
        end if
        call sep_write(dou)
     end do
  end do

  deallocate(din,dou)
  
  call exit(0)
end program ZOF2PRE
