base_basketwPOLY.py
5.6 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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
title = "BasketWeavingPolynom"
tip = "Corrects baseline effects by fitting polynomials in both scanning directions iteratively"
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):
order = IntItem('Order', default=7, max=13, min=1)
clip = FloatItem('SigmaClip', default=-1, max=10.0, min=-1.0)
autocal = BoolItem('Autocal', default=False)
name = title.replace(" ", "")
if args == {}:
#param = FuncParam(_("Press"), "Apply a n-order polynomial fit in scanning direction")
param = FuncParam(_(title), "Apply a n-order polynomial fit in both scanning directions")
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 weight(self, data, sigma):
#mask = -np.isnan(x)
sigma2 = 2*sigma*sigma
w = 0.0*data
rows, cols = w.shape
for row in range(rows):
for col in range(cols):
val = data[row][col]**2
w[row][col] = np.exp(-val/sigma2)
return w
#return lambda x: np.exp(-x*x/sigma2)
def autocal(self, data1, data2, ix):
d1 = 1*np.ravel(data1)
d2 = 1*np.ravel(data2)
d1.sort()
d2.sort()
l = len(d1)/2
d = np.array([d1[l:], np.ones(len(d1[l:]))])
x = np.linalg.lstsq(d.T, d2[l:])
b, a = x[0]
return a + b*data1, data2
def autocal_Old(self, data1, data2, dummy):
mask = (data1 != dummy)
d1 = list(np.ravel(data1[mask]))
mask = (data2 != dummy)
d2 = list(np.ravel(data2[mask]))
d1.sort()
d2.sort()
nl = max(10, len(d1)/10)
a1, a0 = np.polyfit(d2[-nl:], d1[-nl:], 1)
rows, cols = data2.shape
for row in range(rows):
for col in range(cols):
if data2[row, col] != dummy:
data2[row, col] = a0 + a1*data2[row, col]
return data1, data2
def clip(self, Data1, Data2, c):
data1 = 1*Data1
data2 = 1*Data2
for i in range(2):
mask1 = np.isnan(data1)
mask2 = np.isnan(data2)
rms1 = np.std(data1[~mask1])
rms2 = np.std(data2[~mask2])
data1 = np.where(np.abs(data1) > c*rms1, np.nan, data1)
data2 = np.where(np.abs(data2) > c*rms1, np.nan, data2)
return data1, data2
def function(self, ms, p):
dummy = 0.0
lon = 0
lat = 0
lon_data = []
lat_data = []
meanEL = []
for m in ms:
data, w = self.parent.nan_check(m.data, dummy, weight=True)
if "MEANEL" in m.header:
meanEL.append(m.header['MEANEL'])
if m.header['SCANDIR'] in ('ALON', 'XLON', 'ULON', 'GLON', 'RA', 'HA'):
if lon_data == []:
lon_data = data
wlon = w
else:
lon_data += data
wlon += w
lon += 1
elif m.header['SCANDIR'] in ('ALAT', 'XLAT', 'ULAT', 'GLAT', 'DEC'):
if lat_data == []:
lat_data = data
wlat = w
else:
lat_data += data
wlat += w
lat += 1
else:
self.Error(str("sorry, SCANDIR=%s not defined" % m.header['SCANDIR']))
return [], p
if lon == 0 or lat == 0:
if lon == 0: sd = "Longitude"
else: sd = "Latitude"
self.Error(str("sorry, missing maps scanning direction %s" % sd))
return [], p
lon_data, w1 = self.parent.nan_check(lon_data/wlon, dummy, weight=True)
lat_data, w2 = self.parent.nan_check(lat_data/wlat, dummy, weight=True)
if p.autocal: lon_data, lat_data = self.autocal(lon_data, lat_data, dummy)
w = w1 + w2
aver = (lon_data + lat_data) / w
if p.clip > 0.0:
lon_data, lat_data = self.clip(lon_data, lat_data, p.clip)
diff = (lon_data - lat_data) / w
rows, cols = diff.shape
pfit_col = np.zeros((rows, cols))
pfit_row = np.zeros((rows, cols))
x = np.arange(cols)
y = np.arange(rows)
rms = 5.0*np.std(diff)
for row in range(rows):
mask = ~np.isnan(diff[row])
if len(x[mask]) > p.order:
poly = np.poly1d(np.polyfit(x[mask], diff[row][mask], p.order))
pfit_row[row] = poly(x)
for col in range(cols):
mask = ~np.isnan(diff[:,col])
if len(y[mask]) > p.order:
poly = np.poly1d(np.polyfit(y[mask], diff[:,col][mask], p.order))
pfit_col[:,col] = poly(y)
m.data = aver + pfit_col - pfit_row
if not m.header['SCANDIR'] in ("ALON", "ALAT"): m.header.__delitem__('SCANDIR')
if 'SCANNUM' in m.header and m.header['SCANNUM']: m.header.__delitem__('SCANNUM')
if meanEL != []:
m.header['MEANEL'] = np.mean(meanEL)
return m, p