-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrint.py
101 lines (67 loc) · 2.13 KB
/
rint.py
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
"""
Explore robust methods for interpolation, for use with Level 3 data products for
the SWiPS instrument on SWFO-L1.
"""
import numpy as np
import matplotlib.pyplot as plt
import scipy.interpolate as interp
from scipy.stats import norm, cauchy
#------------------------------------------------------------------------------
# define an input time grid that has a cadence of approximately
# but not exactly 1 minute
# size of data sample
N = 50
tmax = float(N)
t2 = np.linspace(0, tmax, num = N+1, endpoint = True, dtype = 'float')
# define random seed for reproducibility
rseed = 52134
np.random.seed(rseed)
eps = norm.rvs(loc = 0.0, scale = 0.1, size = N+1)
# input time grid is regular grid plus random fluctuations and an offset.
t1 = t2 + eps + 0.021
t1 -= t1[0]
# remove a few data points
mask = np.ones(len(t1), dtype=bool)
mask[[7, 12, 43]] = False
t1 = t1[mask]
N = len(t1)
# only use data between 0 and tmax
t2 = t2[np.logical_and(t2 > t1[0], t2 < t1[N-1])]
print(t1)
#------------------------------------------------------------------------------
# define a function on t1 with lots of outliers
# smooth profile
#b1 = np.cos(2*np.pi*t1/tmax)
# here is a shock-like profile
beta = 2.0
b1 = 2*np.arctan(beta*(t1-0.5*tmax))/np.pi
# add outliers
#b1 += cauchy.rvs(loc = 0.0, scale = 0.1, size = N)
b1 += cauchy.rvs(loc = 0.0, scale = 0.01, size = N)
#------------------------------------------------------------------------------
# interpolate
f = interp.interp1d(t1,b1,kind='linear')
b2 = f(t2)
f = interp.interp1d(t1,b1,kind='slinear')
b3 = f(t2)
#------------------------------------------------------------------------------
# Inverse distance weighing
b4 = np.zeros(len(t2))
dt = 2.0
nw = 4
j1 = 0
j2 = 4
for i in np.arange(len(t2)):
t = t2[i]
while t1[j1] < t-dt:
j1 += 1
j2 = j1 + nw
dd = np.abs(t1[j1:j2] - t)
w = np.where(dd > 0.0, 1.0/dd, 1.0)
b4[i] = np.sum(w*b1[j1:j2])/np.sum(w)
#------------------------------------------------------------------------------
plt.figure(figsize=(12,6))
plt.plot(t1,b1,'ko')
plt.plot(t2,b2,'k-',linewidth=4)
plt.plot(t2,b4,'b-',linewidth=2)
plt.show()