#! /usr/bin/env python3 """ Module to make kepler plots from dumps. Example: >>> from kepler.dumpplot import plot as P >>> p = P('~/kepler/znuc/z40/z40#presn') >>> p = P('~/kepler/znuc/z40/z40#presn', 9) >>> p = P('~/kepler/znuc/z40/z40#presn', 9, iproyld=9) for plotting publication-quality plots, you could use >>> p = P('s3z', style='white', showhead=False, showzones=False) >>> p.fig.tight_layout() There is also an command line tool 'd' that takes the plot parameters: > d /home/alex/kepler/znuc/z40/z40#presn plot=9 iproyld=9 > d /home/alex/kepler/znuc/z40/z40#presn style=\"white\" showhead=False, showzones=False """ import time import sys from pathlib import Path from collections import OrderedDict import numpy as np import matplotlib.pylab as plt import kepdump from kepler.plot import plot as pt from kepler.datainterface import DataInterface class DumpData(DataInterface): def __init__(self, dump = None): if not isinstance(dump, kepdump.KepDump): dump = kepdump.load(dump) self.dump = dump @property def filename(self): return Path(self.dump.filename).name @property def runpath(self): return Path(self.dump.filename).absolute().parent @property def datatime(self): return time.asctime(time.strptime(self.dump.lastrun,'%H:%M:%S %d%b%y')) def __getattr__(self, var): o = object() val = getattr(self.dump, var, o) if val is not o: return val if var in ('bfvisc', 'bfdiff', 'bfbr', 'bfbt', 'bfviscef', 'bfdiffef',): return np.zeros((self.qparm.jm+2,)) raise AttributeError(var) @property def idtcsym(self): sym = [x.strip() for x in self.dump.idtcsym] return [''] + sym @property def entropies(self): """ return array of entropes stored data only contains total entropy """ data = np.zeros((self.qparm.jm+2, 6), dtype = np.float64) data[:,0] = self.stot return data class Plot(object): def __init__(self, dump = None): self.dd = DumpData(dump) self.dump = self.dd.dump def plot(self, *args, **kwargs): """ Make kepler plot from dump. """ if len(args) > 0: plot = args[0] args = args[1:] else: plot = kwargs.pop('plot', None) if plot is None: plot = self.dd.ipixtype # this is to be adjusted for plot types return pt(self.dd, plot, *args, **kwargs) def plot(dump = None, *args, **kwargs): """ Shortcut function to make plots from dumps """ return Plot(dump).plot(*args, **kwargs) if __name__ == "__main__": argv = sys.argv args = argv[1:] kwargs = OrderedDict() d = dict() for i,a in enumerate(args): try: args[i] = eval(a, d, d) except: pass for a in list(args): try: kw = eval(f'dict({a})', d, d) kwargs.update(kw) args.remove(a) except: pass # if 'p' in args: # args.remove('p') # kwargs.setdefault("nparallel", 8) p = plot(*args, **kwargs) p.fig.tight_layout() plt.show()