misc_mean.py 2.44 KB
title = "Average maps"
tip = "calculates statistical value from map extraction"
onein = False

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):
        class FuncParam(DataSet):
            row1 = IntItem(_("First row index"), default=0, min=-1)
            row2 = IntItem(_("Last row index"), default=-1, min=-1)
            col1 = IntItem(_("First column index"), default=0, min=-1)
            col2 = IntItem(_("Last column index"), default=-1, min=-1)
        name = title.replace(" ", "")
        if args == {}:
           param = FuncParam(_(title), "select ROI from map")
        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 check_center_position(self, ms):
        pos = []
        rot = []
        l = []
        b = []
        d = []
        p = []
        for m in ms:
            l.append(m.header['CRVAL1'])
            b.append(m.header['CRVAL2'])
            d.append(m.header['CDELT1'])
            p.append(m.header['CTYPE1'])
            pos.append((m.header['CRPIX1'], m.header['CRPIX2']))
            if 'CROTA2' in m.header.keys():
               rot.append(m.header['CROTA2'])
        if p.count(p[0]) != len(p):
           self.Error("Maps have different projection types")
           return [], p
        if rot != [] and abs(max(rot)-min(rot)) > 1.e-6:
           self.Error("Maps have different rotation angles")
           return [], p
        if abs(max(d)-min(d)) > 1.e-6:
           self.Error("Maps have different spacings")
           return [], p
        dm = abs(np.mean(d))
        if abs(max(l)-min(l)) > dm/10.0 or abs(max(b)-min(b)) > dm/10.0:        
           self.Error("Maps have different coordinates")
           return [], p
        return pos 

    def function(self, ms, p):
        pos = self.check_center_position(ms)
        print pos
        return [], None