Using the TFA toolbox#

Imports:

import datetime as dt
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 = "B_NEC"
remove_chaos_model = True
time_start = dt.datetime(2015, 3, 14)
time_end = dt.datetime(2015, 3, 14, 3, 59, 59)

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,
)

We can now use the full arsenal of functions to perform pre-processing of the data.

inputs.flag_cleaning()
inputs.subtract_chaos()
inputs.convert_to_mfa()
# inputs.calculate_magnitude()

Now that the model field has been removed and the transformation to Mean Field Aligned coordinate system has been performed, we can select one of the components for Wavelet processing, e.g. the compressional, by initiating the TfaProcessor object and specifying the name of the variable and the component number (0, 1, or 2). For MFA, 0 is the poloidal component, 1 the toroidal and 2 the compressional. Similarly for B_NEC, the numbers correspond to the North (0), East (1) or Center (2) components.

processor = tfa.TfaProcessor(
    inputs, active_variable={"varname": "B_MFA", "component": 2}
)
processor.plotX()
../../_images/c7ff3c5d4053b92ab4612093c6ba30cf28d81041c47a5a07cbf8e0cdfee52a9e.png

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({"Window_Size": 300, "Method": "iqr", "Multiplier": 1})
processor.apply(c)
processor.plotX()
../../_images/0b18fc076ea80917cee0168b14c394142d71e5787d9e37fced0d6fc7c0b95f24.png

Similarly, the filtering can be performed by first initializing a Filtering object with the parameters of our choosing and then applying in on the data.

f = tfa.Filtering({"Cutoff_Frequency": 20 / 1000})
processor.apply(f)
processor.plotX()
../../_images/1a7ef53a729d55bdafd9fe4f26e4f662af6533001326d77694e41d4cb533194a.png

In the same way, the wavelet transform is applied. The result of the wavelet can be visualized by means of the image() function

w = tfa.Wavelet(
    {
        "Min_Frequency": 20 / 1000,
        "Max_Frequency": 100 / 1000,
        "dj": 0.1,
    }
)
processor.apply(w)
processor.image()
../../_images/951b5a506412dcc33c7b5e61f7e4a8c134eba2fb4807fe686a092de24b43da3e.png

This is not very useful for large time intervals e.g. an entire day, so the signal can be cut-down into segments, based on the magnetic latitude of the satellite. By default the segments correspond to tracks (half-orbits) from -75 to +75 degrees in Mag. Lat.

processor.create_segment_index()

All the plotting functions can now be used to visualize a specific segment.

processor.image(segment=4)
../../_images/858f131c283a6dd8bde3c6b9c7ba03e1e045e489040ec9f98cd69d8cf6a77b09.png

Additional positional information (Magnetic Latitude and MLT) can also be plotted (again either for the entire series or for a specific segment)

processor.plotAUX(segment=4)
../../_images/fd6e64028939a753c6a578ee38adde7e0a4273dfecedf73c240717d7ef6fb1f5.png

A simple for loop can be written to produce plots for all segments and save them locally.

# import matplotlib as mpl
# import matplotlib.pyplot as plt

# mpl.rcParams[
#     "figure.max_open_warning"
# ] = 100  # stops the warning when many plots are being produced

# for i in range(int(processor.Max_Segment) + 1):
#     fig, ax = plt.subplots(3, 1, gridspec_kw={"height_ratios": [1, 1.5, 1]})
#     plt.subplot(3, 1, 1)
#     processor.plotX(segment=i)
#     plt.subplot(3, 1, 2)
#     processor.image(segment=i, cbar_lims=[-3, 3])
#     plt.subplot(3, 1, 3)
#     processor.plotAUX(segment=i)

#     plt.savefig("segment_%02d.png" % i)
#     plt.close(fig)