anchor.diff:          allocate(rowmeans(nx)) !this is only used when ydim=3, so nz is really ny
           nrows=windowsize*2+1
           ncols=windowsize*2+1
  +
           ! $omp do schedule(static)
           do j=1,ny
   
               ! so we pre-compute the sum over rows for each column in the current window
               if (ydim==3) then
                   rowsums=inputwind(1:nx,j,1)*(windowsize+1)
  -                do i=1,windowsize
  +                do i=1, min(windowsize,nz)
                       rowsums=rowsums+inputwind(1:nx,j,i)
                   enddo
  +                if (windowsize > nz) then
  +                    rowsums = rowsums + inputwind(1:nx,j,nz) * (windowsize-nz)
  +                endif
               endif
               ! don't parallelize over this loop because it is much more efficient to be able to assume
               ! that you ran the previous row in serial for the slow (ydim=3) case
  
positive.code:                     ! if we are pinned to the top edge
                      if ((k-windowsize)<=1) then
                          starty=1
                          endy  =k+windowsize
                          rowsums=rowsums-inputwind(1:nx,j,starty)+inputwind(1:nx,j,endy)
                      ! if we are pinned to the bottom edge
                      else if ((k+windowsize)>nz) then
                          starty=k-windowsize
                          endy  =nz
                          rowsums=rowsums-inputwind(1:nx,j,starty-1)+inputwind(1:nx,j,endy)
                      ! if we are in the middle (this is the most common)
                      else
                          starty=k-windowsize
                          endy  =k+windowsize
                          rowsums=rowsums-inputwind(1:nx,j,starty-1)+inputwind(:,j,endy)
                      endif
                      rowmeans=rowsums/nrows
  
negative.code:                         ! if we are pinned to the left edge
                          if ((i-windowsize)<=1) then
                              startx=1
                              endx  =min(nx,i+windowsize)
                              cursum=cursum-rowmeans(startx)+rowmeans(endx)
                          ! if we are pinned to the right edge
                          else if ((i+windowsize)>nx) then
                              startx=max(2,i-windowsize)
                              endx  =nx
                              cursum=cursum-rowmeans(startx-1)+rowmeans(endx)
                          ! if we are in the middle (this is the most common)
                          else
                              startx=max(2,i-windowsize)
                              endx  =min(nx,i+windowsize)
                              cursum=cursum-rowmeans(startx-1)+rowmeans(endx)
                          endif
  
  