def ImportTuning(mode, wavelength, filename='tuning'): """Import tuning for *mode* and *wavelength* from a CSV file. Mode can be "nrse" or "mieze". """ echotime = session.getDevice('echotime') exp = session.getDevice('Exp') filename = path.join(exp.dataroot, filename + '_%s_%sA.csv' % (mode, wavelength)) printinfo('Importing from %s' % filename) if not path.exists(filename): printerror('File does not exist. Please select another name.') return newtable = {} with open(filename, 'r', encoding='utf-8') as fp: reader = iter(csv.reader(fp)) headers = reader.next() if headers[0] != 'echotime': printerror('This does not appear to be a tuning table.') return devices = headers[1:] for row in reader: etime = try_float(row[0]) devs = {} for (d, val) in zip(devices, row[1:]): if val != 'None': devs[d] = try_float(val) newtable[etime] = devs echotime.setTable(mode, wavelength, newtable) printinfo('Done.')
def test_output_commands(self, session, log): with log.assert_msg_matches(r'printdebugtest1 printdebugtest2'): printdebug('printdebugtest1', 'printdebugtest2') with log.assert_msg_matches(r'printinfo testing\.\.\.'): printinfo('printinfo testing...') with log.assert_warns(): printwarning('warn!') assert raises(ErrorLogged, printerror, 'error!') assert raises(ErrorLogged, printexception, 'exception!')
def ExportTuning(mode, wavelength, filename='tuning'): """Export tuning for *mode* and *wavelength* to a CSV file. Mode can be "nrse" or "mieze". """ echotime = session.getDevice('echotime') exp = session.getDevice('Exp') # get the right table try: tables = echotime.tables[mode] except KeyError: printerror('Need a valid mode (mieze or nrse)') return try: table = tables[wavelength] except KeyError: printerror('No table for this wavelength (available: %s)' % ', '.join(map(str, tables))) # build list of devices it = iter(table.values()) devices = sorted(it.next()) for otherdevs in it: devices.extend(set(otherdevs) - set(devices)) # export to CSV filename = path.join(exp.dataroot, filename + '_%s_%sA.csv' % (mode, wavelength)) printinfo('Exporting to %s' % filename) if path.exists(filename): printerror('File already exists. Please select another name.') return with open(filename, 'w', encoding='utf-8') as fp: writer = csv.writer(fp) writer.writerow(['echotime'] + devices) for (etime, devs) in table.items(): writer.writerow([repr(etime)] + [repr(devs.get(d)) for d in devices]) printinfo('Done.')
def RemoveHeCellFromPolariser(count_time): """Remove old 3He cell from polariser (only).""" gamma = session.getDevice('gamma') det = session.getDevice('det') printinfo(LARGE_SEP) printinfo('Remove old 3He cell from polariser (only)') printinfo(SMALL_SEP) gamma_cell = 60.0 printinfo(SMALL_SEP) printinfo('Measurement with 1 cell: in polariser. {}'.format( format_datetime())) printinfo(SMALL_SEP) cells1 = count(det, count_time) AddCountsToCellsFile(False, True, cells1) maw(gamma, gamma_cell) pause('Remove cell from polariser and press "Continue script" after that.') printinfo(SMALL_SEP) printinfo('Measurement without cells. {}'.format(format_datetime())) printinfo(SMALL_SEP) cells0 = count(det, count_time) AddCountsToCellsFile(False, False, cells0) polariser_cell_transmission = polariser_trans(cells0, cells1) AddPolariserTransToCellsFile(polariser_cell_transmission) AddFooterToCellsFile() pause('Cell is removed.\n\nPolariser cell:\n Transmission = {:8.2f} %'. format(polariser_cell_transmission)) printinfo(SMALL_SEP) printinfo('Cell is removed.') printinfo('Polariser cell: transmission = {:9.4f} %'.format( polariser_cell_transmission)) printinfo(LARGE_SEP)
def AddHeCellToPolariser(count_time, polariser_cell_name, polariser_cell_pressure): """Add new 3He cell to polariser (only).""" gamma = session.getDevice('gamma') wavelength = session.getDevice('wavelength') det = session.getDevice('det') printinfo(LARGE_SEP) printinfo('Add new 3He cell to polariser (only)') printinfo(SMALL_SEP) gamma_cell = 60.0 printinfo(SMALL_SEP) printinfo('Measurement without cell. {}'.format(format_datetime())) printinfo(SMALL_SEP) cells0 = count(det, count_time) wavelen = wavelength() AddPolariserHeaderToCellsFile(polariser_cell_name, polariser_cell_pressure, wavelen) AddCountsToCellsFile(False, False, cells0) maw(gamma, gamma_cell) pause( 'Insert cell {} into polariser and press "Continue script" after that.' .format(polariser_cell_name)) printinfo(SMALL_SEP) printinfo('Measurement with 1 cell: {} [{} bar] in polariser. {}'.format( polariser_cell_name, polariser_cell_pressure, format_datetime())) printinfo(SMALL_SEP) cells1 = count(det, count_time) AddCountsToCellsFile(False, True, cells1) polariser_cell_transmission = polariser_trans(cells0, cells1) AddPolariserTransToCellsFile(polariser_cell_transmission) pause('Cell is inserted.\n\nPolariser cell {}:\n' ' Pressure = {} bar, Transmission = {:8.2f} %'.format( polariser_cell_name, polariser_cell_pressure, polariser_cell_transmission)) printinfo(SMALL_SEP) printinfo('Cell is inserted.') printinfo('Polariser cell {}: pressure = {} bar, transmission = {:9.4f} %'. format(polariser_cell_name, polariser_cell_pressure, polariser_cell_transmission)) printinfo(LARGE_SEP)
def RemoveHeCells(h_index, k_index, l_index, count_time): """Remove old 3He cells from polariser and analyser.""" gamma = session.getDevice('gamma') det = session.getDevice('det') printinfo(LARGE_SEP) printinfo('Remove old 3He cells') printinfo(SMALL_SEP) printinfo('Go to hkl ({} {} {})'.format(h_index, k_index, l_index)) printinfo(SMALL_SEP) pos(h_index, k_index, l_index) gamma_hkl = gamma() gamma_cell = 60.0 printinfo(SMALL_SEP) printinfo('Measurement with 2 cells: in analyser/decpol and polariser. {}'. format(format_datetime())) printinfo(SMALL_SEP) cells2 = count(det, count_time) AddCountsToCellsFile(True, True, cells2) maw(gamma, gamma_cell) pause('Remove cell from polariser.') maw(gamma, gamma_hkl) printinfo(SMALL_SEP) printinfo('Measurement with 1 cell: in analyser/decpol. {}'.format( format_datetime())) printinfo(SMALL_SEP) cells1 = count(det, count_time) AddCountsToCellsFile(True, False, cells1) polariser_cell_transmission = polariser_trans(cells1, cells2) maw(gamma, gamma_cell) pause('Polariser cell:\n Transmission = {:8.2f} %.\n\n' 'Now, remove cell from analyser/decpol.'.format( polariser_cell_transmission)) maw(gamma, gamma_hkl) printinfo(SMALL_SEP) printinfo('Measurement without cells. {}'.format(format_datetime())) printinfo(SMALL_SEP) cells0 = count(det, count_time) AddCountsToCellsFile(False, False, cells0) analyser_cell_transmission = analyser_trans(cells0, cells1) AddTransToCellsFile(analyser_cell_transmission, polariser_cell_transmission) AddFooterToCellsFile() pause('Cells are removed.\n\nPolariser cell:\n' ' Transmission = {:8.2f} %,\n\nAnalyser/decpol cell:\n' ' Transmission = {:8.2f} %'.format(polariser_cell_transmission, analyser_cell_transmission)) printinfo(SMALL_SEP) printinfo('Cells are removed.') printinfo('Polariser cell: transmission = {:9.4f} %'.format( polariser_cell_transmission)) printinfo('Analyser/decpol cell: transmission = {:9.4f} %'.format( analyser_cell_transmission)) printinfo(LARGE_SEP)
def AddHeCells(h_index, k_index, l_index, count_time, analyser_cell_name, analyser_cell_pressure, polariser_cell_name, polariser_cell_pressure): """Add new 3He cells to analyser and polariser.""" gamma = session.getDevice('gamma') wavelength = session.getDevice('wavelength') det = session.getDevice('det') printinfo(LARGE_SEP) printinfo('Add new 3He cells') printinfo(SMALL_SEP) printinfo('Go to hkl ({} {} {})'.format(h_index, k_index, l_index)) printinfo(SMALL_SEP) pos(h_index, k_index, l_index) gamma_hkl = gamma() gamma_cell = 60.0 printinfo(SMALL_SEP) printinfo('Measurement without cells. {}'.format(format_datetime())) printinfo(SMALL_SEP) cells0 = count(det, count_time) wavelen = wavelength() AddHeaderToCellsFile(analyser_cell_name, analyser_cell_pressure, polariser_cell_name, polariser_cell_pressure, wavelen) AddCountsToCellsFile(False, False, cells0) maw(gamma, gamma_cell) pause('Insert cell {} into analyser/decpol.'.format(analyser_cell_name)) maw(gamma, gamma_hkl) printinfo(SMALL_SEP) printinfo( 'Measurement with 1 cell: {} [{} bar] in analyser/decpol. {}'.format( analyser_cell_name, analyser_cell_pressure, format_datetime())) printinfo(SMALL_SEP) cells1 = count(det, count_time) AddCountsToCellsFile(True, False, cells1) analyser_cell_transmission = analyser_trans(cells0, cells1) maw(gamma, gamma_cell) pause('Analyser/decpol cell {}:\n Pressure = {} bar, ' 'Transmission = {:8.2f} %.\n\nNow, insert cell {} into polariser.'. format(analyser_cell_name, analyser_cell_pressure, analyser_cell_transmission, polariser_cell_name)) maw(gamma, gamma_hkl) printinfo(SMALL_SEP) printinfo('Measurement with 2 cells: {} [{} bar] in analyser/decpol and ' '{} [{} bar] in polariser. {}'.format(analyser_cell_name, analyser_cell_pressure, polariser_cell_name, polariser_cell_pressure, format_datetime())) printinfo(SMALL_SEP) cells2 = count(det, count_time) AddCountsToCellsFile(True, True, cells2) polariser_cell_transmission = polariser_trans(cells1, cells2) AddTransToCellsFile(analyser_cell_transmission, polariser_cell_transmission) pause('Cells are inserted.\n\nAnalyser/decpol cell {}:\n' ' Pressure = {} bar, Transmission = {:8.2f} %,\n\n' 'Polariser cell {}:\n Pressure = {} bar, Transmission = {:8.2f} %'. format(analyser_cell_name, analyser_cell_pressure, analyser_cell_transmission, polariser_cell_name, polariser_cell_pressure, polariser_cell_transmission)) printinfo(SMALL_SEP) printinfo('Cells are inserted.') printinfo( 'Analyser/decpol cell {}: pressure = {} bar, transmission = {:9.4f} %'. format(analyser_cell_name, analyser_cell_pressure, analyser_cell_transmission)) printinfo('Polariser cell {}: pressure = {} bar, transmission = {:9.4f} %'. format(polariser_cell_name, polariser_cell_pressure, polariser_cell_transmission)) printinfo(LARGE_SEP)
def tableexe(csvfile): """ Tableexe executes an execution table provided as a CSV file :param csvfile: The csv file to run """ fname = _csvfilename(csvfile) if not fname or \ (not path.isfile(fname) and os.access(fname, os.R_OK)): raise UsageError('The file %r does not exist or is not readable' % fname) with open(fname, 'r') as fin: csv_data = csv.reader(fin, delimiter=',') # Analyse header devlist = [] parlist = [] comlist = [] strlist = [] comnames = ['timer', 'monitor', 'command'] header = next(csv_data) for col_name in header: if not col_name: continue if col_name in comnames: comlist.append(col_name) continue if col_name in session.devices: dev = session.getDevice(col_name) if isinstance(dev.read(), str): strlist.append(col_name) devlist.append(col_name) continue try: devname, par = col_name.split('.') dev = session.getDevice(devname) if par in dev.parameters: parlist.append(col_name) if isinstance(dev.parameters[par], str): strlist.append(col_name) else: raise UsageError('%s has no parameter %s' % (devname, par)) except Exception: raise UsageError('Unrecognised column name %s' % col_name) from None # Actually execute the CSV data idx = 0 printinfo('Executing CSV from %s' % csvfile) for data in csv_data: if not data: continue idx = idx + 1 dev_value = [] commands = [] printinfo('Working line %d of %s' % (idx, os.path.basename(csvfile))) for col_name, value in zip(header, data): if col_name in devlist: dev_value.append(value) continue if col_name in parlist: lv = col_name.split('.') dev = session.getDevice(lv[0]) setattr(dev, lv[1], value) continue if col_name == 'timer': commands.append('count(t=%s)' % value) elif col_name == 'monitor': commands.append('count(m=%s)' % value) else: commands.append(value) dev_poslist = () for dev, value in zip(devlist, dev_value): dev_poslist += (dev, ) if dev in strlist: dev_poslist += (value, ) else: dev_poslist += (float(value), ) _basemove(dev_poslist, waithook=_wait_hook) for com in commands: if com: code, _ = parseScript(com) for c in code: exec(c, session.namespace) session.breakpoint(2)