anchor.diff:                              npos=step
                           endif
                       end do
  -                    
  +
                       top=min(j+1,nz)
                       if (top==j) then
                           bottom=j-1
                       else
                           bottom=j
                       endif
  -                    
  +
                       ! calculate the weights and the "next" u/v position
                       ! "next" usually = pos+1 but for edge cases next = 1 or n
                       dweight=calc_weight(dir_values, dpos,nextd,curdir)
  
positive.code:                     south = max(k-winsz,1)
                      north = min(k+winsz,ny)
                      n = (((east-west)+1) * ((top-bottom)+1) * ((north-south)+1))
                      
                      curnsq = sum(log(domain%nsquared(west:east, bottom:top,south:north)))
                      curnsq = curnsq / n
  
  
negative.code:                 do i=1,nx
                      xs=max(1, i-window)
                      xe=min(nx,i+window)
  
                      ys=max(1, buffer-j+1-window)
                      ye=min(ny,buffer-j+1+window)
  
                      buffer_topo(i,buffer-j+1)=sum(buffer_topo(xs:xe,ys:ye))/((xe-xs+1) * (ye-ys+1))
  
                      ys=max(1, ny-(buffer-j)-window)
                      ye=min(ny,ny-(buffer-j)+window)
  
                      buffer_topo(i,ny-(buffer-j))=sum(buffer_topo(xs:xe,ys:ye))/((xe-xs+1) * (ye-ys+1))
                  end do
                  ! smooth the left and right borders
  