module image_gather_mod

  use type_mod

  type(axis), private                           :: ahx, ahy
  integer, private                              :: nw

contains

  subroutine image_gather_init(ahx_in, ahy_in, nw_in)
    type(axis)                         :: ahx_in, ahy_in
    integer                            :: nw_in

    ahx = ahx_in
    ahy = ahy_in
    nw  = nw_in


  end subroutine

  subroutine multi_off_img(adj, add, wfld_d, wfld_u, model)
    logical, intent(in)                    :: adj, add
    complex, dimension(:,:,:)  , pointer   :: wfld_d, wfld_u
    real,    dimension(:,:), pointer   :: model

    integer                                :: ihx, ihy, iw, itx, ity
    integer                                :: fx1, fx2, jx1, jx2
    integer                                :: fy1, fy2, jy1, jy2
    integer                                :: i1, i2
    real                                   :: fac

    fac = 2.0


!    write(0,*)".... imaging condition ...."
!    write(0,*)"ahx:", ahx%o, ahx%d, ahx%n
!    write(0,*)"ahy:", ahy%o, ahy%d, ahy%n
!    write(0,*)"nw :", nw    

    do iw=1, nw
       model = model + real(conjg(wfld_d(:,:,iw)) * wfld_u(:,:,iw))
    end do

!    do ihx=1, ahx%n
!       itx = ihx + ahx%o -1
!       itx = fac * itx
!       fx1 = - floor((real(itx)+0.1)/fac)
!       fx2 = fx1 + itx
!       jx1 = max(1, max(1-fx1, 1-fx2))
!       jx2 = min(size(wfld_u, 1), min(size(wfld_u, 1) - fx1, size(wfld_u, 1) - fx2))

!       do ihy=1, ahy%n
!          ity = ihy + ahy%o -1
!          ity = fac * ity
!          fy1 = - floor((real(ity)+0.1)/fac)
!          fy2 = fy1 + ity
!          jy1 = max(1, max(1-fy1, 1-fy2))
!          jy2 = min(size(wfld_u, 2), min(size(wfld_u, 2) - fy1, size(wfld_u, 2) - fy2))

!          do iw=1, nw
!             model(jx1:jx2,jy1:jy2,ihx,ihy) = model(jx1:jx2,jy1:jy2,ihx,ihy) &
!                                            + wfld_u(jx1+fx2:jx2+fx2,jy1+fy2:jy2+fy2,iw) &
!                                            * wfld_d(jx1+fx1:jx2+fx1,jy1+fy1:jy2+fy1,iw)
!          end do

!       end do

!    end do

  end subroutine

end module
