-
Notifications
You must be signed in to change notification settings - Fork 12
/
example_analysis_like_ncl_cli.py
157 lines (141 loc) · 5.89 KB
/
example_analysis_like_ncl_cli.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
import argparse
from pathlib import Path
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
def wf_analysis(x, **kwargs):
"""Return normalized spectra of x using standard processing parameters."""
# Get the "raw" spectral power
# OPTIONAL kwargs:
# segsize, noverlap, spd, latitude_bounds (tuple: (south, north)), dosymmetries, rmvLowFrq
z2 = wf.spacetime_power(x, **kwargs)
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_sym, nspec_asy
def plot_normalized_symmetric_spectrum(s, ofil=None):
"""Basic plot of normalized symmetric power spectrum with shallow water curves."""
fb = [0, .8] # frequency bounds for plot
# get data for dispersion curves:
swfreq,swwn = wf.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(*fb), wavenumber=slice(-15,15))
z.loc[{'frequency':0}] = np.nan
kmesh0, vmesh0 = np.meshgrid(z['wavenumber'], z['frequency'])
img = ax.contourf(kmesh0, vmesh0, z, levels=np.linspace(0.2, 3.0, 16), cmap='Spectral_r', extend='both')
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(fb)
ax.set_title("Normalized Symmetric Component")
fig.colorbar(img)
if ofil is not None:
fig.savefig(ofil, bbox_inches='tight', dpi=144)
def plot_normalized_asymmetric_spectrum(s, ofil=None):
"""Basic plot of normalized symmetric power spectrum with shallow water curves."""
fb = [0, .8] # frequency bounds for plot
# get data for dispersion curves:
swfreq,swwn = wf.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(*fb), wavenumber=slice(-15,15))
z.loc[{'frequency':0}] = np.nan
kmesh0, vmesh0 = np.meshgrid(z['wavenumber'], z['frequency'])
img = ax.contourf(kmesh0, vmesh0, z, levels=np.linspace(0.2, 1.8, 16), cmap='Spectral_r', extend='both')
for ii in range(0,3):
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(fb)
ax.set_title("Normalized Anti-symmetric Component")
fig.colorbar(img)
if ofil is not None:
fig.savefig(ofil, bbox_inches='tight', dpi=144)
#
# LOAD DATA, x = DataArray(time, lat, lon), e.g., daily mean precipitation
#
def get_data(filename, variablename, hfil=None):
if Path(filename).is_file():
try:
ds = xr.open_dataset(filename)
except ValueError:
ds = xr.open_dataset(filename, decode_times=False)
elif Path(filename).is_dir():
assert hfil is not None, "When a directory is provided, must also provide a hfil string to search for files."
fils = Path(filename).glob(f"*.{hfil}.*.nc")
if fils:
ds = xr.open_mfdataset(sorted(fils))
else:
print("ERROR get_data unable to figure out what data to load")
return None
return ds[variablename]
if __name__ == "__main__":
#
# input from arguments
#
parser = argparse.ArgumentParser()
parser.add_argument("ifil")
parser.add_argument("vname")
parser.add_argument("--hfil", required=False)
args = parser.parse_args()
fili = args.ifil
vari = args.vname
#
# Loading data ... example is very simple
#
data = get_data(fili, vari, args.hfil) # returns OLR
#
# Determine sampling (in samples per day)
#
spd = (86400 / (86400.*(data.time[1]-data.time[0]).dt.days + (data.time[1]-data.time[0]).dt.seconds)).astype(int).item()
print(f"Determined samples per day = {spd}")
#
# We need to have data in memory:
if hasattr(data, "compute"):
data = data.compute()
print("Moved data into memory.")
#
# Options ... right now these only go into wk.spacetime_power()
#
latBound = (-15,15) # latitude bounds for analysis
nDayWin = 96 # Wheeler-Kiladis [WK] temporal window length (days)
nDaySkip = -65 # time (days) between temporal windows [segments]
# negative means there will be overlapping temporal segments
twoMonthOverlap = 65
opt = {'segsize': nDayWin,
'noverlap': twoMonthOverlap,
'spd': spd,
'latitude_bounds': latBound,
'dosymmetries': True,
'rmvLowFrq':True}
# in this example, the smoothing & normalization will happen and use defaults
symComponent, asymComponent = wf_analysis(data, **opt)
#
# Plots ... sort of matching NCL, but not worrying much about customizing.
#
outPlotName = "example_symmetric_plot.png"
plot_normalized_symmetric_spectrum(symComponent, outPlotName)
outPlotName = "example_asymmetric_plot.png"
plot_normalized_asymmetric_spectrum(asymComponent, outPlotName)