!!---------------------------------------------------------------------
!!.. Wave-equation shot-profile migration, driver's module
!!.. OpenMP parallelized
!!.. Version 3.0
!!.. This version includes the forward and adjoint operators
!!---------------------------------------------------------------------
!!.. Author: Yaxun Tang, tang@sep.stanford.edu
!!---------------------------------------------------------------------
!!.. Note: 1. To make full usage of the threads, 
!!..          it is better to keep nws >= nth 
!!..       2. For threads safty purpose, the memory allocation is
!!..          considerably increased compared to serial implementation.
!!..          Mem of parallel ~ Mem of serial * nth
!!---------------------------------------------------------------------
module wem_shpmig_mod2
  use  base_blkcal_mod
  use  wem_struct_mod
  use  wem_glocal_mod !!.. global calculation
  use  wem_fileio_mod !!.. file I/O
  use  wem_velref_mod !!.. reference velocity
  use  wem_wfdexp_mod !!.. wavefield extrapolation
  use  wem_imgcon_mod !!.. imaging condition 
  use  wem_fftwv3_mod !!.. FFTW3
  use  wem_tapers_mod !!.. taper
  implicit none

  type(dat_grid)                             , private :: dgrid    !!.. receiver grid
  type(vel_grid)                             , private :: vgrid    !!.. velocity grid
  type(pro_grid)                             , private :: pgrid    !!.. propagation grid
  type(img_grid)                             , private :: igrid    !!.. image grid
  type(out_grid)                             , private :: ogrid    !!.. image output grid
  type(hes_grid)                             , private :: hgrid

  type(block)                                , private :: wblock

  type(wem_ftag)                             , private :: ftag_

  real     , dimension(:,:,:)   , allocatable, private :: vmod     !!.. velocity model
  real     , dimension(:,:)     , allocatable, private :: vref     !!.. reference velocity
  real     , dimension(:,:,:)   , allocatable, private :: modl     !!.. output image

  complex  , dimension(:,:)     , allocatable, private :: crec
  complex  , dimension(:,:)     , allocatable, private :: csou
  complex  , dimension(:,:,:,:) , allocatable, private :: wfds
  complex  , dimension(:,:,:,:) , allocatable, private :: wfdr

  real     , dimension(:)       , allocatable, private :: kx2
  real     , dimension(:)       , allocatable, private :: ky2
  real     , dimension(:)       , allocatable, private :: tapx
  real     , dimension(:)       , allocatable, private :: tapy

  complex  , dimension(:,:,:)   , allocatable, private :: wfds_slice
  complex  , dimension(:,:,:)   , allocatable, private :: wfdr_slice

  integer*8, dimension(:)       , allocatable, private :: plan_2dsF, plan_2dsI
  integer*8, dimension(:)       , allocatable, private :: plan_2drF, plan_2drI

  integer  , dimension(:,:,:)   , allocatable, private :: iwrk2d
  real     , dimension(:,:)     , allocatable, private :: rwrk1d
  complex  , dimension(:,:)     , allocatable, private :: cwrk1d
  complex  , dimension(:,:,:,:) , allocatable, private :: cwrk3d

  integer                                    , private :: ompchunk, nth, nwstep
  integer  ,                         external, private :: omp_get_num_procs
  integer  ,                         external, private :: omp_get_num_threads
  integer  ,                         external, private :: omp_set_num_threads
  integer  ,                         external, private :: omp_get_thread_num

  real     ,                        parameter, private :: PI = 3.14159265359
contains

  integer function wem_shpmig_init(adj, dgrid_out, igrid_out, ftag_out_) result(stat)
    logical           :: adj
    type(dat_grid)    :: dgrid_out
    type(img_grid)    :: igrid_out
    type(wem_ftag)    :: ftag_out_

    stat = 1

    ftag_%csou_ = "csou"
    ftag_%crec_ = "crec"
    ftag_%vmod_ = "vmod"
    ftag_%imag_ = "imag"
    ftag_%hsou_ = "hsou"

!   read parameters
!   ---------------
    call from_param("radian", dgrid%radian, .true. )
    call from_param("offset", dgrid%offset, .false.)
    call from_param("comsou", dgrid%comsou, .false.)
    call from_param("hdrsou", dgrid%hdrsou, .false.)
    call from_param("ntpx"  , pgrid%ntpx  , 1      )
    call from_param("ntpy"  , pgrid%ntpy  , 1      )
    call from_param("nvrf"  , pgrid%nvrf  , 1      )
    call from_param("pdip"  , pgrid%pdip  , 65     )
    call from_param("oper"  , pgrid%oper  , "ssfpi")
    call from_param("trick" , pgrid%trick , 0.14   )
    call from_param("nws"   , wblock%ns   , 1      )
    call from_param("nwstep", nwstep      , 50     )

