module srm_mod
  !! Stolt Residual Migration
  !! Paul Sava (paul@sep.stanford.edu)
  !! 12/1/00
  use sep
  use sep3d_struct_mod
  use utilities_mod
  use srm_storm_mod
  implicit none
  logical, private , save :: verb_in
  type(paxis) :: az,ahx,ahy,amx,amy,ar
  integer, parameter:: dbl=selected_int_kind(15)
  logical, private, save :: verb,push, scale
  real,private,save        :: ro, eps
  complex, private,dimension(:,:,:,:), pointer :: data
  real,private,save    :: maxmem    !! max memory available  (Mb)
  real,private,save    :: gathermem !! memory for one gather (Mb)
  integer,private,save :: nth,      ith
  integer,private,save :: nblocks,  iblocks
  integer(kind=dbl),private,save :: ngathers, igathers, allgathers


contains 

logical function init_srm_params(ngam,ogam,dgam)
integer :: ngam
real    :: ogam,dgam
  call from_param("ngam",ngam,41)
  call from_param("dgam",dgam,.0025)
  call from_param("ogam",ogam,.9)
init_srm_params=.true.
end function

logical function init_srm(input)
  type(sep3d) :: input
  integer :: stat
  init_srm=.false.
  call axis_setup_s(input,1,az)
  call axis_setup_s(input,2,ahx)
  call axis_setup_s(input,3,ahy)
  call axis_setup_s(input,4,amx)
  call axis_setup_s(input,5,amy)
  
  call from_param("scale",scale,.false.)
  call from_param("eps_srm",eps,0.5)

  !! total # of gathers
  allgathers = amy%n * amx%n

  nth=1
  call storm_srm_init(nth,az,ahx,ahy,amx,amy,eps,push,scale)

  init_srm=.true.
end function

subroutine axis_setup_s(struct,iax,axis)
 type(sep3d) :: struct
 integer :: iax
 type(paxis) :: axis
   if(iax <= sep3d_ndims(struct)) then
   axis%n=struct%n(iax)
   axis%o=struct%o(iax)
   axis%d=struct%d(iax)
   else
     axis%o=0
     axis%d=1.
     axis%n=1
  end if
end subroutine


logical function srm_it(data,ro)
complex  :: data(:,:,:,:,:)
integer :: ir,imy,iblocks,imx
real :: ro 

srm_it=.false.
  call storm_srm_compute(data,ro,az,ahx,ahy)
srm_it=.true.
end function


logical function srm_clean()
integer :: stat
srm_clean=.false.
   call storm_srm_close()
srm_clean=.true.
end function



end module
