def _changref2(transforms, is_native): # list of cm or deg values for t in transforms: if t.transformType == 'none': continue if t.transformType in ('XS', 'YS', 'ZS'): t.transformValue = ModelUnits.scale_value( t.transformValue, 'cm_to_m', is_native) elif t.transformType in ('XR', 'YR', 'ZR'): t.transformValue = ModelUnits.scale_value( t.transformValue, 'deg_to_rad', is_native) else: assert False, 'invalid transformType: {}'.format( t.transformType) return transforms
def test_model_units(): from pykern.pkunit import pkeq from sirepo.template.template_common import ModelUnits import re def _xpas(value, is_native): cm_to_m = lambda v: ModelUnits.scale_value(v, 'cm_to_m', is_native) if is_native: if re.search(r'^#', str(value)): value = re.sub(r'^#', '', value) return list(map(cm_to_m, value.split('|'))) else: if type(value) is list: return '#' + '|'.join( map(str, map(lambda v: int(cm_to_m(v)), value))) return cm_to_m(value) units = ModelUnits({ 'CHANGREF': { 'XCE': 'cm_to_m', 'YCE': 'cm_to_m', 'ALE': 'deg_to_rad', 'XPAS': _xpas, }, }) native_model = { 'XCE': 2, 'YCE': 0, 'ALE': 8, 'XPAS': '******', } sirepo_model = units.scale_from_native('CHANGREF', native_model.copy()) assert sirepo_model == { 'XCE': 2e-2, 'YCE': 0, 'ALE': 0.13962634015954636, 'XPAS': [2e-1, 2e-1, 2e-1], } assert native_model == units.scale_to_native('CHANGREF', sirepo_model.copy()) assert units.scale_from_native('CHANGREF', { 'XPAS': '******', })['XPAS'] == 0.2 assert ModelUnits.scale_value(2, 'cm_to_m', True) == 2e-2 assert ModelUnits.scale_value(0.02, 'cm_to_m', False) == 2
def _xpas(value, is_native): cm_to_m = lambda v: ModelUnits.scale_value(v, 'cm_to_m', is_native) if is_native: if re.search(r'^#', str(value)): value = re.sub(r'^#', '', value) return list(map(cm_to_m, value.split('|'))) else: if type(value) is list: return '#' + '|'.join( map(str, map(lambda v: int(cm_to_m(v)), value))) return cm_to_m(value)
def _xpas(v, is_native): if is_native: if re.search(r'\#', v): return v v2 = zgoubi_parser.parse_float(v) if v2 > 1e10: # old step size format m = re.search(r'^0*(\d+)\.0*(\d+)', v) assert m, 'XPAS failed to parse step size: {}'.format(v) return '#{}|{}|{}'.format(m.group(2), m.group(1), m.group(2)) else: if re.search(r'\#', str(v)): v = re.sub(r'^#', '', v) return '[{}]'.format(','.join(v.split('|'))) return ModelUnits.scale_value(v, 'cm_to_m', is_native)