Skip to content

Commit

Permalink
rudimentary implementation of file save (png)
Browse files Browse the repository at this point in the history
  • Loading branch information
jaga-mcstas committed Aug 19, 2015
1 parent 8ae891f commit 1f2fa7e
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 8 deletions.
40 changes: 33 additions & 7 deletions tools/Python/mcplot/gnuplot/mcgnuplotter.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
import re
import os

# supported file save terminals and the corresponding file extensions
class McGnuplotFileTerminals():
png = ['png', 'png']
gif = ['gif', 'gif']
postscript = ['postscript', 'ps']

# base class for mcstas gnuplot objects
class McGnuplotObject():
def __init__(self, key, data_struct, gp):
Expand All @@ -18,6 +24,16 @@ def __init__(self, key, data_struct, gp):
self.log_scale = False
return

def save(self, term):
""" saves to file depending on term: [<gnuplot terminal>, <file extension>] """
self.gp('set terminal %s' % term[0])
out_file_noext = '%s' % os.path.splitext(self.data['fullpath'])[0]
if self.log_scale:
out_file_noext = out_file_noext + '_log'
self.gp('set output "%s.%s"' % (out_file_noext, term[1]))
self.plot()
self.gp('set term pop')

def plot(self):
""" give data member to plot_impl """
self.plot_impl(self.gp, self.data)
Expand All @@ -40,10 +56,10 @@ def setLog_impl(gp, log_scale):
# implements overview plotting
# NOTE: overrides plot() and setLog() rather than the standard plot_impl() and setLog_impl()
class McGnuplotOverview(McGnuplotObject):
def __init__(self, key, gp, siblings):
def __init__(self, key, data_struct, gp, siblings):
self.__setLogOnce = True
self.__siblings = siblings
return McGnuplotObject.__init__(self, key, None, gp)
return McGnuplotObject.__init__(self, key, data_struct, gp)

def plot(self):
""" plots all files to a singe window as multiplot (individually as array_1d or array_2d) """
Expand All @@ -65,6 +81,8 @@ def plot(self):
for sib in self.__siblings:
sib.setLog_impl(self.gp, self.log_scale)
sib.plot_impl(self.gp, sib.data)

self.gp('unset multiplot')

@staticmethod
def __calc_panel_size(num):
Expand Down Expand Up @@ -147,12 +165,12 @@ def setLog_impl(gp, log_scale):
else:
gp('unset logscale y')

# mcgnuplot proxy and constructor
# mediator for all gnuplot objects associated to a .sim file (or of a single .dat file) - see mcplot.py, gnuplot version
class McGnuplotter():
__overview_key = '< overview >'

def __init__(self, input_file, noqt=False, log_scale=False):
""" constructor - takes a .sim file or a .dat file name (for single vs. multiplot usage) """
""" constructor - takes a .sim file or a .dat file name (for single vs. multiplot usage) NOTE: must be absolute file """
# remember construction args
self.arg_input_file = input_file
self.arg_noqt = noqt
Expand All @@ -178,8 +196,9 @@ def __init__(self, input_file, noqt=False, log_scale=False):
else:
gpo = McGnuplot1D(data_struct['file'], data_struct, Gnuplot.Gnuplot(persist=gp_persist))
siblings.append(gpo)

overview = McGnuplotOverview(McGnuplotter.__overview_key, Gnuplot.Gnuplot(persist=gp_persist), siblings)
overview_data_struct = {}
overview_data_struct['fullpath'] = input_file
overview = McGnuplotOverview(McGnuplotter.__overview_key, overview_data_struct, Gnuplot.Gnuplot(persist=gp_persist), siblings)
self.__gnuplot_objs[overview.key] = overview
for gpo in siblings:
self.__gnuplot_objs[gpo.key] = gpo
Expand All @@ -200,12 +219,19 @@ def plot(self, key):
self.__gnuplot_objs[key].plot()
else:
raise Exception('McGnuplotter.plot: no such key')

def save(self, key):
""" like plot, but saves to file """
if key in self.__gnuplot_objs:
self.__gnuplot_objs[key].save(McGnuplotFileTerminals.png)
else:
raise Exception('McGnuplotter.save: no such key: %s' % key)

def get_data_keys(self):
""" returns an alpha-num sorted list of all McGnuplotObject instances installed at construction time by key """
return sorted(self.__gnuplot_objs.keys(), key=lambda item: (int(item.partition(' ')[0])
if item[0].isdigit() else float('inf'), item))

def setLogscale(self, log_scale):
for key in self.__gnuplot_objs:
self.__gnuplot_objs[key].setLog(log_scale)
Expand Down
8 changes: 7 additions & 1 deletion tools/Python/mcplot/gnuplot/mcgnuview.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def initUi(self, log_scale):
def setupCallbacks(self):
self.__mcgv.ui.lstvMonitors.clicked.connect(self.itemMouseClick)
self.__mcgv.ui.btnCloseAll.clicked.connect(self.handleCloseAll)
self.__mcgv.ui.btnSaveAs.clicked.connect(lambda: self.__mcgv.ui.statusBar.showMessage('not implemented'))
self.__mcgv.ui.btnSaveAs.clicked.connect(lambda: self.__plotter.save(self.__mcgv.getSelectedKey()))
self.__mcgv.ui.cbxLogScale.stateChanged.connect(self.setLogscale)

def handleCloseAll(self):
Expand Down Expand Up @@ -79,6 +79,12 @@ def showMessage(self, msg):
def isLogScaleEnabled(self):
return self.ui.cbxLogScale.isChecked()

def getSelectedKey(self):
selected_items = self.ui.lstvMonitors.selectedItems()
if len(selected_items)==0:
return str(self.ui.lstvMonitors.item(0).text())
return str(self.ui.lstvMonitors.selectedItems()[0].text())

# enables this class as an event filter
def eventFilter(self, obj, event):
if event.type() == QtCore.QEvent.KeyPress:
Expand Down

0 comments on commit 1f2fa7e

Please sign in to comment.