program OW_windowing

use sep
use OW_type
use OW_exec_time
use OW_parms_areal
use OW_taper_areal
implicit none

character*80				:: hor1,hor2
real, allocatable				:: intmp(:,:,:),temp(:,:,:)
real, allocatable				:: pick1(:,:),pick2(:,:),taper(:)
real					:: finish,start
integer					:: wavz,n,ihor,ikx,iz,ih,nhorz,it

call sep_init()
call sep_begin_prog()

 call from_history("n1",sog%z%n);call from_history("d1",sog%z%d);call from_history("o1",sog%z%o)
 call from_history("n2",sog%sh%n);call from_history("d2",sog%sh%d);call from_history("o2",sog%sh%o)
 call from_history("n3",sog%xm%n);call from_history("d3",sog%xm%d);call from_history("o3",sog%xm%o)  

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

 call to_history("n1",sog%z%n);call to_history("d1",sog%z%d);call to_history("o1",sog%z%o)
 call to_history("n2",sog%sh%n);call to_history("d2",sog%sh%d);call to_history("o2",sog%sh%o)
 call to_history("n3",sog%xm%n);call to_history("d3",sog%xm%d);call to_history("o3",sog%xm%o)  
 call to_history("eout",4);call to_history("label3","")

!--------------------------------------------------
!
! Read rotated CIGs
!
allocate(intmp(sog%z%n,sog%sh%n,sog%xm%n));intmp=0.
open(unit=2,file="lixo")
call from_param("wavz",wavz,13)
call sseek("in",0,0)
call sreed("in",intmp,4*sog%z%n*sog%sh%n*sog%xm%n)
call owexec_time(start)
call from_param('hor1',hor1)
open(51,file=hor1,status='old')
allocate(pick1(sog%xm%n,sog%sh%n));pick1=9999.
do n=1,sog%xm%n
   read(51,*,end=1) pick1(n,:)
end do
1 close(51)
if (exist_file("hor2")) then
   call from_param('hor2',hor2)
   open(51,file=hor2,status='old')
   allocate(pick2(sog%xm%n,sog%sh%n));pick2=9999.
   do n=1,sog%xm%n
      read(51,*,end=2) pick2(n,:)
   end do
   2 close(51)
end if
allocate(taper(2*wavz+1))
call owtaper(wavz,taper)
write(0,*) taper
allocate(temp(sog%z%n,sog%sh%n,sog%xm%n));temp=0.
do ikx=1,sog%xm%n
   do ih=1,sog%sh%n
      do iz=1,sog%z%n
         if (exist_file("hor2")) then
            if (iz .ge. int(pick1(ikx,ih))-wavz .and. iz .le. int(pick1(ikx,ih))+wavz) then
               temp(iz,ih,ikx)=intmp(iz,ih,ikx)*taper(iz-int(pick1(ikx,ih))+wavz+1)**2
            else if (iz .ge. int(pick2(ikx,ih))-wavz .and. iz .le. int(pick2(ikx,ih))+wavz) then
               temp(iz,ih,ikx)=intmp(iz,ih,ikx)*taper(iz-int(pick2(ikx,ih))+wavz+1)**2
            end if
         else
            if (iz .ge. int(pick1(ikx,ih))-wavz .and. iz .le. int(pick1(ikx,ih))+wavz) then
               temp(iz,ih,ikx)=intmp(iz,ih,ikx)*taper(iz-int(pick1(ikx,ih))+wavz+1)**2
            end if
         end if
      end do
   end do
end do

intmp=temp

if (exist_file("pickr")) then
   call to_history("n1",sog%xm%n,"pickr"); call to_history("d1",sog%xm%d,"pickr"); call to_history("o1",sog%xm%o,"pickr")
   call to_history("n2",sog%sh%n,"pickr"); call to_history("d2",sog%sh%d,"pickr"); call to_history("o2",sog%sh%o,"pickr")
   call to_history("n3",2,"pickr"); call to_history("d3",1.,"pickr"); call to_history("o3",1.,"pickr")  
   call to_history("n4",1,"pickr"); call to_history("d4",1.,"pickr"); call to_history("o4",1.,"pickr")
   call to_history("esize",4,"pickr")
   pick1=pick1*sog%z%d
   if (exist_file("hor2")) then
      pick2=pick2*sog%z%d
      call srite("pickr",pick2,4*sog%xm%n*sog%sh%n)
   end if
   call srite("pickr",pick1,4*sog%xm%n*sog%sh%n)
 end if

if (exist_file("mask")) then
 call to_history("n1",sog%z%n,"mask");call to_history("d1",sog%z%d,"mask");call to_history("o1",sog%z%o,"mask")
 call to_history("n2",sog%sh%n,"mask");call to_history("d2",sog%sh%d,"mask");call to_history("o2",sog%sh%o,"mask")
 call to_history("n3",sog%xm%n,"mask");call to_history("d3",sog%xm%d,"mask");call to_history("o3",sog%xm%o,"mask")  
 call to_history("eout",4,"mask");call to_history("label3","","mask")

do ikx=1,sog%xm%n
   do ih=1,sog%sh%n
      do iz=1,sog%z%n
         if (iz .ge. int(pick1(ikx,ih))-wavz .and. iz .le. int(pick1(ikx,ih))+wavz) temp(iz,ih,ikx)=1
         if (exist_file("hor2")) then
            if (iz .ge. int(pick2(ikx,ih))-wavz .and. iz .le. int(pick2(ikx,ih))+wavz) then
               temp(iz,ih,ikx)=1
            end if
         end if
      end do
   end do
end do
call srite("mask",temp,4*sog%z%n*sog%xm%n*sog%sh%n)
end if
call owexec_time(finish)
write(0,*)  "FINISHED WINDOWING ",int(finish-start),'(s)'
call srite("out",intmp,4*sog%z%n*sog%xm%n*sog%sh%n)
if (exist_file("hor2")) deallocate(pick2)
deallocate(temp,pick1,intmp)
close(2)
call sep_end_prog()
end program
