module chain_me_mod
 use chain_mod
 use mask1
  use npolydiv
 use nhelicon
 use nhelicon2
implicit none
real,pointer :: t(:),t2(:)

contains
  subroutine chain_me_init(n, msk, nfilt_p,nfilt_h)
    integer :: n
    logical, pointer :: msk(:)
    type(nfilter) :: nfilt_h,nfilt_p
   
   allocate(t(n),t2(n))
    call mask1_init(msk)
    call npolydiv_init(n,nfilt_p)
    call nhelicon_init(nfilt_h)
    

  end subroutine

  subroutine chain_me_close()
     call mask1_close()
     call npolydiv_close()
     call nhelicon_close()
     deallocate(t,t2)
  end subroutine

  integer function chain_me_reg(adj,add,m,d)
    logical,intent(in) :: adj,add
    real :: m(:),d(:)
   ! chain_me_reg=nhelicon_lop(adj,add,m,d)
   call chain2(nhelicon_lop,prec_chain,adj,add,m,d,t)
     chain_me_reg=0
  end function
  integer function prec_chain(adj,add,m,d)
    logical,intent(in) :: adj,add
    real :: m(:),d(:)
         call chain2(nhelicon_lop,npolydiv_lop,adj,add,m,d,t2)
    if(.not. adj) then
      ! call srite("ff.H",m,size(m)*4)
      ! call srite("ff.H",d,size(m)*4)
    end if
    prec_chain=0
   !  chain_me_lop=mask1_lop(adj,add,m,d)
  end function
  integer function chain_me_lop(adj,add,m,d)
    logical,intent(in) :: adj,add
    real :: m(:),d(:)
     call chain2(mask1_lop,prec_chain,adj,add,m,d,t)
     chain_me_lop=0
   !  chain_me_lop=mask1_lop(adj,add,m,d)
  end function

  subroutine to_model(p,m)
    real :: m(:),p(:)
    integer :: i

    i =prec_chain(.false.,.false.,p,m)
!m=p
   end subroutine
      




end module