!   initialize file headers
!   -----------------------
    call auxinout(ftag_%csou_)
    call auxinout(ftag_%crec_)
    call auxinout(ftag_%vmod_)
    call auxinout(ftag_%imag_)
    if (dgrid%hdrsou) call auxinout(ftag_%hsou_)

    call wem_histo_data(adj, dgrid, vgrid, igrid, pgrid, ftag_)
    call wem_histo_imag(adj, igrid, vgrid, ftag_)

!   setup OpenMP stuff
!   ------------------
    ompchunk = 1
    nth      = 1
   !$OMP PARALLEL
    if (omp_get_thread_num() == 0) then
       nth = omp_get_num_threads()
       write(0,*)"nthread =", nth, omp_get_num_procs()
    end if
    !$OMP END PARALLEL

!   if present the source header, read it
!   -------------------------------------
    if (dgrid%hdrsou) then
       allocate(dgrid%hsou(2,dgrid%nshg))
       call sep_read(dgrid%hsou, ftag_%hsou_)
    end if

    if (.not.dgrid%radian) then
       dgrid%aw%d = dgrid%aw%d*2*PI; dgrid%aw%o = dgrid%aw%o*2*PI
    end if

!   calculate the global parameters for the first shot
!   --------------------------------------------------
    call wem_glocal(pgrid, ogrid, vgrid, dgrid, igrid, hgrid, 1)

!   calculate the frequency blocks
!   ------------------------------
    call base_blkcal(wblock, dgrid%aw)

    allocate(csou(dgrid%ntrs,wblock%ns))                       !!.. source array
    allocate(crec(dgrid%ntrg,wblock%ns))                       !!.. receiver array
    allocate(wfds(pgrid%ax%n,pgrid%ay%n,pgrid%az%n,wblock%ns)) !!.. source wavefield
    allocate(wfdr(pgrid%ax%n,pgrid%ay%n,pgrid%az%n,wblock%ns)) !!.. receiver wavefield
    allocate(vmod(pgrid%ax%n,pgrid%ay%n,pgrid%az%n))           !!.. velocity model
    allocate(vref(pgrid%nvrf,pgrid%az%n))                      !!.. reference velocity
    allocate(modl(ogrid%ax%n,ogrid%ay%n,ogrid%az%n))
    allocate(wfds_slice(pgrid%ax%n,pgrid%ay%n,nth))
    allocate(wfdr_slice(pgrid%ax%n,pgrid%ay%n,nth))
    allocate(kx2(pgrid%ax%n),ky2(pgrid%ay%n))
    allocate(tapx(pgrid%ntpx),tapy(pgrid%ntpy))

    allocate(plan_2dsF(nth),plan_2dsI(nth))
    allocate(plan_2drF(nth),plan_2drI(nth))

    allocate(cwrk1d(pgrid%ax%n*5+pgrid%ay%n*5,nth)) !!.. ifd or ffd
    allocate(cwrk3d(pgrid%ax%n,pgrid%ay%n,2,nth))   !!.. ssf-epi
    allocate(iwrk2d(pgrid%ax%n,pgrid%ay%n,nth))     !!.. ssf-epi
    allocate(rwrk1d(pgrid%ax%n+pgrid%ay%n,nth))     !!.. ifd or ffd

    call k2_map(kx2, pgrid%ax%n, pgrid%ax%d)
    call k2_map(ky2, pgrid%ay%n, pgrid%ay%d)
    call wem_fftw_init(plan_2dsF, plan_2dsI, wfds_slice, pgrid%ax%n, pgrid%ay%n, nth)
    call wem_fftw_init(plan_2drF, plan_2drI, wfdr_slice, pgrid%ax%n, pgrid%ay%n, nth)

    stat = wem_tapers_init(tapx, tapy, pgrid%ntpx, pgrid%ntpy)

    dgrid_out = dgrid
    igrid_out = igrid
    ftag_out_ = ftag_

    stat = 0
  end function

  integer function wem_shpmig_clos() result(stat)
    stat = 0
  end function

  integer function wem_shpmig_oper(adj, add, imag_, crec_) result(stat)
    logical           :: adj
    logical           :: add
    character(len=*)  :: imag_
    character(len=*)  :: crec_ 

    type(axis)        :: aws
    integer           :: iws, iwb, izs, ish, ith
    real              :: ws, PI
    integer           :: ogrid_nx_bak
    integer           :: ogrid_ny_bak
    integer           :: ogrid_nz_bak


    if (.not.add) then
       if (adj) then
          call wem_fio_imag_init(igrid, imag_)
       else
          call wem_fio_crec_init(dgrid, crec_)
       end if 
    end if

