def modify_ha(old_ha, mod_ha, path):
    """
    Construct a new hybrid automaton that contains only those new locations and transitions in the modified automaton
    that are necessary along the given path.

    :param old_ha: old hybrid automaton
    :param mod_ha: modified hybrid automaton
    :param path: path in the modified hybrid automaton
    :return: new hybrid automaton (a modification of 'old_ha')
    """
    new_ha = old_ha.copy()

    for loc in path:

        if loc != None:

            inv = mod_ha.get_invariant(loc)

            if loc not in new_ha.locations:
                # add location
                new_ha.add_location(loc)
                new_ha.set_flow(loc, mod_ha.get_flow(loc), rounding=True)

            # set/overwrite the invariant
            new_ha.set_invariant(loc, inv)

    for loc1, loc2 in zip(path[:-1], path[1:]):

        if loc1 != None and loc2 != None:

            trans = (loc1, loc2)
            grd = mod_ha.get_guard(trans)

            if trans not in new_ha.edges:
                # add transition
                new_ha.add_edge(loc1, loc2)

            # set/overwrite the guard
            new_ha.set_guard(trans, grd)

    return new_ha