program OW_targmig2d

use sep 
use OW_type
use OW_grabvel_areal
use OW_exec_time
use OW_parms_areal
use OW_taper_areal
use OW_wei_areal

implicit none

real						:: start, finish, start1, finish1, o3,a,xs,xr
integer						:: ixs,ino,ni,i,st,idip,er,ixr,idat,j
integer						:: ixg,iw,iz,nodes,izm,ixm,jxm,jzm,inode
real, allocatable					:: velint(:,:),taper(:)
real, allocatable					:: R_image(:,:,:),image(:,:,:)
real, allocatable					:: vel(:,:),vref(:,:,:)
complex, allocatable				:: recv(:,:,:),source(:),greenup(:,:,:),greendown(:,:,:)
complex, allocatable				:: isocrona(:,:),source1(:,:),recdat(:,:,:),greensyn(:,:,:)
integer, external 					:: omp_get_thread_num,omp_get_num_threads

!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
 call sep_init()
 call sep_begin_prog()
 open(unit=2,file='lixo')

 start=0.;finish=0.
 call owexec_time(start)
 call owparam()

if (greencomp) then
   allocate(source(sou%w%n))
   allocate(taper(img%xm%n))
   call owtaper(tapertr,taper)

   allocate(velint(v%z%n,v%xm%n),vel(v%z%n,img%xm%n))
   call sreed("vel",velint,v%z%n*v%xm%n*4)
   if (exist_file("vref")) then
     allocate(vref(nref,2,v%z%n))
     call sreed("vref",vref,v%z%n*2*nref*4)
   end if
   call owgrab_vel(velint,vel,ni)
   deallocate(velint)

   call owexec_time(finish)
   write(0,'(8x,a,f10.2,a)')' Init.phase - Total elapsed time:', (finish - start),' (sec)'

! Computing target-oriented Greens functions

   call sreed("sou",source,8*sou%w%n)
   allocate(source1(img%xm%n,sou%w%n))
   do idip=1,numxdat
      source1=0.
      i=int(((idip-1)*xintdat+xmindat-img%xm%o)/img%xm%d)!+1.5
      source1(i,:)=source
      start=0.;finish=0. 
      call owexec_time(start)
      if (exist_file("vref")) then
         call owtgtwei( idip,taper,source1,vel,vref)
      else
         call owtgtwei( idip,taper,source1,vel)
      end if
      call owexec_time(finish)
      write(0,222) "GENERATED GF = ",idip," OUT OF ",numxdat," IN ",(finish-start)," s"
   end do
  
   222 format(a15,i4,a8,i4,a4,f6.2,a4)

else if (greensynth) then

   allocate(greensyn(nxtgt,nztgt,nshots));greensyn=0.

   if (node.eq.9999) then
     !$OMP PARALLEL
     nodes = omp_get_num_threads()
     !$OMP END PARALLEL
   else
     nodes=node
   end if
   write(0,*) "****************"
   write(0,*) "* NUM.THREADS = ",nodes
   write(0,*) "****************"

   allocate(greenup(nshots,1,numxdat),stat=er);greenup=0.
   allocate(greendown(img%xm%n,img%z%n,numxdat),stat=er);greendown=0.

   do iw=1,rec%w%n

      start=0.;finish=0.;greensyn=0;greenup=0.;greendown=0.
      call owexec_time(start)

      call sreed("greenup",greenup,8*nshots*1*numxdat)
      call sreed("greendown",greendown,8*img%xm%n*img%z%n*numxdat)

   !$OMP parallel do private(ixs,&
                             ixg,&
                             izm,&
                             ixm,&
                             jxm,&
                             jzm) ordered schedule(dynamic,1) num_threads(nodes)

      do ixs=1,nshots
         do ixg=1,numxdat
            do ixm=1,nxtgt;jxm=int((x0tgt-img%xm%o)/img%xm%d)+ixm
               do izm=1,nztgt;jzm=int((z0tgt-img%z%o)/img%z%d)+izm
!                  greensyn(ixm,izm,ixs)=conjg(greenup(ixs,1,ixg))*greendown(jxm,jzm,ixg)
                  greensyn(ixm,izm,ixs)=greenup(ixs,1,ixg)*greendown(jxm,jzm,ixg)
               end do
            end do
         end do
      end do
      !$OMP END PARALLEL DO
       call srite("greensyn",greensyn,8*nxtgt*nztgt*nshots)
       call owexec_time(finish)
       write(0,555) "Used frequency # ",iw," OUT OF ",rec%w%n," IN ",(finish-start)," s"
   end do

   deallocate(greenup,greendown)

