diff options
author | Anthony LaTorre <telatorre@gmail.com> | 2011-06-09 20:46:59 -0400 |
---|---|---|
committer | Anthony LaTorre <telatorre@gmail.com> | 2011-06-09 20:46:59 -0400 |
commit | daee9b48ac6bbc42e1bd406dc4a1c138cb19d257 (patch) | |
tree | 876bf89c626b4ca512b88ca7dadb6957885f94e6 /fetch.py | |
parent | 76982ea70f1eefa481af3cd9f7bafdf92733aad0 (diff) | |
download | lecrunch-daee9b48ac6bbc42e1bd406dc4a1c138cb19d257.tar.gz lecrunch-daee9b48ac6bbc42e1bd406dc4a1c138cb19d257.tar.bz2 lecrunch-daee9b48ac6bbc42e1bd406dc4a1c138cb19d257.zip |
you can now take data using sequence mode! also, updated the README file to include more examples. sock.py now replaces scope.py. sock.py is almost identical, but i renamed the class to Socket and fixed it so that data headers are properly received; previously, it was assumed that the 8 byte header was sent in a single recv() call, which is not always the case. draw.py is a new script to draw waveforms using matplotlib instead of root.
Diffstat (limited to 'fetch.py')
-rwxr-xr-x | fetch.py | 115 |
1 files changed, 69 insertions, 46 deletions
@@ -21,21 +21,21 @@ import time import socket import struct import h5py +import setup from lecroy import LeCroyScope from config import get_settings -def fetch(filename, events): +def fetch(filename, nevents): """ Fetch and save waveform traces from the oscilloscope. Args: - filename: str Filename to store traces in (in hdf5 format). - - events: int + - nevents: int Number of triggered events to save in `filename`. """ - scope = LeCroyScope('scope01.hep.upenn.edu') - scope.connect() + scope = LeCroyScope(setup.scope_ip, timeout=20.0) # turn off the display scope.send('display off') @@ -52,33 +52,38 @@ def fetch(filename, events): # 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) - # prime the output queue - for i in range(10): - scope.trigger() - for channel in channels: - scope.send('c%i:wf? dat1' % channel) - # open up the output file f = h5py.File(filename, 'w') + # set scope configuration for command, setting in settings.items(): f.attrs[command] = setting - dataset = {} + + 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: - samples = wavedesc[channel]['wave_array_count'] - chunks = (max(1,min(100, events//100)), samples) - dataset[channel] = f.create_dataset('channel%i' % channel, - (events, samples), - dtype=wavedesc[channel]['dtype'], - chunks=chunks, - compression='gzip') + 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: - dataset[channel].attrs[key] = value + f['channel%i' % channel].attrs[key] = value except ValueError: pass @@ -86,53 +91,68 @@ def fetch(filename, events): time0 = time.time() try: - for i in range(events): - print '\rSaving event: %i' % (i+1), + i = 0 + while True: + print '\rsaving event: %i' % i, sys.stdout.flush() try: scope.trigger() for channel in channels: - dataset[channel][i] = scope.getwaveform(channel, - wavedesc[channel]) - except (socket.error, struct.error, RuntimeError, OverflowError): - print "\nFailed to receive waveform %i" % (i+1) - # clear the output queue + 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() - # reprime the queue - for i in range(10): - scope.trigger() - for channel in channels: - scope.send('c%i:wf? dat1' % channel) continue + + i += sequence_count + + if i >= nevents: + print '\rsaving event: %i' % i, + break + print + except KeyboardInterrupt: - print - print 'Resizing datasets...' + print '\nresizing datasets...' + for channel in channels: - dataset[channel].resize((i, wavedesc[channel]['wave_array_count'])) - raise KeyboardInterrupt + 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() - scope.close() elapsed = time.time() - time0 - print 'Completed %i events in %.3f seconds.' % (i+1, elapsed) - print 'Averaged %.5f seconds per acquisition.' % (elapsed/(i+1)) - print "Wrote to file '%s'." % filename + 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 __name__ == '__main__': import optparse usage = "usage: %prog <filename/prefix> [-n] [-r]" parser = optparse.OptionParser(usage, version="%prog 0.1.0") - parser.add_option("-n", type="int", dest="events", + parser.add_option("-n", type="int", dest="nevents", help="number of events to store per run", default=1000) - parser.add_option("-r", type="int", dest="runs", + parser.add_option("-r", type="int", dest="nruns", help="number of runs", default=1) parser.add_option("--time", action="store_true", dest="time", help="append time string to filename", default=False) @@ -141,16 +161,19 @@ if __name__ == '__main__': if len(args) < 1: sys.exit(parser.format_help()) - if options.events < 1 or options.runs < 1: + if options.nevents < 1 or options.nruns < 1: sys.exit("Please specify a number >= 1 for number of events/runs") - if options.runs == 1 and not options.time: - fetch(args[0], options.events) + if options.nruns == 1 and not options.time: + try: + fetch(args[0], options.nevents) + except KeyboardInterrupt: + pass else: import time import string - for i in range(options.runs): + for i in range(options.nruns): timestr = string.replace(time.asctime(time.localtime()), ' ', '-') filename = args[0] + '_' + timestr + '.hdf5' print '-' * 65 @@ -158,6 +181,6 @@ if __name__ == '__main__': print '-' * 65 try: - fetch(filename, options.events) + fetch(filename, options.nevents) except KeyboardInterrupt: break |