From 32fcbee90916833cec99594b95d4eb96d25cfbec Mon Sep 17 00:00:00 2001 From: Anthony LaTorre Date: Mon, 22 Aug 2011 15:08:12 -0400 Subject: add ability to specify file and dataset attributes by prompting the user when running fetch.py --- fetch.py | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++--- run_setup.py | 33 +++++++++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 run_setup.py diff --git a/fetch.py b/fetch.py index eaeca88..91dfe4a 100755 --- a/fetch.py +++ b/fetch.py @@ -25,7 +25,7 @@ import setup from lecroy import LeCroyScope from config import get_settings -def fetch(filename, nevents): +def fetch(filename, nevents, runattrs=None): """ Fetch and save waveform traces from the oscilloscope. @@ -87,6 +87,11 @@ def fetch(filename, nevents): 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() @@ -147,6 +152,7 @@ def fetch(filename, nevents): if __name__ == '__main__': import optparse + import run_setup usage = "usage: %prog [-n] [-r]" parser = optparse.OptionParser(usage, version="%prog 0.1.0") @@ -156,6 +162,8 @@ if __name__ == '__main__': help="number of runs", default=1) parser.add_option("--time", action="store_true", dest="time", help="append time string to filename", default=False) + parser.add_option("-c", dest="run_config", + help="run configuration dictionary name", default=None) (options, args) = parser.parse_args() if len(args) < 1: @@ -164,9 +172,58 @@ if __name__ == '__main__': 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(): + prompt = '/%s? ' % key + while True: + try: + runattrs['/'][key] = fmt(raw_input(prompt)) + except ValueError as e: + print e + continue + break + + for name in ['channel%i' % i for i in channels]: + runattrs[name] = {} + + print '/' + name + + for key, fmt in options.run_config['dataset'].items(): + prompt = '/%s.%s? ' % (name, key) + while True: + try: + runattrs[name][key] = fmt(raw_input(prompt)) + except ValueError as e: + print e + continue + break + else: + runattrs = None + if options.nruns == 1 and not options.time: try: - fetch(args[0], options.nevents) + fetch(args[0], options.nevents, runattrs) except KeyboardInterrupt: pass else: @@ -181,6 +238,6 @@ if __name__ == '__main__': print '-' * 65 try: - fetch(filename, options.nevents) + fetch(filename, options.nevents, runattrs) except KeyboardInterrupt: break diff --git a/run_setup.py b/run_setup.py new file mode 100644 index 0000000..ff220e9 --- /dev/null +++ b/run_setup.py @@ -0,0 +1,33 @@ +""" +This file contains dictionaries for defining attributes to attach to data +files. Each dictionary should contain a 'file' and 'dataset' key whose values +are dictionaries specifying attributes to attach to the root of the hdf5 file +and each dataset in the file respectively. The values in these dictionaries +should contain the type to convert the user's input to before storing them +in the data file. + +Example: + >>> fetch.py test.hdf5 -c default + / + /docstring? new batch of pmts + /channel1 + /channel1.pmtid? trig + /channel1.voltage? 800 + /channel1.termination? 50.0 + /channel2 + /channel2.pmtid? ZN0103 + /channel2.voltage? 1800.0 + /channel2.termination? 50.0 + ... + saving event: 1000 + Completed 1000 events in 2.535 seconds. + Averaged 0.00253 seconds per acquisition. + Wrote to file 'test.hdf5'. +""" + +import string + +default = { 'file' : { 'docstring' : str }, + 'dataset' : { 'pmtid' : string.lower, + 'voltage' : float, + 'termination' : float } } -- cgit