module wemva_kerspl_mod
  use  wemva_struct_mod
  use  wemva_basfnc_mod
  use  wemva_kerpix_mod
  use  oc_chains_mod
  implicit none

  type(vel_grid)  , private :: vgrid
  type(img_grid)  , private :: igrid
  type(wemva_ftag), private :: ftag_
contains 

  integer function wemva_kerspl_init(vgrid_out, igrid_out, ftag_out_) result(stat)
    type(vel_grid)   :: vgrid_out
    type(img_grid)   :: igrid_out
    type(wemva_ftag) :: ftag_out_

    stat = wemva_kerpix_init(vgrid, igrid, ftag_)

    ftag_%dspl_ = "dspl"
    call auxinout(ftag_%dspl_)

    vgrid_out = vgrid
    igrid_out = igrid
    ftag_out_ = ftag_
    stat= 0 
  end function

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

  integer function wemva_kerspl_oper(adj, add, dspl_, dimg_) result(stat)
    logical          :: adj, add
    character(len=*) :: dspl_, dimg_

    type(axis)       :: axk, ayk, azk

    if (adj) then 
       call from_param("oxk", axk%o, 0.)
       call from_param("dxk", axk%d, 1.)
       call from_param("nxk", axk%n, 1 )

       call from_param("oyk", ayk%o, 0.)
       call from_param("dyk", ayk%d, 1.)
       call from_param("nyk", ayk%n, 1 )

       call from_param("ozk", azk%o, 0.)
       call from_param("dzk", azk%d, 1.)
       call from_param("nzk", azk%n, 1 ) 
    
       call houaxis(dspl_, axk, 1)
       call houaxis(dspl_, ayk, 2)
       call houaxis(dspl_, azk, 3)

    else

       call hinaxis(dspl_, axk, 1)
       call hinaxis(dspl_, ayk, 2)
       call hinaxis(dspl_, azk, 3)
       call houaxis(ftag_%dslw_, vgrid%ax, 1)
       call houaxis(ftag_%dslw_, vgrid%ay, 2)
       call houaxis(ftag_%dslw_, vgrid%az, 3)
    end if

    stat = wemva_basfnc_init(vgrid%ax,vgrid%ay,vgrid%az,axk,ayk,azk)

    call oc_chain2_oper(op1=wemva_kerpix_oper, op2=wemva_basfnc_oper, &
                       adj=adj, add=add, m_=dspl_, d_=dimg_, t1_=ftag_%dslw_)

    stat = 0
  end function
end module
