def modify_fields(trajectory, fields=None, include=None, exclude=None):
    """
    Modify fields of a trajectory.

    Either provide a new list of fields, such as ['id', 'x', 'y'], or
    specify explicit patterns to exclude or include.
    """
    if fields is not None:
        trajectory.fields = fields
    else:
        if exclude is not None:
            for pattern in exclude:
                if pattern in trajectory.fields:
                    trajectory.fields.remove(pattern)
        if include is not None:
            for pattern in include:
                if pattern not in trajectory.fields:
                    trajectory.fields.append(pattern)
    return trajectory
