Intro to the TFA tool and the Wavelet Transform
Intro to the TFA tool and the Wavelet Transform#
Imports:
import datetime as dt
import matplotlib.pyplot as plt
import numpy as np
import swarmpal.toolboxes.tfa.tfa_processor as tfa
Provide values for the parameters of the analysis. The dataset can be chosen from one of the compatible magnetic data collections, and the var is a VirES-compatible variable string (see viresclient for more information). The “start” and “end” times must be given as a datetime object. If the data are required as they are, set the remove_chaos_model parameter to False. Otherwise, if the inputs are magnetic field data and the analysis requires subtraction of the model field, set the parameter to True.
dataset = "SW_OPER_MAGA_LR_1B"
var = "F"
remove_chaos_model = False
time_start = dt.datetime(2015, 3, 18)
time_end = dt.datetime(2015, 3, 18, 0, 15, 0)
Now run the TfaInput to retrieve the selected data.
inputs = tfa.TfaInput(
collection=dataset,
start_time=time_start,
end_time=time_end,
initialise=True,
varname=var,
sampling_time="PT1S",
remove_chaos=remove_chaos_model,
)
# inputs.to_file('using_tfa.nc')
# inputs.initialise('using_tfa.nc')
The data now contains everything we need to start the processing. This data is contained in the .xarray attribute:
inputs.xarray
<xarray.Dataset>
Dimensions: (Timestamp: 44100)
Coordinates:
* Timestamp (Timestamp) datetime64[ns] 2015-03-17T18:00:00 ... 2015-03-18...
Data variables:
Spacecraft (Timestamp) object 'A' 'A' 'A' 'A' 'A' ... 'A' 'A' 'A' 'A' 'A'
QDLon (Timestamp) float64 -81.1 -81.09 -81.08 ... 92.25 92.24 92.23
F (Timestamp) float64 2.722e+04 2.724e+04 ... 3.958e+04 3.957e+04
Flags_F (Timestamp) uint8 1 1 1 1 1 1 1 1 1 1 1 ... 1 1 1 1 1 1 1 1 1 1
Latitude (Timestamp) float64 -9.178 -9.242 -9.306 ... 48.55 48.48 48.42
QDLat (Timestamp) float64 -8.813 -8.879 -8.944 ... 44.06 43.99 43.91
MLT (Timestamp) float64 7.623 7.624 7.625 ... 7.442 7.442 7.441
Longitude (Timestamp) float64 -155.3 -155.3 -155.3 ... 16.79 16.8 16.8
Radius (Timestamp) float64 6.838e+06 6.838e+06 ... 6.828e+06 6.828e+06
Attributes:
Sources: ['SW_OPER_MAGA_LR_1B_20150317T000000_20150317T235959_060...
MagneticModels: []
AppliedFilters: []Let’s first test the analysis with an artificial series, so we’ll replace the F variable with a time series of our choice, with a specific frequency of 40 mHz (i.e. 25 sec) and amplitude of 4 nT.
To test the cleaning we’ll add some random spikes as well
N = inputs.xarray["F"].shape[0] # get the length of the data
test_wave = 4 * np.sin(2 * np.pi * np.arange(N) / 50)
# test_wave = np.ones(N)
# create ten spikes at ten random locations
spike_locations = np.random.randint(0, 1000, 10) + int(N / 2) - 500
test_wave[spike_locations] = test_wave[spike_locations] + 4
inputs.xarray["F"].data = test_wave
To use the processing capabilities of TFA we create a TfaProcessor object, that contains the variable of choice.
processor = tfa.TfaProcessor(inputs, active_variable={"varname": var})
processor.plotX()
To perform cleaning on the data, we initialize a Cleaning object with the parameters of our choice and then apply it on the data with the TfaProcessor apply() function.
The active variable series can be plotted by means of the plotX() function.
c = tfa.Cleaning()
processor.apply(c)
processor.plotX()
Similarly, the filtering can be performed by first initializing a Filtering object with the parameters of our choosing and the applying in on the data.
f = tfa.Filtering()
processor.apply(f)
In the same way, the wavelet transform is applied
w = tfa.Wavelet()
processor.apply(w)
The result of the wavelet can be visualized by means of the image() function. Set the log parameter to True to plot the log-10 of the results or False to plot them as they are. The optional parameter cbar_lims can be set to specify the limits of the colorbar for the spectral plot.
processor.image(log=False, cbar_lims=[0, 4])
plt.plot(
1 / processor.input_data.xarray["scale"].data,
processor.input_data.xarray["wavelet_power"][:, int(N / 2)],
"-x",
)
[<matplotlib.lines.Line2D at 0x7f54010d40d0>]
fft_freq = np.arange(1, 2190) / N
F = np.abs(np.fft.fft(processor.input_data.xarray["X"].data)[1:2190]) / (N / 2)
plt.plot(fft_freq, F)
[<matplotlib.lines.Line2D at 0x7f5401051100>]
(
np.sum(processor.input_data.xarray["wavelet_power"].data[:, int(N / 2)]),
np.sum(F**2),
)
(12.959744228575715, 12.941911738953591)
The same result holds even if one changes the frequency resolution.
w = tfa.Wavelet(
{
"Min_Scale": 1000 / 50,
"Max_Scale": 1000 / 5,
"dj": 0.01,
}
)
processor.apply(w)
plt.subplot(1, 2, 1)
plt.plot(
1 / processor.input_data.xarray["scale"].data,
processor.input_data.xarray["wavelet_power"][:, int(N / 2)],
"-x",
)
plt.subplot(1, 2, 2)
processor.image(log=False, cbar_lims=[0, 0.2])
np.sum(processor.input_data.xarray["wavelet_power"].data[:, int(N / 2)])
12.9597414132198