module patch {                     # N-dimensional patch extract and putback;
use cartesian
integer, private                :: ipatch	  # count till product(npatch)
integer, dimension (:), pointer :: npatch,  nwind,  nwall
                # overlap where    npatch * nwind > nwall
#% _init( npatch, nwall, nwind)
ipatch = 1
#% _lop ( wall, wind) 
  integer, dimension(:),allocatable :: ii, jj	     # Ndim=size(npatch)
  integer                            :: i, j, shift
		allocate(ii(size(npatch)),jj(size(npatch)))
  call line2cart(  npatch, ipatch, jj )  # (j1,j2) = (1,1) to (npatch1,npatch2)
	      # round jj to shift end of last patch flush against the far wall.
  where( npatch==1) { jj = 1   }	
  elsewhere         { jj = 1.5 +( nwall - nwind)*( jj - 1.)/( npatch - 1.)}
  call cart2line( nwall,   jj, shift)	 # jj is where the patch begins.
  shift -= 1                             # 
  do i  = 1, size( wind) { 		 # sweep through a window
     call line2cart(  nwind, i, ii)      #   ii(i)   is (i1,i2) in window
     call cart2line(  nwall,    ii, j)   # j(ii)     linear subscript in window
     if( adj)
	wall( j + shift) += wind( i)
     else 
        wind( i)         += wall( j + shift)
  }
	deallocate(ii,jj)
#% _close
ipatch = ipatch + 1
}
