maped_ellipse.py
2.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# -*- 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