maped_ellipse.py 2.45 KB
# -*- coding: utf-8 -*-
title = "Ellipse"
tip = "selects a elliptic part of a map"
onein = -1

import numpy as np
from nodmath import extract

from guidata.qt.QtGui import QMessageBox, QApplication, QCursor
from guidata.dataset.datatypes import DataSet
from guidata.dataset.dataitems import (IntItem, StringItem, ChoiceItem, FloatItem, BoolItem)
from guidata.qt.QtCore import Qt
from guiqwt.events import setup_standard_tool_filter, KeyEventMatch
from guiqwt.tools import AnnotatedRectangle, RectangularShapeTool
from guiqwt.config import _

def nint(x):
    if x > 0: return int(x+0.5)
    else: return int(x-0.5)


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):
            posx = IntItem('center x')
            posy = IntItem('center y')
            naxis1 = IntItem('shape x')
            naxis2 = IntItem('shape y')
        self.name = title.replace(" ", "")
        if args == {}:
           param = FuncParam(_(title), "Use box to select a map extraction:\n")
        else:
           param = self.parent.ScriptParameter(name, args)

        # if no parameter needed set param to None. activate next line
        param = None
        self.Pixel = self.parent.Pixel
        self.parent.view_axes_pixel()
        self.parent.get_box_positions(self.name, 
                                      lambda m, p, pos: self.function(m, p, pos),
                                      param, nextbox=False, mshape="ellipse")

    def Ellipse(self, data, pos):
        rows, cols = data.shape
        x0, y0 = self.parent.get_pixel_coordinates(*pos[0][:2])
        a = max(self.parent.get_pixel_coordinates(*pos[1][:2]))/2
        b = min(self.parent.get_pixel_coordinates(*pos[1][:2]))/2
        phi = -pos[2] * np.pi/180.0
        rot = np.array([[np.cos(phi), -np.sin(phi)], [np.sin(phi), np.cos(phi)]])
        y, x = np.ogrid[:rows, :cols]
        xr, yr = np.dot(rot, np.array([x-x0,y-y0]))
        r = (xr/a)**2 + (yr/b)**2        
        mask = r <= 1.0 
        data[~mask] = np.nan
        QApplication.restoreOverrideCursor()
        return data

    def function(self, m, p, pos):
        m.data = self.Ellipse(m.data, pos)
        if not self.Pixel:
           self.parent.view_axes_coordinate()
        return m, p