misc_print.py 2.75 KB
title = "Print"
tip = "print hist-data to file"
onein = True

import numpy as np

from guidata.qt.QtGui import QMessageBox
from guidata.dataset.datatypes import DataSet
from guidata.dataset.dataitems import (IntItem, StringItem, ChoiceItem, FloatItem, BoolItem)
from guiqwt.config import _

class NOD3_App:

    def __init__(self, parent):
        self.parent = parent
        self.parent.activateWindow()

    def Error(self, msg):
        QMessageBox.critical(self.parent.parent(), title,
                              _(u"Error:")+"\n%s" % str(msg))

    def compute_app(self, **args):
        units = ('Pixel', 'Degree', 'Arcmin', 'Arcsec')
        class FuncParam(DataSet):
            fname = StringItem('Filename:', default="histo.txt")
            #i = IntItem('i', default=0, max=100, min=0)
            #a = FloatItem('a', default=1.)
            #b = BoolItem("bool", default=True)
            #c = ChoiceItem(_(u"Mode"), zip(units, units), default=units[0])
        name = title.replace(" ", "")
        if args == {}:
           param = FuncParam(_(title), "Prints histogram data to file")
        else:
           param = self.parent.ScriptParameter(name, args)

        # if no parameter needed set param to None. activate next line
        #param = None
        self.parent.compute_11(name, lambda m, p: self.function(m, p), param, onein) 

    def gnuplot(self, fname):
        fgnu = fname[:fname.find(".")] + ".gnu"
        xmin = int(self.parent.hist_x.min())
        xmax = int(self.parent.hist_x.max())
        ymin = int(self.parent.hist_y.min())
        ymax = int(self.parent.hist_y.max())
        text = str('"Fitted values:\\n RMS: %.3f\\n Off: %.3f"' % (self.parent.hist_f_rms, self.parent.hist_f_off))
        f = open(fgnu, "w")
        f.write("set key autotitle columnhead\n")
        f.write(str("text = %s\n" % text))
        f.write("set xlab 'Intensity'\n")
        f.write("set ylab 'Frequency'\n")
        f.write(str("set xrange [%d:%d]\n" % (xmin, xmax)))
        f.write(str("set yrange [%d:%d]\n" % (0, ymax+int(ymax/10))))
        f.write(str("plot '%s' using 1:2 with boxes lc 3 title 'Data', '%s' using 1:3 with line lc 1 lw 2 title text" % (fname, fname)))
        f.close()
        
    def function(self, m, p):
        if hasattr(self.parent, 'hist_x'):
           f = open(p.fname, "w")
           for i in range(len(self.parent.hist_x)):
               if self.parent.hist_f == None:
                  f.write(str("%f  %f \n" % (self.parent.hist_x[i], self.parent.hist_y[i])))
               else:
                  f.write(str("%f  %f %f \n" % (self.parent.hist_x[i], self.parent.hist_y[i], self.parent.hist_f[i])))
                  self.gnuplot(p.fname)
           f.close()
        else:
           self.Error("no histogram data available")
        return [], p