module wei_sg_basic_scale_mod
  use sep
  use wei_step_mod
  use wei_kmap_mod
  use wei_dsr_mod
  use wei_damp_mod
  use wei_slow_mod
  use wei_wavespace_scale_mod
  implicit none
  integer, private, save :: nw_tot


  contains

  subroutine wei_sg_basic_init(wave)
    type(wspace)   :: wave
#ifdef DEBUG
    write(0,*) "IN WEI SG BASIC INIT"
#endif
    call from_param("nw_tot",nw_tot)

    call setup_sg_axes()   !We are doing shot receiver migration


    call dsr_init()  !Initialize lookup table
    call init_damping(wave)
    call weikmap_init(wave)  !Initialize kmaps

    call from_param("nzb",wave%nzb,1) !Depth block
    call init_one_slow(wave,"S","ref_slow")

#ifdef DEBUG
    write(0,*) "OUT WEI SG BASIC INIT"
#endif
  end subroutine


  subroutine s_g_basic_image(step,wfld,image)
    type(westep)   :: step
    complex        :: wfld(:,:,:,:,:),image(:,:,:,:,:)
    integer        :: i1,i2,i3,i4
#ifdef DEBUG
    write(0,*) "IN SG BAISC IMAGE"
#endif

    if(step%wave%adj) then !migration
      do i4=1,size(wfld,4)
        do i3=1,size(wfld,3)
          do i2=1,size(wfld,2)
            do i1=1,size(wfld,1)
              image(i1,i2,i3,i4,step%iz)=image(i1,i2,i3,i4,step%iz)+wfld(i1,i2,i3,i4,step%iw)/nw_tot
            end do
          end do
        end do
      end do
    else
      do i4=1,size(wfld,4)
        do i3=1,size(wfld,3)
          do i2=1,size(wfld,2)
            do i1=1,size(wfld,1)
              wfld(i1,i2,i3,i4,step%iw)=wfld(i1,i2,i3,i4,step%iw)+image(i1,i2,i3,i4,step%iz)/nw_tot
            end do
          end do
        end do
      end do
    end if
    

#ifdef DEBUG
    write(0,*) "OUT SG BAISC IMAGE"
#endif


  end subroutine
  



end module
