def apply_ROI(data, logfile, top=0, left=0, right=None, bottom=None):
    """
    Apply an ROI to an image or a series of images.

    Args:
        data (2D or 3D array): The source data.
        top (int): the index of the top edge of the ROI.
        left (int): the index of the left edge of the ROI.
        right (int): the index of the right edge of the ROI.
        bottom (int): the index of the bottom edge of the ROI.
    """
    N = len(data.shape)
    if N == 2:
        nx, ny = data.shape
        if right == None:
            right = nx
        if bottom == None:
            bottom = ny
        data_ROI = data[left:right, top:bottom]
    elif N == 3:
        _, nx, ny = data.shape
        if right == None:
            right = nx
        if bottom == None:
            bottom = ny
        data_ROI = data[:, left:right, top:bottom]
    else:
        raise Exception('ROI only defined for 2D and 3D arrays')
    logfile['detector']['ROI_software'] = {}
    logfile['detector']['ROI_software']['top'] = top
    logfile['detector']['ROI_software']['left'] = left
    logfile['detector']['ROI_software']['right'] = right
    logfile['detector']['ROI_software']['bottom'] = bottom
    return data_ROI, logfile
