# 1 "<stdin>"
# 1 "<built-in>"
# 1 "<command line>"
# 1 "<stdin>"
module inter_fill2x
  use sep
  use matmult
  use solver_smp_mod
  use cgstep_mod
implicit none
  contains
!--------
  subroutine inter_fillx(nw1, nw2, f, size, prep, h, ct, win2x_mis,&
    & win2x_interp, alpha)
! ----definitions--------------------
    integer :: prep,i2,nw1,nw2,j2,f,a1,a2,i1,h,lenw1,lenw2,f2,f1,kx,ky&
      &,k1,k2,ct,ci,size,p,l,e2,e1
    real, dimension(:,:), pointer :: win2x_mis, win2x_interp
    real, dimension(:), allocatable :: pos, temp, c1, c2
    real, dimension(:), pointer :: alpha
    real :: energy_factor, sum_alpha
! ----end definitions----------------
    allocate (pos(nw2), c1(nw2), c2(nw2))
    win2x_interp = win2x_mis
! Generating a Position vector to indicate the position of missing columns. 1-known, 0-missing
    write(0,*), "No zero slope Bondary Condition in 2D"
    allocate(temp(nw1))
    do j2 = 1,nw2
      temp = win2x_interp(1:nw1,j2)
      if (sum(temp).eq.0 ) then
        pos(j2)=0
      else
        pos(j2)=1
      end if
    end do
! Sum of all components of alpha not equal to one, so normalization
!sum_alpha = sum(alpha)
!alpha = alpha*(1./sum_alpha)
!write(0,*), alpha
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
! Filling in the missing columns
    if (prep.eq.1) then
      do a1 = 1,nw1
        do a2 = 1+f+(size-1)*2,nw2-f-(size-1)*2
          if (pos(a2).eq.0) then
            do p=1,size
              win2x_interp(a1,a2) = alpha(1+(p-1)*4)*win2x_interp(a1-f-(p-1),a2-f) + alpha(2+(&
                &p-1)*4)*win2x_interp(a1-f-(p-1),a2+f) + alpha(3+(p-1)*4)*win2x_interp(a1+f+(p-1),a2-f) + alpha(4+(p-1)*4)&
                &*win2x_interp(a1+f+(p-1),a2+f)
            end do
          end if
        end do
      end do
! Energy Equalization - trying
      do e2 = 1+f+(size-1)*2,nw2-f-(size-1)*2
        if (pos(e2).eq.0) then
          do e1=1,nw1
            if (win2x_interp(e1,e2).ne.0) then
              energy_factor = ((win2x_interp(e1-1,e2-1)**2+ win2x_interp(e1+1,e2-1)**2 + win2x_interp(e1-1,e2+1)**2 + win2x_interp(e1+1,e2+1)**2)/((4*win2x_interp(e1,e2)**2)))*&
                &*.5
! win2x_interp(e1,e2) = energy_factor*win2x_interp(e1,e2)
            end if
          end do
        end if
      end do
    end if
    if (prep.eq.2) then
      do a1 = 1+f,nw1-f
        do a2 = 1+f,nw2-f
          k1=0
          k2=0
          if (pos(a2).eq.0) then
!This is trying to improve filter geometry for big holes
            do kx = a2, a2+f
              if (pos(kx).eq.1) then
                c1(a2)=k1
                exit
              end if
              k1=k1+1
            end do
            do ky = a2, a2-f, -1
              if (pos(ky).eq.1) then
                c2(a2)=k2
                exit
              end if
              k2=k2+1
            end do
!if(c1(a2)>c2(a2)) { c2(a2)=c1(a2) }
!if(c1(a2)<=c2(a2)) { c1(a2)=c2(a2) }
            c1(a2)=f
            c2(a2)=f
!write(0,*), c1(a2), c2(a2)
          end if
        end do
      end do
      do ci = 1,ct
        do a1 = 1+f,nw1-f
          do a2 = 1+f,nw2-f
            if (pos(a2).eq.0) then
              win2x_interp(a1,a2) = alpha(1)*win2x_interp(a1-c2(a2),a2-c2&
                &(a2)) + alpha(2)*win2x_interp(a1-c1(a2),a2+c1(a2)) + alpha(3)*win2x_interp(a1+c2(a2),a2-c2(a2)) + alpha(4)*win2x_interp(a1+c1(a2),a2+c1(a2)) &
            end if
          end do
        end do
      end do
    end if
  end subroutine
end module
