# it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os import sys import time import socket import config from lecroy import LeCroyScope if __name__ == '__main__': scope = LeCroyScope(config.ip, port=config.port, timeout=config.timeout) scope.clear() settings = scope.get_settings() print settings while True: cmd = raw_input("> ") if len(cmd) <= 0: break scope.send(cmd) print scope.recv(),
def fetch(filename, nevents, nsequence): ''' Fetch and save waveform traces from the oscilloscope. ''' scope = LeCroyScope(config.ip, timeout=config.timeout) scope.clear() scope.set_sequence_mode(nsequence) channels = scope.get_channels() # print(channels) settings = scope.get_settings() # print(settings) if b'ON' in settings['SEQUENCE']: sequence_count = int(settings['SEQUENCE'].split(',')[1]) else: sequence_count = 1 if nsequence != sequence_count: print('Could not configure sequence mode properly') if sequence_count != 1: print('Using sequence mode with %i traces per aquisition' % sequence_count) current_dim = {} for channel in channels: wave_desc = scope.get_wavedesc(channel) current_dim[channel] = wave_desc['wave_array_count'] // sequence_count try: i = 0 while i < nevents: print('\rfetching event: %i' % i) sys.stdout.flush() try: scope.trigger() for channel in channels: wave_desc, wave_array = scope.get_waveform(channel) print("wave_array = ", wave_array) num_samples = wave_desc[ 'wave_array_count'] // sequence_count if current_dim[channel] < num_samples: current_dim[channel] = num_samples traces = wave_array.reshape( sequence_count, wave_array.size // sequence_count) #necessary because h5py does not like indexing and this is the fastest (and man is it slow) way scratch = numpy.zeros((current_dim[channel], ), dtype=wave_array.dtype) except (socket.error, struct.error) as e: print('\n' + str(e)) scope.clear() continue i += sequence_count except KeyboardInterrupt: print('\rUser interrupted fetch early') finally: print('\r') scope.clear() return i
def fetch(filename, nevents, nsequence): ''' Fetch and save waveform traces from the oscilloscope. ''' scope = LeCroyScope(config.ip, timeout=config.timeout) scope.clear() scope.set_sequence_mode(nsequence) channels = scope.get_channels() settings = scope.get_settings() if 'ON' in settings['SEQUENCE']: sequence_count = int(settings['SEQUENCE'].split(',')[1]) else: sequence_count = 1 if nsequence != sequence_count: print 'Could not configure sequence mode properly' if sequence_count != 1: print 'Using sequence mode with %i traces per aquisition' % sequence_count f = h5py.File(filename, 'w') for command, setting in settings.items(): f.attrs[command] = setting current_dim = {} for channel in channels: wave_desc = scope.get_wavedesc(channel) current_dim[channel] = wave_desc['wave_array_count'] // sequence_count f.create_dataset("c%i_samples" % channel, (nevents, current_dim[channel]), dtype=wave_desc['dtype'], compression='gzip', maxshape=(nevents, None)) for key, value in wave_desc.items(): try: f["c%i_samples" % channel].attrs[key] = value except ValueError: pass f.create_dataset("c%i_vert_offset" % channel, (nevents, ), dtype='f8') f.create_dataset("c%i_vert_scale" % channel, (nevents, ), dtype='f8') f.create_dataset("c%i_horiz_offset" % channel, (nevents, ), dtype='f8') f.create_dataset("c%i_horiz_scale" % channel, (nevents, ), dtype='f8') f.create_dataset("c%i_num_samples" % channel, (nevents, ), dtype='f8') try: i = 0 while i < nevents: print '\rfetching event: %i' % i, sys.stdout.flush() try: scope.trigger() for channel in channels: wave_desc, wave_array = scope.get_waveform(channel) num_samples = wave_desc[ 'wave_array_count'] // sequence_count if current_dim[channel] < num_samples: current_dim[channel] = num_samples f['c%i_samples' % channel].resize( current_dim[channel], 1) traces = wave_array.reshape( sequence_count, wave_array.size // sequence_count) #necessary because h5py does not like indexing and this is the fastest (and man is it slow) way scratch = numpy.zeros((current_dim[channel], ), dtype=wave_array.dtype) for n in xrange(0, sequence_count): scratch[0:num_samples] = traces[ n] #'fast' copy to right size f['c%i_samples' % channel][i + n] = scratch #'fast' add to dataset f['c%i_num_samples' % channel][i + n] = num_samples f['c%i_vert_offset' % channel][i + n] = wave_desc['vertical_offset'] f['c%i_vert_scale' % channel][i + n] = wave_desc['vertical_gain'] f['c%i_horiz_offset' % channel][i + n] = -wave_desc['horiz_offset'] f['c%i_horiz_scale' % channel][i + n] = wave_desc['horiz_interval'] except (socket.error, struct.error) as e: print '\n' + str(e) scope.clear() continue i += sequence_count except KeyboardInterrupt: print '\rUser interrupted fetch early' finally: print '\r', f.close() scope.clear() return i
def fetch(filename, nevents, nsequence): ''' Fetch and save waveform traces from the oscilloscope. ''' scope = LeCroyScope(config.ip, timeout=config.timeout) scope.clear() scope.set_sequence_mode(nsequence) channels = scope.get_channels() settings = scope.get_settings() if 'ON' in settings['SEQUENCE']: sequence_count = int(settings['SEQUENCE'].split(',')[1]) else: sequence_count = 1 if nsequence != sequence_count: print 'Could not configure sequence mode properly' if sequence_count != 1: print 'Using sequence mode with %i traces per aquisition' % sequence_count f = {} for channel in channels: f[channel] = open('%s.ch%s.traces'%(filename,channel),'wb') params_pattern = '=IBdddd' # (num_samples, sample_bytes, v_off, v_scale, h_off, h_scale, [samples]) ... try: i = 0 while i < nevents: print '\rfetching event: %i' % i, sys.stdout.flush() try: scope.trigger() for channel in channels: wave_desc,wave_array = scope.get_waveform(channel) num_samples = wave_desc['wave_array_count']//sequence_count traces = wave_array.reshape(sequence_count, wave_array.size//sequence_count) out = f[channel] for n in xrange(0,sequence_count): out.write(struct.pack(params_pattern,num_samples,wave_desc['dtype'].itemsize,wave_desc['vertical_offset'], wave_desc['vertical_gain'], -wave_desc['horiz_offset'], wave_desc['horiz_interval'])) traces[n].tofile(out) except (socket.error) as e: print '\n' + str(e) scope.clear() continue i += sequence_count except KeyboardInterrupt: print '\rUser interrupted fetch early' except Exception as e: print "\rUnexpected error:", e finally: print '\r', for channel in channels: f[channel].close() scope.clear() return i
import numpy as np import matplotlib.pyplot as plt from lecroy import LeCroyScope scope = LeCroyScope(host="131.225.138.156") nsequence = 10 scope.clear() scope.set_sequence_mode(nsequence) settings = scope.get_settings() for a, b in settings.items(): print(a, ":", b) if b'ON' in settings['SEQUENCE']: sequence_count = int(settings['SEQUENCE'].split(b',')[1]) else: sequence_count = 1 if nsequence != sequence_count: print( 'Could not configure sequence mode properly') if sequence_count != 1: print( 'Using sequence mode with %i traces per aquisition' % sequence_count) scope.trigger() desc, array = scope.get_waveform(2) # wf = desc['vertical_gain']*array - desc['vertical_offset'] print("---------- Descritption of waveform ---------") for a, b in desc.items(): print(a, ":", b)
def fetch(filename, nevents, nsequence, trigchannel, triglevel, voltdiv): ''' Fetch and save waveform traces from the oscilloscope. ''' scope = LeCroyScope(config.ip, timeout=config.timeout) scope.clear() scope.set_sequence_mode(nsequence) channels = scope.get_channels() print channels settings = scope.get_settings() print settings print trigchannel, triglevel commands = {} if (trigchannel != 999): commands['TRIG_SELECT'] = 'TRSE EDGE,SR,C' + str( trigchannel) + ',HT,OFF' if (triglevel != 999): commands['C2:TRIG_LEVEL'] = 'C2:TRLV ' + str(triglevel) + ' V' commands['C3:TRIG_LEVEL'] = 'C3:TRLV ' + str(triglevel) + ' V' commands['C4:TRIG_LEVEL'] = 'C4:TRLV ' + str(triglevel) + ' V' if (voltdiv != 999): commands['C2:VOLT_DIV'] = 'C2:VDIV ' + str(voltdiv) + ' V' commands['C3:VOLT_DIV'] = 'C3:VDIV ' + str(voltdiv) + ' V' commands['C4:VOLT_DIV'] = 'C4:VDIV ' + str(voltdiv) + ' V' scope.set_settings(commands) newsettings = scope.get_settings() # print newsettings if 'ON' in settings['SEQUENCE']: sequence_count = int(settings['SEQUENCE'].split(',')[1]) else: sequence_count = 1 if nsequence != sequence_count: print 'Could not configure sequence mode properly' if sequence_count != 1: print 'Using sequence mode with %i traces per aquisition' % sequence_count f = {} timef = {} # output file with trigger times for channel in channels: f[channel] = open('%s.ch%s.traces' % (filename, channel), 'wb') timef[channel] = open('%s.ch%s.traces.times' % (filename, channel), 'wb') params_pattern = '=IBdddd' # (num_samples, sample_bytes, v_off, v_scale, h_off, h_scale, [samples]) ... try: i = 0 while i < nevents: print '\rfetching event: %i' % i, sys.stdout.flush() try: scope.trigger() print channels for channel in channels: # print channel wave_desc, wave_array, trig_time_array, trigger_time, acq_duration = scope.get_waveform( channel) num_samples = wave_desc[ 'wave_array_count'] // sequence_count traces = wave_array.reshape( sequence_count, wave_array.size // sequence_count) out = f[channel] outtime = timef[channel] # print num_samples,sequence_count # print trigger_time # print acq_duration outtime.write(str(trigger_time) + ' ') outtime.write(str(acq_duration) + '\n') #New way of writing files # waveform=LeCroyWaveformChannel(wave_desc,wave_array,trig_time_array) # waveform.to_file(out) # waveform.from_file(out) for n in xrange(0, sequence_count): # print "Here",n tempsturct = struct.pack(params_pattern, num_samples, wave_desc['dtype'].itemsize, wave_desc['vertical_offset'], wave_desc['vertical_gain'], -wave_desc['horiz_offset'], wave_desc['horiz_interval']) out.write(tempsturct) traces[n].tofile(out) except (socket.error) as e: print '\n' + str(e) scope.clear() continue i += sequence_count except KeyboardInterrupt: print '\rUser interrupted fetch early' except Exception as e: print "\rUnexpected error:", e finally: print '\r', for channel in channels: print "Closing", channel f[channel].close() timef[channel].close() scope.clear() return i
# it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see <http://www.gnu.org/licenses/>. import os import sys import time import socket import config from lecroy import LeCroyScope if __name__ == '__main__': scope = LeCroyScope(config.ip, timeout=config.timeout) scope.clear() settings = scope.get_settings() print settings while True: cmd = raw_input("> ") if len(cmd) <= 0: break scope.send(cmd); print scope.recv(),
def crunch(filename, nevents, nsequence, ped_start, ped_end, win_start, win_end, load): ''' Fetch and crunch waveform traces from the oscilloscope. ''' scope = LeCroyScope(config.ip, timeout=config.timeout) scope.clear() scope.set_sequence_mode(nsequence) channels = scope.get_channels() settings = scope.get_settings() if 'ON' in settings['SEQUENCE']: sequence_count = int(settings['SEQUENCE'].split(',')[1]) else: sequence_count = 1 if nsequence != sequence_count: print 'Could not configure sequence mode properly' if sequence_count != 1: print 'Using sequence mode with %i traces per aquisition' % sequence_count f = {} for channel in channels: f[channel] = open('%s.ch%s.crunch'%(filename,channel),'wb') try: i = 0 while i < nevents: print '\rfetching event: %i' % i, sys.stdout.flush() try: scope.trigger() for channel in channels: wave_desc,wave_array = scope.get_waveform(channel) num_samples = wave_desc['wave_array_count']//sequence_count traces = wave_array.reshape(sequence_count, wave_array.size//sequence_count) offset = wave_desc['vertical_offset'] gain = wave_desc['vertical_gain'] tinc = wave_desc['horiz_interval'] out = f[channel] for n in xrange(0,sequence_count): ped = traces[n,ped_start:ped_end]*gain-offset win = traces[n,win_start:win_end]*gain-offset values = numpy.asarray([ (ped_end-ped_start+1.0)*tinc, numpy.sum(ped)*tinc/load, numpy.amin(ped), numpy.amax(ped), numpy.std(ped), (win_end-win_start+1.0)*tinc, numpy.sum(win)*tinc/load, numpy.amin(win), numpy.amax(win), numpy.std(win) ],dtype=numpy.float64) values.tofile(out) except (socket.error) as e: print '\n' + str(e) scope.clear() continue i += sequence_count except KeyboardInterrupt: print '\rUser interrupted fetch early' except Exception as e: print "\rUnexpected error:", e finally: print '\r', for channel in channels: f[channel].close() scope.clear() return i
def fetch(filename, nevents, runattrs=None): """ Fetch and save waveform traces from the oscilloscope. Args: - filename: str Filename to store traces in (in hdf5 format). - nevents: int Number of triggered events to save in `filename`. """ scope = LeCroyScope(setup.scope_ip, timeout=20.0) # turn off the display scope.send('display off') scope.check_last_command() # clear the output queue scope.clear() # get active channels channels = scope.getchannels() # get scope configuration settings = get_settings(scope) # get wave descriptors for each channel # important to do this before queue is primed! # need to trigger in order to get correct wave_array_count scope.trigger() time.sleep(5.0) wavedesc = {} for channel in channels: wavedesc[channel] = scope.getwavedesc(channel) # open up the output file f = h5py.File(filename, 'w') # set scope configuration for command, setting in settings.items(): f.attrs[command] = setting if 'ON' in f.attrs['SEQUENCE']: sequence_count = int(f.attrs['SEQUENCE'].split(',')[1]) if sequence_count < 1: raise Exception('sequence count must be a positive number.') else: sequence_count = 1 for channel in channels: nsamples = wavedesc[channel]['wave_array_count'] // sequence_count f.create_dataset('channel%i' % channel, (nevents, nsamples), dtype=wavedesc[channel]['dtype'], chunks=(max(1, min(100, nevents // 100)), nsamples), compression='gzip') for key, value in wavedesc[channel].items(): try: f['channel%i' % channel].attrs[key] = value except ValueError: pass if runattrs is not None: for name in runattrs: for key, value in runattrs[name].items(): f[name].attrs[key] = value # start a timer time0 = time.time() try: i = 0 while True: print '\rsaving event: %i' % i, sys.stdout.flush() try: scope.trigger() for channel in channels: wave_array = scope.getwaveform(channel, wavedesc[channel]) if sequence_count > 1: try: f['channel%i' % channel][i:i+sequence_count] = \ wave_array.reshape(sequence_count, wave_array.size//sequence_count) except ValueError: f['channel%i' % channel][i:i+sequence_count] = \ wave_array.reshape(sequence_count, wave_array.size//sequence_count)[:len(f['channel%i' % channel])-i] else: f['channel%i' % channel][i] = wave_array except (socket.error, struct.error) as e: print '\n' + str(e) scope.clear() continue i += sequence_count if i >= nevents: print '\rsaving event: %i' % i, break print except KeyboardInterrupt: print '\nresizing datasets...' for channel in channels: f['channel%i' % channel].resize( (i, wavedesc[channel]['wave_array_count'] // sequence_count)) raise finally: f.close() scope.clear() scope.send('display on') scope.check_last_command() elapsed = time.time() - time0 if i > 0: print 'Completed %i events in %.3f seconds.' % (i, elapsed) print 'Averaged %.5f seconds per acquisition.' % (elapsed / i) print "Wrote to file '%s'." % filename
if len(args) < 1: sys.exit(parser.format_help()) if options.nevents < 1 or options.nruns < 1: sys.exit("Please specify a number >= 1 for number of events/runs") if options.run_config is not None: options.run_config = getattr(run_setup, options.run_config) if 'file' not in options.run_config or \ 'dataset' not in options.run_config: raise AttributeError( "run configuration must contain 'file' and 'dataset' keys") scope = LeCroyScope(setup.scope_ip, timeout=20.0) # clear the output queue scope.clear() # get active channels channels = scope.getchannels() # close the socket connection del scope runattrs = {'/': {}} print '/' for key, fmt in options.run_config['file'].items():
def fetch(filename, nevents, nsequence): ''' Fetch and save waveform traces from the oscilloscope. ''' scope = LeCroyScope(config.ip, timeout=config.timeout) scope.clear() scope.set_sequence_mode(nsequence) channels = scope.get_channels() settings = scope.get_settings() if 'ON' in settings['SEQUENCE']: sequence_count = int(settings['SEQUENCE'].split(',')[1]) else: sequence_count = 1 if nsequence != sequence_count: print 'Could not configure sequence mode properly' if sequence_count != 1: print 'Using sequence mode with %i traces per aquisition' % sequence_count f = h5py.File(filename, 'w') for command, setting in settings.items(): f.attrs[command] = setting current_dim = {} for channel in channels: wave_desc = scope.get_wavedesc(channel) current_dim[channel] = wave_desc['wave_array_count']//sequence_count f.create_dataset("c%i_samples"%channel, (nevents,current_dim[channel]), dtype=wave_desc['dtype'], compression='gzip', maxshape=(nevents,None)) for key, value in wave_desc.items(): try: f["c%i_samples"%channel].attrs[key] = value except ValueError: pass f.create_dataset("c%i_vert_offset"%channel, (nevents,), dtype='f8') f.create_dataset("c%i_vert_scale"%channel, (nevents,), dtype='f8') f.create_dataset("c%i_horiz_offset"%channel, (nevents,), dtype='f8') f.create_dataset("c%i_horiz_scale"%channel, (nevents,), dtype='f8') f.create_dataset("c%i_num_samples"%channel, (nevents,), dtype='f8') try: i = 0 while i < nevents: print '\rfetching event: %i' % i, sys.stdout.flush() try: scope.trigger() for channel in channels: wave_desc,wave_array = scope.get_waveform(channel) num_samples = wave_desc['wave_array_count']//sequence_count if current_dim[channel] < num_samples: current_dim[channel] = num_samples f['c%i_samples'%channel].resize(current_dim[channel],1) traces = wave_array.reshape(sequence_count, wave_array.size//sequence_count) #necessary because h5py does not like indexing and this is the fastest (and man is it slow) way scratch = numpy.zeros((current_dim[channel],),dtype=wave_array.dtype) for n in xrange(0,sequence_count): scratch[0:num_samples] = traces[n] #'fast' copy to right size f['c%i_samples'%channel][i+n] = scratch #'fast' add to dataset f['c%i_num_samples'%channel][i+n] = num_samples f['c%i_vert_offset'%channel][i+n] = wave_desc['vertical_offset'] f['c%i_vert_scale'%channel][i+n] = wave_desc['vertical_gain'] f['c%i_horiz_offset'%channel][i+n] = -wave_desc['horiz_offset'] f['c%i_horiz_scale'%channel][i+n] = wave_desc['horiz_interval'] except (socket.error, struct.error) as e: print '\n' + str(e) scope.clear() continue i += sequence_count except KeyboardInterrupt: print '\rUser interrupted fetch early' finally: print '\r', f.close() scope.clear() return i