<commit_before>{%- macro field(item, name) %}
v.op_{{ item.name }}_{{ nameMap.get(name, name) }}
{%- endmacro %}
{%- macro height_profile(item) -%}
{%- if item.heightProfileDimension -%}
            mirror_file = {{ field(item, 'heightProfileFile') }}
            assert os.path.isfile(mirror_file), \
                'Missing input file {}, required by {{ item.name }} beamline element'.format(mirror_file)
{% if item.heightProfileDimension == 1 %}
            el.append(srwlib.srwl_opt_setup_surf_height_1d(
                srwlib.srwl_uti_read_data_cols(mirror_file, "\t", 0, 1),
{% else %}
            el.append(srwlib.srwl_opt_setup_surf_height_2d(
                srwlib.srwl_uti_read_data_cols(mirror_file, "\t"),
{% endif %}
                _dim={{ field(item, 'orientation') }},
                _ang=abs({{ field(item, 'grazingAngle') }}),
                _amp_coef={{ field(item, 'heightAmplification') }},
{% if 'horizontalTransverseSize' in item %}
                _size_x={{ field(item, 'horizontalTransverseSize') }},
                _size_y={{ field(item, 'verticalTransverseSize') }},
{% endif %}
            ))
{%- if item.type != 'mirror' %}

            pp.append([0, 0, 1.0, 0, 0, 1.0, 1.0, 1.0, 1.0])
{%- endif -%}
{%- endif -%}
{%- endmacro %}
    el = []
    pp = []
    names = {{ names }}
{% if items|length > 0 %}
    for el_name in names:
{% endif %}
{% for item in items %}
{% if loop.index == 1 %}
        if el_name == '{{ item.name }}':
{% else %}
        elif el_name == '{{ item.name }}':
{% endif %}
            # {{ item.name }}: {{ item.type }} {{ item.position }}m
{% if item.type == 'aperture' %}
            el.append(srwlib.SRWLOptA(
                _shape={{ field(item, 'shape') }},
                _ap_or_ob='a',
                _Dx={{ field(item, 'horizontalSize') }},
                _Dy={{ field(item, 'verticalSize') }},
                _x={{ field(item, 'horizontalOffset') }},
                _y={{ field(item, 'verticalOffset') }},
            ))
            pp.append(v.op_{{ item.name }}_pp)
{% elif item.type == 'crl' %}
            el.append(srwlib.srwl_opt_setup_CRL(
                _foc_plane={{ field(item, 'focalPlane') }},
                _delta={{ field(item, 'refractiveIndex') }},
                _atten_len={{ field(item, 'attenuationLength') }},
                _shape={{ field(item, 'shape') }},
                _apert_h={{ field(item, 'horizontalApertureSize') }},
                _apert_v={{ field(item, 'verticalApertureSize') }},
                _r_min={{ field(item, 'tipRadius') }},
                _n={{ field(item, 'numberOfLenses') }},
                _wall_thick={{ field(item, 'tipWallThickness') }},
                _xc={{ field(item, 'horizontalOffset') }},
                _yc={{ field(item, 'verticalOffset') }},
            ))
            pp.append(v.op_{{ item.name }}_pp)
{% elif item.type == 'crystal' %}
            crystal = srwlib.SRWLOptCryst(
                _d_sp={{ field(item, 'dSpacing') }},
                _psi0r={{ field(item, 'psi0r') }},
                _psi0i={{ field(item, 'psi0i') }},
                _psi_hr={{ field(item, 'psiHr') }},
                _psi_hi={{ field(item, 'psiHi') }},
                _psi_hbr={{ field(item, 'psiHBr') }},
                _psi_hbi={{ field(item, 'psiHBi') }},
                _tc={{ field(item, 'crystalThickness') }},
                _ang_as={{ field(item, 'asymmetryAngle') }},
                _nvx={{ field(item, 'nvx') }},
                _nvy={{ field(item, 'nvy') }},
                _nvz={{ field(item, 'nvz') }},
                _tvx={{ field(item, 'tvx') }},
                _tvy={{ field(item, 'tvy') }},
                _uc={{ field(item, 'uc') }},
                _e_avg={{ field(item, 'energy') }},
                _ang_roll={{ field(item, 'diffractionAngle') }}
            )
            el.append(crystal)
	    #[ex,ey,ez] = crystal.get_orient(_e= v.w_e if(v.w_ef <= 0) else 0.5*(v.w_e + v.w_ef))[1]
	    #print('Beam frame vectors after Crystal:')
	    #print('ex=', ex, 'ez=', ez)
            #Updating PP from estimated orientation
	    #print('Original propagation parameters for Crystal:')
	    #print(v.op_{{ item.name }}_pp)
	    #v.op_{{ item.name }}_pp[12] = ez[0]
	    #v.op_{{ item.name }}_pp[13] = ez[1]
	    #v.op_{{ item.name }}_pp[14] = ez[2]
	    #v.op_{{ item.name }}_pp[15] = ex[0]
	    #v.op_{{ item.name }}_pp[16] = ex[1]
	    #print('Updated propagation parameters for Crystal:')
	    #print(v.op_{{ item.name }}_pp)
            pp.append(v.op_{{ item.name }}_pp)
	    {{ height_profile(item) }}
{% elif item.type == 'drift' %}
            el.append(srwlib.SRWLOptD(
                _L={{ field(item, 'length') }},
            ))
            pp.append(v.op_{{ item.name }}_pp)
{% elif item.type == 'ellipsoidMirror' %}
            el.append(srwlib.SRWLOptMirEl(
                _p={{ field(item, 'firstFocusLength') }},
                _q={{ field(item, 'focalLength') }},
                _ang_graz={{ field(item, 'grazingAngle') }},
                _size_tang={{ field(item, 'tangentialSize') }},
                _size_sag={{ field(item, 'sagittalSize') }},
                _nvx={{ field(item, 'normalVectorX') }},
                _nvy={{ field(item, 'normalVectorY') }},
                _nvz={{ field(item, 'normalVectorZ') }},
                _tvx={{ field(item, 'tangentialVectorX') }},
                _tvy={{ field(item, 'tangentialVectorY') }},
                _x={{ field(item, 'horizontalOffset') }},
                _y={{ field(item, 'verticalOffset') }},
            ))
            pp.append(v.op_{{ item.name }}_pp)
            {{ height_profile(item) }}
{% elif item.type == 'fiber' %}
            el.append(srwlib.srwl_opt_setup_cyl_fiber(
                _foc_plane={{ field(item, 'focalPlane') }},
                _delta_ext={{ field(item, 'externalRefractiveIndex') }},
                _delta_core={{ field(item, 'coreRefractiveIndex') }},
                _atten_len_ext={{ field(item, 'externalAttenuationLength') }},
                _atten_len_core={{ field(item, 'coreAttenuationLength') }},
                _diam_ext={{ field(item, 'externalDiameter') }},
                _diam_core={{ field(item, 'coreDiameter') }},
                _xc={{ field(item, 'horizontalCenterPosition') }},
                _yc={{ field(item, 'verticalCenterPosition') }},
            ))
            pp.append(v.op_{{ item.name }}_pp)
{% elif item.type == 'grating' %}
            mirror = srwlib.SRWLOptMirPl(
                _size_tang={{ field(item, 'tangentialSize') }},
                _size_sag={{ field(item, 'sagittalSize') }},
                _nvx={{ field(item, 'nvx') }},
                _nvy={{ field(item, 'nvy') }},
                _nvz={{ field(item, 'nvz') }},
                _tvx={{ field(item, 'tvx') }},
                _tvy={{ field(item, 'tvy') }},
                _x={{ field(item, 'horizontalOffset') }},
                _y={{ field(item, 'verticalOffset') }},
            )
            opEl=srwlib.SRWLOptG(
                _mirSub=mirror,
                _m={{ field(item, 'diffractionOrder') }},
                _grDen={{ field(item, 'grooveDensity0') }},
                _grDen1={{ field(item, 'grooveDensity1') }},
                _grDen2={{ field(item, 'grooveDensity2') }},
                _grDen3={{ field(item, 'grooveDensity3') }},
                _grDen4={{ field(item, 'grooveDensity4') }},
                _e_avg={{ field(item, 'energyAvg') }},
                _cff={{ field(item, 'cff') }},
                _ang_graz={{ field(item, 'grazingAngle') }},
                _ang_roll={{ field(item, 'rollAngle') }},
            )
            el.append(opEl)
	    #[ex,ey,ez] = opEl.get_orient(_e= v.w_e if(v.w_ef <= 0) else 0.5*(v.w_e + v.w_ef))[1]
	    #print('Beam frame vectors after Crystal:')
	    #print('ex=', ex, 'ez=', ez)
            #Updating PP from estimated orientation
	    #print('Original propagation parameters for grating:')
	    #print(v.op_{{ item.name }}_pp)
    	    #v.op_{{ item.name }}_pp[12] = ez[0]
    	    #v.op_{{ item.name }}_pp[13] = ez[1]
    	    #v.op_{{ item.name }}_pp[14] = ez[2]
    	    #v.op_{{ item.name }}_pp[15] = ex[0]
    	    #v.op_{{ item.name }}_pp[16] = ex[1]
    	    #print('Updated propagation parameters for grating:')
    	    #print(v.op_{{ item.name }}_pp)
            pp.append(v.op_{{ item.name }}_pp)
    	    {{ height_profile(item) }}
{% elif item.type == 'lens' %}
            el.append(srwlib.SRWLOptL(
                _Fx={{ field(item, 'horizontalFocalLength') }},
                _Fy={{ field(item, 'verticalFocalLength') }},
                _x={{ field(item, 'horizontalOffset') }},
                _y={{ field(item, 'verticalOffset') }},<commit_msg>clean up for beamline_optics.py.jinja<commit_after>{%- macro field(item, name) %}
v.op_{{ item.name }}_{{ nameMap.get(name, name) }}
{%- endmacro %}
{%- macro height_profile(item) -%}
{%- if item.heightProfileDimension -%}
            mirror_file = {{ field(item, 'heightProfileFile') }}
            assert os.path.isfile(mirror_file), \
                'Missing input file {}, required by {{ item.name }} beamline element'.format(mirror_file)
{% if item.heightProfileDimension == 1 %}
            el.append(srwlib.srwl_opt_setup_surf_height_1d(
                srwlib.srwl_uti_read_data_cols(mirror_file, "\t", 0, 1),
{% else %}
            el.append(srwlib.srwl_opt_setup_surf_height_2d(
                srwlib.srwl_uti_read_data_cols(mirror_file, "\t"),
{% endif %}
                _dim={{ field(item, 'orientation') }},
                _ang=abs({{ field(item, 'grazingAngle') }}),
                _amp_coef={{ field(item, 'heightAmplification') }},
{% if 'horizontalTransverseSize' in item %}
                _size_x={{ field(item, 'horizontalTransverseSize') }},
                _size_y={{ field(item, 'verticalTransverseSize') }},
{% endif %}
            ))
{%- if item.type != 'mirror' %}

            pp.append([0, 0, 1.0, 0, 0, 1.0, 1.0, 1.0, 1.0])
{%- endif -%}
{%- endif -%}
{%- endmacro %}
    el = []
    pp = []
    names = {{ names }}
{% if items|length > 0 %}
    for el_name in names:
{% endif %}
{% for item in items %}
{% if loop.index == 1 %}
        if el_name == '{{ item.name }}':
{% else %}
        elif el_name == '{{ item.name }}':
{% endif %}
            # {{ item.name }}: {{ item.type }} {{ item.position }}m
{% if item.type == 'aperture' %}
            el.append(srwlib.SRWLOptA(
                _shape={{ field(item, 'shape') }},
                _ap_or_ob='a',
                _Dx={{ field(item, 'horizontalSize') }},
                _Dy={{ field(item, 'verticalSize') }},
                _x={{ field(item, 'horizontalOffset') }},
                _y={{ field(item, 'verticalOffset') }},
            ))
            pp.append(v.op_{{ item.name }}_pp)
{% elif item.type == 'crl' %}
            el.append(srwlib.srwl_opt_setup_CRL(
                _foc_plane={{ field(item, 'focalPlane') }},
                _delta={{ field(item, 'refractiveIndex') }},
                _atten_len={{ field(item, 'attenuationLength') }},
                _shape={{ field(item, 'shape') }},
                _apert_h={{ field(item, 'horizontalApertureSize') }},
                _apert_v={{ field(item, 'verticalApertureSize') }},
                _r_min={{ field(item, 'tipRadius') }},
                _n={{ field(item, 'numberOfLenses') }},
                _wall_thick={{ field(item, 'tipWallThickness') }},
                _xc={{ field(item, 'horizontalOffset') }},
                _yc={{ field(item, 'verticalOffset') }},
            ))
            pp.append(v.op_{{ item.name }}_pp)
{% elif item.type == 'crystal' %}
            crystal = srwlib.SRWLOptCryst(
                _d_sp={{ field(item, 'dSpacing') }},
                _psi0r={{ field(item, 'psi0r') }},
                _psi0i={{ field(item, 'psi0i') }},
                _psi_hr={{ field(item, 'psiHr') }},
                _psi_hi={{ field(item, 'psiHi') }},
                _psi_hbr={{ field(item, 'psiHBr') }},
                _psi_hbi={{ field(item, 'psiHBi') }},
                _tc={{ field(item, 'crystalThickness') }},
                _ang_as={{ field(item, 'asymmetryAngle') }},
                _nvx={{ field(item, 'nvx') }},
                _nvy={{ field(item, 'nvy') }},
                _nvz={{ field(item, 'nvz') }},
                _tvx={{ field(item, 'tvx') }},
                _tvy={{ field(item, 'tvy') }},
                _uc={{ field(item, 'uc') }},
                _e_avg={{ field(item, 'energy') }},
                _ang_roll={{ field(item, 'diffractionAngle') }}
            )
            el.append(crystal)
            pp.append(v.op_{{ item.name }}_pp)
	    {{ height_profile(item) }}
{% elif item.type == 'drift' %}
            el.append(srwlib.SRWLOptD(
                _L={{ field(item, 'length') }},
            ))
            pp.append(v.op_{{ item.name }}_pp)
{% elif item.type == 'ellipsoidMirror' %}
            el.append(srwlib.SRWLOptMirEl(
                _p={{ field(item, 'firstFocusLength') }},
                _q={{ field(item, 'focalLength') }},
                _ang_graz={{ field(item, 'grazingAngle') }},
                _size_tang={{ field(item, 'tangentialSize') }},
                _size_sag={{ field(item, 'sagittalSize') }},
                _nvx={{ field(item, 'normalVectorX') }},
                _nvy={{ field(item, 'normalVectorY') }},
                _nvz={{ field(item, 'normalVectorZ') }},
                _tvx={{ field(item, 'tangentialVectorX') }},
                _tvy={{ field(item, 'tangentialVectorY') }},
                _x={{ field(item, 'horizontalOffset') }},
                _y={{ field(item, 'verticalOffset') }},
            ))
            pp.append(v.op_{{ item.name }}_pp)
            {{ height_profile(item) }}
{% elif item.type == 'fiber' %}
            el.append(srwlib.srwl_opt_setup_cyl_fiber(
                _foc_plane={{ field(item, 'focalPlane') }},
                _delta_ext={{ field(item, 'externalRefractiveIndex') }},
                _delta_core={{ field(item, 'coreRefractiveIndex') }},
                _atten_len_ext={{ field(item, 'externalAttenuationLength') }},
                _atten_len_core={{ field(item, 'coreAttenuationLength') }},
                _diam_ext={{ field(item, 'externalDiameter') }},
                _diam_core={{ field(item, 'coreDiameter') }},
                _xc={{ field(item, 'horizontalCenterPosition') }},
                _yc={{ field(item, 'verticalCenterPosition') }},
            ))
            pp.append(v.op_{{ item.name }}_pp)
{% elif item.type == 'grating' %}
            mirror = srwlib.SRWLOptMirPl(
                _size_tang={{ field(item, 'tangentialSize') }},
                _size_sag={{ field(item, 'sagittalSize') }},
                _nvx={{ field(item, 'nvx') }},
                _nvy={{ field(item, 'nvy') }},
                _nvz={{ field(item, 'nvz') }},
                _tvx={{ field(item, 'tvx') }},
                _tvy={{ field(item, 'tvy') }},
                _x={{ field(item, 'horizontalOffset') }},
                _y={{ field(item, 'verticalOffset') }},
            )
            opEl=srwlib.SRWLOptG(
                _mirSub=mirror,
                _m={{ field(item, 'diffractionOrder') }},
                _grDen={{ field(item, 'grooveDensity0') }},
                _grDen1={{ field(item, 'grooveDensity1') }},
                _grDen2={{ field(item, 'grooveDensity2') }},
                _grDen3={{ field(item, 'grooveDensity3') }},
                _grDen4={{ field(item, 'grooveDensity4') }},
                _e_avg={{ field(item, 'energyAvg') }},
                _cff={{ field(item, 'cff') }},
                _ang_graz={{ field(item, 'grazingAngle') }},
                _ang_roll={{ field(item, 'rollAngle') }},
            )
            el.append(opEl)
            pp.append(v.op_{{ item.name }}_pp)
    	    {{ height_profile(item) }}
{% elif item.type == 'lens' %}
            el.append(srwlib.SRWLOptL(
                _Fx={{ field(item, 'horizontalFocalLength') }},
                _Fy={{ field(item, 'verticalFocalLength') }},
                _x={{ field(item, 'horizontalOffset') }},
                _y={{ field(item, 'verticalOffset') }},
{'fraction_non_alphanumeric': 0.18659252394192152, 'fraction_numerical': 0.006549274019153537, 'mean_word_length': 1.9742741639103272, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1214810', 'n_tokens_mistral': 5694, 'n_tokens_neox': 5437, 'n_words': 923}