import torch
import math


def _get_survey_extents_one_side(pad, side, source_locations,
                                 receiver_locations, shape, dx):
    """Get the survey extent for the left or right side of one dimension.

    Args:
        pad: Positive float specifying padding for the side
        side: 'left' or 'right'
        source/receiver_locations: Tensor with coordinates for the current
            dimension
        shape: Int specifying length of full model in current dimension
        dx: Float specifying cell spacing in current dimension

    Returns:
        Min/max index as int or None
    """
    if pad is None:
        return None
    if side == 'left':
        pad = -pad
        op = torch.min
        nearest = math.floor
    else:
        pad = +pad
        op = torch.max
        nearest = math.ceil
    extreme_source = op(source_locations + pad)
    extreme_receiver = op(receiver_locations + pad)
    extreme_cell = nearest(op(extreme_source, extreme_receiver).item() / dx)
    if side == 'right':
        extreme_cell += 1
    if (extreme_cell <= 0) or (extreme_cell >= shape):
        extreme_cell = None
    return extreme_cell