pol_mapclip.py 2.84 KB
title = "PerPol"
tip = "clips maps and devide them"
onein = 2

import numpy as np
import scipy.stats as ss
from scipy import optimize, signal

from guiqwt import pyplot
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):
        class FuncParam(DataSet):
            #s = StringItem('s', default="string")
            #i = IntItem('i', default=0, max=100, min=0)
            clip = FloatItem('Clip', default=1.)
            #b = BoolItem("bool", default=True)
            #clipval = ChoiceItem("Clip value", (("Minimum", "Minimum"), ("Dummy", "Dummy"),
            clipval = ChoiceItem("Clip value", (("Dummy", "Dummy"), ("RMS", "RMS")), default="Dummy")
        name = title.replace(" ", "")
        if args == {}:
           param = FuncParam(_(title), "description")
        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 plot(self, xy, p, lh, x0):
        x = xy[1][:lh]
        y = xy[0][:lh]
        self.parent.fig = pyplot.figure("Rice distribution")
        sig = str("sigma = %f" % x0)
        pyplot.plot(x, y, "b-", label=sig)
        pyplot.legend()
        pyplot.plot(x, p(x), "r-", label="fit")
        pyplot.xlabel("sqrt(U^2 + Q^2)")
        pyplot.ylabel("#bins")
        pyplot.zlabel("Rice distribution")
        #self.parent.fig.win.setWindowIcon(get_icon(sys.path[0]+'/MPIfR.svg'))
        pyplot.show(mainloop=False)

    def function(self, ms, p):
        data = None
        for m in ms:
            if m.header["MAPTYPE"] == "PI":
               PI = m.data
               rms = m.header['PSIG']
            elif m.header["MAPTYPE"][0] == "I":
               m.header["MAPTYPE"] = "I"
               I = m.data 
            else:
               self.Error("sorry, please check map type")
               return [], p
        if p.clipval == "Minimum": 
           cval = np.nanmin(PI)
        elif p.clipval == "RMS":
           cval = p.clip*rms
        else:
           cval = np.nan
        Iclip = np.where(I < p.clip*rms, cval, I)
        m.data = PI / Iclip
        m.header["MAPTYPE"] = 'PP'
        if 'EXTNAME' in m.header:
           extname = m.header['EXTNAME'].replace("PI", 'PP').replace("I", 'PP')
        else:
           extname = "MAP-PP"
        m.header["EXTNAME"] = extname
        return m, p