Ejemplo n.º 1
0
def import_file(text, data=None):
    models = elegant_lattice_parser.parse_file(text, max_id(data) if data else 0)
    name_to_id, default_beamline_id = _create_name_map(models)
    if 'default_beamline_name' in models and models['default_beamline_name'] in name_to_id:
        default_beamline_id = name_to_id[models['default_beamline_name']]
    element_names = {}
    rpn_cache = {}

    for el in models['elements']:
        el['type'] = _validate_type(el, element_names)
        element_names[el['name'].upper()] = el
        validate_fields(el, rpn_cache, models['rpnVariables'])

    for bl in models['beamlines']:
        bl['items'] = _validate_beamline(bl, name_to_id, element_names)

    if len(models['elements']) == 0 or len(models['beamlines']) == 0:
        raise IOError('no beamline elements found in file')

    if not data:
        data = simulation_db.default_data(elegant_common.SIM_TYPE)
    data['models']['elements'] = models['elements']
    data['models']['beamlines'] = models['beamlines']
    data['models']['rpnVariables'] = models['rpnVariables']
    elegant_common.sort_elements_and_beamlines(data)

    if default_beamline_id:
        data['models']['simulation']['activeBeamlineId'] = default_beamline_id
        data['models']['simulation']['visualizationBeamlineId'] = default_beamline_id

    # used by synergia app to get values for rpn expressions
    data['models']['rpnCache'] = rpn_cache
    return data
Ejemplo n.º 2
0
def import_file(text, data=None):
    models = elegant_lattice_parser.parse_file(text, max_id(data) if data else 0)
    name_to_id, default_beamline_id = _create_name_map(models)
    if 'default_beamline_name' in models and models['default_beamline_name'] in name_to_id:
        default_beamline_id = name_to_id[models['default_beamline_name']]
    element_names = {}
    rpn_cache = {}

    for el in models['elements']:
        el['type'] = _validate_type(el, element_names)
        element_names[el['name'].upper()] = el
        validate_fields(el, rpn_cache, models['rpnVariables'])

    for bl in models['beamlines']:
        bl['items'] = _validate_beamline(bl, name_to_id, element_names)

    if len(models['elements']) == 0 or len(models['beamlines']) == 0:
        raise IOError('no beamline elements found in file')
    _calculate_beamline_metrics(models, rpn_cache)

    if not data:
        data = default_data()
    data['models']['elements'] = sorted(models['elements'], key=lambda el: el['type'])
    data['models']['elements'] = sorted(models['elements'], key=lambda el: (el['type'], el['name'].lower()))
    data['models']['beamlines'] = sorted(models['beamlines'], key=lambda b: b['name'].lower())
    data['models']['rpnVariables'] = models['rpnVariables']

    if default_beamline_id:
        data['models']['simulation']['activeBeamlineId'] = default_beamline_id
        data['models']['simulation']['visualizationBeamlineId'] = default_beamline_id

    return data
def import_file(text, data=None):
    models = elegant_lattice_parser.parse_file(text,
                                               max_id(data) if data else 0)
    name_to_id, default_beamline_id = _create_name_map(models)
    if 'default_beamline_name' in models and models[
            'default_beamline_name'] in name_to_id:
        default_beamline_id = name_to_id[models['default_beamline_name']]
    element_names = {}
    rpn_cache = {}

    for el in models['elements']:
        el['type'] = _validate_type(el, element_names)
        element_names[el['name'].upper()] = el
        validate_fields(el, rpn_cache, models['rpnVariables'])

    for bl in models['beamlines']:
        bl['items'] = _validate_beamline(bl, name_to_id, element_names)

    if len(models['elements']) == 0 or len(models['beamlines']) == 0:
        raise IOError('no beamline elements found in file')

    if not data:
        data = simulation_db.default_data(elegant_common.SIM_TYPE)
    data['models']['elements'] = sorted(models['elements'],
                                        key=lambda el: el['type'])
    data['models']['elements'] = sorted(models['elements'],
                                        key=lambda el:
                                        (el['type'], el['name'].lower()))
    data['models']['beamlines'] = sorted(models['beamlines'],
                                         key=lambda b: b['name'].lower())
    data['models']['rpnVariables'] = models['rpnVariables']

    if default_beamline_id:
        data['models']['simulation']['activeBeamlineId'] = default_beamline_id
        data['models']['simulation'][
            'visualizationBeamlineId'] = default_beamline_id

    # used by synergia app to get values for rpn expressions
    data['models']['rpnCache'] = rpn_cache
    return data
Ejemplo n.º 4
0
def import_file(text, data=None, update_filenames=True):
    if not data:
        data = simulation_db.default_data(elegant_common.SIM_TYPE)
    models = elegant_lattice_parser.parse_file(
        text,
        data.models.rpnVariables,
        lattice.LatticeUtil.max_id(data),
    )
    name_to_id, default_beamline_id = _create_name_map(models)
    if 'default_beamline_name' in models and models[
            'default_beamline_name'] in name_to_id:
        default_beamline_id = name_to_id[models['default_beamline_name']]
    element_names = PKDict()
    rpn_cache = PKDict()
    code_var = elegant_code_var(models.rpnVariables)

    for el in models['elements']:
        el['type'] = _validate_type(el, element_names)
        element_names[el['name'].upper()] = el
        validate_fields(el, rpn_cache, code_var, update_filenames)

    for bl in models['beamlines']:
        bl['items'] = _validate_beamline(bl, name_to_id, element_names)

    if len(models['elements']) == 0 or len(models['beamlines']) == 0:
        raise IOError('no beamline elements found in file')

    data['models']['elements'] = models['elements']
    data['models']['beamlines'] = models['beamlines']
    data['models']['rpnVariables'] = models['rpnVariables']
    lattice.LatticeUtil(data, _SCHEMA).sort_elements_and_beamlines()

    if default_beamline_id:
        data['models']['simulation']['activeBeamlineId'] = default_beamline_id
        data['models']['simulation'][
            'visualizationBeamlineId'] = default_beamline_id

    # used by synergia app to get values for rpn expressions
    data['models']['rpnCache'] = rpn_cache
    return data