end if
 444 format(a15,i4,a8,i4,a4,f6.2,a4)
 555 format(a17,i4,a8,i4,a4,f6.2,a4)

 close(2)

! deallocate(source)
! deallocate(recv)
! deallocate(R_image,deltaI)
! deallocate(taper)
! deallocate(vel)
! if (exist_file("vref")) then
!    deallocate(vref)
! end if


 call sep_end_prog()

end program OW_targmig2d



!else if (tgtmig) then
!!
!!! Computing migrated image
!!
!   allocate(R_image(nz,1,numxdat));R_image=0.
!   allocate(recv(rec%h%n,rec%w%n,rec%xd%n));recv=0.
!   allocate(recdat(rec%h%n,rec%w%n,rec%xd%n));recdat=0.
!   call sreed("in",recv,8*rec%h%n*rec%w%n*rec%xd%n)
!
!   if (node.eq.9999) then
!     !$OMP PARALLEL
!     nodes = omp_get_num_threads()
!     !$OMP END PARALLEL
!   else
!     nodes=node
!   end if
!   write(0,*) "****************"
!   write(0,*) "* NUM.THREADS = ",nodes
!   write(0,*) "****************"
!
!   allocate(green(img%xm%n,img%z%n,rec%w%n,2),stat=er);green=0
!
!   do ixg=1,numxdat
!
!      start=0.;finish=0. 
!      call owexec_time(start)
!      call sreed("green",green,8*img%xm%n*img%z%n*rec%w%n*2)
!
!   !$OMP parallel do private(ixs,&
!                             ixr,&
!                              iw,&
!                              iz,&
!                              xs,&
!                              xr,&
!                               i,&
!                               j,&
!                           start,&
!                            finish) ordered schedule(dynamic,1) num_threads(nodes)
!
!      do ixs=1,rec%xd%n
!         xs=(ixs-1)*rec%xd%d+rec%xd%o
!         i=int((xs-img%xm%o)/img%xm%d)+1.5
!         do ixr=1,rec%h%n
!            xr=(ixr-1)*rec%h%d+rec%h%o+xs
!            j=int((xr-img%xm%o)/img%xm%d)+1.5
!            do iw=1,rec%w%n
!!               do iz=1,numzdat-1
!!                  recdat(ixr,iw,ixs)=recdat(ixr,iw,ixs)+recv(ixr,iw,ixs)* &
!!                                          green(i,iz,iw,2)*green(j,iz,iw,2)
!!               end do
!               recdat(ixr,iw,ixs)=recdat(ixr,iw,ixs)+recv(ixr,iw,ixs)* &
!                                 green(i,1,iw,2)*green(j,1,iw,2)
!               do iz=numzdat,nz
!                  R_image(iz,1,ixg)=R_image(iz,1,ixg)+recdat(ixr,iw,ixs)* &
!                                          conjg(green(i,iz,iw,1))*conjg(green(j,iz,iw,1))
!!                                          green(i,1,iw,2)*green(j,1,iw,2)* &
!               end do
!            end do
!          end do
!       end do
!      !$OMP END PARALLEL DO
!       call owexec_time(finish)
!       write(0,444) "Used Green's # ",ixg," OUT OF ",numxdat," IN ",(finish-start)," s"
!   end do
!
!if(exist_file("recdat")) then
!  call srite("recdat",recdat, 8*rec%h%n*rec%w%n*rec%xd%n)
!end if
!  
!   call srite("image",R_image,4*(nz*numxdat))
!   deallocate(green)

