|
1 |
| -import numpy as np |
2 |
| -from waveletFunctions import wavelet, wave_signif |
3 | 1 | import matplotlib.pylab as plt
|
4 |
| -from matplotlib.gridspec import GridSpec |
5 | 2 | import matplotlib.ticker as ticker
|
6 |
| -from mpl_toolkits.axes_grid1 import make_axes_locatable |
| 3 | +from matplotlib.gridspec import GridSpec |
| 4 | + |
| 5 | +import numpy as np |
| 6 | + |
| 7 | +# from mpl_toolkits.axes_grid1 import make_axes_locatable |
| 8 | + |
| 9 | +from waveletFunctions import wave_signif, wavelet |
7 | 10 |
|
8 | 11 | __author__ = 'Evgeniya Predybaylo'
|
9 | 12 |
|
10 | 13 |
|
11 | 14 | # WAVETEST Example Python script for WAVELET, using NINO3 SST dataset
|
12 | 15 | #
|
13 | 16 | # See "http://paos.colorado.edu/research/wavelets/"
|
14 |
| -# The Matlab code written January 1998 by C. Torrence is modified to Python by Evgeniya Predybaylo, December 2014 |
| 17 | +# The Matlab code written January 1998 by C. Torrence |
| 18 | +# modified to Python by Evgeniya Predybaylo, December 2014 |
15 | 19 | #
|
16 | 20 | # Modified Oct 1999, changed Global Wavelet Spectrum (GWS) to be sideways,
|
17 | 21 | # changed all "log" to "log2", changed logarithmic axis on GWS to
|
18 | 22 | # a normal axis.
|
19 |
| -# ------------------------------------------------------------------------------------------------------------------ |
| 23 | +# --------------------------------------------------------------------------- |
20 | 24 |
|
21 | 25 | # READ THE DATA
|
22 | 26 | sst = np.loadtxt('sst_nino3.dat') # input SST time series
|
23 | 27 | sst = sst - np.mean(sst)
|
24 | 28 | variance = np.std(sst, ddof=1) ** 2
|
25 | 29 | print("variance = ", variance)
|
26 | 30 |
|
27 |
| -#----------C-O-M-P-U-T-A-T-I-O-N------S-T-A-R-T-S------H-E-R-E------------------------------------------------------ |
| 31 | +# ----------C-O-M-P-U-T-A-T-I-O-N------S-T-A-R-T-S------H-E-R-E--------------- |
28 | 32 |
|
29 | 33 | # normalize by standard deviation (not necessary, but makes it easier
|
30 | 34 | # to compare with plot on Interactive Wavelet page, at
|
|
52 | 56 | # Significance levels:
|
53 | 57 | signif = wave_signif(([variance]), dt=dt, sigtest=0, scale=scale,
|
54 | 58 | lag1=lag1, mother=mother)
|
55 |
| -sig95 = signif[:, np.newaxis].dot(np.ones(n)[np.newaxis, :]) # expand signif --> (J+1)x(N) array |
| 59 | +# expand signif --> (J+1)x(N) array |
| 60 | +sig95 = signif[:, np.newaxis].dot(np.ones(n)[np.newaxis, :]) |
56 | 61 | sig95 = power / sig95 # where ratio > 1, power is significant
|
57 | 62 |
|
58 | 63 | # Global wavelet spectrum & significance levels:
|
|
63 | 68 | # Scale-average between El Nino periods of 2--8 years
|
64 | 69 | avg = np.logical_and(scale >= 2, scale < 8)
|
65 | 70 | Cdelta = 0.776 # this is for the MORLET wavelet
|
66 |
| -scale_avg = scale[:, np.newaxis].dot(np.ones(n)[np.newaxis, :]) # expand scale --> (J+1)x(N) array |
| 71 | +# expand scale --> (J+1)x(N) array |
| 72 | +scale_avg = scale[:, np.newaxis].dot(np.ones(n)[np.newaxis, :]) |
67 | 73 | scale_avg = power / scale_avg # [Eqn(24)]
|
68 | 74 | scale_avg = dj * dt / Cdelta * sum(scale_avg[avg, :]) # [Eqn(24)]
|
69 | 75 | scaleavg_signif = wave_signif(variance, dt=dt, scale=scale, sigtest=2,
|
70 | 76 | lag1=lag1, dof=([2, 7.9]), mother=mother)
|
71 | 77 |
|
72 |
| -#------------------------------------------------------ Plotting |
| 78 | +# ------------------------------------------------------ Plotting |
73 | 79 |
|
74 |
| -#--- Plot time series |
| 80 | +# --- Plot time series |
75 | 81 | fig = plt.figure(figsize=(9, 10))
|
76 | 82 | gs = GridSpec(3, 4, hspace=0.4, wspace=0.75)
|
77 |
| -plt.subplots_adjust(left=0.1, bottom=0.05, right=0.9, top=0.95, wspace=0, hspace=0) |
| 83 | +plt.subplots_adjust(left=0.1, bottom=0.05, right=0.9, top=0.95, |
| 84 | + wspace=0, hspace=0) |
78 | 85 | plt.subplot(gs[0, 0:3])
|
79 | 86 | plt.plot(time, sst, 'k')
|
80 | 87 | plt.xlim(xlim[:])
|
81 | 88 | plt.xlabel('Time (year)')
|
82 | 89 | plt.ylabel('NINO3 SST (\u00B0C)')
|
83 | 90 | plt.title('a) NINO3 Sea Surface Temperature (seasonal)')
|
84 | 91 |
|
85 |
| -plt.text(time[-1] + 35, 0.5,'Wavelet Analysis\nC. Torrence & G.P. Compo\n' + |
| 92 | +plt.text(time[-1] + 35, 0.5, 'Wavelet Analysis\nC. Torrence & G.P. Compo\n' |
86 | 93 | 'http://paos.colorado.edu/\nresearch/wavelets/',
|
87 | 94 | horizontalalignment='center', verticalalignment='center')
|
88 | 95 |
|
89 |
| -#--- Contour plot wavelet power spectrum |
| 96 | +# --- Contour plot wavelet power spectrum |
90 | 97 | # plt3 = plt.subplot(3, 1, 2)
|
91 | 98 | plt3 = plt.subplot(gs[1, 0:3])
|
92 | 99 | levels = [0, 0.5, 1, 2, 4, 999]
|
93 |
| -CS = plt.contourf(time, period, power, len(levels)) #*** or use 'contour' |
94 |
| -im = plt.contourf(CS, levels=levels, colors=['white','bisque','orange','orangered','darkred']) |
| 100 | +# *** or use 'contour' |
| 101 | +CS = plt.contourf(time, period, power, len(levels)) |
| 102 | +im = plt.contourf(CS, levels=levels, |
| 103 | + colors=['white', 'bisque', 'orange', 'orangered', 'darkred']) |
95 | 104 | plt.xlabel('Time (year)')
|
96 | 105 | plt.ylabel('Period (years)')
|
97 | 106 | plt.title('b) Wavelet Power Spectrum (contours at 0.5,1,2,4\u00B0C$^2$)')
|
98 | 107 | plt.xlim(xlim[:])
|
99 | 108 | # 95# significance contour, levels at -99 (fake) and 1 (95# signif)
|
100 | 109 | plt.contour(time, period, sig95, [-99, 1], colors='k')
|
101 | 110 | # cone-of-influence, anything "below" is dubious
|
| 111 | +plt.fill_between(time, coi * 0 + period[-1], coi, facecolor="none", |
| 112 | + edgecolor="#00000040", hatch='x') |
102 | 113 | plt.plot(time, coi, 'k')
|
103 | 114 | # format y-scale
|
104 |
| -plt3.set_yscale('log', basey=2, subsy=None) |
| 115 | +plt3.set_yscale('log', base=2, subs=None) |
105 | 116 | plt.ylim([np.min(period), np.max(period)])
|
106 | 117 | ax = plt.gca().yaxis
|
107 | 118 | ax.set_major_formatter(ticker.ScalarFormatter())
|
108 | 119 | plt3.ticklabel_format(axis='y', style='plain')
|
109 | 120 | plt3.invert_yaxis()
|
110 | 121 | # set up the size and location of the colorbar
|
111 |
| -# position=fig.add_axes([0.5,0.36,0.2,0.01]) |
112 |
| -# plt.colorbar(im, cax=position, orientation='horizontal') #, fraction=0.05, pad=0.5) |
| 122 | +# position=fig.add_axes([0.5,0.36,0.2,0.01]) |
| 123 | +# plt.colorbar(im, cax=position, orientation='horizontal') |
| 124 | +# , fraction=0.05, pad=0.5) |
113 | 125 |
|
114 | 126 | # plt.subplots_adjust(right=0.7, top=0.9)
|
115 | 127 |
|
116 |
| -#--- Plot global wavelet spectrum |
| 128 | +# --- Plot global wavelet spectrum |
117 | 129 | plt4 = plt.subplot(gs[1, -1])
|
118 | 130 | plt.plot(global_ws, period)
|
119 | 131 | plt.plot(global_signif, period, '--')
|
120 | 132 | plt.xlabel('Power (\u00B0C$^2$)')
|
121 | 133 | plt.title('c) Global Wavelet Spectrum')
|
122 | 134 | plt.xlim([0, 1.25 * np.max(global_ws)])
|
123 | 135 | # format y-scale
|
124 |
| -plt4.set_yscale('log', basey=2, subsy=None) |
| 136 | +plt4.set_yscale('log', base=2, subs=None) |
125 | 137 | plt.ylim([np.min(period), np.max(period)])
|
126 | 138 | ax = plt.gca().yaxis
|
127 | 139 | ax.set_major_formatter(ticker.ScalarFormatter())
|
|
138 | 150 | plt.plot(xlim, scaleavg_signif + [0, 0], '--')
|
139 | 151 |
|
140 | 152 | plt.show()
|
141 |
| - |
142 |
| -# end of code |
143 |
| - |
|
0 commit comments