-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprocessing.py
More file actions
235 lines (177 loc) · 7.45 KB
/
processing.py
File metadata and controls
235 lines (177 loc) · 7.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
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# Module for performing various processing of data
# Written by rhwhite rachel.white@cantab.net
import numpy as np
import xarray as xr
from scipy import stats,signal
import scipy.fftpack as fftpack
from scipy.interpolate import interp1d,CubicSpline
# Lanczos function weight calculation. confirmed it matches that from NCL when normalization added.
def low_pass_weights(window, cutoff):
"""Calculate weights for a low pass Lanczos filter.
Args:
window: int
The length of the filter window.
cutoff: float
The cutoff frequency in inverse time steps.
"""
order = ((window - 1) // 2 ) + 1
nwts = 2 * order + 1
w = np.zeros([nwts])
n = nwts // 2
w[n] = 2 * cutoff
k = np.arange(1., n)
sigma = np.sin(np.pi * k / n) * n / (np.pi * k)
firstfactor = np.sin(2. * np.pi * cutoff * k) / (np.pi * k)
w[n-1:0:-1] = firstfactor * sigma
w[n+1:-1] = firstfactor * sigma
# Needed to add a normalization factor to make the weights add up to 1.
norm = np.sum(w)
w = w/norm
return w[1:-1]
def Lanczos_filter(datain,nwghts,fca):
# Calculate Lanczos weights
lp_wghts = low_pass_weights(nwghts,fca)
timefilt = np.ndarray(datain.shape)
try:
lons = datain.lon
except AttributeError:
lons = datain.longitude
try:
lats = datain.lat
except AttributeError:
lats = datain.latitude
nlons = len(lons)
nlats = len(lats)
ntimes = len(datain.time)
temp = xr.DataArray(datain.values,coords={'time':datain.time,
'longitude':lons.values,'latitude':lats.values},
dims = ('time','latitude','longitude'))
# convolve data with weights
for ilat in np.arange(0,nlats):
for ilon in np.arange(0,nlons):
timefilt[:,ilat,ilon] = np.convolve(datain[:,ilat,ilon], lp_wghts, 'same')
timefilt[0:int((nwghts-1)/2),:,:] = np.nan
timefilt[int(ntimes - (nwghts-1)/2):ntimes,:,:] = np.nan
# save to xarray
xr.DataArray(np.random.randn(2, 3), coords={'x': ['a', 'b']}, dims=('x', 'y'))
xrtimefilt = xr.DataArray(timefilt,coords={'time':datain.time,
'longitude':lons.values,'latitude':lats.values},
dims = ('time','latitude','longitude'))
return(xrtimefilt)
def fourier_Tukey(indata,nlons,peak_freq,ndegs=360):
X_fft = fftpack.fft(indata)
f_s = nlons
freqs = fftpack.fftfreq(len(indata)) * f_s
t = np.linspace(0, ndegs, f_s, endpoint=False)
filt_fft = X_fft.copy()
filt_fft[np.abs(freqs) > peak_freq] = 0
filtered_sig = fftpack.ifft(filt_fft)
# create Tukey window to smooth the wavenumbers removed (so no exact cutoff at k=5,
#which will change at different latitudes)
# Window is 2 wavenumbers more than the peak, but multiplied by 2 because the Tukey window is symmetric
M = (peak_freq + 2)*2
alpha = 0.3 # co-sine weighting covers 30% of the window
tukeyWin = signal.tukey(M, alpha=0.3, sym=True)[int(M/2):M]
turfilt_fft = X_fft.copy()
n = len(turfilt_fft)
turfilt_fft[0:int(M/2)] = turfilt_fft[0:int(M/2)]*tukeyWin
turfilt_fft[int(M/2):n-int(M/2)] = 0
turfilt_fft[n-int(M/2):n] = turfilt_fft[n-int(M/2):n]*tukeyWin[::-1]
tur_filtered_sig = fftpack.ifft(turfilt_fft)
return(tur_filtered_sig,filtered_sig,t)
def fourier_Tukey_hilow_3D(indata,nlons,s1,s2,ndegs=360):
try:
ntimes = len(indata.times)
except AttributeError:
ntimes = 1
X_fft = fftpack.fft(indata)
f_s = nlons
freqs = fftpack.fftfreq(nlons) * f_s
t = np.linspace(0, ndegs, f_s, endpoint=False)
filt_fft = X_fft.copy()
filt_fft[:,np.abs(freqs) > s2] = 0
filt_fft[:,np.abs(freqs) < s1] = 0
filtered_sig = fftpack.ifft(filt_fft)
# create Tukey window to smooth the wavenumbers removed (so no exact cutoff at any particular wavenumber,
# which will change at different latitudes)
# Window is 2 wavenumbers more than the peak, but multiplied by 2 because the Tukey window is symmetric
limit1 = np.amax([0,s1 - 1.5])
limit2 = s2 + 1.5
diff = limit2-limit1
M = 100
tukeymax = int(np.amax([limit1,limit2])) + 10
tukeydata = signal.tukey(M, alpha=0.3, sym=True)
# Add zeros at front and back of window for interpolation
tukeydata = np.insert(tukeydata,0,np.zeros(40))
tukeydata = np.append(tukeydata,np.zeros(40))
# wavenumber values for Tukey window with added zeros
xs = np.arange(limit1-40.0*diff/M,limit2+ 39.999*diff/M,diff/M)
# model tukey window with cubic splines
wn_cs = np.linspace(0,tukeymax-1,tukeymax)
CS = CubicSpline(xs,tukeydata)
tukeyWin = CS(wn_cs)
turfilt_fft = X_fft.copy()
n = len(turfilt_fft[0,:])
turfilt_fft[:,0:tukeymax] = turfilt_fft[:,0:tukeymax]*tukeyWin
turfilt_fft[:,tukeymax:n-tukeymax] = 0
turfilt_fft[:,n-tukeymax:n] = turfilt_fft[:,n-tukeymax:n]*tukeyWin[::-1]
tur_filtered_sig = fftpack.ifft(turfilt_fft)
return(tur_filtered_sig,filtered_sig,t)
def fourier_Tukey_hilow(indata,nlons,s1,s2,ndegs=360):
X_fft = fftpack.fft(indata)
f_s = nlons
freqs = fftpack.fftfreq(len(indata)) * f_s
t = np.linspace(0, ndegs, f_s, endpoint=False)
filt_fft = X_fft.copy()
filt_fft[np.abs(freqs) > s2] = 0
filt_fft[np.abs(freqs) < s1] = 0
filtered_sig = fftpack.ifft(filt_fft)
# create Tukey window to smooth the wavenumbers removed (so no exact cutoff at any particular wavenumber,
# which will change at different latitudes)
# Window is 2 wavenumbers more than the peak, but multiplied by 2 because the Tukey window is symmetric
limit1 = np.amax([0,s1 - 1.5])
limit2 = s2 + 1.5
diff = limit2-limit1
M = 100
tukeymax = int(np.amax([limit1,limit2])) + 10
tukeydata = signal.tukey(M, alpha=0.3, sym=True)
# Add zeros at front and back of window for interpolation
tukeydata = np.insert(tukeydata,0,np.zeros(40))
tukeydata = np.append(tukeydata,np.zeros(40))
# wavenumber values for Tukey window with added zeros
xs = np.arange(limit1-40.0*diff/M,limit2+ 39.999*diff/M,diff/M)
# model tukey window with cubic splines
wn_cs = np.linspace(0,tukeymax-1,tukeymax)
CS = CubicSpline(xs,tukeydata)
tukeyWin = CS(wn_cs)
turfilt_fft = X_fft.copy()
n = len(turfilt_fft)
turfilt_fft[0:tukeymax] = turfilt_fft[0:tukeymax]*tukeyWin
turfilt_fft[tukeymax:n-tukeymax] = 0
turfilt_fft[n-tukeymax:n] = turfilt_fft[n-tukeymax:n]*tukeyWin[::-1]
tur_filtered_sig = fftpack.ifft(turfilt_fft)
return(tur_filtered_sig,filtered_sig,t)
def fourier_Hilbert_3D(indata,nlons,peak_freq=0,ndegs=360):
X_fft = fftpack.fft(indata)
f_s = nlons
freqs = fftpack.fftfreq(nlons) * f_s
t = np.linspace(0, ndegs, f_s, endpoint=False)
filt_fft = X_fft.copy()
if peak_freq != 0:
filt_fft[:,np.abs(freqs) > peak_freq] = 0
# Hilbert transform: set negatives = 0
filt_fft[:,freqs<0] = 0
filtered_sig = fftpack.ifft(filt_fft)
return(filtered_sig,t)
def fourier_Hilbert(indata,nlons,peak_freq=0,ndegs=360):
X_fft = fftpack.fft(indata)
f_s = nlons
freqs = fftpack.fftfreq(len(indata)) * f_s
t = np.linspace(0, ndegs, f_s, endpoint=False)
filt_fft = X_fft.copy()
if peak_freq != 0:
filt_fft[np.abs(freqs) > peak_freq] = 0
# Hilbert transform: set negatives = 0
filt_fft[freqs<0] = 0
filtered_sig = fftpack.ifft(filt_fft)
return(filtered_sig,t)