!else if (tgthess) then
!
!! Computing migrated image
!
!   allocate(hessian(numzdat,numxdat));hessian=0.
!   allocate(hestemp(numzdat,numxdat,nodes));hestemp=0.
!
!   if (node.eq.9999) then
!     !$OMP PARALLEL
!     nodes = omp_get_num_threads()
!     !$OMP END PARALLEL
!   else
!     nodes=node
!   end if
!   write(0,*) "****************"
!   write(0,*) "* NUM.THREADS = ",nodes
!   write(0,*) "****************"
!
!   allocate(green(img%xm%n,img%z%n,numxdat,2),stat=er);green=0
!
!   do iw=1,rec%w%n
!
!      start=0.;finish=0. 
!      call owexec_time(start)
!      call sreed("green",green,8*img%xm%n*img%z%n*numxdat*2)
!
!   !$OMP parallel do private(ixs,&
!                             ixr,&
!                              iw,&
!                              iz,&
!                              xs,&
!                              xr,&
!                               i,&
!                               j,&
!                           start,&
!                            finish) ordered schedule(dynamic,1) num_threads(nodes)
!
!      do ixg=1,numxdat
!      do ixm=1,nxtgt
!      do izm=1,nztgt
!         gr_sou(ixm,izm)=green(ixm,izm,ixg,2)*conjg(green(ixm,izm,ixg,2))*green(ixm,1,ixg,1)*conjg(green(ixm,1,ixg,1))
!!      do ixs=1,rec%xd%n
!         xs=(ixs-1)*rec%xd%d+rec%xd%o
!         i=int((xs-img%xm%o)/img%xm%d)+1.5
!         do ixr=1,rec%h%n
!            xr=(ixr-1)*rec%h%d+rec%h%o+xs
!            j=int((xr-img%xm%o)/img%xm%d)+1.5
!            do iw=1,rec%w%n
!!               do iz=1,numzdat-1
!!                  recdat(ixr,iw,ixs)=recdat(ixr,iw,ixs)+recv(ixr,iw,ixs)* &
!!                                          green(i,iz,iw,2)*green(j,iz,iw,2)
!!               end do
!               recdat(ixr,iw,ixs)=recdat(ixr,iw,ixs)+recv(ixr,iw,ixs)* &
!                                 green(i,1,iw,2)*green(j,1,iw,2)
!               do iz=numzdat,nz
!                  R_image(iz,1,ixg)=R_image(iz,1,ixg)+recdat(ixr,iw,ixs)* &
!                                          conjg(green(i,iz,iw,1))*conjg(green(j,iz,iw,1))
!!                                          green(i,1,iw,2)*green(j,1,iw,2)* &
!               end do
!            end do
!          end do
!       end do
!      !$OMP END PARALLEL DO
!       call owexec_time(finish)
!       write(0,444) "Used Green's # ",ixg," OUT OF ",numxdat," IN ",(finish-start)," s"
!   end do
!
!if(exist_file("recdat")) then
!  call srite("recdat",recdat, 8*rec%h%n*rec%w%n*rec%xd%n)
!end if
!  
!   call srite("image",R_image,4*(nz*numxdat))
!   deallocate(green)

! Computing migration operators
! common-offset

! allocate(isocrona(img%xm%n,img%z%n),stat=er);isocrona=0
! if(er/=0) call seperr('cant allocate isocrona')
!    if (exist_file("isocrona")) then
!      call to_history("n1",img%xm%n,"isocrona"); call to_history("d1",img%xm%d,"isocrona"); call to_history("o1",img%xm%o,"isocrona")  
!      call to_history("n2",nz,"isocrona") ; call to_history("d2",img%z%d,"isocrona") ; call to_history("o2",zmin,"isocrona")
!      call to_history("n3",rec%w%n,"isocrona"); call to_history("d3",rec%w%d/(2*pi),"isocrona"); call to_history("o3",rec%w%o/(2*pi),"isocrona")
!      call to_history("n4",1,"isocrona"); call to_history("d4",1.,"isocrona"); call to_history("o4",1.,"isocrona")  
!      call to_history("n5",1,"isocrona"); call to_history("d5",1.,"isocrona"); call to_history("o5",1.,"isocrona")
!      call to_history("esize",8,"isocrona")
!    end if
! do i=1,rec%w%n
!    isocrona=0.
!    isocrona=green(:,:,i,1,numxdat)*green(:,:,i,1,1)
!    if (exist_file("isocrona")) then
!      call srite("isocrona",isocrona, 8*(img%xm%n*img%z%n))
!    end if
! end do

! do ixs=1,rec%xd%n
!    start=0.;finish=0. 
!    call owexec_time(start)
!    recv=0;
!    xs=(ixs-1)*rec%xd%d+rec%xd%o
!    i=int((xs-img%xm%o)/img%xm%d)+1.5    
!    do idat=1,numxdat
!       do ixr=1,rec%h%n
!          xr=(ixr-1)*rec%h%d+rec%h%o+xs
!          j=int((xr-img%xm%o)/img%xm%d)+1.5
!          recv(ixs,:)=recv(ixs,:)+recdat(ixr,ixs,:)*green(j,1,:,2,idat)!conjg(green(j,1,:,2,idat))
!       end do
!    end do    
!    if (exist_file("recdat")) then
!      call srite("recdat",recv, 8*(rec%h%n*rec%w%n))
!    end if
!    call owexec_time(finish)
!    write(0,333) "Datumized shot = ",ixs," OUT OF ",rec%xd%n," IN ",(finish-start)," s"
! end do
! 333 format(a17,i4,a8,i4,a4,f6.2,a4)