!   shot-profile migration 
!   ----------------------
    if (adj) then
       do ish=1, dgrid%nshg
          ogrid_nx_bak = ogrid%ax%n; ogrid_ny_bak = ogrid%ay%n; ogrid_nz_bak = ogrid%az%n

!         calculate the global parameters
!         -------------------------------
          call wem_glocal(pgrid, ogrid, vgrid, dgrid, igrid, hgrid, ish)
          if (igrid%ax%n>1.and.ogrid%ax%n == 0) cycle
          if (igrid%ay%n>1.and.ogrid%ay%n == 0) cycle
          if (ogrid%ax%n/=ogrid_nx_bak .or. ogrid%ay%n/=ogrid_ny_bak .or. ogrid%az%n/=ogrid_nz_bak) then
             deallocate(modl); allocate(modl(ogrid%ax%n,ogrid%ay%n,ogrid%az%n)); modl = 0.
          end if
          write(0,*)"----------------------------------------------"
          write(0,*)"ish = ", ish, pgrid%sx, pgrid%sy, dgrid%nshg
          write(0,*)"pgrid =", pgrid%ax%n,pgrid%ay%n,pgrid%az%n
          write(0,*)"ogrid%ax =", ogrid%ax%n,ogrid%ax%o,ogrid%ax%d
          write(0,*)"ogrid%ay =", ogrid%ay%n,ogrid%ay%o,ogrid%ay%d
          write(0,*)"ogrid%az =", ogrid%az%n,ogrid%az%o,ogrid%az%d
          write(0,*)"----------------------------------------------"

!         initialize the extrapolator
!         ---------------------------
          stat = wem_wfdexp_init(kx2, ky2, pgrid%ax%n, pgrid%ay%n, pgrid%ax%d, pgrid%ay%d, pgrid%az%d, &
                                 pgrid%pdip, pgrid%nvrf, pgrid%oper, pgrid%trick)

!         initialize the reference velocity
!         ---------------------------------
          stat = wem_velref_init(pgrid%nvrf)
          call wem_fio_velo(vmod, vgrid, pgrid, 1, pgrid%az%n, ftag_%vmod_)
          do izs=1, pgrid%az%n
             stat = wem_velref_oper(vmod(:,:,izs), vref(:,izs))
          end do
                       
!         initialize the imaging condition
!         --------------------------------
          stat = wem_imgcon_init(igrid, ogrid, pgrid, dgrid%aw%n, dgrid%nshg) 
                                    
          do iwb=1, wblock%nb
             aws%o = wblock%os(iwb); aws%d = wblock%ds; aws%n = wblock%blen(iwb)

!            read source wavefield
!            ---------------------
             call wem_fio_csou(csou, dgrid, wblock%indx(iwb), aws%n, ish, ftag_%csou_, "reed")
                    
!            read receiver wavefield
!            -----------------------
             call wem_fio_crec(crec, dgrid, wblock%indx(iwb), aws%n, ish,       crec_, "reed")

!            map source wavefield onto propagation grid
!            ------------------------------------------
             call wem_wfs_init(.true., .false., wfds, csou, dgrid, pgrid)

!            map receiver wavefield onto propagation grid
!            --------------------------------------------
             call wem_wfr_init(.true., .false., wfdr, crec, dgrid, pgrid)

!            loop over frequency
!            -------------------
             !$OMP PARALLEL DO SCHEDULE(DYNAMIC,OMPCHUNK) DEFAULT(SHARED) PRIVATE(iws,izs,ith,ws)
             do iws=1, aws%n
                ith = omp_get_thread_num() + 1
                ws  = aws%o + (iws-1)*aws%d

                if (iws/nwstep*nwstep==iws) write(0,*)"iws=", iws, ws, ith

!               for depth=0
!               -----------
                wfds_slice(:,:,ith) = wfds(:,:,1,iws)
                wfdr_slice(:,:,ith) = wfdr(:,:,1,iws)

!               loop over depth
!               ---------------
                do izs=2, pgrid%nzstep !pgrid%az%n

