-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwavepackets.py
More file actions
177 lines (138 loc) · 6.46 KB
/
wavepackets.py
File metadata and controls
177 lines (138 loc) · 6.46 KB
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
# import
import os, errno
import numpy as np
import datetime as dt
import pandas as pd
import xarray as xr
#import Ngl
import math
import timeit
from copy import copy
from scipy import stats,signal
from scipy.interpolate import interp1d,CubicSpline
import scipy.fftpack as fftpack
from scipy.signal import argrelextrema
from sklearn.linear_model import LinearRegression
import cartopy.crs as ccrs
import cftime
# My packages
import rhwhitepackages3
from rhwhitepackages3.readwrite import *
from rhwhitepackages3.stats import regressmaps
#from rhwhitepackages3.CESMmanip import *
from rhwhitepackages3.wavenumber import *
#from rhwhitepackages3.CESMconst import *
from rhwhitepackages3.processing import *
from rhwhitepackages3.clim_tools import *
from rhwhitepackages3.physconst import *
def calc_wavepacket_3D_tur(datain,len_min,len_max):
# Subtract zonal mean
test_data = datain - datain.mean(dim='longitude')
lats = test_data.latitude
lons = test_data.longitude
times = test_data.time
wavepacket = xr.DataArray(np.zeros([len(times),len(lats),len(lons)]),
coords={'time':times,'latitude':lats,'longitude':lons},
dims = ('time','latitude','longitude'))
templats = lats.sel(latitude=slice(85,20))
ilatstart = np.where(lats == templats[0])[0]
ilatend = np.where(lats == templats[-1])[0]
for latsel in np.arange(ilatstart,ilatend+1):
# Find wavenumbers for km values at given latitude
# include 0.001 factor to convert rearth from m to km
circum = (2.0 * np.pi * (0.001 * rearth * np.cos(np.deg2rad(lats.isel(latitude=latsel)))))
s2 = (circum/len_min).values
s1 = (circum/len_max).values
indata = test_data.isel(latitude=latsel)
inlat = indata.latitude.values
# Tukey transform the data
nlons = len(indata.longitude)
tukey_transform, std_transform,t = fourier_Tukey_hilow_3D(indata,nlons,s1,s2,ndegs=360)
# Now hilbert transfrom (For now, ignoring the semi-geostrophic filter)
hilbert_transform = fourier_Hilbert_3D(tukey_transform,nlons)
wavepacket[:,latsel,:] = 2.0 *np.abs(hilbert_transform)[0]
return(wavepacket)
def calc_wavepacket_3D_std(datain,len_min,len_max):
# Subtract zonal mean
test_data = datain - datain.mean(dim='longitude')
lats = test_data.latitude
lons = test_data.longitude
times = test_data.time
wavepacket = xr.DataArray(np.zeros([len(times),len(lats),len(lons)]),
coords={'time':times,'latitude':lats,'longitude':lons},
dims = ('time','latitude','longitude'))
templats = lats.sel(latitude=slice(85,20))
ilatstart = np.where(lats == templats[0])[0]
ilatend = np.where(lats == templats[-1])[0]
for latsel in np.arange(ilatstart,ilatend+1):
# Find wavenumbers for km values at given latitude
# include 0.001 factor to convert rearth from m to km
circum = (2.0 * np.pi * (0.001 * rearth * np.cos(np.deg2rad(lats.isel(latitude=latsel)))))
s2 = (circum/len_min).values
s1 = (circum/len_max).values
indata = test_data.isel(latitude=latsel)
inlat = indata.latitude.values
# Tukey transform the data
nlons = len(indata.longitude)
tukey_transform, std_transform,t = fourier_Tukey_hilow_3D(indata,nlons,s1,s2,ndegs=360)
# Now hilbert transfrom (For now, ignoring the semi-geostrophic filter)
hilbert_transform = fourier_Hilbert_3D(std_transform,nlons)
wavepacket[:,latsel,:] = 2.0 *np.abs(hilbert_transform)[0]
return(wavepacket)
def calc_wavepacket_3D_fixed_std(datain,wn_min,wn_max,hemi='NH'):
# Subtract zonal mean
test_data = datain - datain.mean(dim='longitude')
lats = test_data.latitude
lons = test_data.longitude
times = test_data.time
wavepacket = xr.DataArray(np.zeros([len(times),len(lats),len(lons)]),
coords={'time':times,'latitude':lats,'longitude':lons},
dims = ('time','latitude','longitude'))
if hemi == 'NH':
templats = lats.sel(latitude=slice(85,20))
elif hemi == 'SH':
templats = lats.sel(latitude=slice(-20,-85))
ilatstart = np.where(lats == templats[0])[0]
ilatend = np.where(lats == templats[-1])[0]
for latsel in np.arange(ilatstart,ilatend+1):
# Find wavenumbers for km values at given latitude
# include 0.001 factor to convert rearth from m to km
circum = (2.0 * np.pi * (0.001 * rearth * np.cos(np.deg2rad(lats.isel(latitude=latsel)))))
s2 = wn_max
s1 = wn_min
indata = test_data.isel(latitude=latsel)
inlat = indata.latitude.values
# Tukey transform the data
nlons = len(indata.longitude)
tukey_transform, std_transform,t = fourier_Tukey_hilow_3D(indata.values,nlons,s1,s2,ndegs=360)
# Now hilbert transfrom (For now, ignoring the semi-geostrophic filter)
hilbert_transform = fourier_Hilbert_3D(std_transform,nlons)
wavepacket[:,latsel,:] = 2.0 *np.abs(hilbert_transform)[0]
return(wavepacket)
def calc_wavepacket(datain,len_min,len_max):
# Subtract zonal mean
test_data = datain - datain.mean(dim='longitude')
lats = test_data.latitude
lons = test_data.longitude
times = test_data.time
wavepacket = xr.DataArray(np.zeros([len(lats),len(lons)]),
coords={'latitude':lats,'longitude':lons},
dims = ('latitude','longitude'))
templats = lats.sel(latitude=slice(85,20))
ilatstart = np.where(lats == templats[0])[0]
ilatend = np.where(lats == templats[-1])[0]
for latsel in np.arange(ilatstart,ilatend+1):
# Find wavenumbers for km values at given latitude
# include 0.001 factor to convert rearth from m to km
circum = (2.0 * np.pi * (0.001 * rearth * np.cos(np.deg2rad(lats.isel(latitude=latsel)))))
s2 = (circum/len_min).values
s1 = (circum/len_max).values
indata = test_data.isel(latitude=latsel)
inlat = indata.latitude.values
# Tukey transform the data
nlons = len(indata.longitude)
tukey_transform, std_transform,t = fourier_Tukey_hilow(indata,nlons,s1,s2,ndegs=360)
# Now hilbert transfrom (For now, ignoring the semi-geostrophic filter)
hilbert_transform = fourier_Hilbert(tukey_transform,nlons)
wavepacket[latsel,:] = 2.0 *np.abs(hilbert_transform)[0]
return(wavepacket)