maped_pixel.py
2.18 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
title = "MapPixel"
tip = "selects a part of a map"
onein = True
import numpy as np
from nodmath import extract
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 _
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')
posx = FloatItem('center coordinate l')
posy = FloatItem('center coordinate b')
naxis1 = IntItem('pixel shape x')
naxis2 = IntItem('pixel shape y')
name = title.replace(" ", "")
if args == {}:
param = FuncParam(_(title), "Select center pixel and dimensions:\n")
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 function(self, m, p):
if not 'CRVAL1' in m.header.keys() or not 'CRVAL2' in m.header.keys():
posx = p.posx
posy = p.posy
else:
if m.header['CTYPE1'].find("DES") > 0:
l = 0.0
b = 0.0
else:
l = m.header['CRVAL1']
b = m.header['CRVAL2']
posx = m.header['CRPIX1'] + (p.posx-l)/m.header['CDELT1']
posy = m.header['CRPIX2'] + (p.posy-b)/m.header['CDELT2']
m.data = extract(m.data, shape=(p.naxis2, p.naxis1), position=(posy, posx))
try:
m.header['CRPIX1'] -= p.posx - int(0.5*p.naxis1)
m.header['CRPIX2'] -= p.posy - int(0.5*p.naxis2)
except: pass
m.header['NAXIS1'] = p.naxis1
m.header['NAXIS2'] = p.naxis2
return m, p