!                  downward continue the source wavefield
!                  --------------------------------------
                   stat = wem_wfdexp_oper(wfds_slice(:,:,ith), vmod(:,:,izs-1), vref(:,izs-1), ws, +1,                     &
                          iwrk2d(:,:,ith), rwrk1d(:,ith), cwrk1d(:,ith), cwrk3d(:,:,:,ith), plan_2dsF(ith), plan_2dsI(ith))

!                  downward continue the receiver wavefield
!                  ----------------------------------------
                   stat = wem_wfdexp_oper(wfdr_slice(:,:,ith), vmod(:,:,izs-1), vref(:,izs-1), ws, -1,                     &
                          iwrk2d(:,:,ith), rwrk1d(:,ith), cwrk1d(:,ith), cwrk3d(:,:,:,ith), plan_2drF(ith), plan_2drI(ith))

!                  applying tapers to both wavefields
!                  ----------------------------------
                   stat = wem_tapers_oper(wfds_slice(:,:,ith), pgrid%ax%n, pgrid%ay%n)
                   stat = wem_tapers_oper(wfdr_slice(:,:,ith), pgrid%ax%n, pgrid%ay%n)

!                  store the wavefields
!                  --------------------
                   wfds(:,:,izs,iws) = wfds_slice(:,:,ith)
                   wfdr(:,:,izs,iws) = wfdr_slice(:,:,ith)
                end do

!               applying the imaging condition
!               ------------------------------
                !$OMP CRITICAL
                stat = wem_imgcon_oper(.false., modl, wfds(:,:,:,iws), wfdr(:,:,:,iws), ws, "all", imag_)
                !$OMP END CRITICAL
             end do
             !$OMP END PARALLEL DO
          end do

!         clean up stuff
!         --------------
          stat = wem_velref_clos()
          stat = wem_wfdexp_clos()
          stat = wem_imgcon_clos()
       end do

    else

!      shot-profile demigration
!      ------------------------

!      loop over shots
!      ---------------
       do ish=1, dgrid%nshg
          ogrid_nx_bak = ogrid%ax%n; ogrid_ny_bak = ogrid%ay%n; ogrid_nz_bak = ogrid%az%n

!         calculate the global paramters
!         ------------------------------
          call wem_glocal(pgrid, ogrid, vgrid, dgrid, igrid, hgrid, ish)
          if (igrid%ax%n>1.and.ogrid%ax%n == 0) cycle
          if (igrid%ay%n>1.and.ogrid%ay%n == 0) cycle
          if (ogrid%ax%n/=ogrid_nx_bak .or. ogrid%ay%n/=ogrid_ny_bak .or. ogrid%az%n/=ogrid_nz_bak) then
             deallocate(modl); allocate(modl(ogrid%ax%n,ogrid%ay%n,ogrid%az%n)); modl = 0.
          end if
          write(0,*)"----------------------------------------------"
          write(0,*)"ish = ", ish, pgrid%sx, pgrid%sy, dgrid%nshg
          write(0,*)"pgrid =", pgrid%ax%n,pgrid%ay%n,pgrid%az%n
          write(0,*)"ogrid%ax =", ogrid%ax%n,ogrid%ax%o,ogrid%ax%d
          write(0,*)"ogrid%ay =", ogrid%ay%n,ogrid%ay%o,ogrid%ay%d
          write(0,*)"ogrid%az =", ogrid%az%n,ogrid%az%o,ogrid%az%d
          write(0,*)"----------------------------------------------"

!         initialize the extrapolator
!         ---------------------------
          stat = wem_wfdexp_init(kx2, ky2, pgrid%ax%n, pgrid%ay%n, pgrid%ax%d, pgrid%ay%d, pgrid%az%d, &
                                 pgrid%pdip, pgrid%nvrf, pgrid%oper, pgrid%trick)

!         initialize the reference velocity
!         --------------------------------- 
          stat = wem_velref_init(pgrid%nvrf)
          call wem_fio_velo(vmod, vgrid, pgrid, 1, pgrid%az%n, ftag_%vmod_)
          do izs=1, pgrid%az%n
             stat = wem_velref_oper(vmod(:,:,izs), vref(:,izs))
          end do

!         initialize the imaging condition
!         --------------------------------
          stat = wem_imgcon_init(igrid, ogrid, pgrid, dgrid%aw%n, dgrid%nshg)

          do iwb=1, wblock%nb
             aws%o = wblock%os(iwb); aws%d = wblock%ds; aws%n = wblock%blen(iwb)
             wfds = 0.; wfdr = 0.

