def rreplace(s, old, new, occurrence=1):
    """ Replace portion of a string (from the right side). Handy utility to
        change a filename extension, e.g. rreplace('a/b/c.fits', 'fits', 'im') -> 'a/b/c.im'

        Parameters
        ----------
        s : str
            The string to process

        old : str
            The substring to replace

        new : str
            Whet to replace the 'old' substring with

        occurrence : int
            The number of occurrences to replace
            Default: 1
    """
    li = s.rsplit(old, occurrence)
    return new.join(li)