module mis2 {
  use mask1 + helicon + polydiv + cgstep_mod 
  use solver_smp_mod + solver_prc_mod
contains
# fill in missing data by minimizing power out of a given filter.
# by helix magic works in any number of dimensions
  subroutine mis1( niter, xx, aa, known, doprec) {
    logical,                intent( in)     :: doprec
    integer,                intent( in)     :: niter
    type( filter),          intent( in)     :: aa
    logical, dimension( :), intent( in)     :: known
    real,    dimension( :), intent( in out) :: xx	# fitting variables
    real,    dimension( :), allocatable     :: dd
    logical, dimension( :), pointer         :: msk
    integer                                 :: nx
    nx = size( xx)
    if( doprec) {                          #  preconditioned
       allocate( dd( nx)); dd=xx
       allocate(msk(nx))
       msk = known
       call mask1_init( msk)
       call polydiv_init( nx, aa)
       call solver_prc( m=xx, d=dd, Fop=mask1_lop, stepper=cgstep, niter=niter,
                        Sop=polydiv_lop, nSop=nx, eps=0.)
       call polydiv_close()
       deallocate( msk)
    } else {                               #  regularized
       allocate( dd( nx)); dd = 0.
       allocate( msk( nx))
       msk = .not.known
       call mask1_init( msk)
       call helicon_init( aa)
       call solver_smp( m=xx, d=dd, Fop=helicon_lop, stepper=cgstep, niter=niter, 
                        Jop=mask1_lop, m0=xx)
       deallocate( dd)
       deallocate(msk)
    }
    call cgstep_close()
  }
}