!            read the source wavefield
!            -------------------------
             call wem_fio_csou(csou, dgrid, wblock%indx(iwb), aws%n, ish, ftag_%csou_, "reed")

!            map the source wavefield onto the propagation grid
!            --------------------------------------------------
             call wem_wfs_init(.true., .false., wfds, csou, dgrid, pgrid)

!            loop over frequency
!            -------------------
             !$OMP PARALLEL DO SCHEDULE(DYNAMIC,OMPCHUNK) DEFAULT(SHARED) PRIVATE(iws,izs,ith,ws)
             do iws=1, aws%n
                ith = omp_get_thread_num() + 1
                ws  = aws%o + (iws-1)*aws%d
                if (iws/nwstep*nwstep==iws) write(0,*)"iws=", iws, ws, ith

!               for depth=0
!               -----------
                wfds_slice(:,:,ith) = wfds(:,:,1,iws)

!               loop over depth
!               ---------------
                do izs=2, pgrid%nzstep !pgrid%az%n

!                  downward continue the source wavefield
!                  --------------------------------------
                   stat = wem_wfdexp_oper(wfds_slice(:,:,ith), vmod(:,:,izs-1), vref(:,izs-1), ws, +1,                     &
                          iwrk2d(:,:,ith), rwrk1d(:,ith), cwrk1d(:,ith), cwrk3d(:,:,:,ith), plan_2dsF(ith), plan_2dsI(ith))

!                  apply the taper to the source wavefield
!                  ---------------------------------------
                   stat = wem_tapers_oper(wfds_slice(:,:,ith), pgrid%ax%n, pgrid%ay%n)

!                  store the source wavefield
!                  --------------------------
                   wfds(:,:,izs,iws) = wfds_slice(:,:,ith)
                end do

!               apply the adjoint of imaging condition
!               --------------------------------------
                !$OMP CRITICAL
                stat = wem_imgcon_oper(.true., modl, wfds(:,:,:,iws), wfdr(:,:,:,iws), ws, "rec", imag_)
                !$OMP END CRITICAL

!               loop over depth
!               ---------------
                do izs=pgrid%nzstep, 2, -1

!                  D_{n-1} = D_{n} + P_{n-1}
!                  -------------------------
                   wfdr_slice(:,:,ith) = wfdr(:,:,izs,iws)

!                  upward continuation of the receiver wavefield: D_{n-1} = E_{n-1}D_n
!                  -------------------------------------------------------------------
                   stat = wem_wfdexp_oper(wfdr_slice(:,:,ith), vmod(:,:,izs-1), vref(:,izs-1), ws, +1,                     &
                          iwrk2d(:,:,ith), rwrk1d(:,ith), cwrk1d(:,ith), cwrk3d(:,:,:,ith), plan_2drF(ith), plan_2drI(ith))

!                  apply the taper to the receiver wavefield
!                  -----------------------------------------
                   stat = wem_tapers_oper(wfdr_slice(:,:,ith), pgrid%ax%n, pgrid%ay%n)

!                  store the receiver wavefield
!                  ----------------------------
                   wfdr(:,:,izs-1,iws) = wfdr(:,:,izs-1,iws) + wfdr_slice(:,:,ith)
                end do
             end do
             !$OMP END PARALLEL DO

             if (add) call wem_fio_crec(crec, dgrid, wblock%indx(iwb), aws%n, ish, crec_, "reed")

!            map the receiver wavefield from the propagation grid back to the receiver array
!            ------------------------------------------------------------------------------- 
             call wem_wfr_init(.false., add, wfdr, crec, dgrid, pgrid)

!            write out the receiver wavefield
!            --------------------------------
             call wem_fio_crec(crec, dgrid, wblock%indx(iwb), aws%n, ish, crec_, "rite")
          end do

!         clean up stuff
!         -------------- 
          stat = wem_velref_clos()
          stat = wem_wfdexp_clos()
          stat = wem_imgcon_clos()
       end do
    end if

    call wem_fftw_clos(plan_2dsF, plan_2dsI, nth)
    call wem_fftw_clos(plan_2drF, plan_2drI, nth)

    stat = wem_tapers_clos()

    deallocate(modl)
    deallocate(csou, crec)
    deallocate(wfds, wfdr)
    deallocate(vmod, vref)
    deallocate(wfds_slice)
    deallocate(wfdr_slice)
    deallocate(tapx, tapy)
    deallocate(kx2 , ky2 )
    deallocate(plan_2dsF, plan_2dsI)
    deallocate(plan_2drF, plan_2drI)

    stat = 0
  end function

end module

