Commit b290d098 authored by W. Trevor King's avatar W. Trevor King
Browse files

Add -p/--plot option so scripts will plot waves.

parent e0a5ed57
...@@ -27,13 +27,16 @@ from igor.binarywave import load ...@@ -27,13 +27,16 @@ from igor.binarywave import load
from igor.script import Script from igor.script import Script
def run(args): class WaveScript (Script):
wave = load(args.infile) def _run(self, args):
numpy.savetxt(args.outfile, wave['wave']['wData'], fmt='%g', delimiter='\t') wave = load(args.infile)
if args.verbose > 0: numpy.savetxt(
wave['wave'].pop('wData') args.outfile, wave['wave']['wData'], fmt='%g', delimiter='\t')
pprint.pprint(wave) self.plot_wave(args, wave)
if args.verbose > 0:
s = Script(description=__doc__) wave['wave'].pop('wData')
s._run = run pprint.pprint(wave)
s = WaveScript(description=__doc__)
s.run() s.run()
...@@ -23,25 +23,34 @@ import pprint ...@@ -23,25 +23,34 @@ import pprint
import numpy import numpy
from igor.packed import load from igor.packed import load, walk
from igor.record.wave import WaveRecord
from igor.script import Script from igor.script import Script
def run(args): class PackedScript (Script):
records,filesystem = load(args.infile) def _run(self, args):
if hasattr(args.outfile, 'write'): self.args = args
f = args.outfile # filename is actually a stream object records,filesystem = load(args.infile)
else: if hasattr(args.outfile, 'write'):
f = open(args.outfile, 'w') f = args.outfile # filename is actually a stream object
try: else:
f.write(pprint.pformat(records)) f = open(args.outfile, 'w')
f.write('\n') try:
finally: f.write(pprint.pformat(records))
if f != args.outfile: f.write('\n')
f.close() finally:
if args.verbose > 0: if f != args.outfile:
pprint.pprint(filesystem) f.close()
if args.verbose > 0:
s = Script(description=__doc__, filetype='IGOR Packed Experiment (.pxp) file') pprint.pprint(filesystem)
s._run = run walk(filesystem, self._plot_wave_callback)
def _plot_wave_callback(self, dirpath, key, value):
if isinstance(value, WaveRecord):
self.plot_wave(self.args, value.wave, title=dirpath + [key])
s = PackedScript(
description=__doc__, filetype='IGOR Packed Experiment (.pxp) file')
s.run() s.run()
...@@ -22,6 +22,12 @@ import argparse as _argparse ...@@ -22,6 +22,12 @@ import argparse as _argparse
import logging as _logging import logging as _logging
import sys as _sys import sys as _sys
try:
import matplotlib as _matplotlib
import matplotlib.pyplot as _matplotlib_pyplot
except ImportError as _matplotlib_import_error:
_matplotlib = None
from . import __version__ from . import __version__
from . import LOG as _LOG from . import LOG as _LOG
...@@ -38,9 +44,13 @@ class Script (object): ...@@ -38,9 +44,13 @@ class Script (object):
self.parser.add_argument( self.parser.add_argument(
'-o', '--outfile', metavar='FILE', default='-', '-o', '--outfile', metavar='FILE', default='-',
help='file for ASCII output') help='file for ASCII output')
self.parser.add_argument(
'-p', '--plot', action='store_const', const=True,
help='use Matplotlib to plot any IGOR waves')
self.parser.add_argument( self.parser.add_argument(
'-V', '--verbose', action='count', default=0, '-V', '--verbose', action='count', default=0,
help='increment verbosity') help='increment verbosity')
self._num_plots = 0
def run(self, *args, **kwargs): def run(self, *args, **kwargs):
args = self.parser.parse_args(*args, **kwargs) args = self.parser.parse_args(*args, **kwargs)
...@@ -52,6 +62,24 @@ class Script (object): ...@@ -52,6 +62,24 @@ class Script (object):
log_level = self.log_levels[min(args.verbose-1, len(self.log_levels)-1)] log_level = self.log_levels[min(args.verbose-1, len(self.log_levels)-1)]
_LOG.setLevel(log_level) _LOG.setLevel(log_level)
self._run(args) self._run(args)
self.display_plots()
def _run(self, args): def _run(self, args):
raise NotImplementedError() raise NotImplementedError()
def plot_wave(self, args, wave, title=None):
if not args.plot:
return # no-op
if not _matplotlib:
raise _matplotlib_import_error
if title is None:
title = wave['wave']['wave_header']['bname']
figure = _matplotlib_pyplot.figure()
axes = figure.add_subplot(1, 1, 1)
axes.set_title(title)
axes.plot(wave['wave']['wData'], 'r.')
self._num_plots += 1
def display_plots(self):
if self._num_plots:
_matplotlib_pyplot.show()
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment