anchor.diff:              return
           endif
   
  -        start_z = minval(z_bottom)
  -        end_z = maxval(z_top)
  -
  -        allocate(internal_z_top(size(lt_data%u_perturb,1),size(lt_data%u_perturb,2)))
  -        internal_z_top = maxval(z_top)
  -        internal_z_top(buffer:buffer+size(z_top,1)-1, buffer:buffer+size(z_top,2)-1) = z_top(:,:)
  -
  -        allocate(internal_z_bottom(size(lt_data%u_perturb,1),size(lt_data%u_perturb,2)), source=maxval(z_bottom))
  +        nx = size(z_top,1)
  +        ny = size(z_top,2)
  +
  +        ! print*, this_image(), "minval(z_bottom), maxval(z_top)", minval(z_bottom), maxval(z_top)
  +
  +        start_z = minval(z_bottom(2:nx-1,2:ny-1))
  +        end_z = maxval(z_top(2:nx-1,2:ny-1))
  +        ! print*, this_image(), start_z, end_z
  +        allocate(internal_z_top(size(lt_data%u_perturb,1),size(lt_data%u_perturb,2)), source=maxval(z_top))
  +        ! internal_z_top = maxval(z_top(2:nx-1,2:ny-1))
  +        ! internal_z_top(buffer+1:buffer+size(z_top,1)-2, buffer+1:buffer+size(z_top,2)-2) = z_top(2:nx-1,2:ny-1)
  +        internal_z_top(buffer:buffer+size(z_top,1)-1, buffer:buffer+size(z_top,2)-1) = z_top
  +
  +        allocate(internal_z_bottom(size(lt_data%u_perturb,1),size(lt_data%u_perturb,2)), source=minval(z_bottom))
  +        ! internal_z_bottom(buffer+1:buffer+size(z_bottom,1)-2, buffer+1:buffer+size(z_bottom,2)-2) = z_bottom(2:nx-1,2:ny-1)
           internal_z_bottom(buffer:buffer+size(z_bottom,1)-1, buffer:buffer+size(z_bottom,2)-1) = z_bottom
   
           allocate(layer_count(size(lt_data%u_perturb,1), size(lt_data%u_perturb,2)))
  
positive.code:                 u1d(i)  = sum(u3d(i+ims_u-1,:,uk+jms-1)) / nz
                  v1d(i)  = sum(v3d(vi+ims-1, :,k+jms_v-1)) / nz
              enddo
  
  
              do j=1, nz
                  do i=1, nxu
  
  
negative.code:         complex(C_DOUBLE_COMPLEX),intent(in)    :: fourier_terrain(:,:) ! FFT(terrain)
          type(linear_theory_type), intent(inout) :: lt_data          ! intermediate arrays needed for LT calc
  
          integer :: n_steps, i, nx,ny
          real    :: step_size, current_z, start_z, end_z
          real,   allocatable :: layer_count(:,:), layer_fraction(:,:)
          real,   allocatable :: internal_z_top(:,:), internal_z_bottom(:,:)
  