Skip to content

Commit 10c8c40

Browse files
author
Chris Torrence
committed
Fix argument handling for 'mother'; add cross-hatching for COI; clean up code formatting
1 parent 44ea9eb commit 10c8c40

File tree

2 files changed

+100
-79
lines changed

2 files changed

+100
-79
lines changed

wave_python/waveletAnalysis.py

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,34 @@
1-
import numpy as np
2-
from waveletFunctions import wavelet, wave_signif
31
import matplotlib.pylab as plt
4-
from matplotlib.gridspec import GridSpec
52
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
710

811
__author__ = 'Evgeniya Predybaylo'
912

1013

1114
# WAVETEST Example Python script for WAVELET, using NINO3 SST dataset
1215
#
1316
# 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
1519
#
1620
# Modified Oct 1999, changed Global Wavelet Spectrum (GWS) to be sideways,
1721
# changed all "log" to "log2", changed logarithmic axis on GWS to
1822
# a normal axis.
19-
# ------------------------------------------------------------------------------------------------------------------
23+
# ---------------------------------------------------------------------------
2024

2125
# READ THE DATA
2226
sst = np.loadtxt('sst_nino3.dat') # input SST time series
2327
sst = sst - np.mean(sst)
2428
variance = np.std(sst, ddof=1) ** 2
2529
print("variance = ", variance)
2630

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---------------
2832

2933
# normalize by standard deviation (not necessary, but makes it easier
3034
# to compare with plot on Interactive Wavelet page, at
@@ -52,7 +56,8 @@
5256
# Significance levels:
5357
signif = wave_signif(([variance]), dt=dt, sigtest=0, scale=scale,
5458
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, :])
5661
sig95 = power / sig95 # where ratio > 1, power is significant
5762

5863
# Global wavelet spectrum & significance levels:
@@ -63,65 +68,72 @@
6368
# Scale-average between El Nino periods of 2--8 years
6469
avg = np.logical_and(scale >= 2, scale < 8)
6570
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, :])
6773
scale_avg = power / scale_avg # [Eqn(24)]
6874
scale_avg = dj * dt / Cdelta * sum(scale_avg[avg, :]) # [Eqn(24)]
6975
scaleavg_signif = wave_signif(variance, dt=dt, scale=scale, sigtest=2,
7076
lag1=lag1, dof=([2, 7.9]), mother=mother)
7177

72-
#------------------------------------------------------ Plotting
78+
# ------------------------------------------------------ Plotting
7379

74-
#--- Plot time series
80+
# --- Plot time series
7581
fig = plt.figure(figsize=(9, 10))
7682
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)
7885
plt.subplot(gs[0, 0:3])
7986
plt.plot(time, sst, 'k')
8087
plt.xlim(xlim[:])
8188
plt.xlabel('Time (year)')
8289
plt.ylabel('NINO3 SST (\u00B0C)')
8390
plt.title('a) NINO3 Sea Surface Temperature (seasonal)')
8491

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'
8693
'http://paos.colorado.edu/\nresearch/wavelets/',
8794
horizontalalignment='center', verticalalignment='center')
8895

89-
#--- Contour plot wavelet power spectrum
96+
# --- Contour plot wavelet power spectrum
9097
# plt3 = plt.subplot(3, 1, 2)
9198
plt3 = plt.subplot(gs[1, 0:3])
9299
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'])
95104
plt.xlabel('Time (year)')
96105
plt.ylabel('Period (years)')
97106
plt.title('b) Wavelet Power Spectrum (contours at 0.5,1,2,4\u00B0C$^2$)')
98107
plt.xlim(xlim[:])
99108
# 95# significance contour, levels at -99 (fake) and 1 (95# signif)
100109
plt.contour(time, period, sig95, [-99, 1], colors='k')
101110
# cone-of-influence, anything "below" is dubious
111+
plt.fill_between(time, coi * 0 + period[-1], coi, facecolor="none",
112+
edgecolor="#00000040", hatch='x')
102113
plt.plot(time, coi, 'k')
103114
# format y-scale
104-
plt3.set_yscale('log', basey=2, subsy=None)
115+
plt3.set_yscale('log', base=2, subs=None)
105116
plt.ylim([np.min(period), np.max(period)])
106117
ax = plt.gca().yaxis
107118
ax.set_major_formatter(ticker.ScalarFormatter())
108119
plt3.ticklabel_format(axis='y', style='plain')
109120
plt3.invert_yaxis()
110121
# 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)
113125

114126
# plt.subplots_adjust(right=0.7, top=0.9)
115127

116-
#--- Plot global wavelet spectrum
128+
# --- Plot global wavelet spectrum
117129
plt4 = plt.subplot(gs[1, -1])
118130
plt.plot(global_ws, period)
119131
plt.plot(global_signif, period, '--')
120132
plt.xlabel('Power (\u00B0C$^2$)')
121133
plt.title('c) Global Wavelet Spectrum')
122134
plt.xlim([0, 1.25 * np.max(global_ws)])
123135
# format y-scale
124-
plt4.set_yscale('log', basey=2, subsy=None)
136+
plt4.set_yscale('log', base=2, subs=None)
125137
plt.ylim([np.min(period), np.max(period)])
126138
ax = plt.gca().yaxis
127139
ax.set_major_formatter(ticker.ScalarFormatter())
@@ -138,6 +150,3 @@
138150
plt.plot(xlim, scaleavg_signif + [0, 0], '--')
139151

140152
plt.show()
141-
142-
# end of code
143-

0 commit comments

Comments
 (0)