
module shaping_op_mod
use adj_mod
use sep
use chain_mod
use sep3d_struct_mod
real, pointer,private :: temp(:)

contains
logical function load_shaping_op(slow_s)
integer :: rect1,rect2
type(sep3d) :: slow_s,dip_s
real, allocatable :: dip(:)
real :: eps
integer :: nmod
load_shaping_op=.false.
call from_param("rect1",rect1,3)
call from_param("rect2",rect2,3)
call from_param("eps_shape",eps,.01)

nmod=product(slow_s%n(1:sep3d_ndims(slow_s)))
                                                                                
  allocate(dip(nmod))
!if(-1==auxin("dip")) then
! call seperr("must provide the dip")
!else
  call init_sep3d("dip",dip_s,"INPUT")
  if(product(dip_s%n) /= size(dip)) then
    write(0,*) "size of dip must be the same as the size of the slowness space"
    return
  end if
                                                                                
  if(4*size(dip)/=sreed("dip",dip,size(dip)*4)) then
    write(0,*) "trouble reading in dip"
    return
  end if
!end if

!dip=0

allocate(temp(size(dip)))
call SF_PWDSL_INIT(dip_s%n(1),dip_s%n(2),rect1,rect2,eps)
call SF_TRIANGLEN_INIT(2,(/rect1,rect2/),dip_s%n)
call SF_PWDSL_DIP_SET(dip,dip_s%n(1),dip_s%n(2))

load_shaping_op=.true.

end function

integer function shaping_op(adj,add,model,data)
logical,intent(in) :: adj,add
real :: model(:),data(:)


!call chain(smooth_dip_op,shaping_dip_op,adj,add,model,data,temp)
write(0,*) "CHECK SIZE",size(temp),size(model),size(temp)
!shaping_op=smooth_dip_op(adj,add,model,data)
shaping_op=shaping_dip_op(adj,add,model,data)
shaping_op=0
end function

integer function smooth_dip_op(adj,add,model,data)
logical,intent(in) :: adj,add
real :: model(:),data(:)
integer ad,aj

smooth_dip_op=0
ad=0; aj=0
if(adj) aj=1.
if(add) ad=1.
call SF_TRIANGLEN_LOP(ad,aj,size(model),size(data),model,data)
end function

integer function shaping_dip_op(adj,add,model,data)
logical,intent(in) :: adj,add
real :: model(:),data(:)
integer ad,aj

shaping_dip_op=0
ad=0; aj=0
if(adj) aj=1.
if(add) ad=1.
call SF_PWDSL_LOP(ad,aj,size(model),size(data),model,data)
end function


end module
