<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def update_scenario(scenario,update_data=True,update_groups=True,flush=True,**kwargs):
 """ Update a single scenario as all resources already exist, there is no need to worry about negative IDS flush = True flushes to the DB at the end of the function. flush = False does not flush, assuming that it will happen as part of another process, like update_network. """

    user_id = kwargs.get('user_id')
    scen = _get_scenario(scenario.id, user_id)

    if scen.locked == 'Y':
        raise PermissionError('Scenario is locked. Unlock before editing.')

    start_time = None
    if isinstance(scenario.start_time, float):
        start_time = six.text_type(scenario.start_time)
    else:
        start_time = timestamp_to_ordinal(scenario.start_time)
        if start_time is not None:
            start_time = six.text_type(start_time)

    end_time = None
    if isinstance(scenario.end_time, float):
        end_time = six.text_type(scenario.end_time)
    else:
        end_time = timestamp_to_ordinal(scenario.end_time)
        if end_time is not None:
            end_time = six.text_type(end_time)

    scen.name                 = scenario.name
    scen.description          = scenario.description
    scen.layout               = scenario.get_layout()
    scen.start_time           = start_time
    scen.end_time             = end_time
    scen.time_step            = scenario.time_step

    if scenario.resourcescenarios == None:
        scenario.resourcescenarios = []
    if scenario.resourcegroupitems == None:
        scenario.resourcegroupitems = []

    #lazy load resourcescenarios from the DB
    scen.resourcescenarios

    if update_data is True:
        datasets = [rs.dataset for rs in scenario.resourcescenarios]
        updated_datasets = data._bulk_insert_data(datasets, user_id, kwargs.get('app_name'))
        for i, r_scen in enumerate(scenario.resourcescenarios):
            _update_resourcescenario(scen, r_scen, dataset=updated_datasets[i], user_id=user_id, source=kwargs.get('app_name'))

    #lazy load resource grou items from the DB
    scen.resourcegroupitems

    if update_groups is True:
        #Get all the exiting resource group items for this scenario.
        #THen process all the items sent to this handler.
        #Any in the DB that are not passed in here are removed.
        for group_item in scenario.resourcegroupitems:
            _add_resourcegroupitem(group_item, scenario.id)

    if flush is True:
        db.DBSession.flush()

    return scen