program OW3D_windowing

use sep
use OW_type
use OW_exec_time
implicit none

logical					:: taperon
character*80				:: hor
real, allocatable				:: intmp(:,:,:),temp(:,:,:)
real, allocatable				:: pick(:,:,:,:),taper(:)
real					:: finish,start
integer					:: wavz,n,i,ihor,ikx,iz,ih,it,iky,m
type(mod_grid)                                    :: shx,xm,ym,z
type(axis)                                        :: sog

call sep_init()
call sep_begin_prog()

 call from_param("nhorz",nhorz)
 call from_history("n1",sog%xm%n);call from_history("d1",sog%xm%d);call from_history("o1",sog%xm%o)  
 call from_history("n2",sog%shx%n);call from_history("d2",sog%shx%d);call from_history("o2",sog%shx%o)
 call from_history("n3",sog%ym%n);call from_history("d3",sog%ym%d);call from_history("o3",sog%ym%o)  
 call from_history("n4",sog%z%n);call from_history("d4",sog%z%d);call from_history("o4",sog%z%o)

 write(0,*) sog%z%n,sog%z%d,sog%z%o
 write(0,*) sog%shx%n,sog%shx%d,sog%shx%o
 write(0,*) sog%xm%n,sog%xm%d,sog%xm%o
 write(0,*) sog%ym%n,sog%ym%d,sog%ym%o

 call to_history("n1",sog%xm%n);call to_history("d1",sog%xm%d);call to_history("o1",sog%xm%o)  
 call to_history("n2",sog%shx%n);call to_history("d2",sog%shx%d);call to_history("o2",sog%shx%o)
 call to_history("n3",sog%ym%n);call to_history("d3",sog%ym%d);call to_history("o3",sog%ym%o)  
 call to_history("n4",sog%z%n);call to_history("d4",sog%z%d);call to_history("o4",sog%z%o)
 call to_history("n4",nhorz);call to_history("d4",1.);call to_history("o4",1.)  
 call to_history("eout",4);call to_history("label3","")

!--------------------------------------------------
!
! Read rotated CIGs
!
allocate(intmp(sog%xm%n,sog%shx%n,sog%ym%n));intmp=0.
call from_param("wavz",wavz,13)
call from_param("taperon",taperon,.true.)
call sseek("in",0,0)
call owexec_time(start)
allocate(pick(sog%ym%n,sog%xm%n,sog%shx%n,nhorz));pick=9999.
do i=1,nhorz
   if (i.eq.1) then
      call from_param('hor1',hor)
      open(51,file=hor,status='old')
      do n=1,sog%xm%n
         do m=1,sog%ym%n
            read(51,*,end=1) pick(m,n,:,i)
         end do
      end do
   else if (i.eq.2) then
      call from_param('hor2',hor)
      open(51,file=hor,status='old')
      do n=1,sog%xm%n
         do m=1,sog%ym%n
            read(51,*,end=1) pick(m,n,:,i)
         end do
      end do
   else if (i.eq.3) then
      call from_param('hor3',hor)
      open(51,file=hor,status='old')
      do n=1,sog%xm%n
         do m=1,sog%ym%n
            read(51,*,end=1) pick(m,n,:,i)
         end do
      end do
   else if (i.eq.4) then
      call from_param('hor4',hor)
      open(51,file=hor,status='old')
      do n=1,sog%xm%n
         do m=1,sog%ym%n
            read(51,*,end=1) pick(m,n,:,i)
         end do
      end do
   else if (i.eq.5) then
      call from_param('hor5',hor)
      open(51,file=hor,status='old')
      do n=1,sog%xm%n
         do m=1,sog%ym%n
            read(51,*,end=1) pick(m,n,:,i)
         end do
      end do
   else if (i.eq.6) then
      call from_param('hor6',hor)
      open(51,file=hor,status='old')
      do n=1,sog%xm%n
         do m=1,sog%ym%n
            read(51,*,end=1) pick(m,n,:,i)
         end do
      end do
   else if (i.eq.7) then
      call from_param('hor7',hor)
      open(51,file=hor,status='old')
      do n=1,sog%xm%n
         do m=1,sog%ym%n
            read(51,*,end=1) pick(m,n,:,i)
         end do
      end do
   else if (i.eq.8) then
      call from_param('hor8',hor)
      open(51,file=hor,status='old')
      do n=1,sog%xm%n
         do m=1,sog%ym%n
            read(51,*,end=1) pick(m,n,:,i)
         end do
      end do
   else if (i.eq.9) then
      call from_param('hor9',hor)
      open(51,file=hor,status='old')
      do n=1,sog%xm%n
         do m=1,sog%ym%n
            read(51,*,end=1) pick(m,n,:,i)
         end do
      end do
   else if (i.eq.10) then
      call from_param('hor10',hor)
      open(51,file=hor,status='old')
      do n=1,sog%xm%n
         do m=1,sog%ym%n
            read(51,*,end=1) pick(m,n,:,i)
         end do
      end do
   else if (i.eq.11) then
      call from_param('hor11',hor)
      open(51,file=hor,status='old')
      do n=1,sog%xm%n
         do m=1,sog%ym%n
            read(51,*,end=1) pick(m,n,:,i)
         end do
      end do
   else if (i.eq.12) then
      call from_param('hor12',hor)
      open(51,file=hor,status='old')
      do n=1,sog%xm%n
         do m=1,sog%ym%n
            read(51,*,end=1) pick(m,n,:,i)
         end do
      end do
   end if
1 close(51)
end do

allocate(taper(2*wavz+1))
pi=4*atan(1.)
taper=1.
do i=1,wavz
   taper(i)=(1+cos(pi*(wavz-i+1)/wavz))/2
   taper(size(taper)-i+1)=taper(i)
end do

if (taperon) then
   call owtaper_init(wavz)
   call owtaper(taper)
else
   taper=1
end if

allocate(temp(sog%xm%n,sog%shx%n,sog%ym%n));
do i=1,nhorz
   temp=0.
   do iz=1,sog%z%n
      call sreed("in",intmp,4*sog%xm%n*sog%shx%n*sog%ym%n)
      do iky=1,sog%ym%n
         do ih=1,sog%shx%n
            do ikx=1,sog%xm%n
               if (iz .ge. int(pick(iky,ikx,ih,i))-wavz .and. iz .le. int(pick(iky,ikx,ih,i))+wavz) &
               temp(ikx,ih,iky)=intmp(ikx,ih,iky)*taper(iz-int(pick(iky,ikx,ih,i))+wavz+1)**2
            end do
         end do
      end do
      call srite("out",intmp,4*sog%xm%n*sog%shx%n*sog%ym%n)
   end do
end do

call owexec_time(finish)
write(0,*)  "FINISHED WINDOWING ",int(finish-start),'(s)'
deallocate(temp,pick,intmp,taper)

call sep_end_prog()
end program
