-
Notifications
You must be signed in to change notification settings - Fork 12
/
example_analysis_script.py
50 lines (45 loc) · 1.92 KB
/
example_analysis_script.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
import numpy as np
import xarray as xr
# our local module:
import wavenumber_frequency_functions as wf
import matplotlib as mpl
import matplotlib.pyplot as plt
#
# LOAD DATA, x = DataArray(time, lat, lon), e.g., daily mean precipitation
#
def wf_analysis(x):
"""Return normalized spectra of x using standard processing parameters."""
# Get the "raw" spectral power
z2 = wf.spacetime_power(x, segsize=96, noverlap=96-30, spd=1, latitude_bounds=(-15,15), dosymmetries=True, rmvLowFrq=True)
z2avg = z2.mean(dim='component')
z2.loc[{'frequency':0}] = np.nan # get rid of spurious power at \nu = 0
# the background is supposed to be derived from both symmetric & antisymmetric
background = wf.smooth_wavefreq(z2avg, kern=wf.simple_smooth_kernel(), nsmooth=50, freq_name='frequency')
# separate components
z2_sym = z2[0,...]
z2_asy = z2[1,...]
# normalize
nspec_sym = z2_sym / background
nspec_asy = z2_asy / background
return nspec_sy, nspec_asy
def plot_normalized_symmetric_spectrum(s):
"""Basic plot of normalized symmetric power spectrum with shallow water curves."""
# get data for dispersion curves:
swfreq,swwn = wk.genDispersionCurves()
# swfreq.shape # -->(6, 3, 50)
swf = np.where(swfreq == 1e20, np.nan, swfreq)
swk = np.where(swwn == 1e20, np.nan, swwn)
fig, ax = plt.subplots()
c = 'darkgray' # COLOR FOR DISPERSION LINES/LABELS
z = s.transpose().sel(frequency=slice(0,0.5), wavenumber=slice(-15,15))
z.loc[{'frequency':0}] = np.nan
kmesh0, vmesh0 = np.meshgrid(z['wavenumber'], z['frequency'])
img = ax.contourf(kmesh0, vmesh0, p0)
for ii in range(3,6):
ax.plot(swk[ii, 0,:], swf[ii,0,:], color=c)
ax.plot(swk[ii, 1,:], swf[ii,1,:], color=c)
ax.plot(swk[ii, 2,:], swf[ii,2,:], color=c)
ax.axvline(0, linestyle='dashed', color='lightgray')
ax.set_xlim([-15,15])
ax.set_ylim([0,0.5])
fig.colorbar(img)