/
spectrum.py
293 lines (223 loc) · 10.6 KB
/
spectrum.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
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
import numpy
import scipy.fftpack as fftpack
import scipy.signal as signal
import math
import const
import sys
NA = numpy.newaxis
pi = math.pi
###########################################################
#ALL in one method for W99 space-time analisis class
###########################################################
class WK99spectrum:
def __init__(self, datain, spd, nDayWin, nDaySkip, tim_taper=0.1, ifmask=True,\
rawsmooth=1):
"""space-time spectram WK99
Arcuments:
'datain' -- analysis array. dimension must be (lon,lat,...,time).
causion!!
- meridional components is symetric against equator
- longitudinal component is cubic
'spd' -- samples per day
'ndaywin' -- days of window elements size
'ndayskip' -- days of lag. negative means there will be overlap segments
'dim' -- dimension for (time, lat, lon)
'tim_taper'-- tapering range tim_taper*TotalTimeSample
'rawsmooth' -- number of 1-2-1 smoothin for wavenumber & frequency direction to raw spectrum.
'backsmooth' --
"""
ntim, nlat, nlon = datain.shape
nDayTot = ntim/spd # days of input variable
nSampTot = nDayTot*spd # samples of total input variable
nSampWin = nDayWin*spd # samples of per temporal window
nSampSkip = nDaySkip*spd # samples to skip between window segments
nWindow = (nSampTot - nSampWin)/(nSampWin + nSampSkip) + 1
#remove annual cycle(The first three harmonics of seasonal cycle removed)
if (nDayTot >= 365/3):
rf = fftpack.rfft(datain,axis=0)
freq = fftpack.rfftfreq(nSampWin, d=1./float(spd))
rf[(freq <= 3./365) & (freq >=1./365),:,:] = 0.0 #freq<=3./365 only??
datain = fftpack.irfft(rf,axis=0)
#decompose sym and antisym componet. NH is symmetric
symm = 0.5*(datain[:,:nlat/2+1,:] + datain[:,nlat:nlat/2-1:-1,:])
anti = 0.5*(datain[:,:nlat/2,:] - datain[:,nlat:nlat/2:-1,:])
datain = numpy.concatenate([anti, symm],axis=1)
sumpower = numpy.zeros((nSampWin,nlat,nlon))
ntstrt = 0
ntlast = nSampWin
for i in range(nWindow):
data = datain[ntstrt:ntlast,:,:]
# - remove dominant signals ---------------------------------------------
# mean & linear trend removed
#------------------------------------------------------------------------
data = signal.detrend(data,axis=0)
#- terpaing -------------------------------------------------------------
# multipling window function to taper segments to zero
#------------------------------------------------------------------------
if tim_taper == 'hann':
window = signal.hann(nSampWin)
data = data * window[:,NA,NA]
elif tim_taper > 0:
tp = int(nSampWin*tim_taper)
window = numpy.ones(nSampWin)
x = numpy.arange(tp)
window[:tp] = 0.5*(1.0-numpy.cos(x*pi/tp))
window[-tp:] = 0.5*(1.0-numpy.cos(x[::-1]*pi/tp))
data = data * window[:,NA,NA]
# data = data * signal.hann(nSampWin)[:,NA,NA]
#- computing space-time spectrum -----------------------------------------
# 2d complex FFT & nomarized & sort in ascendig order
#-------------------------------------------------------------------------
# CAUTION!
# scipy.fftpack.fft's formula is
# f(x) = sum_k c(k)+exp(-i*k*x)
# so wave is rebresented by exp(i(-k*x-omega*t)). In this case,
# if frequency positive, negative wavenumber means eastward, positive frequency
# means westward.
power = fftpack.fft2(data,axes=(0,2))/nlon/nSampWin #normalized by sample size
sumpower = sumpower + numpy.abs(power)**2
ntstrt = ntlast + nSampSkip
ntlast = ntstrt + nSampWin #loop for nSampWin
sumpower = sumpower / nWindow
# - sort k, f, in ascending order----------------------------------------------------------
# This procedure sort array under matrix in each latitude
# power = (after this procedure)
# [0<=f<=freqmax,lat(symm is NH),kmin<=k<=kmax]
#-------------------------------------------------------------------------------------------
if (nlon%2 == 0):
wavenumber = fftpack.fftshift(fftpack.fftfreq(nlon)*nlon)[1:]
sumpower = fftpack.fftshift(sumpower,axes=2)[:,:,nlon:0:-1]
else:
wavenumber = fftpack.fftshift(fftpack.fftfreq(nlon)*nlon)
sumpower = fftpack.fftshift(sumpower,axes=2)[:,:,::-1]
frequency = fftpack.fftshift(fftpack.fftfreq(nSampWin,d=1./float(spd)))[nSampWin/2:]
sumpower = fftpack.fftshift(sumpower,axes=0)[nSampWin/2:,:,:]
power_symm = 2.0*numpy.add.reduce(sumpower[:,nlat/2:,:],axis=1) #NH is symm
power_anti = 2.0*numpy.add.reduce(sumpower[:,:nlat/2,:],axis=1) #SH is anti
#smoothing
for i in range(rawsmooth):
power_symm = _smooth121_2d(power_symm)
power_symm = _smooth121_2d(power_symm, axis=1)
power_anti = _smooth121_2d(power_anti)
power_anti = _smooth121_2d(power_anti, axis=1)
###########################################################
# background spectrum
###########################################################
background = numpy.add.reduce(sumpower, axis=1)
freqtimes = 30 #the number of passes of filter in freqency
stepfreq = [0.1,0.2,0.3,1.]
wavetimes = [5, 10, 20, 40]
# wavetimes = [10, 10, 10, 10]
# wavetimes=[5,5,5,5]
i = 1
while i <= freqtimes:
background[1:,:] = _smooth121_2d(background[1:,:],axis=1)
i += 1
m = 0
for n in range(1,len(frequency)):
if frequency[n] > stepfreq[m]:
m = m + 1
i = 1
while i<=wavetimes[m]:
background[n,:] = _smooth121(background[n,:])
i += 1
self.wavenumber = wavenumber
self.frequency = frequency
if ifmask:
power_symm = numpy.ma.array(power_symm)
power_anti = numpy.ma.array(power_anti)
background = numpy.ma.array(background)
power_symm[0,:] = numpy.ma.masked
power_anti[0,:] = numpy.ma.masked
background[0,:] = numpy.ma.masked
self.power_symm = power_symm
self.power_anti = power_anti
self.background = background
self.sumpower = sumpower
def calcspectrum(sumpower, frequency, rawsmooth=1, freqtimes=30, stepfreq=[0.1,0.2,0.3,1.], wavetimes=[5,10,20,40], ifmask=True):
"""calculation symmetric & antisymmetric componet & background spectram
Arguments:
'sumpower' -- power spcectrum array (f, lat, k). NH is antisymmetric, SH is symmetric componet
'frequency' -- frequency array
'rawsmooth' -- number of times to smooth along wavenumber & frequency domain
'freqtimes' -- smoothing times along frequency domain to calculate background
'wavetimes' -- smoothing times along wavenumber domain to calculate background.
'stepfreq' -- step frquency to change smoothing times along frequency domain to calculate background
"""
nf, nlat, nk = sumpower.shape
if nf != len(frequency):
print "error! array size missmatch."
sys.exit()
power_symm = 2.0*numpy.add.reduce(sumpower[:,nlat/2:,:],axis=1) #NH is symm
power_anti = 2.0*numpy.add.reduce(sumpower[:,:nlat/2,:],axis=1) #SH is anti
#smoothing
for i in range(rawsmooth):
power_symm = _smooth121_2d(power_symm)
power_symm = _smooth121_2d(power_symm, axis=1)
power_anti = _smooth121_2d(power_anti)
power_anti = _smooth121_2d(power_anti, axis=1)
background = numpy.add.reduce(sumpower, axis=1)
i = 1
while i <= freqtimes:
background[1:,:] = _smooth121_2d(background[1:,:],axis=1)
i += 1
m = 0
for n in range(1,len(frequency)):
if frequency[n] > stepfreq[m]:
m = m + 1
i = 1
while i<=wavetimes[m]:
background[n,:] = _smooth121(background[n,:])
i += 1
if ifmask:
power_symm = numpy.ma.array(power_symm)
power_anti = numpy.ma.array(power_anti)
background = numpy.ma.array(background)
power_symm[0,:] = numpy.ma.masked
power_anti[0,:] = numpy.ma.masked
background[0,:] = numpy.ma.masked
return power_symm, power_anti, background
def powershape(data, spd, nDayWin):
""" return turple that represent WK99 data array.
"""
ntim, nlat, nlon = data.shape
nSampWin = nDayWin*spd # samples of per temporal window
powershape = (nSampWin-nSampWin/2, nlat, nlon-(nlon+1)%2)
return powershape
def genwavenumber(nlon):
if (nlon%2 == 0):
wavenumber = fftpack.fftshift(fftpack.fftfreq(nlon)*nlon)[1:]
else:
wavenumber = fftpack.fftshift(fftpack.fftfreq(nlon)*nlon)
return wavenumber
def genfrequency(spd, nDayWin):
"""return freqency array. units is cpd (Cycle per Day)
Arguments:
'nDayWin' -- number of days per one segment
'spd' -- number of samples per one day
"""
nSampWin = nDayWin*spd
if (nSampWin%2 == 0):
frequency = fftpack.fftfreq(nSampWin,d=1./float(spd))[:nSampWin/2]
else:
frequency = fftpack.fftfreq(nSampWin,d=1./float(spd))[:nSampWin/2+1]
return frequency
def decompose_antisymm(datain):
"""decompose sym and antisym component
"""
nlat = datain.shape[1]
symm = 0.5*(datain[:,:nlat/2+1,:] + datain[:,nlat:nlat/2-1:-1,:])
anti = 0.5*(datain[:,:nlat/2,:] - datain[:,nlat:nlat/2:-1,:])
return numpy.concatenate([anti, symm],axis=1)
def _smooth121(array):
#smoothin by moving average with weight (1,2,1)
#if array.ndim != 1:
# print "ERROR!! smooth_121:: input array must be 1-D"
# exit
weight = numpy.array([1.,2.,1.])/4.0
return numpy.convolve(numpy.r_[array[0],array,array[-1]],weight,'valid')
def _smooth121_2d(array,axis=1):
w = numpy.array([[0,0,0],[1,2,1],[0,0,0]])/4.0
if (axis==0):
w = w.T
return signal.convolve2d(array, w, 'same','symm')