From 093d4c7571bdff03835d0bdb5fe613b3fc99bb25 Mon Sep 17 00:00:00 2001 From: lemke_h Date: Thu, 12 Jul 2018 15:14:14 +0200 Subject: [PATCH] restructure 1 --- eco/alvra/__init__.py | 1 + eco/alvra/alvra.py | 174 ++++ eco/alvra/config.py | 282 ++++++ eco/bernina/__init__.py | 1 + eco/bernina/bernina.py | 178 ++++ eco/bernina/config.py | 195 ++++ eco/utilities/utilities_instruments.py | 66 ++ eco/utilities/xsim/__init__.py | 2 + eco/utilities/xsim/consts.py | 1124 ++++++++++++++++++++++++ eco/utilities/xsim/materials.py | 90 ++ eco/utilities/xsim/swissfel.py | 43 + eco/utilities/xsim/utilities.py | 78 ++ 12 files changed, 2234 insertions(+) create mode 100644 eco/alvra/__init__.py create mode 100755 eco/alvra/alvra.py create mode 100755 eco/alvra/config.py create mode 100644 eco/bernina/__init__.py create mode 100755 eco/bernina/bernina.py create mode 100755 eco/bernina/config.py create mode 100644 eco/utilities/utilities_instruments.py create mode 100644 eco/utilities/xsim/__init__.py create mode 100644 eco/utilities/xsim/consts.py create mode 100644 eco/utilities/xsim/materials.py create mode 100644 eco/utilities/xsim/swissfel.py create mode 100644 eco/utilities/xsim/utilities.py diff --git a/eco/alvra/__init__.py b/eco/alvra/__init__.py new file mode 100644 index 0000000..dddce7b --- /dev/null +++ b/eco/alvra/__init__.py @@ -0,0 +1 @@ +from .alvra import * diff --git a/eco/alvra/alvra.py b/eco/alvra/alvra.py new file mode 100755 index 0000000..35d98e6 --- /dev/null +++ b/eco/alvra/alvra.py @@ -0,0 +1,174 @@ + +from .config import elog as _elog_info +from ..utilities.elog import Elog as _Elog +from ..utilities.elog import Screenshot as _Screenshot +from ..utilities.config import loadConfig +from epics import PV +import sys,os + + +from colorama import Fore as _color +import traceback +import datetime + +elog = _Elog(_elog_info['url'],user='gac-alvra',screenshot_directory=_elog_info['screenshot_directory']) +screenshot = _Screenshot(screenshot_directory=_elog_info['screenshot_directory']) +#stationMessage = _stationMessage('ESA') + +_smaractappends = [] + +########### configurations ######################## +currexp_file_path = '/sf/alvra/config/exp/current_experiment.json' +if os.path.exists(currexp_file_path): + exp_config = loadConfig(currexp_file_path) +else: + print('NB: Could not load experiment config in path %s .'%currexp_file_path) + +########### GENERAL IMPLEMENTATIONS ################## +from .config import aliases as _aliases +def _attach_device(devDict,devId,args,kwargs): + imp_p = devDict['eco_type'].split(sep='.') + dev_alias = devDict['alias'] + dev_alias = dev_alias[0].lower() + dev_alias[1:] + eco_type_name = imp_p[-1] + istr = 'from ..'+'.'.join(imp_p[:-1])+' import ' + istr += '%s as _%s'%(eco_type_name,eco_type_name) + #print(istr) + print(('Configuring %s '%(dev_alias)).ljust(25), end='') + print(('(%s)'%(devId)).ljust(25), end='') + error = None + try: + exec(istr) + tdev = eval('_%s(Id=\'%s\',*args,**kwargs)'%(eco_type_name,devId)) + tdev.name = dev_alias + tdev._z_und = devDict['z_und'] + globals().update([(dev_alias,tdev)]) + print((_color.GREEN+'OK'+_color.RESET).rjust(5)) + except Exception as e: + print((_color.RED+'FAILED'+_color.RESET).rjust(5)) + error = e + return error + +errors = [] +_composed = {} +for device_Id in _aliases.keys(): + alias = _aliases[device_Id] + if 'eco_type' in alias.keys() \ + and alias['eco_type']: + if 'args' in alias.keys() \ + and alias['args']: + args = alias['args'] + else: + args = tuple() + + if 'kwargs' in alias.keys() \ + and alias['kwargs']: + kwargs = alias['kwargs'] + else: + kwargs = dict() + + e = _attach_device(alias,device_Id,args,kwargs) + if e: + errors.append((alias['alias'],e)) + else: + _device = globals()[alias['alias'][0].lower()+alias['alias'][1:]] + if hasattr(_device, '_smaractaxes'): + _smaractappends.append(_device) + else: + if alias['eco_type'].endswith('SmarActRecord') \ + and 'device' in alias and 'axis' in alias: + if alias['device'] not in _composed: + _composed[alias['device']] = {} + _composed[alias['device']][alias['axis']] = globals()[alias['alias']] + +print('Integrating SmarAct devices:') + +for _device in _smaractappends: + print(" Appending %s: %s" \ + %(_device.name,', '.join(map(str, list(_device._smaractaxes.keys()))))) + for _axis in _device._smaractaxes.keys(): + setattr(_device, _axis, globals()[_device._smaractaxes[_axis]]) + +from ..devices_general.smaract import SmarActStage as _SmarActStage +for _key in _composed.keys(): + print(' Composing %s: %s'%(_key,', '.join(map(str, list(_composed[_key].keys()))))) + globals()[_key] = _SmarActStage(_composed[_key], _key) + +if len(errors)>0: + print('Found errors when configuring %s'%[te[0] for te in errors]) + if input('Would you like to see error traces? (y/n)')=='y': + for error in errors: + print('---> Error when configuring %s'%error[0]) + traceback.print_tb(error[1].__traceback__) + + +########### DAQ SECTION ######################## +# configuring bs daq + +def parseChannelListFile(fina): + out = [] + with open(fina,'r') as f: + done = False + while not done: + d = f.readline() + if not d: + done=True + if len(d)>0: + if not d.isspace(): + if not d[0]=='#': + out.append(d.strip()) + return out + + +from ..acquisition.bs_data import BStools +from ..acquisition import scan as _scan +#from ..acquisition.ioxos_data import Ioxostools + +channellist = dict(alvra_channel_list= + parseChannelListFile('/sf/alvra/config/com/channel_lists/default_channel_list')) +bsdaq = BStools(default_channel_list=channellist,default_file_path='%s') + +#channellistioxos = dict(alvra_channel_list= +# parseChannelListFile('/sf/alvra/config/com/channel_lists/default_channel_list_ioxos')) +#ioxosdaq = Ioxostools(default_channel_list=channellistioxos,default_file_path='%s') + +channellistPhotonDiag = dict(alvra_channel_list= + parseChannelListFile('/sf/alvra/config/com/channel_lists/default_channel_list_PhotonDiag')) +bsdaqPhotonDiag = BStools(default_channel_list=channellistPhotonDiag,default_file_path='%s') + +from eco.devices_general.alvradetectors import DIAClient +bsdaqJF = DIAClient('bsdaqJF', instrument="alvra", api_address = "http://sf-daq-alvra:10000", jf_name="JF_4.5M") + +try: + bsdaqJF.pgroup = int(exp_config['pgroup'][1:]) +except: + print('Could not set p group in bsdaqJF !!') + +checkerPV=PV('SARFE10-PBPG050:HAMP-INTENSITY-CAL') + +def checker_function(limits): + cv = checkerPV.get() + if cv>limits[0] and cv0: + print('Found errors when configuring %s'%[te[0] for te in errors]) + if input('Would you like to see error traces? (y/n)')=='y': + for error in errors: + print('---> Error when configuring %s'%error[0]) + traceback.print_tb(error[1].__traceback__) + + + + ########### DAQ SECTION ######################## + # configuring bs daq + + def parseChannelListFile(fina): + out = [] + with open(fina,'r') as f: + done = False + while not done: + d = f.readline() + if not d: + done=True + if len(d)>0: + if not d.isspace(): + if not d[0]=='#': + out.append(d.strip()) + return out + + + from ..acquisition.bs_data import BStools + from ..acquisition import scan as _scan + from ..acquisition.ioxos_data import Ioxostools + + channellist = dict(bernina_channel_list= + parseChannelListFile('/sf/bernina/config/channel_lists/default_channel_list')) + bsdaq = BStools(default_channel_list=channellist,default_file_path='%s') + + channellistioxos = dict(bernina_channel_list= + parseChannelListFile('/sf/bernina/config/channel_lists/default_channel_list_ioxos')) + ioxosdaq = Ioxostools(default_channel_list=channellistioxos,default_file_path='%s') + + + #from eco.devices_general.detectors import JF_BS_writer + #bsdaqJF = JF_BS_writer('bsdaqJF') d + from eco.devices_general.detectors import DIAClient + bsdaqJF = DIAClient('bsdaqJF', instrument="bernina", api_address = "http://sf-daq-1:10000") + + try: + bsdaqJF.pgroup = int(exp_config['pgroup'][1:]) + except: + print('Could not set p group in bsdaqJF !!') + + checkerPV=PV('SARFE10-PBPG050:HAMP-INTENSITY-CAL') + + def checker_function(limits): + cv = checkerPV.get() + if cv>limits[0] and cv>> cartesian(([1, 2, 3], [4, 5], [6, 7])) + array([[1, 4, 6], + [1, 4, 7], + [1, 5, 6], + [1, 5, 7], + [2, 4, 6], + [2, 4, 7], + [2, 5, 6], + [2, 5, 7], + [3, 4, 6], + [3, 4, 7], + [3, 5, 6], + [3, 5, 7]]) + + """ + + arrays = [np.asarray(x) for x in arrays] + dtype = arrays[0].dtype + + n = np.prod([x.size for x in arrays]) + if out is None: + out = np.zeros([n, len(arrays)], dtype=dtype) + + m = n / arrays[0].size + out[:,0] = np.repeat(arrays[0], m) + if arrays[1:]: + cartesian(arrays[1:], out=out[0:m,1:]) + for j in range(1, arrays[0].size): + out[j*m:(j+1)*m,1:] = out[0:m,1:] + return out + +def E2lam(energy): + """energy in eV, lambda in Ångstrøm""" + return constants.h*constants.c/constants.e / energy *1e10 + +def QE2theta(Q,energy): + """Q in Å**(-1), energy in eV, theta in radians""" + return np.arcsin(E2lam(energy)/4/np.pi*Q) + + +def absorptionEdge(element,edge=None): + if type(element) is str: + element = xl.SymbolToAtomicNumber(element) + shells = ['K','L1','L2','L3','M1','M2','M3','M4','M5'] + if edge is not None: + shell_ind = shells.index(edge) + return xl.EdgeEnergy(element,shell_ind) + else: + shell_inds = range(8) + print('Absorption edges %s'%xl.AtomicNumberToSymbol(element)) + for shell_ind in shell_inds: + print(' '\ + +shells[shell_ind].ljust(3)\ + +' = %7.1f eV'%(xl.EdgeEnergy(element,shell_ind)*1000))